]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2402-worktree-list.sh
The third batch
[thirdparty/git.git] / t / t2402-worktree-list.sh
CommitLineData
bb9c03b8
MR
1#!/bin/sh
2
3test_description='test git worktree list'
4
883b98ef 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
c65d18cb 8TEST_PASSES_SANITIZE_LEAK=true
bb9c03b8
MR
9. ./test-lib.sh
10
11test_expect_success 'setup' '
12 test_commit init
13'
14
098aa867 15test_expect_success 'rev-parse --git-common-dir on main worktree' '
17f1365d
NTND
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 &&
5de8a549 21 echo ../.git >expected2 &&
17f1365d
NTND
22 test_cmp expected2 actual2
23'
24
098aa867 25test_expect_success 'rev-parse --git-path objects linked worktree' '
5de8a549 26 echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
210e5dba 27 test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
883b98ef 28 git worktree add --detach linked-tree main &&
5de8a549
MR
29 git -C linked-tree rev-parse --git-path objects >actual &&
30 test_cmp expect actual
31'
32
bb9c03b8
MR
33test_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 &&
210e5dba 35 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
883b98ef 36 git worktree add --detach here main &&
bb9c03b8 37 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
210e5dba
PC
38 git worktree list >out &&
39 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
40 test_cmp expect actual
41'
42
43test_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 &&
210e5dba 45 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
883b98ef 46 git worktree add --detach here main &&
bb9c03b8 47 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
210e5dba
PC
48 git -C here worktree list >out &&
49 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
50 test_cmp expect actual
51'
52
53test_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 &&
210e5dba 58 test_when_finished "rm -rf here actual expect && git worktree prune" &&
883b98ef 59 git worktree add --detach here main &&
bb9c03b8
MR
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
66'
67
d97eb302
PW
68test_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
81'
82
83test_expect_success '"list" -z fails without --porcelain' '
84 test_must_fail git worktree list -z
85'
86
8d889311 87test_expect_success '"list" all worktrees with locked annotation' '
c57b3367 88 test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
883b98ef
JS
89 git worktree add --detach locked main &&
90 git worktree add --detach unlocked main &&
c57b3367 91 git worktree lock locked &&
47409e75 92 test_when_finished "git worktree unlock locked" &&
c57b3367
RS
93 git worktree list >out &&
94 grep "/locked *[0-9a-f].* locked$" out &&
95 ! grep "/unlocked *[0-9a-f].* locked$" out
96'
97
862c723d
RS
98test_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
113'
114
115test_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
128'
129
9b19a58f
RS
130test_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$"
138'
139
140test_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 &&
6789275d 146 test_grep "^prunable gitdir file points to non-existent location$" only_prunable
9b19a58f
RS
147'
148
149test_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 &&
da8fb6be 157 git worktree prune --verbose 2>out &&
6789275d
JH
158 test_grep "^Removing worktrees/prunable" out &&
159 test_grep ! "^Removing worktrees/unprunable" out
9b19a58f
RS
160'
161
076b444a
RS
162test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
163 test_must_fail git worktree list --verbose --porcelain
164'
165
166test_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
180'
181
182test_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 &&
1108cea7 190 test_cmp actual expect
076b444a
RS
191'
192
bb9c03b8
MR
193test_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" &&
883b98ef 198 git push bare1 main &&
bb9c03b8
MR
199 git reset --hard HEAD^
200'
201
202test_expect_success '"list" all worktrees from bare main' '
210e5dba 203 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
883b98ef 204 git -C bare1 worktree add --detach ../there main &&
bb9c03b8
MR
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 &&
210e5dba
PC
207 git -C bare1 worktree list >out &&
208 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
209 test_cmp expect actual
210'
211
212test_expect_success '"list" all worktrees --porcelain from bare main' '
210e5dba 213 test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
883b98ef 214 git -C bare1 worktree add --detach ../there main &&
bb9c03b8
MR
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
224'
225
226test_expect_success '"list" all worktrees from linked with a bare main' '
210e5dba 227 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
883b98ef 228 git -C bare1 worktree add --detach ../there main &&
bb9c03b8
MR
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 &&
210e5dba
PC
231 git -C there worktree list >out &&
232 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
233 test_cmp expect actual
234'
235
236test_expect_success 'bare repo cleanup' '
237 rm -rf bare1
238'
239
a234563a
NTND
240test_expect_success 'broken main worktree still at the top' '
241 git init broken-main &&
242 (
243 cd broken-main &&
244 test_commit new &&
245 git worktree add linked &&
246 cat >expected <<-EOF &&
247 worktree $(pwd)
8125a58b 248 HEAD $ZERO_OID
a234563a
NTND
249
250 EOF
251 cd linked &&
252 echo "worktree $(pwd)" >expected &&
100ac47b 253 (cd ../ && test-tool ref-store main create-symref HEAD .broken ) &&
210e5dba
PC
254 git worktree list --porcelain >out &&
255 head -n 3 out >actual &&
a234563a 256 test_cmp ../expected actual &&
210e5dba
PC
257 git worktree list >out &&
258 head -n 1 out >actual.2 &&
a234563a
NTND
259 grep -F "(error)" actual.2
260 )
261'
262
4df1d4d4
NTND
263test_expect_success 'linked worktrees are 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 &&
210e5dba
PC
272 git worktree list --porcelain >out &&
273 grep ^worktree out >actual
4df1d4d4
NTND
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
4d864895 283test_expect_success 'worktree path when called in .git directory' '
64d1022e 284 git worktree list >list1 &&
4d864895
HV
285 git -C .git worktree list >list2 &&
286 test_cmp list1 list2
287'
288
bb9c03b8 289test_done