Skip to content

Commit 327864a

Browse files
pcloudsgitster
authored andcommitted
worktree prune: improve prune logic when worktree is moved
Automatic detection of worktree relocation by a user (via 'mv', for instance) was removed by 618244e (worktree: stop supporting moving worktrees manually - 2016-01-22). Prior to that, .git/worktrees/<tag>/gitdir was updated whenever the worktree was accessed in order to let the pruning logic know that the worktree was "active" even if it disappeared for a while (due to being located on removable media, for instance). "git worktree move" has come so we don't really need this, but since it's easy to do, perhaps we could keep supporting manual worktree move a bit longer. Notice that when a worktree is active, the "index" file should be updated pretty often in common case. The logic is updated to check for index mtime to see if the worktree is alive. The old logic of checking gitdir's mtime is dropped because nobody updates it anyway. The new corner case is, if the index file does not exist, we immediately remove the stale worktree. But if the "index" file does not exist, you may have a bigger problem. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3800135 commit 327864a

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

builtin/worktree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ static int prune_worktree(const char *id, struct strbuf *reason)
100100
path[len] = '\0';
101101
if (!file_exists(path)) {
102102
free(path);
103-
if (st.st_mtime <= expire) {
103+
if (stat(git_path("worktrees/%s/index", id), &st) ||
104+
st.st_mtime <= expire) {
104105
strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
105106
return 1;
106107
} else {

t/t2026-worktree-prune.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ test_expect_success 'not prune locked checkout' '
7878

7979
test_expect_success 'not prune recent checkouts' '
8080
test_when_finished rm -r .git/worktrees &&
81-
mkdir zz &&
82-
mkdir -p .git/worktrees/jlm &&
83-
echo "$(pwd)"/zz >.git/worktrees/jlm/gitdir &&
84-
rmdir zz &&
81+
git worktree add jlm HEAD &&
82+
test -d .git/worktrees/jlm &&
83+
rm -rf jlm &&
8584
git worktree prune --verbose --expire=2.days.ago &&
8685
test -d .git/worktrees/jlm
8786
'

0 commit comments

Comments
 (0)