Skip to content

Commit 3112c3f

Browse files
committed
Merge branch 'nd/shared-index-fix' into maint
Code clean-up. * nd/shared-index-fix: read-cache: don't write index twice if we can't write shared index read-cache.c: move tempfile creation/cleanup out of write_shared_index read-cache.c: change type of "temp" in write_shared_index()
2 parents bffce88 + ef5b3a6 commit 3112c3f

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

read-cache.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,32 +2472,21 @@ static int clean_shared_index_files(const char *current_hex)
24722472
}
24732473

24742474
static int write_shared_index(struct index_state *istate,
2475-
struct lock_file *lock, unsigned flags)
2475+
struct tempfile **temp)
24762476
{
2477-
struct tempfile *temp;
24782477
struct split_index *si = istate->split_index;
24792478
int ret;
24802479

2481-
temp = mks_tempfile(git_path("sharedindex_XXXXXX"));
2482-
if (!temp) {
2483-
hashclr(si->base_sha1);
2484-
return do_write_locked_index(istate, lock, flags);
2485-
}
24862480
move_cache_to_base_index(istate);
2487-
ret = do_write_index(si->base, temp, 1);
2488-
if (ret) {
2489-
delete_tempfile(&temp);
2481+
ret = do_write_index(si->base, *temp, 1);
2482+
if (ret)
24902483
return ret;
2491-
}
2492-
ret = adjust_shared_perm(get_tempfile_path(temp));
2484+
ret = adjust_shared_perm(get_tempfile_path(*temp));
24932485
if (ret) {
2494-
int save_errno = errno;
2495-
error("cannot fix permission bits on %s", get_tempfile_path(temp));
2496-
delete_tempfile(&temp);
2497-
errno = save_errno;
2486+
error("cannot fix permission bits on %s", get_tempfile_path(*temp));
24982487
return ret;
24992488
}
2500-
ret = rename_tempfile(&temp,
2489+
ret = rename_tempfile(temp,
25012490
git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
25022491
if (!ret) {
25032492
hashcpy(si->base_sha1, si->base->sha1);
@@ -2565,7 +2554,22 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
25652554
new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED;
25662555

25672556
if (new_shared_index) {
2568-
ret = write_shared_index(istate, lock, flags);
2557+
struct tempfile *temp;
2558+
int saved_errno;
2559+
2560+
temp = mks_tempfile(git_path("sharedindex_XXXXXX"));
2561+
if (!temp) {
2562+
hashclr(si->base_sha1);
2563+
ret = do_write_locked_index(istate, lock, flags);
2564+
goto out;
2565+
}
2566+
ret = write_shared_index(istate, &temp);
2567+
2568+
saved_errno = errno;
2569+
if (is_tempfile_active(temp))
2570+
delete_tempfile(&temp);
2571+
errno = saved_errno;
2572+
25692573
if (ret)
25702574
goto out;
25712575
}

t/t1700-split-index.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,25 @@ done <<\EOF
401401
0642 -rw-r---w-
402402
EOF
403403

404+
test_expect_success POSIXPERM,SANITY 'graceful handling when splitting index is not allowed' '
405+
test_create_repo ro &&
406+
(
407+
cd ro &&
408+
test_commit initial &&
409+
git update-index --split-index &&
410+
test -f .git/sharedindex.*
411+
) &&
412+
cp ro/.git/index new-index &&
413+
test_when_finished "chmod u+w ro/.git" &&
414+
chmod u-w ro/.git &&
415+
GIT_INDEX_FILE="$(pwd)/new-index" git -C ro update-index --split-index &&
416+
chmod u+w ro/.git &&
417+
rm ro/.git/sharedindex.* &&
418+
GIT_INDEX_FILE=new-index git ls-files >actual &&
419+
echo initial.t >expected &&
420+
test_cmp expected actual
421+
'
422+
404423
test_expect_success 'writing split index with null sha1 does not write cache tree' '
405424
git config core.splitIndex true &&
406425
git config splitIndex.maxPercentChange 0 &&

0 commit comments

Comments
 (0)