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 <[email protected]>
---
 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

Reply via email to