packed-backend: fsck should warn when "packed-refs" file is empty

We assume the "packed-refs" won't be empty and instead has at least one line in it (even when there are no refs packed, there is the file header line). Because there is no terminating LF in the empty file, we will report "packedRefEntryNotTerminated(ERROR)" to the user. However, the runtime code paths would accept an empty "packed-refs" file, for example, "create_snapshot" would simply return the "snapshot" without checking the content of "packed-refs". So, we should skip checking the content of "packed-refs" when it is empty during fsck. After 694b7a1999 (repack_without_ref(): write peeled refs in the rewritten file, 2013-04-22), we would always write a header into the "packed-refs" file. So, versions of Git that are not too ancient never write such an empty "packed-refs" file. As an empty file often indicates a sign of a filesystem-level issue, the way we want to resolve this inconsistency is not make everybody totally silent but notice and report the anomaly. Let's create a "FSCK_INFO" message id "EMPTY_PACKED_REFS_FILE" to report to the users that "packed-refs" is empty. Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

shejialuo committed May 14, 2025 at 23:50 UTC 784ceccb91b82dc8a2c69ddd6f1f5ccc2e2f96f2
4 files changed +33
Documentation/fsck-msgids.adoc
+6
@@ -59,6 +59,12 @@
59 `emptyName`::
60 (WARN) A path contains an empty name.
61
62 +`emptyPackedRefsFile`::
63 + (INFO) "packed-refs" file is empty. Report to the
64 + git@vger.kernel.org mailing list if you see this error. As only
65 + very early versions of Git would create such an empty
66 + "packed_refs" file, we might tighten this rule in the future.
67 +
68 `extraHeaderEntry`::
69 (IGNORE) Extra headers found after `tagger`.
70
fsck.h
+1
@@ -84,6 +84,7 @@ enum fsck_msg_type {
84 FUNC(LARGE_PATHNAME, WARN) \
85 /* infos (reported as warnings, but ignored by default) */ \
86 FUNC(BAD_FILEMODE, INFO) \
87 + FUNC(EMPTY_PACKED_REFS_FILE, INFO) \
88 FUNC(GITMODULES_PARSE, INFO) \
89 FUNC(GITIGNORE_SYMLINK, INFO) \
90 FUNC(GITATTRIBUTES_SYMLINK, INFO) \
refs/packed-backend.c
+9
@@ -2103,6 +2103,15 @@ static int packed_fsck(struct ref_store *ref_store,
2103 goto cleanup;
2104 }
2105
2106 + if (!st.st_size) {
2107 + struct fsck_ref_report report = { 0 };
2108 + report.path = "packed-refs";
2109 + ret = fsck_report_ref(o, &report,
2110 + FSCK_MSG_EMPTY_PACKED_REFS_FILE,
2111 + "file is empty");
2112 + goto cleanup;
2113 + }
2114 +
2115 if (strbuf_read(&packed_ref_content, fd, 0) < 0) {
2116 ret = error_errno(_("unable to read '%s'"), refs->path);
2117 goto cleanup;
t/t0602-reffiles-fsck.sh
+17
@@ -647,6 +647,23 @@ test_expect_success SYMLINKS 'the filetype of packed-refs should be checked' '
647 )
648 '
649
650 +test_expect_success 'empty packed-refs should be reported' '
651 + test_when_finished "rm -rf repo" &&
652 + git init repo &&
653 + (
654 + cd repo &&
655 + test_commit default &&
656 +
657 + >.git/packed-refs &&
658 + git refs verify 2>err &&
659 + cat >expect <<-EOF &&
660 + warning: packed-refs: emptyPackedRefsFile: file is empty
661 + EOF
662 + rm .git/packed-refs &&
663 + test_cmp expect err
664 + )
665 +'
666 +
667 test_expect_success 'packed-refs header should be checked' '
668 test_when_finished "rm -rf repo" &&
669 git init repo &&