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

[PATCH v2 14/17] refs/reftable: introduce generic checks for refs

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

In a preceding commit we have extracted generic checks for both direct and symbolic refs that apply for all backends. Wire up those checks for the "reftable" backend.

Note that this is done by iterating through all refs manually with the low-level reftable ref iterator. We explicitly don't want to use the higher-level iterator that is exposed to users of the reftable backend as that iterator may swallow for example broken refs.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 refs/reftable-backend.c  | 82 ++++++++++++++++++++++++++++++++++++++++++++----
 t/t0614-reftable-fsck.sh | 12 +++++++
 2 files changed, 88 insertions(+), 6 deletions(-)
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 6361b27015..fe74af73af 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -2767,19 +2767,89 @@ static int reftable_be_fsck(struct ref_store *ref_store, struct fsck_options *o,
 {
 	struct reftable_ref_store *refs =
 		reftable_be_downcast(ref_store, REF_STORE_READ, "fsck");
+	struct reftable_ref_iterator *iter = NULL;
+	struct reftable_ref_record ref = { 0 };
+	struct fsck_ref_report report = { 0 };
+	struct strbuf refname = STRBUF_INIT;
 	struct reftable_backend *backend;
+	int ret, errors = 0;
 
 	if (is_main_worktree(wt)) {
 		backend = &refs->main_backend;
 	} else {
-		int ret = backend_for_worktree(&backend, refs, wt->id);
-		if (ret < 0)
-			return error(_("reftable stack for worktree '%s' is broken"),
-				     wt->id);
+		ret = backend_for_worktree(&backend, refs, wt->id);
+		if (ret < 0) {
+			ret = error(_("reftable stack for worktree '%s' is broken"),
+				    wt->id);
+			goto out;
+		}
+	}
+
+	errors |= reftable_fsck_check(backend->stack, reftable_fsck_error_handler,
+				      reftable_fsck_verbose_handler, o);
+
+	iter = ref_iterator_for_stack(refs, backend->stack, "", NULL, 0);
+	if (!iter) {
+		ret = error(_("could not create iterator for worktree '%s'"), wt->id);
+		goto out;
+	}
+
+	while (1) {
+		ret = reftable_iterator_next_ref(&iter->iter, &ref);
+		if (ret > 0)
+			break;
+		if (ret < 0) {
+			ret = error(_("could not read record for worktree '%s'"), wt->id);
+			goto out;
+		}
+
+		strbuf_reset(&refname);
+		if (!is_main_worktree(wt))
+			strbuf_addf(&refname, "worktrees/%s/", wt->id);
+		strbuf_addstr(&refname, ref.refname);
+		report.path = refname.buf;
+
+		switch (ref.value_type) {
+		case REFTABLE_REF_VAL1:
+		case REFTABLE_REF_VAL2: {
+			struct object_id oid;
+			unsigned hash_id;
+
+			switch (reftable_stack_hash_id(backend->stack)) {
+			case REFTABLE_HASH_SHA1:
+				hash_id = GIT_HASH_SHA1;
+				break;
+			case REFTABLE_HASH_SHA256:
+				hash_id = GIT_HASH_SHA256;
+				break;
+			default:
+				BUG("unhandled hash ID %d",
+				    reftable_stack_hash_id(backend->stack));
+			}
+
+			oidread(&oid, reftable_ref_record_val1(&ref),
+				&hash_algos[hash_id]);
+
+			errors |= refs_fsck_ref(ref_store, o, &report, ref.refname, &oid);
+			break;
+		}
+		case REFTABLE_REF_SYMREF:
+			errors |= refs_fsck_symref(ref_store, o, &report, ref.refname,
+						   ref.value.symref);
+			break;
+		default:
+			BUG("unhandled reference value type %d", ref.value_type);
+		}
 	}
 
-	return reftable_fsck_check(backend->stack, reftable_fsck_error_handler,
-				   reftable_fsck_verbose_handler, o);
+	ret = errors ? -1 : 0;
+
+out:
+	if (iter)
+		ref_iterator_free(&iter->base);
+	reftable_ref_record_release(&ref);
+	strbuf_release(&refname);
+	return ret;
 }
 
 struct ref_storage_be refs_be_reftable = {
diff --git a/t/t0614-reftable-fsck.sh b/t/t0614-reftable-fsck.sh
index 4757eb5931..d24b87f961 100755
--- a/t/t0614-reftable-fsck.sh
+++ b/t/t0614-reftable-fsck.sh
@@ -87,4 +87,16 @@ test_expect_success 'worktree stacks can be verified' '
 	done
 '
 
+test_expect_success 'invalid symref gets reported' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	test_commit -C repo initial &&
+	git -C repo symbolic-ref refs/heads/symref garbage &&
+	test_must_fail git -C repo refs verify 2>err &&
+	cat >expect <<-EOF &&
+	error: refs/heads/symref: badReferentName: points to invalid refname ${SQ}garbage${SQ}
+	EOF
+	test_cmp expect err
+'
+
 test_done
-- 
2.52.0.590.g1f87b77810.dirty
Previous: Karthik NayakNext: Patrick Steinhardt
Message 54 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.