]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fsck: check index files in all worktrees
authorJeff King <peff@peff.net>
Fri, 24 Feb 2023 08:09:57 +0000 (03:09 -0500)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 Feb 2023 17:32:23 +0000 (09:32 -0800)
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>
builtin/fsck.c
t/t1450-fsck.sh

index fa101e0db2efaf522ab3784c0dfdfd847a014968..c11cb2a95f997d11c8834c04f5cb5af4b9203729 100644 (file)
@@ -984,10 +984,24 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
        }
 
        if (keep_cache_objects) {
+               struct worktree **worktrees, **p;
+
                verify_index_checksum = 1;
                verify_ce_order = 1;
-               repo_read_index(the_repository);
-               fsck_index(the_repository->index);
+
+               worktrees = get_worktrees();
+               for (p = worktrees; *p; p++) {
+                       struct worktree *wt = *p;
+                       struct index_state istate =
+                               INDEX_STATE_INIT(the_repository);
+
+                       if (read_index_from(&istate,
+                                           worktree_git_path(wt, "index"),
+                                           get_worktree_git_dir(wt)) > 0)
+                               fsck_index(&istate);
+                       discard_index(&istate);
+               }
+               free_worktrees(worktrees);
        }
 
        check_connectivity();
index de0f6d5e7fd49ca5276696833d8f9dd034a15629..e01a519a69e44765b4a2745df5b433ff7d303d1d 100755 (executable)
@@ -1023,4 +1023,16 @@ test_expect_success 'fsck error on gitattributes with excessive size' '
        test_cmp expected actual
 '
 
+test_expect_success 'fsck detects problems in worktree index' '
+       test_when_finished "git worktree remove -f wt" &&
+       git worktree add wt &&
+
+       echo "this will be removed to break the worktree index" >wt/file &&
+       git -C wt add file &&
+       blob=$(git -C wt rev-parse :file) &&
+       remove_object $blob &&
+
+       test_must_fail git fsck
+'
+
 test_done