Skip to content

Commit 998330a

Browse files
szedergitster
authored andcommitted
read-cache: look for shared index files next to the index, too
When reading a split index git always looks for its referenced shared base index in the gitdir of the current repository, even when reading an alternate index specified via GIT_INDEX_FILE, and even when that alternate index file is the "main" '.git/index' file of an other repository. However, if that split index and its referenced shared index files were written by a git command running entirely in that other repository, then, naturally, the shared index file is written to that other repository's gitdir. Consequently, a git command attempting to read that shared index file while running in a different repository won't be able find it and will error out. I'm not sure in what use case it is necessary to read the index of one repository by a git command running in a different repository, but it is certainly possible to do so, and in fact the test 'bare repository: check that --cached honors index' in 't0003-attributes.sh' does exactly that. If GIT_TEST_SPLIT_INDEX=1 were to split the index in just the right moment [1], then this test would indeed fail, because the referenced shared index file could not be found. Let's look for the referenced shared index file not only in the gitdir of the current directory, but, if the shared index is not there, right next to the split index as well. [1] We haven't seen this issue trigger a failure in t0003 yet, because: - While GIT_TEST_SPLIT_INDEX=1 is supposed to trigger index splitting randomly, the first index write has always been deterministic and it has never split the index. - That alternate index file in the other repository is written only once in the entire test script, so it's never split. However, the next patch will fix GIT_TEST_SPLIT_INDEX, and while doing so it will slightly change its behavior to always split the index already on the first index write, and t0003 would always fail without this patch. Signed-off-by: SZEDER Gábor <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent daad41c commit 998330a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

read-cache.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,9 +2391,21 @@ int read_index_from(struct index_state *istate, const char *path,
23912391
base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
23922392
trace2_region_enter_printf("index", "shared/do_read_index",
23932393
the_repository, "%s", base_path);
2394-
ret = do_read_index(split_index->base, base_path, 1);
2394+
ret = do_read_index(split_index->base, base_path, 0);
23952395
trace2_region_leave_printf("index", "shared/do_read_index",
23962396
the_repository, "%s", base_path);
2397+
if (!ret) {
2398+
char *path_copy = xstrdup(path);
2399+
const char *base_path2 = xstrfmt("%s/sharedindex.%s",
2400+
dirname(path_copy),
2401+
base_oid_hex);
2402+
free(path_copy);
2403+
trace2_region_enter_printf("index", "shared/do_read_index",
2404+
the_repository, "%s", base_path2);
2405+
ret = do_read_index(split_index->base, base_path2, 1);
2406+
trace2_region_leave_printf("index", "shared/do_read_index",
2407+
the_repository, "%s", base_path2);
2408+
}
23972409
if (!oideq(&split_index->base_oid, &split_index->base->oid))
23982410
die(_("broken index, expect %s in %s, got %s"),
23992411
base_oid_hex, base_path,

t/t1700-split-index.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,27 @@ test_expect_success 'do not refresh null base index' '
510510
)
511511
'
512512

513+
test_expect_success 'reading split index at alternate location' '
514+
git init reading-alternate-location &&
515+
(
516+
cd reading-alternate-location &&
517+
>file-in-alternate &&
518+
git update-index --split-index --add file-in-alternate
519+
) &&
520+
echo file-in-alternate >expect &&
521+
522+
# Should be able to find the shared index both right next to
523+
# the specified split index file ...
524+
GIT_INDEX_FILE=./reading-alternate-location/.git/index \
525+
git ls-files --cached >actual &&
526+
test_cmp expect actual &&
527+
528+
# ... and, for backwards compatibility, in the current GIT_DIR
529+
# as well.
530+
mv -v ./reading-alternate-location/.git/sharedindex.* .git &&
531+
GIT_INDEX_FILE=./reading-alternate-location/.git/index \
532+
git ls-files --cached >actual &&
533+
test_cmp expect actual
534+
'
535+
513536
test_done

0 commit comments

Comments
 (0)