git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 15/17] builtin/fsck: move generic object ID checks into `refs_fsck()`

From
Patrick Steinhardt <ps@pks.im>
Date
Jan 12, 2026, 09:03 UTC
Message-ID
<20260112-pks-refs-verify-fixes-v2-15-2e9e453bd6c3@pks.im>
In-Reply-To
<20260112-pks-refs-verify-fixes-v2-0-2e9e453bd6c3@pks.im>

While most of the logic that verifies the consistency of refs is driven by `refs_fsck()`, we still have a small handful of checks in `fsck_head_link()`. These checks don't use the git-fsck(1) reporting infrastructure, and as such it's impossible to for example disable some of those checks.

One such check detects refs that point to the all-zeroes object ID. Extract this check into the generic `refs_fsck_ref()` function that is used by both the "files" and "reftable" backends.

Note that this will cause us to not return an error code from `fsck_head_link()` anymore in case this error was detected. This is fine though: the only caller of this function does not check the error code anyway. To demonstrate this, adapt the function to drop its return value altogether. The function will be removed in a subsequent commit anyway.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 Documentation/fsck-msgids.adoc |  3 +++
 builtin/fsck.c                 | 41 +++++++++++++++--------------------------
 fsck.h                         |  1 +
 refs.c                         | 11 ++++++++---
 t/t1450-fsck.sh                |  6 +++---
 5 files changed, 30 insertions(+), 32 deletions(-)
diff --git a/Documentation/fsck-msgids.adoc b/Documentation/fsck-msgids.adoc
index acac9683af..76609321f6 100644
--- a/Documentation/fsck-msgids.adoc
+++ b/Documentation/fsck-msgids.adoc
@@ -41,6 +41,9 @@
 `badRefName`::
 	(ERROR) A ref has an invalid format.
 
+`badRefOid`::
+	(ERROR) A ref points to an invalid object ID.
+
 `badReferentName`::
 	(ERROR) The referent name of a symref is invalid.
 
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 4979bc795e..4dd4d74d1e 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -564,9 +564,9 @@ static int fsck_handle_ref(const struct reference *ref, void *cb_data UNUSED)
 	return 0;
 }
 
-static int fsck_head_link(const char *head_ref_name,
-			  const char **head_points_at,
-			  struct object_id *head_oid);
+static void fsck_head_link(const char *head_ref_name,
+			   const char **head_points_at,
+			   struct object_id *head_oid);
 
 static void get_default_heads(void)
 {
@@ -713,12 +713,10 @@ static void fsck_source(struct odb_source *source)
 	stop_progress(&progress);
 }
 
