Skip to content

Commit b0e0fc2

Browse files
committed
Merge branch 'tg/split-index-fixes' into maint
The split-index mode had a few corner case bugs fixed. * tg/split-index-fixes: travis: run tests with GIT_TEST_SPLIT_INDEX split-index: don't write cache tree with null oid entries read-cache: fix reading the shared index for other repos
2 parents 7e44d80 + ae59a4e commit b0e0fc2

File tree

8 files changed

+48
-17
lines changed

8 files changed

+48
-17
lines changed

cache-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, co
608608

609609
hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
610610

611-
entries = read_index_from(index_state, index_path);
611+
entries = read_index_from(index_state, index_path, get_git_dir());
612612
if (entries < 0) {
613613
ret = WRITE_TREE_UNREADABLE_INDEX;
614614
goto out;

cache.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ struct index_state {
345345
struct split_index *split_index;
346346
struct cache_time timestamp;
347347
unsigned name_hash_initialized : 1,
348-
initialized : 1;
348+
initialized : 1,
349+
drop_cache_tree : 1;
349350
struct hashmap name_hash;
350351
struct hashmap dir_hash;
351352
unsigned char sha1[20];
@@ -371,7 +372,7 @@ extern void free_name_hash(struct index_state *istate);
371372
#define active_cache_tree (the_index.cache_tree)
372373

373374
#define read_cache() read_index(&the_index)
374-
#define read_cache_from(path) read_index_from(&the_index, (path))
375+
#define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
375376
#define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec))
376377
#define is_cache_unborn() is_index_unborn(&the_index)
377378
#define read_cache_unmerged() read_index_unmerged(&the_index)
@@ -616,7 +617,8 @@ extern int read_index(struct index_state *);
616617
extern int read_index_preload(struct index_state *, const struct pathspec *pathspec);
617618
extern int do_read_index(struct index_state *istate, const char *path,
618619
int must_exist); /* for testting only! */
619-
extern int read_index_from(struct index_state *, const char *path);
620+
extern int read_index_from(struct index_state *, const char *path,
621+
const char *gitdir);
620622
extern int is_index_unborn(struct index_state *);
621623
extern int read_index_unmerged(struct index_state *);
622624

ci/run-build-and-tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ ln -s $HOME/travis-cache/.prove t/.prove
99

1010
make --jobs=2
1111
make --quiet test
12+
if test "$jobname" = "linux-gcc"
13+
then
14+
GIT_TEST_SPLIT_INDEX=YesPlease make --quiet test
15+
fi
1216

1317
check_unignored_build_artifacts
1418

read-cache.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ int hold_locked_index(struct lock_file *lk, int lock_flags)
16031603

16041604
int read_index(struct index_state *istate)
16051605
{
1606-
return read_index_from(istate, get_index_file());
1606+
return read_index_from(istate, get_index_file(), get_git_dir());
16071607
}
16081608

16091609
static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,
@@ -1863,20 +1863,19 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
18631863
* This way, shared index can be removed if they have not been used
18641864
* for some time.
18651865
*/
1866-
static void freshen_shared_index(char *base_sha1_hex, int warn)
1866+
static void freshen_shared_index(const char *shared_index, int warn)
18671867
{
1868-
char *shared_index = git_pathdup("sharedindex.%s", base_sha1_hex);
18691868
if (!check_and_freshen_file(shared_index, 1) && warn)
18701869
warning("could not freshen shared index '%s'", shared_index);
1871-
free(shared_index);
18721870
}
18731871

1874-
int read_index_from(struct index_state *istate, const char *path)
1872+
int read_index_from(struct index_state *istate, const char *path,
1873+
const char *gitdir)
18751874
{
18761875
struct split_index *split_index;
18771876
int ret;
18781877
char *base_sha1_hex;
1879-
const char *base_path;
1878+
char *base_path;
18801879

18811880
/* istate->initialized covers both .git/index and .git/sharedindex.xxx */
18821881
if (istate->initialized)
@@ -1896,16 +1895,17 @@ int read_index_from(struct index_state *istate, const char *path)
18961895
split_index->base = xcalloc(1, sizeof(*split_index->base));
18971896

18981897
base_sha1_hex = sha1_to_hex(split_index->base_sha1);
1899-
base_path = git_path("sharedindex.%s", base_sha1_hex);
1898+
base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_sha1_hex);
19001899
ret = do_read_index(split_index->base, base_path, 1);
19011900
if (hashcmp(split_index->base_sha1, split_index->base->sha1))
19021901
die("broken index, expect %s in %s, got %s",
19031902
base_sha1_hex, base_path,
19041903
sha1_to_hex(split_index->base->sha1));
19051904

1906-
freshen_shared_index(base_sha1_hex, 0);
1905+
freshen_shared_index(base_path, 0);
19071906
merge_base_index(istate);
19081907
post_read_index_from(istate);
1908+
free(base_path);
19091909
return ret;
19101910
}
19111911

@@ -2243,7 +2243,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
22432243
struct stat st;
22442244
struct ondisk_cache_entry_extended ondisk;
22452245
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
2246-
int drop_cache_tree = 0;
2246+
int drop_cache_tree = istate->drop_cache_tree;
22472247

22482248
for (i = removed = extended = 0; i < entries; i++) {
22492249
if (cache[i]->ce_flags & CE_REMOVE)
@@ -2573,8 +2573,11 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
25732573
ret = write_split_index(istate, lock, flags);
25742574

25752575
/* Freshen the shared index only if the split-index was written */
2576-
if (!ret && !new_shared_index)
2577-
freshen_shared_index(sha1_to_hex(si->base_sha1), 1);
2576+
if (!ret && !new_shared_index) {
2577+
const char *shared_index = git_path("sharedindex.%s",
2578+
sha1_to_hex(si->base_sha1));
2579+
freshen_shared_index(shared_index, 1);
2580+
}
25782581

25792582
out:
25802583
if (flags & COMMIT_LOCK)

repository.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,5 @@ int repo_read_index(struct repository *repo)
236236
if (!repo->index)
237237
repo->index = xcalloc(1, sizeof(*repo->index));
238238

239-
return read_index_from(repo->index, repo->index_file);
239+
return read_index_from(repo->index, repo->index_file, repo->gitdir);
240240
}

revision.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,8 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
13521352
continue; /* current index already taken care of */
13531353

13541354
if (read_index_from(&istate,
1355-
worktree_git_path(wt, "index")) > 0)
1355+
worktree_git_path(wt, "index"),
1356+
get_worktree_git_dir(wt)) > 0)
13561357
do_add_index_objects_to_pending(revs, &istate);
13571358
discard_index(&istate);
13581359
}

split-index.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ void prepare_to_write_split_index(struct index_state *istate)
238238
ALLOC_GROW(entries, nr_entries+1, nr_alloc);
239239
entries[nr_entries++] = ce;
240240
}
241+
if (is_null_oid(&ce->oid))
242+
istate->drop_cache_tree = 1;
241243
}
242244
}
243245

t/t1700-split-index.sh

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

404+
test_expect_success 'writing split index with null sha1 does not write cache tree' '
405+
git config core.splitIndex true &&
406+
git config splitIndex.maxPercentChange 0 &&
407+
git commit -m "commit" &&
408+
{
409+
git ls-tree HEAD &&
410+
printf "160000 commit $_z40\\tbroken\\n"
411+
} >broken-tree &&
412+
echo "add broken entry" >msg &&
413+
414+
tree=$(git mktree <broken-tree) &&
415+
test_tick &&
416+
commit=$(git commit-tree $tree -p HEAD <msg) &&
417+
git update-ref HEAD "$commit" &&
418+
GIT_ALLOW_NULL_SHA1=1 git reset --hard &&
419+
(test-dump-cache-tree >cache-tree.out || true) &&
420+
test_line_count = 0 cache-tree.out
421+
'
422+
404423
test_done

0 commit comments

Comments
 (0)