Skip to content

Commit dd0c256

Browse files
committed
Merge branch 'nd/shared-index-fix'
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 39a1dd8 + ef5b3a6 commit dd0c256

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
@@ -2471,32 +2471,21 @@ static int clean_shared_index_files(const char *current_hex)
24712471
}
24722472

24732473
static int write_shared_index(struct index_state *istate,
2474-
struct lock_file *lock, unsigned flags)
2474+
struct tempfile **temp)
24752475
{
2476-
struct tempfile *temp;
24772476
struct split_index *si = istate->split_index;
24782477
int ret;
24792478

2480-
temp = mks_tempfile(git_path("sharedindex_XXXXXX"));
2481-
if (!temp) {
2482-
hashclr(si->base_sha1);
2483-
return do_write_locked_index(istate, lock, flags);
2484-
}
24852479
move_cache_to_base_index(istate);
2486-
ret = do_write_index(si->base, temp, 1);
2487-
if (ret) {
2488-
delete_tempfile(&temp);
2480+
ret = do_write_index(si->base, *temp, 1);
2481+
if (ret)
24892482
return ret;
2490-
}
2491-
ret = adjust_shared_perm(get_tempfile_path(temp));
2483+
ret = adjust_shared_perm(get_tempfile_path(*temp));
24922484
if (ret) {
2493-
int save_errno = errno;
2494-
error("cannot fix permission bits on %s", get_tempfile_path(temp));
2495-
delete_tempfile(&temp);
2496-
errno = save_errno;
2485+
error("cannot fix permission bits on %s", get_tempfile_path(*temp));
24972486
return ret;
24982487
}
2499-
ret = rename_tempfile(&temp,
2488+
ret = rename_tempfile(temp,
25002489
git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
25012490
if (!ret) {
25022491
hashcpy(si->base_sha1, si->base->sha1);
@@ -2564,7 +2553,22 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
25642553
new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED;
25652554

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

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)