-static int fsck_head_link(const char *head_ref_name,
-			  const char **head_points_at,
-			  struct object_id *head_oid)
+static void fsck_head_link(const char *head_ref_name,
+			   const char **head_points_at,
+			   struct object_id *head_oid)
 {
-	int null_is_error = 0;
-
 	if (verbose)
 		fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
 
@@ -727,27 +725,18 @@ static int fsck_head_link(const char *head_ref_name,
 						  NULL);
 	if (!*head_points_at) {
 		errors_found |= ERROR_REFS;
-		return error(_("invalid %s"), head_ref_name);
+		error(_("invalid %s"), head_ref_name);
+		return;
 	}
-	if (!strcmp(*head_points_at, head_ref_name))
-		/* detached HEAD */
-		null_is_error = 1;
-	else if (!starts_with(*head_points_at, "refs/heads/")) {
+	if (strcmp(*head_points_at, head_ref_name) &&
+	    !starts_with(*head_points_at, "refs/heads/")) {
 		errors_found |= ERROR_REFS;
-		return error(_("%s points to something strange (%s)"),
-			     head_ref_name, *head_points_at);
-	}
-	if (is_null_oid(head_oid)) {
-		if (null_is_error) {
-			errors_found |= ERROR_REFS;
-			return error(_("%s: detached HEAD points at nothing"),
-				     head_ref_name);
-		}
-		fprintf_ln(stderr,
-			   _("notice: %s points to an unborn branch (%s)"),
-			   head_ref_name, *head_points_at + 11);
+		error(_("%s points to something strange (%s)"),
+		      head_ref_name, *head_points_at);
+		return;
 	}
-	return 0;
+
+	return;
 }
 
 static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
diff --git a/fsck.h b/fsck.h
index bfe0d9c6d2..1f472b7daa 100644
--- a/fsck.h
+++ b/fsck.h
@@ -39,6 +39,7 @@ enum fsck_msg_type {
 	FUNC(BAD_REF_CONTENT, ERROR) \
 	FUNC(BAD_REF_FILETYPE, ERROR) \
 	FUNC(BAD_REF_NAME, ERROR) \
+	FUNC(BAD_REF_OID, ERROR) \
 	FUNC(BAD_TIMEZONE, ERROR) \
 	FUNC(BAD_TREE, ERROR) \
 	FUNC(BAD_TREE_SHA1, ERROR) \
diff --git a/refs.c b/refs.c
index 4fc1317cb3..c3528862c6 100644
--- a/refs.c
+++ b/refs.c
@@ -320,10 +320,15 @@ int check_refname_format(const char *refname, int flags)
 	return check_or_sanitize_refname(refname, flags, NULL);
 }
 
-int refs_fsck_ref(struct ref_store *refs UNUSED, struct fsck_options *o UNUSED,
-		  struct fsck_ref_report *report UNUSED,
-		  const char *refname UNUSED, const struct object_id *oid UNUSED)
+int refs_fsck_ref(struct ref_store *refs UNUSED, struct fsck_options *o,
+		  struct fsck_ref_report *report,
+		  const char *refname UNUSED, const struct object_id *oid)
 {
+	if (is_null_oid(oid))
+		return fsck_report_ref(o, report, FSCK_MSG_BAD_REF_OID,
+				       "points to invalid object ID '%s'",
+				       oid_to_hex(oid));
+
 	return 0;
 }
 
diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh
index c4b651c2dc..900c1b2eb2 100755
--- a/t/t1450-fsck.sh
+++ b/t/t1450-fsck.sh
@@ -105,7 +105,7 @@ test_expect_success REFFILES 'HEAD link pointing at a funny object' '
 	echo $ZERO_OID >.git/HEAD &&
 	# avoid corrupt/broken HEAD from interfering with repo discovery
 	test_must_fail env GIT_DIR=.git git fsck 2>out &&
-	test_grep "detached HEAD points" out
+	test_grep "HEAD: badRefOid: points to invalid object ID ${SQ}$ZERO_OID${SQ}" out
 '
 
 test_expect_success 'HEAD link pointing at a funny place' '
@@ -123,7 +123,7 @@ test_expect_success REFFILES 'HEAD link pointing at a funny object (from differe
 	echo $ZERO_OID >.git/HEAD &&
 	# avoid corrupt/broken HEAD from interfering with repo discovery
 	test_must_fail git -C wt fsck 2>out &&
-	test_grep "main-worktree/HEAD: detached HEAD points" out
+	test_grep "HEAD: badRefOid: points to invalid object ID ${SQ}$ZERO_OID${SQ}" out
 '
 
 test_expect_success REFFILES 'other worktree HEAD link pointing at a funny object' '
@@ -131,7 +131,7 @@ test_expect_success REFFILES 'other worktree HEAD link pointing at a funny objec
 	git worktree add other &&
 	echo $ZERO_OID >.git/worktrees/other/HEAD &&
 	test_must_fail git fsck 2>out &&
-	test_grep "worktrees/other/HEAD: detached HEAD points" out
+	test_grep "worktrees/other/HEAD: badRefOid: points to invalid object ID ${SQ}$ZERO_OID${SQ}" out
 '
 
 test_expect_success 'other worktree HEAD link pointing at missing object' '
-- 
2.52.0.590.g1f87b77810.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 55 of 61 in “Fixes and improvements for ref consistency checks”
  1. 00/17 Fixes and improvements for ref consistency checksPatrick Steinhardt, Jan 9, 2026
  2. 01/17 refs/files: simplify iterating through root refsPatrick Steinhardt, Jan 9, 2026
  3. shejialuoJan 10, 2026
  4. 02/17 refs/files: move fsck functions into global scopePatrick Steinhardt, Jan 9, 2026
  5. 03/17 refs/files: remove `refs_check_dir` parameterPatrick Steinhardt, Jan 9, 2026
  6. 04/17 refs/files: remove useless indirectionPatrick Steinhardt, Jan 9, 2026
  7. 05/17 refs/files: extract function to check single refPatrick Steinhardt, Jan 9, 2026
  8. 06/17 refs/files: improve error handling when verifying symrefsPatrick Steinhardt, Jan 9, 2026
  9. shejialuoJan 10, 2026
  10. 07/17 refs/files: perform consistency checks for root refsPatrick Steinhardt, Jan 9, 2026
  11. shejialuoJan 10, 2026
  12. Patrick SteinhardtJan 12, 2026
  13. 08/17 fsck: drop unused fields from `struct fsck_ref_report`Patrick Steinhardt, Jan 9, 2026
  14. 09/17 refs/files: extract generic symref target checksPatrick Steinhardt, Jan 9, 2026
  15. shejialuoJan 10, 2026
  16. Patrick SteinhardtJan 12, 2026
  17. 10/17 refs/files: introduce function to perform normal ref checksPatrick Steinhardt, Jan 9, 2026
  18. shejialuoJan 10, 2026
  19. Patrick SteinhardtJan 12, 2026
  20. 11/17 refs/reftable: adapt includes to become consistentPatrick Steinhardt, Jan 9, 2026
  21. 12/17 refs/reftable: extract function to retrieve backend for worktreePatrick Steinhardt, Jan 9, 2026
  22. 13/17 refs/reftable: fix consistency checks with worktreesPatrick Steinhardt, Jan 9, 2026
  23. 14/17 refs/reftable: introduce generic checks for refsPatrick Steinhardt, Jan 9, 2026
  24. 15/17 builtin/fsck: move generic object ID checks into `refs_fsck()`Patrick Steinhardt, Jan 9, 2026
  25. 16/17 builtin/fsck: move generic HEAD check into `refs_fsck()`Patrick Steinhardt, Jan 9, 2026
  26. shejialuoJan 10, 2026
  27. Patrick SteinhardtJan 12, 2026
  28. shejialuoJan 15, 2026
  29. 17/17 builtin/fsck: drop `fsck_head_link()`Patrick Steinhardt, Jan 9, 2026
  30. shejialuoJan 10, 2026
  31. Patrick SteinhardtJan 12, 2026
  32. 00/17 Fixes and improvements for ref consistency checksPatrick Steinhardt, Jan 12, 2026
  33. 01/17 refs/files: simplify iterating through root refsPatrick Steinhardt, Jan 12, 2026
  34. Karthik NayakJan 12, 2026
  35. 02/17 refs/files: move fsck functions into global scopePatrick Steinhardt, Jan 12, 2026
  36. 03/17 refs/files: remove `refs_check_dir` parameterPatrick Steinhardt, Jan 12, 2026
  37. 04/17 refs/files: remove useless indirectionPatrick Steinhardt, Jan 12, 2026
  38. Karthik NayakJan 12, 2026
  39. 05/17 refs/files: extract function to check single refPatrick Steinhardt, Jan 12, 2026
  40. 06/17 refs/files: improve error handling when verifying symrefsPatrick Steinhardt, Jan 12, 2026
  41. 07/17 refs/files: perform consistency checks for root refsPatrick Steinhardt, Jan 12, 2026
  42. 08/17 fsck: drop unused fields from `struct fsck_ref_report`Patrick Steinhardt, Jan 12, 2026
  43. 09/17 refs/files: extract generic symref target checksPatrick Steinhardt, Jan 12, 2026
  44. 10/17 refs/files: introduce function to perform normal ref checksPatrick Steinhardt, Jan 12, 2026
  45. Karthik NayakJan 12, 2026
  46. Patrick SteinhardtJan 12, 2026
  47. Junio C HamanoJan 12, 2026
  48. Junio C HamanoJan 12, 2026
  49. Patrick SteinhardtJan 12, 2026
  50. 11/17 refs/reftable: adapt includes to become consistentPatrick Steinhardt, Jan 12, 2026
  51. 12/17 refs/reftable: extract function to retrieve backend for worktreePatrick Steinhardt, Jan 12, 2026
  52. 13/17 refs/reftable: fix consistency checks with worktreesPatrick Steinhardt, Jan 12, 2026
  53. Karthik NayakJan 12, 2026
  54. 14/17 refs/reftable: introduce generic checks for refsPatrick Steinhardt, Jan 12, 2026
  55. 15/17 builtin/fsck: move generic object ID checks into `refs_fsck()`Patrick Steinhardt, Jan 12, 2026
  56. 16/17 builtin/fsck: move generic HEAD check into `refs_fsck()`Patrick Steinhardt, Jan 12, 2026
  57. 17/17 builtin/fsck: drop `fsck_head_link()`Patrick Steinhardt, Jan 12, 2026
  58. Karthik NayakJan 12, 2026
  59. Patrick SteinhardtJan 12, 2026
  60. shejialuoJan 15, 2026
  61. Patrick SteinhardtJan 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.