Git fork
1#!/bin/sh
2
3test_description='test git worktree list'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8. ./test-lib.sh
9
10test_expect_success 'setup' '
11 test_commit init
12'
13
14test_expect_success 'rev-parse --git-common-dir on main worktree' '
15 git rev-parse --git-common-dir >actual &&
16 echo .git >expected &&
17 test_cmp expected actual &&
18 mkdir sub &&
19 git -C sub rev-parse --git-common-dir >actual2 &&
20 echo ../.git >expected2 &&
21 test_cmp expected2 actual2
22'
23
24test_expect_success 'rev-parse --git-path objects linked worktree' '
25 echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
26 test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
27 git worktree add --detach linked-tree main &&
28 git -C linked-tree rev-parse --git-path objects >actual &&
29 test_cmp expect actual
30'
31
32test_expect_success '"list" all worktrees from main' '
33 echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
34 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
35 git worktree add --detach here main &&
36 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
37 git worktree list >out &&
38 sed "s/ */ /g" <out >actual &&
39 test_cmp expect actual
40'
41
42test_expect_success '"list" all worktrees from linked' '
43 echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
44 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
45 git worktree add --detach here main &&
46 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
47 git -C here worktree list >out &&
48 sed "s/ */ /g" <out >actual &&
49 test_cmp expect actual
50'
51
52test_expect_success '"list" all worktrees --porcelain' '
53 echo "worktree $(git rev-parse --show-toplevel)" >expect &&
54 echo "HEAD $(git rev-parse HEAD)" >>expect &&
55 echo "branch $(git symbolic-ref HEAD)" >>expect &&
56 echo >>expect &&
57 test_when_finished "rm -rf here actual expect && git worktree prune" &&
58 git worktree add --detach here main &&
59 echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
60 echo "HEAD $(git rev-parse HEAD)" >>expect &&
61 echo "detached" >>expect &&
62 echo >>expect &&
63 git worktree list --porcelain >actual &&
64 test_cmp expect actual
65'
66
67test_expect_success '"list" all worktrees --porcelain -z' '
68 test_when_finished "rm -rf here _actual actual expect &&
69 git worktree prune" &&
70 printf "worktree %sQHEAD %sQbranch %sQQ" \
71 "$(git rev-parse --show-toplevel)" \
72 $(git rev-parse HEAD --symbolic-full-name HEAD) >expect &&
73 git worktree add --detach here main &&
74 printf "worktree %sQHEAD %sQdetachedQQ" \
75 "$(git -C here rev-parse --show-toplevel)" \
76 "$(git rev-parse HEAD)" >>expect &&
77 git worktree list --porcelain -z >_actual &&
78 nul_to_q <_actual >actual &&
79 test_cmp expect actual
80'
81
82test_expect_success '"list" -z fails without --porcelain' '
83 test_must_fail git worktree list -z
84'
85
86test_expect_success '"list" all worktrees with locked annotation' '
87 test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
88 git worktree add --detach locked main &&
89 git worktree add --detach unlocked main &&
90 git worktree lock locked &&
91 test_when_finished "git worktree unlock locked" &&
92 git worktree list >out &&
93 grep "/locked *[0-9a-f].* locked$" out &&
94 ! grep "/unlocked *[0-9a-f].* locked$" out
95'
96
97test_expect_success '"list" all worktrees --porcelain with locked' '
98 test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" &&
99 echo "locked" >expect &&
100 echo "locked with reason" >>expect &&
101 git worktree add --detach locked1 &&
102 git worktree add --detach locked2 &&
103 # unlocked worktree should not be annotated with "locked"
104 git worktree add --detach unlocked &&
105 git worktree lock locked1 &&
106 test_when_finished "git worktree unlock locked1" &&
107 git worktree lock locked2 --reason "with reason" &&
108 test_when_finished "git worktree unlock locked2" &&
109 git worktree list --porcelain >out &&
110 grep "^locked" out >actual &&
111 test_cmp expect actual
112'
113
114test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' '
115 test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" &&
116 printf "locked \"locked\\\\r\\\\nreason\"\n" >expect &&
117 printf "locked \"locked\\\\nreason\"\n" >>expect &&
118 git worktree add --detach locked_lf &&
119 git worktree add --detach locked_crlf &&
120 git worktree lock locked_lf --reason "$(printf "locked\nreason")" &&
121 test_when_finished "git worktree unlock locked_lf" &&
122 git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" &&
123 test_when_finished "git worktree unlock locked_crlf" &&
124 git worktree list --porcelain >out &&
125 grep "^locked" out >actual &&
126 test_cmp expect actual
127'
128
129test_expect_success '"list" all worktrees with prunable annotation' '
130 test_when_finished "rm -rf prunable unprunable out && git worktree prune" &&
131 git worktree add --detach prunable &&
132 git worktree add --detach unprunable &&
133 rm -rf prunable &&
134 git worktree list >out &&
135 grep "/prunable *[0-9a-f].* prunable$" out &&
136 ! grep "/unprunable *[0-9a-f].* prunable$"
137'
138
139test_expect_success '"list" all worktrees --porcelain with prunable' '
140 test_when_finished "rm -rf prunable out && git worktree prune" &&
141 git worktree add --detach prunable &&
142 rm -rf prunable &&
143 git worktree list --porcelain >out &&
144 sed -n "/^worktree .*\/prunable$/,/^$/p" <out >only_prunable &&
145 test_grep "^prunable gitdir file points to non-existent location$" only_prunable
146'
147
148test_expect_success '"list" all worktrees with prunable consistent with "prune"' '
149 test_when_finished "rm -rf prunable unprunable out && git worktree prune" &&
150 git worktree add --detach prunable &&
151 git worktree add --detach unprunable &&
152 rm -rf prunable &&
153 git worktree list >out &&
154 grep "/prunable *[0-9a-f].* prunable$" out &&
155 ! grep "/unprunable *[0-9a-f].* unprunable$" out &&
156 git worktree prune --verbose 2>out &&
157 test_grep "^Removing worktrees/prunable" out &&
158 test_grep ! "^Removing worktrees/unprunable" out
159'
160
161test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
162 test_must_fail git worktree list --verbose --porcelain
163'
164
165test_expect_success '"list" all worktrees --verbose with locked' '
166 test_when_finished "rm -rf locked1 locked2 out actual expect && git worktree prune" &&
167 git worktree add locked1 --detach &&
168 git worktree add locked2 --detach &&
169 git worktree lock locked1 &&
170 test_when_finished "git worktree unlock locked1" &&
171 git worktree lock locked2 --reason "with reason" &&
172 test_when_finished "git worktree unlock locked2" &&
173 echo "$(git -C locked2 rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
174 printf "\tlocked: with reason\n" >>expect &&
175 git worktree list --verbose >out &&
176 grep "/locked1 *[0-9a-f].* locked$" out &&
177 sed -n "s/ */ /g;/\/locked2 *[0-9a-f].*$/,/locked: .*$/p" <out >actual &&
178 test_cmp actual expect
179'
180
181test_expect_success '"list" all worktrees --verbose with prunable' '
182 test_when_finished "rm -rf prunable out actual expect && git worktree prune" &&
183 git worktree add prunable --detach &&
184 echo "$(git -C prunable rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
185 printf "\tprunable: gitdir file points to non-existent location\n" >>expect &&
186 rm -rf prunable &&
187 git worktree list --verbose >out &&
188 sed -n "s/ */ /g;/\/prunable *[0-9a-f].*$/,/prunable: .*$/p" <out >actual &&
189 test_cmp actual expect
190'
191
192test_expect_success 'bare repo setup' '
193 git init --bare bare1 &&
194 echo "data" >file1 &&
195 git add file1 &&
196 git commit -m"File1: add data" &&
197 git push bare1 main &&
198 git reset --hard HEAD^
199'
200
201test_expect_success '"list" all worktrees from bare main' '
202 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
203 git -C bare1 worktree add --detach ../there main &&
204 echo "$(pwd)/bare1 (bare)" >expect &&
205 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
206 git -C bare1 worktree list >out &&
207 sed "s/ */ /g" <out >actual &&
208 test_cmp expect actual
209'
210
211test_expect_success '"list" all worktrees --porcelain from bare main' '
212 test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
213 git -C bare1 worktree add --detach ../there main &&
214 echo "worktree $(pwd)/bare1" >expect &&
215 echo "bare" >>expect &&
216 echo >>expect &&
217 echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
218 echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
219 echo "detached" >>expect &&
220 echo >>expect &&
221 git -C bare1 worktree list --porcelain >actual &&
222 test_cmp expect actual
223'
224
225test_expect_success '"list" all worktrees from linked with a bare main' '
226 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
227 git -C bare1 worktree add --detach ../there main &&
228 echo "$(pwd)/bare1 (bare)" >expect &&
229 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
230 git -C there worktree list >out &&
231 sed "s/ */ /g" <out >actual &&
232 test_cmp expect actual
233'
234
235test_expect_success 'bare repo cleanup' '
236 rm -rf bare1
237'
238
239test_expect_success 'broken main worktree still at the top' '
240 git init broken-main &&
241 (
242 cd broken-main &&
243 test_commit new &&
244 git worktree add linked &&
245 cat >expected <<-EOF &&
246 worktree $(pwd)
247 HEAD $ZERO_OID
248
249 EOF
250 cd linked &&
251 echo "worktree $(pwd)" >expected &&
252 (cd ../ && test-tool ref-store main create-symref HEAD .broken ) &&
253 git worktree list --porcelain >out &&
254 head -n 3 out >actual &&
255 test_cmp ../expected actual &&
256 git worktree list >out &&
257 head -n 1 out >actual.2 &&
258 grep -F "(error)" actual.2
259 )
260'
261
262test_expect_success 'linked worktrees are sorted' '
263 test_when_finished "rm -rf sorted" &&
264 mkdir sorted &&
265 git init sorted/main &&
266 (
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
279 EOF
280 test_cmp expected sorted/main/actual
281'
282
283test_expect_success 'linked worktrees with relative paths are shown with absolute paths' '
284 test_when_finished "rm -rf sorted" &&
285 mkdir sorted &&
286 git init sorted/main &&
287 (
288 cd sorted/main &&
289 test_tick &&
290 test_commit new &&
291 git worktree add --relative-paths ../first &&
292 git worktree add ../second &&
293 git worktree list --porcelain >out &&
294 grep ^worktree out >actual
295 ) &&
296 cat >expected <<-EOF &&
297 worktree $(pwd)/sorted/main
298 worktree $(pwd)/sorted/first
299 worktree $(pwd)/sorted/second
300 EOF
301 test_cmp expected sorted/main/actual
302'
303
304test_expect_success 'worktree path when called in .git directory' '
305 git worktree list >list1 &&
306 git -C .git worktree list >list2 &&
307 test_cmp list1 list2
308'
309
310test_done