Git Mailing List Archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Johannes Sixt <j6t@kdbg.org>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: [PATCH 2/3] fsck: check index files in all worktrees
Date: Fri, 24 Feb 2023 03:09:57 -0500	[thread overview]
Message-ID: <Y/hw1YVgCYWX2yNK@coredump.intra.peff.net> (raw)
In-Reply-To: <Y/hv0MXAyBY3HEo9@coredump.intra.peff.net>

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>
---
I mostly cargo-culted the loop from add_index_objects_to_pending(),
which is how they get included in git-prune, git-repack, etc.

I'm not sure if we should be reporting something if we get a negative
return. Last time I dug into this, I think I found that the
index-reading code could not actually return an error (it dies instead).

If we get zero, there are no entries to check. But I'm not sure if we'd
still have extensions? So maybe this ought to be ignoring the return
value from read_index_from() entirely.

 builtin/fsck.c  | 17 +++++++++++++++--
 t/t1450-fsck.sh | 12 ++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/builtin/fsck.c b/builtin/fsck.c
index fa101e0db2..ddd13cb2b3 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -984,10 +984,23 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 	}
 
 	if (keep_cache_objects) {
+		struct worktree **p;
+
 		verify_index_checksum = 1;
 		verify_ce_order = 1;
-		repo_read_index(the_repository);
-		fsck_index(the_repository->index);
+
+		for (p = get_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);
+		}
+
 	}
 
 	check_connectivity();
diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh
index fdb886dfe4..3b70ad9e22 100755
--- a/t/t1450-fsck.sh
+++ b/t/t1450-fsck.sh
@@ -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
-- 
2.39.2.981.g6157336f25


  parent reply	other threads:[~2023-02-24  8:10 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-18  9:38 Bug: fsck and repack don't agree when a worktree index extension is "broken" Johannes Sixt
2023-02-24  8:05 ` [PATCH 0/3] fsck index files from all worktrees Jeff King
2023-02-24  8:07   ` [PATCH 1/3] fsck: factor out index fsck Jeff King
2023-02-24  8:09   ` Jeff King [this message]
2023-02-24  8:45     ` [PATCH 2/3] fsck: check index files in all worktrees Jeff King
2023-02-24  8:12   ` [PATCH 3/3] fsck: mention file path for index errors Jeff King
2023-05-11  6:39     ` Eric Sunshine
2023-05-11 16:17       ` Jeff King
2023-05-11 16:28         ` Eric Sunshine
2023-05-11 17:01           ` Jeff King
2023-06-29 18:21             ` Eric Sunshine
2023-06-29 19:37               ` Junio C Hamano
2023-06-01 12:15     ` Andreas Schwab
2023-06-01 14:04       ` Jeff King
2023-02-24 17:30   ` [PATCH 0/3] fsck index files from all worktrees Junio C Hamano
2023-02-26 22:29     ` [PATCH 4/3] fsck: check even zero-entry index files Jeff King
2023-02-27 12:09       ` Derrick Stolee
2023-02-27 15:58       ` Junio C Hamano
2023-02-26 21:49   ` [PATCH 0/3] fsck index files from all worktrees Johannes Sixt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y/hw1YVgCYWX2yNK@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).