Skip to content

Commit 562bc08

Browse files
max630gitster
authored andcommitted
prune --worktrees: fix expire vs worktree existence condition
`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 <[email protected]> Acked-by: Duy Nguyen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 466e8d5 commit 562bc08

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

builtin/prune.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ static int prune_worktree(const char *id, struct strbuf *reason)
120120
if (!stat(git_path("worktrees/%s/link", id), &st_link) &&
121121
st_link.st_nlink > 1)
122122
return 0;
123-
strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
124-
return 1;
123+
if (st.st_mtime <= expire) {
124+
strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
125+
return 1;
126+
} else {
127+
return 0;
128+
}
125129
}
126130
free(path);
127-
return st.st_mtime <= expire;
131+
return 0;
128132
}
129133

130134
static void prune_worktrees(void)

t/t2026-prune-linked-checkouts.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ test_description='prune $GIT_DIR/worktrees'
44

55
. ./test-lib.sh
66

7+
test_expect_success initialize '
8+
git commit --allow-empty -m init
9+
'
10+
711
test_expect_success 'prune --worktrees on normal repo' '
812
git prune --worktrees &&
913
test_must_fail git prune --worktrees abc
@@ -77,8 +81,16 @@ test_expect_success 'not prune recent checkouts' '
7781
mkdir zz &&
7882
mkdir -p .git/worktrees/jlm &&
7983
echo "$(pwd)"/zz >.git/worktrees/jlm/gitdir &&
84+
rmdir zz &&
8085
git prune --worktrees --verbose --expire=2.days.ago &&
8186
test -d .git/worktrees/jlm
8287
'
8388

89+
test_expect_success 'not prune proper checkouts' '
90+
test_when_finished rm -r .git/worktrees &&
91+
git checkout "--to=$PWD/nop" --detach master &&
92+
git prune --worktrees &&
93+
test -d .git/worktrees/nop
94+
'
95+
8496
test_done

0 commit comments

Comments
 (0)