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

[PATCH 2/2] worktree: demonstrate a half-working workaround for auto-gc

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Feb 8, 2017, 12:18 UTC
Message-ID
<da25bfcebccdeb5ccdbf1f98027859f894fe5c25.1486556262.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1486556262.git.johannes.schindelin@gmx.de>

While worktrees are marked "experimental", there is really no alternative to using them when trying to work on multiple patch series in parallel.

Sadly, there is still a bug that causes irretrievable data loss caused by worktree's incomplete handling of reflogs (and of the multiple indices of the various worktrees), as this developer can testify a dozen times over.

In the --auto case, we can install a hook that runs before-hand, accumulates all the worktree-specific refs and reflogs and installs them into a very special reflog in the common refs namespace. The only purpose of this stunt is to let gc pick up on those refs and reflogs, of course, and *not* ignore them.

Unfortunately, this still does not address the "git gc" case, but hopefully a real fix will be here some time soon.

Also, if there are simply too many loose objects for Git's liking, it will kick off auto-gc (including the hook, which takes a couple of seconds to run) that subsequently only says that it won't do anything, so the next Git operation will kick off another auto-gc (including the multi-second hook run).

Needless to say: this patch is not actually intended for inclusion, but instead tries to demonstrate the pain and the dear need for a real bug fix (and not another 8 months of buggy Git).

So here is hoping to a timely fix. Cheers.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/t6500-gc.sh | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
index e24a4fb611..2f1e52825d 100755
--- a/t/t6500-gc.sh
+++ b/t/t6500-gc.sh
@@ -67,6 +67,30 @@ test_expect_success 'auto gc with too many loose objects does not attempt to cre
 	test_line_count = 2 new # There is one new pack and its .idx
 '
 
+test_expect_success 'install pre-auto-gc hook for worktrees' '
+	mkdir -p .git/hooks &&
+	write_script .git/hooks/pre-auto-gc <<-\EOF
+	echo "Preserving refs/reflogs of worktrees" >&2 &&
+	dir="$(git rev-parse --git-common-dir)" &&
+	refsdir="$dir/logs/refs" &&
+	rm -f "$refsdir"/preserve &&
+	ident="$(GIT_COMMITTER_DATE= git var GIT_COMMITTER_IDENT)" &&
+	(
+		find "$dir"/logs "$dir"/worktrees/*/logs \
+			-type f -exec cat {} \; |
+		cut -d" " -f1
+		find "$dir"/HEAD "$dir"/worktrees/*/HEAD "$dir"/refs \
+			"$dir"/worktrees/*/refs -type f -exec cat {} \; |
+		grep -v "^ref:"
+	) 2>/dev/null |
+	sort |
+	uniq |
+	sed "s/.*/& & $ident	dummy/" >"$dir"/preserve &&
+	mkdir -p "$refsdir" &&
+	mv "$dir"/preserve "$refsdir"/
+	EOF
+'
+
 trigger_auto_gc () {
 	# This is unfortunately very, very ugly
 	gdir="$(git rev-parse --git-common-dir)" &&
@@ -76,7 +100,7 @@ trigger_auto_gc () {
 	git -c gc.auto=1 -c gc.pruneexpire=now -c gc.autodetach=0 gc --auto
 }
 
-test_expect_failure 'gc respects refs/reflogs in all worktrees' '
+test_expect_success 'gc respects refs/reflogs in all worktrees' '
 	test_commit something &&
 	git worktree add worktree &&
 	(
-- 
2.11.1.windows.1
Previous: Johannes SchindelinNext: Duy Nguyen
Message 3 of 4 in “Demonstrate a critical worktree/gc bug”
  1. 0/2 Demonstrate a critical worktree/gc bugJohannes Schindelin, Feb 8, 2017
  2. 1/2 worktree: demonstrate data lost to auto-gcJohannes Schindelin, Feb 8, 2017
  3. 2/2 worktree: demonstrate a half-working workaround for auto-gcJohannes Schindelin, Feb 8, 2017
  4. Duy NguyenFeb 8, 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.