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 <j6t@kdbg.org> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 24, 2023 at 03:09 UTC fb64ca526a7c695aa137c2d2577585ddea5cce28
2 files changed +28 -2
builtin/fsck.c
+16 -2
@@ -984,10 +984,24 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
984 }
985
986 if (keep_cache_objects) {
987 + struct worktree **worktrees, **p;
988 +
989 verify_index_checksum = 1;
990 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);
1005 }
1006
1007 check_connectivity();
t/t1450-fsck.sh
+12
@@ -1023,4 +1023,16 @@ test_expect_success 'fsck error on gitattributes with excessive size' '
1023 test_cmp expected actual
1024 '
1025
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 +
1038 test_done