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

[PATCH 2/6] fsck: report trees as dangling

From
Jeff King <peff@peff.net>
Date
Jan 16, 2017, 21:25 UTC
Message-ID
<20170116212535.cohvikwkju5zehr4@sigill.intra.peff.net>
In-Reply-To
<20170116212231.ojoqzlajpszifaf3@sigill.intra.peff.net>

After checking connectivity, fsck looks through the list of any objects we've seen mentioned, and reports unreachable and un-"used" ones as dangling. However, it skips any object which is not marked as "parsed", as that is an object that we _don't_ have (but that somebody mentioned).

Since 6e454b9a3 (clear parsed flag when we free tree buffers, 2013-06-05), that flag can't be relied on, and the correct method is to check the HAS_OBJ flag. The cleanup in that commit missed this callsite, though. As a result, we would generally fail to report dangling trees.

We never noticed because there were no tests in this area (for trees or otherwise). Let's add some.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/fsck.c  |  2 +-
 t/t1450-fsck.sh | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/builtin/fsck.c b/builtin/fsck.c
index f01b81eeb..3e67203f9 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -225,7 +225,7 @@ static void check_unreachable_object(struct object *obj)
 	 * to complain about it being unreachable (since it does
 	 * not exist).
 	 */
-	if (!obj->parsed)
+	if (!(obj->flags & HAS_OBJ))
 		return;
 
 	/*
diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh
index 6eef8b28e..e88ec7747 100755
--- a/t/t1450-fsck.sh
+++ b/t/t1450-fsck.sh
@@ -559,4 +559,31 @@ test_expect_success 'fsck --name-objects' '
 	)
 '
 
+# for each of type, we have one version which is referenced by another object
+# (and so while unreachable, not dangling), and another variant which really is
+# dangling.
+test_expect_success 'fsck notices dangling objects' '
+	git init dangling &&
+	(
+		cd dangling &&
+		blob=$(echo not-dangling | git hash-object -w --stdin) &&
+		dblob=$(echo dangling | git hash-object -w --stdin) &&
+		tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
+		dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
+		commit=$(git commit-tree $tree) &&
+		dcommit=$(git commit-tree -p $commit $tree) &&
+
+		cat >expect <<-EOF &&
+		dangling blob $dblob
+		dangling commit $dcommit
+		dangling tree $dtree
+		EOF
+
+		git fsck >actual &&
+		# the output order is non-deterministic, as it comes from a hash
+		sort <actual >actual.sorted &&
+		test_cmp expect actual.sorted
+	)
+'
+
 test_done
-- 
2.11.0.642.gd6f8cda6c
Previous: Jeff KingNext: Junio C Hamano
Message 5 of 15 in “fsck --connectivity-check misses some corruption”
  1. 0/6 fsck --connectivity-check misses some corruptionJeff King, Jan 16, 2017
  2. 1/6 t1450: clean up sub-objects in duplicate-entry testJeff King, Jan 16, 2017
  3. Junio C HamanoJan 17, 2017
  4. Jeff KingJan 17, 2017
  5. 2/6 fsck: report trees as danglingJeff King, Jan 16, 2017
  6. Junio C HamanoJan 17, 2017
  7. 5/6 fsck: do not fallback "git fsck <bogus>" to "git fsck"Jeff King, Jan 16, 2017
  8. 6/6 fsck: check HAS_OBJ more consistentlyJeff King, Jan 16, 2017
  9. 3/6 fsck: prepare dummy objects for --connectivity-checkJeff King, Jan 16, 2017
  10. Junio C HamanoJan 17, 2017
  11. Junio C HamanoJan 17, 2017
  12. Jeff KingJan 17, 2017
  13. 4/6 fsck: tighten error-checks of "git fsck <head>"Jeff King, Jan 16, 2017
  14. Junio C HamanoJan 17, 2017
  15. Jeff KingJan 16, 2017

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.