Sync with 2.44.1
[git/gitster.git] / t / t2402-worktree-list.sh
blob33ea9cb21ba07c9563530b54da06753eaa993fe2
1 #!/bin/sh
3 test_description='test git worktree list'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success 'setup' '
12 test_commit init
15 test_expect_success 'rev-parse --git-common-dir on main worktree' '
16 git rev-parse --git-common-dir >actual &&
17 echo .git >expected &&
18 test_cmp expected actual &&
19 mkdir sub &&
20 git -C sub rev-parse --git-common-dir >actual2 &&
21 echo ../.git >expected2 &&
22 test_cmp expected2 actual2
25 test_expect_success 'rev-parse --git-path objects linked worktree' '
26 echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
27 test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
28 git worktree add --detach linked-tree main &&
29 git -C linked-tree rev-parse --git-path objects >actual &&
30 test_cmp expect actual
33 test_expect_success '"list" all worktrees from main' '
34 echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
35 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
36 git worktree add --detach here main &&
37 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
38 git worktree list >out &&
39 sed "s/ */ /g" <out >actual &&
40 test_cmp expect actual
43 test_expect_success '"list" all worktrees from linked' '
44 echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
45 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
46 git worktree add --detach here main &&
47 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
48 git -C here worktree list >out &&
49 sed "s/ */ /g" <out >actual &&
50 test_cmp expect actual
53 test_expect_success '"list" all worktrees --porcelain' '
54 echo "worktree $(git rev-parse --show-toplevel)" >expect &&
55 echo "HEAD $(git rev-parse HEAD)" >>expect &&
56 echo "branch $(git symbolic-ref HEAD)" >>expect &&
57 echo >>expect &&
58 test_when_finished "rm -rf here actual expect && git worktree prune" &&
59 git worktree add --detach here main &&
60 echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
61 echo "HEAD $(git rev-parse HEAD)" >>expect &&
62 echo "detached" >>expect &&
63 echo >>expect &&
64 git worktree list --porcelain >actual &&
65 test_cmp expect actual
68 test_expect_success '"list" all worktrees --porcelain -z' '
69 test_when_finished "rm -rf here _actual actual expect &&
70 git worktree prune" &&
71 printf "worktree %sQHEAD %sQbranch %sQQ" \
72 "$(git rev-parse --show-toplevel)" \
73 $(git rev-parse HEAD --symbolic-full-name HEAD) >expect &&
74 git worktree add --detach here main &&
75 printf "worktree %sQHEAD %sQdetachedQQ" \
76 "$(git -C here rev-parse --show-toplevel)" \
77 "$(git rev-parse HEAD)" >>expect &&
78 git worktree list --porcelain -z >_actual &&
79 nul_to_q <_actual >actual &&
80 test_cmp expect actual
83 test_expect_success '"list" -z fails without --porcelain' '
84 test_must_fail git worktree list -z
87 test_expect_success '"list" all worktrees with locked annotation' '
88 test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
89 git worktree add --detach locked main &&
90 git worktree add --detach unlocked main &&
91 git worktree lock locked &&
92 test_when_finished "git worktree unlock locked" &&
93 git worktree list >out &&
94 grep "/locked *[0-9a-f].* locked$" out &&
95 ! grep "/unlocked *[0-9a-f].* locked$" out
98 test_expect_success '"list" all worktrees --porcelain with locked' '
99 test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" &&
100 echo "locked" >expect &&
101 echo "locked with reason" >>expect &&
102 git worktree add --detach locked1 &&
103 git worktree add --detach locked2 &&
104 # unlocked worktree should not be annotated with "locked"
105 git worktree add --detach unlocked &&
106 git worktree lock locked1 &&
107 test_when_finished "git worktree unlock locked1" &&
108 git worktree lock locked2 --reason "with reason" &&
109 test_when_finished "git worktree unlock locked2" &&
110 git worktree list --porcelain >out &&
111 grep "^locked" out >actual &&
112 test_cmp expect actual
115 test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' '
116 test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" &&
117 printf "locked \"locked\\\\r\\\\nreason\"\n" >expect &&
118 printf "locked \"locked\\\\nreason\"\n" >>expect &&
119 git worktree add --detach locked_lf &&
120 git worktree add --detach locked_crlf &&
121 git worktree lock locked_lf --reason "$(printf "locked\nreason")" &&
122 test_when_finished "git worktree unlock locked_lf" &&
123 git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" &&
124 test_when_finished "git worktree unlock locked_crlf" &&
125 git worktree list --porcelain >out &&
126 grep "^locked" out >actual &&
127 test_cmp expect actual
130 test_expect_success '"list" all worktrees with prunable annotation' '
131 test_when_finished "rm -rf prunable unprunable out && git worktree prune" &&
132 git worktree add --detach prunable &&
133 git worktree add --detach unprunable &&
134 rm -rf prunable &&
135 git worktree list >out &&
136 grep "/prunable *[0-9a-f].* prunable$" out &&
137 ! grep "/unprunable *[0-9a-f].* prunable$"
140 test_expect_success '"list" all worktrees --porcelain with prunable' '
141 test_when_finished "rm -rf prunable out && git worktree prune" &&
142 git worktree add --detach prunable &&
143 rm -rf prunable &&
144 git worktree list --porcelain >out &&
145 sed -n "/^worktree .*\/prunable$/,/^$/p" <out >only_prunable &&
146 test_grep "^prunable gitdir file points to non-existent location$" only_prunable
149 test_expect_success '"list" all worktrees with prunable consistent with "prune"' '
150 test_when_finished "rm -rf prunable unprunable out && git worktree prune" &&
151 git worktree add --detach prunable &&
152 git worktree add --detach unprunable &&
153 rm -rf prunable &&
154 git worktree list >out &&
155 grep "/prunable *[0-9a-f].* prunable$" out &&
156 ! grep "/unprunable *[0-9a-f].* unprunable$" out &&
157 git worktree prune --verbose 2>out &&
158 test_grep "^Removing worktrees/prunable" out &&
159 test_grep ! "^Removing worktrees/unprunable" out
162 test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
163 test_must_fail git worktree list --verbose --porcelain
166 test_expect_success '"list" all worktrees --verbose with locked' '
167 test_when_finished "rm -rf locked1 locked2 out actual expect && git worktree prune" &&
168 git worktree add locked1 --detach &&
169 git worktree add locked2 --detach &&
170 git worktree lock locked1 &&
171 test_when_finished "git worktree unlock locked1" &&
172 git worktree lock locked2 --reason "with reason" &&
173 test_when_finished "git worktree unlock locked2" &&
174 echo "$(git -C locked2 rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
175 printf "\tlocked: with reason\n" >>expect &&
176 git worktree list --verbose >out &&
177 grep "/locked1 *[0-9a-f].* locked$" out &&
178 sed -n "s/ */ /g;/\/locked2 *[0-9a-f].*$/,/locked: .*$/p" <out >actual &&
179 test_cmp actual expect
182 test_expect_success '"list" all worktrees --verbose with prunable' '
183 test_when_finished "rm -rf prunable out actual expect && git worktree prune" &&
184 git worktree add prunable --detach &&
185 echo "$(git -C prunable rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
186 printf "\tprunable: gitdir file points to non-existent location\n" >>expect &&
187 rm -rf prunable &&
188 git worktree list --verbose >out &&
189 sed -n "s/ */ /g;/\/prunable *[0-9a-f].*$/,/prunable: .*$/p" <out >actual &&
190 test_cmp actual expect
193 test_expect_success 'bare repo setup' '
194 git init --bare bare1 &&
195 echo "data" >file1 &&
196 git add file1 &&
197 git commit -m"File1: add data" &&
198 git push bare1 main &&
199 git reset --hard HEAD^
202 test_expect_success '"list" all worktrees from bare main' '
203 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
204 git -C bare1 worktree add --detach ../there main &&
205 echo "$(pwd)/bare1 (bare)" >expect &&
206 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
207 git -C bare1 worktree list >out &&
208 sed "s/ */ /g" <out >actual &&
209 test_cmp expect actual
212 test_expect_success '"list" all worktrees --porcelain from bare main' '
213 test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
214 git -C bare1 worktree add --detach ../there main &&
215 echo "worktree $(pwd)/bare1" >expect &&
216 echo "bare" >>expect &&
217 echo >>expect &&
218 echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
219 echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
220 echo "detached" >>expect &&
221 echo >>expect &&
222 git -C bare1 worktree list --porcelain >actual &&
223 test_cmp expect actual
226 test_expect_success '"list" all worktrees from linked with a bare main' '
227 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
228 git -C bare1 worktree add --detach ../there main &&
229 echo "$(pwd)/bare1 (bare)" >expect &&
230 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
231 git -C there worktree list >out &&
232 sed "s/ */ /g" <out >actual &&
233 test_cmp expect actual
236 test_expect_success 'bare repo cleanup' '
237 rm -rf bare1
240 test_expect_success 'broken main worktree still at the top' '
241 git init broken-main &&
243 cd broken-main &&
244 test_commit new &&
245 git worktree add linked &&
246 cat >expected <<-EOF &&
247 worktree $(pwd)
248 HEAD $ZERO_OID
251 cd linked &&
252 echo "worktree $(pwd)" >expected &&
253 (cd ../ && test-tool ref-store main create-symref HEAD .broken ) &&
254 git worktree list --porcelain >out &&
255 head -n 3 out >actual &&
256 test_cmp ../expected actual &&
257 git worktree list >out &&
258 head -n 1 out >actual.2 &&
259 grep -F "(error)" actual.2
263 test_expect_success 'linked worktrees are sorted' '
264 mkdir sorted &&
265 git init sorted/main &&
267 cd sorted/main &&
268 test_tick &&
269 test_commit new &&
270 git worktree add ../first &&
271 git worktree add ../second &&
272 git worktree list --porcelain >out &&
273 grep ^worktree out >actual
274 ) &&
275 cat >expected <<-EOF &&
276 worktree $(pwd)/sorted/main
277 worktree $(pwd)/sorted/first
278 worktree $(pwd)/sorted/second
280 test_cmp expected sorted/main/actual
283 test_expect_success 'worktree path when called in .git directory' '
284 git worktree list >list1 &&
285 git -C .git worktree list >list2 &&
286 test_cmp list1 list2
289 test_done