]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2402-worktree-list.sh
Merge branch 'ab/detox-gettext-tests'
[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
bb9c03b8
MR
8. ./test-lib.sh
9
10test_expect_success 'setup' '
11 test_commit init
12'
13
098aa867 14test_expect_success 'rev-parse --git-common-dir on main worktree' '
17f1365d
NTND
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 &&
5de8a549 20 echo ../.git >expected2 &&
17f1365d
NTND
21 test_cmp expected2 actual2
22'
23
098aa867 24test_expect_success 'rev-parse --git-path objects linked worktree' '
5de8a549 25 echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
210e5dba 26 test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
883b98ef 27 git worktree add --detach linked-tree main &&
5de8a549
MR
28 git -C linked-tree rev-parse --git-path objects >actual &&
29 test_cmp expect actual
30'
31
bb9c03b8
MR
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 &&
210e5dba 34 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
883b98ef 35 git worktree add --detach here main &&
bb9c03b8 36 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
210e5dba
PC
37 git worktree list >out &&
38 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
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 &&
210e5dba 44 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
883b98ef 45 git worktree add --detach here main &&
bb9c03b8 46 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
210e5dba
PC
47 git -C here worktree list >out &&
48 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
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 &&
210e5dba 57 test_when_finished "rm -rf here actual expect && git worktree prune" &&
883b98ef 58 git worktree add --detach here main &&
bb9c03b8
MR
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
8d889311 67test_expect_success '"list" all worktrees with locked annotation' '
c57b3367 68 test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
883b98ef
JS
69 git worktree add --detach locked main &&
70 git worktree add --detach unlocked main &&
c57b3367 71 git worktree lock locked &&
47409e75 72 test_when_finished "git worktree unlock locked" &&
c57b3367
RS
73 git worktree list >out &&
74 grep "/locked *[0-9a-f].* locked$" out &&
75 ! grep "/unlocked *[0-9a-f].* locked$" out
76'
77
862c723d
RS
78test_expect_success '"list" all worktrees --porcelain with locked' '
79 test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" &&
80 echo "locked" >expect &&
81 echo "locked with reason" >>expect &&
82 git worktree add --detach locked1 &&
83 git worktree add --detach locked2 &&
84 # unlocked worktree should not be annotated with "locked"
85 git worktree add --detach unlocked &&
86 git worktree lock locked1 &&
87 test_when_finished "git worktree unlock locked1" &&
88 git worktree lock locked2 --reason "with reason" &&
89 test_when_finished "git worktree unlock locked2" &&
90 git worktree list --porcelain >out &&
91 grep "^locked" out >actual &&
92 test_cmp expect actual
93'
94
95test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' '
96 test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" &&
97 printf "locked \"locked\\\\r\\\\nreason\"\n" >expect &&
98 printf "locked \"locked\\\\nreason\"\n" >>expect &&
99 git worktree add --detach locked_lf &&
100 git worktree add --detach locked_crlf &&
101 git worktree lock locked_lf --reason "$(printf "locked\nreason")" &&
102 test_when_finished "git worktree unlock locked_lf" &&
103 git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" &&
104 test_when_finished "git worktree unlock locked_crlf" &&
105 git worktree list --porcelain >out &&
106 grep "^locked" out >actual &&
107 test_cmp expect actual
108'
109
9b19a58f
RS
110test_expect_success '"list" all worktrees with prunable annotation' '
111 test_when_finished "rm -rf prunable unprunable out && git worktree prune" &&
112 git worktree add --detach prunable &&
113 git worktree add --detach unprunable &&
114 rm -rf prunable &&
115 git worktree list >out &&
116 grep "/prunable *[0-9a-f].* prunable$" out &&
117 ! grep "/unprunable *[0-9a-f].* prunable$"
118'
119
120test_expect_success '"list" all worktrees --porcelain with prunable' '
121 test_when_finished "rm -rf prunable out && git worktree prune" &&
122 git worktree add --detach prunable &&
123 rm -rf prunable &&
124 git worktree list --porcelain >out &&
125 sed -n "/^worktree .*\/prunable$/,/^$/p" <out >only_prunable &&
126 test_i18ngrep "^prunable gitdir file points to non-existent location$" only_prunable
127'
128
129test_expect_success '"list" all worktrees with prunable consistent with "prune"' '
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].* unprunable$" out &&
137 git worktree prune --verbose >out &&
138 test_i18ngrep "^Removing worktrees/prunable" out &&
139 test_i18ngrep ! "^Removing worktrees/unprunable" out
140'
141
076b444a
RS
142test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
143 test_must_fail git worktree list --verbose --porcelain
144'
145
146test_expect_success '"list" all worktrees --verbose with locked' '
147 test_when_finished "rm -rf locked1 locked2 out actual expect && git worktree prune" &&
148 git worktree add locked1 --detach &&
149 git worktree add locked2 --detach &&
150 git worktree lock locked1 &&
151 test_when_finished "git worktree unlock locked1" &&
152 git worktree lock locked2 --reason "with reason" &&
153 test_when_finished "git worktree unlock locked2" &&
154 echo "$(git -C locked2 rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
155 printf "\tlocked: with reason\n" >>expect &&
156 git worktree list --verbose >out &&
157 grep "/locked1 *[0-9a-f].* locked$" out &&
158 sed -n "s/ */ /g;/\/locked2 *[0-9a-f].*$/,/locked: .*$/p" <out >actual &&
159 test_cmp actual expect
160'
161
162test_expect_success '"list" all worktrees --verbose with prunable' '
163 test_when_finished "rm -rf prunable out actual expect && git worktree prune" &&
164 git worktree add prunable --detach &&
165 echo "$(git -C prunable rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
166 printf "\tprunable: gitdir file points to non-existent location\n" >>expect &&
167 rm -rf prunable &&
168 git worktree list --verbose >out &&
169 sed -n "s/ */ /g;/\/prunable *[0-9a-f].*$/,/prunable: .*$/p" <out >actual &&
1108cea7 170 test_cmp actual expect
076b444a
RS
171'
172
bb9c03b8
MR
173test_expect_success 'bare repo setup' '
174 git init --bare bare1 &&
175 echo "data" >file1 &&
176 git add file1 &&
177 git commit -m"File1: add data" &&
883b98ef 178 git push bare1 main &&
bb9c03b8
MR
179 git reset --hard HEAD^
180'
181
182test_expect_success '"list" all worktrees from bare main' '
210e5dba 183 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
883b98ef 184 git -C bare1 worktree add --detach ../there main &&
bb9c03b8
MR
185 echo "$(pwd)/bare1 (bare)" >expect &&
186 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
210e5dba
PC
187 git -C bare1 worktree list >out &&
188 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
189 test_cmp expect actual
190'
191
192test_expect_success '"list" all worktrees --porcelain from bare main' '
210e5dba 193 test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
883b98ef 194 git -C bare1 worktree add --detach ../there main &&
bb9c03b8
MR
195 echo "worktree $(pwd)/bare1" >expect &&
196 echo "bare" >>expect &&
197 echo >>expect &&
198 echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
199 echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
200 echo "detached" >>expect &&
201 echo >>expect &&
202 git -C bare1 worktree list --porcelain >actual &&
203 test_cmp expect actual
204'
205
206test_expect_success '"list" all worktrees from linked with a bare main' '
210e5dba 207 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
883b98ef 208 git -C bare1 worktree add --detach ../there main &&
bb9c03b8
MR
209 echo "$(pwd)/bare1 (bare)" >expect &&
210 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
210e5dba
PC
211 git -C there worktree list >out &&
212 sed "s/ */ /g" <out >actual &&
bb9c03b8
MR
213 test_cmp expect actual
214'
215
216test_expect_success 'bare repo cleanup' '
217 rm -rf bare1
218'
219
a234563a
NTND
220test_expect_success 'broken main worktree still at the top' '
221 git init broken-main &&
222 (
223 cd broken-main &&
224 test_commit new &&
225 git worktree add linked &&
226 cat >expected <<-EOF &&
227 worktree $(pwd)
8125a58b 228 HEAD $ZERO_OID
a234563a
NTND
229
230 EOF
231 cd linked &&
232 echo "worktree $(pwd)" >expected &&
233 echo "ref: .broken" >../.git/HEAD &&
210e5dba
PC
234 git worktree list --porcelain >out &&
235 head -n 3 out >actual &&
a234563a 236 test_cmp ../expected actual &&
210e5dba
PC
237 git worktree list >out &&
238 head -n 1 out >actual.2 &&
a234563a
NTND
239 grep -F "(error)" actual.2
240 )
241'
242
4df1d4d4
NTND
243test_expect_success 'linked worktrees are sorted' '
244 mkdir sorted &&
245 git init sorted/main &&
246 (
247 cd sorted/main &&
248 test_tick &&
249 test_commit new &&
250 git worktree add ../first &&
251 git worktree add ../second &&
210e5dba
PC
252 git worktree list --porcelain >out &&
253 grep ^worktree out >actual
4df1d4d4
NTND
254 ) &&
255 cat >expected <<-EOF &&
256 worktree $(pwd)/sorted/main
257 worktree $(pwd)/sorted/first
258 worktree $(pwd)/sorted/second
259 EOF
260 test_cmp expected sorted/main/actual
261'
262
4d864895 263test_expect_success 'worktree path when called in .git directory' '
64d1022e 264 git worktree list >list1 &&
4d864895
HV
265 git -C .git worktree list >list2 &&
266 test_cmp list1 list2
267'
268
bb9c03b8 269test_done