3 test_description
='prune $GIT_DIR/worktrees'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 test_expect_success initialize
'
11 git commit --allow-empty -m init
14 test_expect_success
'worktree prune on normal repo' '
16 test_must_fail git worktree prune abc
19 test_expect_success
'prune files inside $GIT_DIR/worktrees' '
20 mkdir .git/worktrees &&
21 : >.git/worktrees/abc &&
22 git worktree prune --verbose >actual &&
24 Removing worktrees/abc: not a valid directory
26 test_cmp expect actual &&
27 ! test -f .git/worktrees/abc &&
28 ! test -d .git/worktrees
31 test_expect_success
'prune directories without gitdir' '
32 mkdir -p .git/worktrees/def/abc &&
33 : >.git/worktrees/def/def &&
35 Removing worktrees/def: gitdir file does not exist
37 git worktree prune --verbose >actual &&
38 test_cmp expect actual &&
39 ! test -d .git/worktrees/def &&
40 ! test -d .git/worktrees
43 test_expect_success SANITY
'prune directories with unreadable gitdir' '
44 mkdir -p .git/worktrees/def/abc &&
45 : >.git/worktrees/def/def &&
46 : >.git/worktrees/def/gitdir &&
47 chmod u-r .git/worktrees/def/gitdir &&
48 git worktree prune --verbose >actual &&
49 test_i18ngrep "Removing worktrees/def: unable to read gitdir file" actual &&
50 ! test -d .git/worktrees/def &&
51 ! test -d .git/worktrees
54 test_expect_success
'prune directories with invalid gitdir' '
55 mkdir -p .git/worktrees/def/abc &&
56 : >.git/worktrees/def/def &&
57 : >.git/worktrees/def/gitdir &&
58 git worktree prune --verbose >actual &&
59 test_i18ngrep "Removing worktrees/def: invalid gitdir file" actual &&
60 ! test -d .git/worktrees/def &&
61 ! test -d .git/worktrees
64 test_expect_success
'prune directories with gitdir pointing to nowhere' '
65 mkdir -p .git/worktrees/def/abc &&
66 : >.git/worktrees/def/def &&
67 echo "$(pwd)"/nowhere >.git/worktrees/def/gitdir &&
68 git worktree prune --verbose >actual &&
69 test_i18ngrep "Removing worktrees/def: gitdir file points to non-existent location" actual &&
70 ! test -d .git/worktrees/def &&
71 ! test -d .git/worktrees
74 test_expect_success
'not prune locked checkout' '
75 test_when_finished rm -r .git/worktrees &&
76 mkdir -p .git/worktrees/ghi &&
77 : >.git/worktrees/ghi/locked &&
79 test -d .git/worktrees/ghi
82 test_expect_success
'not prune recent checkouts' '
83 test_when_finished rm -r .git/worktrees &&
84 git worktree add jlm HEAD &&
85 test -d .git/worktrees/jlm &&
87 git worktree prune --verbose --expire=2.days.ago &&
88 test -d .git/worktrees/jlm
91 test_expect_success
'not prune proper checkouts' '
92 test_when_finished rm -r .git/worktrees &&
93 git worktree add --detach "$PWD/nop" main &&
95 test -d .git/worktrees/nop
98 test_expect_success
'prune duplicate (linked/linked)' '
99 test_when_finished rm -fr .git/worktrees w1 w2 &&
100 git worktree add --detach w1 &&
101 git worktree add --detach w2 &&
102 sed "s/w2/w1/" .git/worktrees/w2/gitdir >.git/worktrees/w2/gitdir.new &&
103 mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir &&
104 git worktree prune --verbose >actual &&
105 test_i18ngrep "duplicate entry" actual &&
106 test -d .git/worktrees/w1 &&
107 ! test -d .git/worktrees/w2
110 test_expect_success
'prune duplicate (main/linked)' '
111 test_when_finished rm -fr repo wt &&
112 test_create_repo repo &&
113 test_commit -C repo x &&
114 git -C repo worktree add --detach ../wt &&
117 git -C wt worktree prune --verbose >actual &&
118 test_i18ngrep "duplicate entry" actual &&
119 ! test -d .git/worktrees/wt