prune --worktrees: fix expire vs worktree existence condition
authorMax Kirillov <max@max630.net>
Mon, 30 Mar 2015 20:47:47 +0000 (23:47 +0300)
committerJunio C Hamano <gitster@pobox.com>
Tue, 31 Mar 2015 18:02:11 +0000 (11:02 -0700)
`git prune --worktrees` was pruning worktrees which were non-existent OR
expired, while it rather should prune those which are orphaned AND
expired, as git-checkout documentation describes. Fix it.

Add test 'not prune proper checkouts', which uses valid but expired
worktree.

Modify test 'not prune recent checkouts' to remove the worktree before
pruning - link in worktrees still must survive. In older form it is
useless because would pass always when the other test passes.

Signed-off-by: Max Kirillov <max@max630.net>
Acked-by: Duy Nguyen <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/prune.c
t/t2026-prune-linked-checkouts.sh
index f08670a984a9f697e40b12f40dfaa946bb2b5e1f..86282b244758d7c248dc2829291c8b6b05a4708e 100644 (file)
@@ -120,11 +120,15 @@ static int prune_worktree(const char *id, struct strbuf *reason)
                if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
                    st_link.st_nlink > 1)
                        return 0;
-               strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
-               return 1;
+               if (st.st_mtime <= expire) {
+                       strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
+                       return 1;
+               } else {
+                       return 0;
+               }
        }
        free(path);
-       return st.st_mtime <= expire;
+       return 0;
 }
 
 static void prune_worktrees(void)
index e885bafc97f73cd6f86581e7d5a40b1575b15e04..1821a480c5ce6fd31416201e3a3dcb0f17b1de91 100755 (executable)
@@ -4,6 +4,10 @@ test_description='prune $GIT_DIR/worktrees'
 
 . ./test-lib.sh
 
+test_expect_success initialize '
+       git commit --allow-empty -m init
+'
+
 test_expect_success 'prune --worktrees on normal repo' '
        git prune --worktrees &&
        test_must_fail git prune --worktrees abc
@@ -77,8 +81,16 @@ test_expect_success 'not prune recent checkouts' '
        mkdir zz &&
        mkdir -p .git/worktrees/jlm &&
        echo "$(pwd)"/zz >.git/worktrees/jlm/gitdir &&
+       rmdir zz &&
        git prune --worktrees --verbose --expire=2.days.ago &&
        test -d .git/worktrees/jlm
 '
 
+test_expect_success 'not prune proper checkouts' '
+       test_when_finished rm -r .git/worktrees &&
+       git checkout "--to=$PWD/nop" --detach master &&
+       git prune --worktrees &&
+       test -d .git/worktrees/nop
+'
+
 test_done