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

[PATCH v2 05/17] refs/files: extract function to check single ref

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

When checking the consistency of references we create a directory iterator and then verify each single reference in a loop. The logic to perform the actual checks is embedded into that loop, which makes it hard to reuse. But In a subsequent commit we're about to introduce a second path that wants to verify references.

Prepare for this by extracting the logic to check a single reference into a standalone function.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 refs/files-backend.c | 80 +++++++++++++++++++++++++++++++++-------------------
 1 file changed, 51 insertions(+), 29 deletions(-)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 4cbee23dad..9972221f9f 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3715,7 +3715,8 @@ static int files_ref_store_remove_on_disk(struct ref_store *ref_store,
 typedef int (*files_fsck_refs_fn)(struct ref_store *ref_store,
 				  struct fsck_options *o,
 				  const char *refname,
-				  struct dir_iterator *iter);
+				  const char *path,
+				  int mode);
 
 static int files_fsck_symref_target(struct fsck_options *o,
 				    struct fsck_ref_report *report,
@@ -3772,7 +3773,8 @@ static int files_fsck_symref_target(struct fsck_options *o,
 static int files_fsck_refs_content(struct ref_store *ref_store,
 				   struct fsck_options *o,
 				   const char *target_name,
-				   struct dir_iterator *iter)
+				   const char *path,
+				   int mode)
 {
 	struct strbuf ref_content = STRBUF_INIT;
 	struct strbuf abs_gitdir = STRBUF_INIT;
@@ -3786,7 +3788,7 @@ static int files_fsck_refs_content(struct ref_store *ref_store,
 
 	report.path = target_name;
 
-	if (S_ISLNK(iter->st.st_mode)) {
+	if (S_ISLNK(mode)) {
 		const char *relative_referent_path = NULL;
 
 		ret = fsck_report_ref(o, &report,
@@ -3798,7 +3800,7 @@ static int files_fsck_refs_content(struct ref_store *ref_store,
 		if (!is_dir_sep(abs_gitdir.buf[abs_gitdir.len - 1]))
 			strbuf_addch(&abs_gitdir, '/');
 
-		strbuf_add_real_path(&ref_content, iter->path.buf);
+		strbuf_add_real_path(&ref_content, path);
 		skip_prefix(ref_content.buf, abs_gitdir.buf,
 			    &relative_referent_path);
 
@@ -3811,7 +3813,7 @@ static int files_fsck_refs_content(struct ref_store *ref_store,
 		goto cleanup;
 	}
 
-	if (strbuf_read_file(&ref_content, iter->path.buf, 0) < 0) {
+	if (strbuf_read_file(&ref_content, path, 0) < 0) {
 		/*
 		 * Ref file could be removed by another concurrent process. We should
 		 * ignore this error and continue to the next ref.
@@ -3819,7 +3821,7 @@ static int files_fsck_refs_content(struct ref_store *ref_store,
 		if (errno == ENOENT)
 			goto cleanup;
 
-		ret = error_errno(_("cannot read ref file '%s'"), iter->path.buf);
+		ret = error_errno(_("cannot read ref file '%s'"), path);
 		goto cleanup;
 	}
 
@@ -3861,16 +3863,20 @@ static int files_fsck_refs_content(struct ref_store *ref_store,
 static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
 				struct fsck_options *o,
 				const char *refname,
-				struct dir_iterator *iter)
+				const char *path,
+				int mode UNUSED)
 {
 	struct strbuf sb = STRBUF_INIT;
+	const char *filename;
 	int ret = 0;
 
+	filename = basename((char *) path);
+
 	/*
 	 * Ignore the files ending with ".lock" as they may be lock files
 	 * However, do not allow bare ".lock" files.
 	 */
-	if (iter->basename[0] != '.' && ends_with(iter->basename, ".lock"))
+	if (filename[0] != '.' && ends_with(filename, ".lock"))
 		goto cleanup;
 
 	/*
@@ -3896,6 +3902,35 @@ static const files_fsck_refs_fn fsck_refs_fn[]= {
 	NULL,
 };
 
+static int files_fsck_ref(struct ref_store *ref_store,
+			  struct fsck_options *o,
+			  const char *refname,
+			  const char *path,
+			  int mode)
+{
+	int ret = 0;
+
+	if (o->verbose)
+		fprintf_ln(stderr, "Checking %s", refname);
+
+	if (!S_ISREG(mode) && !S_ISLNK(mode)) {
+		struct fsck_ref_report report = { .path = refname };
+
+		if (fsck_report_ref(o, &report,
+				    FSCK_MSG_BAD_REF_FILETYPE,
+				    "unexpected file type"))
+			ret = -1;
+		goto out;
+	}
+
+	for (size_t i = 0; fsck_refs_fn[i]; i++)
+		if (fsck_refs_fn[i](ref_store, o, refname, path, mode))
+			ret = -1;
+
+out:
+	return ret;
+}
+
 static int files_fsck_refs_dir(struct ref_store *ref_store,
 			       struct fsck_options *o,
 			       struct worktree *wt)
@@ -3918,30 +3953,17 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
 	}
 
 	while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
-		if (S_ISDIR(iter->st.st_mode)) {
+		if (S_ISDIR(iter->st.st_mode))
 			continue;
-		} else if (S_ISREG(iter->st.st_mode) ||
-			   S_ISLNK(iter->st.st_mode)) {
-			strbuf_reset(&refname);
-
-			if (!is_main_worktree(wt))
-				strbuf_addf(&refname, "worktrees/%s/", wt->id);
-			strbuf_addf(&refname, "refs/%s", iter->relative_path);
 
-			if (o->verbose)
-				fprintf_ln(stderr, "Checking %s", refname.buf);
+		strbuf_reset(&refname);
+		if (!is_main_worktree(wt))
+			strbuf_addf(&refname, "worktrees/%s/", wt->id);
+		strbuf_addf(&refname, "refs/%s", iter->relative_path);
 
-			for (size_t i = 0; fsck_refs_fn[i]; i++) {
-				if (fsck_refs_fn[i](ref_store, o, refname.buf, iter))
-					ret = -1;
-			}
-		} else {
-			struct fsck_ref_report report = { .path = iter->basename };
-			if (fsck_report_ref(o, &report,
-					    FSCK_MSG_BAD_REF_FILETYPE,
-					    "unexpected file type"))
-				ret = -1;
-		}
+		if (files_fsck_ref(ref_store, o, refname.buf,
+				   iter->path.buf, iter->st.st_mode) < 0)
+			ret = -1;
 	}
 
 	if (iter_status != ITER_DONE)
-- 
2.52.0.590.g1f87b77810.dirty
Previous: Karthik NayakNext: Patrick Steinhardt
Message 39 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.