Skip to content

Commit fb64ca5

Browse files
peffgitster
authored andcommitted
fsck: check index files in all worktrees
We check the index file for the main worktree, but completely ignore the index files in other worktrees. These should be checked, too, as they are part of the repository state (and in particular, errors in those index files may cause repo-wide operations like "git gc" to complain). Reported-by: Johannes Sixt <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8840069 commit fb64ca5

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

builtin/fsck.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,10 +984,24 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
984984
}
985985

986986
if (keep_cache_objects) {
987+
struct worktree **worktrees, **p;
988+
987989
verify_index_checksum = 1;
988990
verify_ce_order = 1;
989-
repo_read_index(the_repository);
990-
fsck_index(the_repository->index);
991+
992+
worktrees = get_worktrees();
993+
for (p = worktrees; *p; p++) {
994+
struct worktree *wt = *p;
995+
struct index_state istate =
996+
INDEX_STATE_INIT(the_repository);
997+
998+
if (read_index_from(&istate,
999+
worktree_git_path(wt, "index"),
1000+
get_worktree_git_dir(wt)) > 0)
1001+
fsck_index(&istate);
1002+
discard_index(&istate);
1003+
}
1004+
free_worktrees(worktrees);
9911005
}
9921006

9931007
check_connectivity();

t/t1450-fsck.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,4 +1023,16 @@ test_expect_success 'fsck error on gitattributes with excessive size' '
10231023
test_cmp expected actual
10241024
'
10251025

1026+
test_expect_success 'fsck detects problems in worktree index' '
1027+
test_when_finished "git worktree remove -f wt" &&
1028+
git worktree add wt &&
1029+
1030+
echo "this will be removed to break the worktree index" >wt/file &&
1031+
git -C wt add file &&
1032+
blob=$(git -C wt rev-parse :file) &&
1033+
remove_object $blob &&
1034+
1035+
test_must_fail git fsck
1036+
'
1037+
10261038
test_done

0 commit comments

Comments
 (0)