3 test_description
='prune $GIT_DIR/worktrees'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK
=true
11 test_expect_success initialize
'
12 git commit --allow-empty -m init
15 test_expect_success
'worktree prune on normal repo' '
17 test_must_fail git worktree prune abc
20 test_expect_success
'prune files inside $GIT_DIR/worktrees' '
21 mkdir .git/worktrees &&
22 : >.git/worktrees/abc &&
23 git worktree prune --verbose 2>actual &&
25 Removing worktrees/abc: not a valid directory
27 test_cmp expect actual &&
28 ! test -f .git/worktrees/abc &&
29 ! test -d .git/worktrees
32 test_expect_success
'prune directories without gitdir' '
33 mkdir -p .git/worktrees/def/abc &&
34 : >.git/worktrees/def/def &&
36 Removing worktrees/def: gitdir file does not exist
38 git worktree prune --verbose 2>actual &&
39 test_cmp expect actual &&
40 ! test -d .git/worktrees/def &&
41 ! test -d .git/worktrees
44 test_expect_success SANITY
'prune directories with unreadable gitdir' '
45 mkdir -p .git/worktrees/def/abc &&
46 : >.git/worktrees/def/def &&
47 : >.git/worktrees/def/gitdir &&
48 chmod u-r .git/worktrees/def/gitdir &&
49 git worktree prune --verbose 2>actual &&
50 test_grep "Removing worktrees/def: unable to read gitdir file" actual &&
51 ! test -d .git/worktrees/def &&
52 ! test -d .git/worktrees
55 test_expect_success
'prune directories with invalid gitdir' '
56 mkdir -p .git/worktrees/def/abc &&
57 : >.git/worktrees/def/def &&
58 : >.git/worktrees/def/gitdir &&
59 git worktree prune --verbose 2>actual &&
60 test_grep "Removing worktrees/def: invalid gitdir file" actual &&
61 ! test -d .git/worktrees/def &&
62 ! test -d .git/worktrees
65 test_expect_success
'prune directories with gitdir pointing to nowhere' '
66 mkdir -p .git/worktrees/def/abc &&
67 : >.git/worktrees/def/def &&
68 echo "$(pwd)"/nowhere >.git/worktrees/def/gitdir &&
69 git worktree prune --verbose 2>actual &&
70 test_grep "Removing worktrees/def: gitdir file points to non-existent location" actual &&
71 ! test -d .git/worktrees/def &&
72 ! test -d .git/worktrees
75 test_expect_success
'not prune locked checkout' '
76 test_when_finished rm -r .git/worktrees &&
77 mkdir -p .git/worktrees/ghi &&
78 : >.git/worktrees/ghi/locked &&
80 test -d .git/worktrees/ghi
83 test_expect_success
'not prune recent checkouts' '
84 test_when_finished rm -r .git/worktrees &&
85 git worktree add jlm HEAD &&
86 test -d .git/worktrees/jlm &&
88 git worktree prune --verbose --expire=2.days.ago &&
89 test -d .git/worktrees/jlm
92 test_expect_success
'not prune proper checkouts' '
93 test_when_finished rm -r .git/worktrees &&
94 git worktree add --detach "$PWD/nop" main &&
96 test -d .git/worktrees/nop
99 test_expect_success
'prune duplicate (linked/linked)' '
100 test_when_finished rm -fr .git/worktrees w1 w2 &&
101 git worktree add --detach w1 &&
102 git worktree add --detach w2 &&
103 sed "s/w2/w1/" .git/worktrees/w2/gitdir >.git/worktrees/w2/gitdir.new &&
104 mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir &&
105 git worktree prune --verbose 2>actual &&
106 test_grep "duplicate entry" actual &&
107 test -d .git/worktrees/w1 &&
108 ! test -d .git/worktrees/w2
111 test_expect_success
'prune duplicate (main/linked)' '
112 test_when_finished rm -fr repo wt &&
113 test_create_repo repo &&
114 test_commit -C repo x &&
115 git -C repo worktree add --detach ../wt &&
118 git -C wt worktree prune --verbose 2>actual &&
119 test_grep "duplicate entry" actual &&
120 ! test -d .git/worktrees/wt