Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / t / t2402-worktree-list.sh
blob79e0fce2d90fb9e947a76052c32e41d20519e6bd
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-lib.sh
10 test_expect_success 'setup' '
11 test_commit init
14 test_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
24 test_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
32 test_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
42 test_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
52 test_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
67 test_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
82 test_expect_success '"list" -z fails without --porcelain' '
83 test_must_fail git worktree list -z
86 test_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
97 test_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
114 test_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
129 test_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$"
139 test_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_i18ngrep "^prunable gitdir file points to non-existent location$" only_prunable
148 test_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_i18ngrep "^Removing worktrees/prunable" out &&
158 test_i18ngrep ! "^Removing worktrees/unprunable" out
161 test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
162 test_must_fail git worktree list --verbose --porcelain
165 test_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
181 test_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
192 test_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^
201 test_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
211 test_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
225 test_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
235 test_expect_success 'bare repo cleanup' '
236 rm -rf bare1
239 test_expect_success 'broken main worktree still at the top' '
240 git init broken-main &&
242 cd broken-main &&
243 test_commit new &&
244 git worktree add linked &&
245 cat >expected <<-EOF &&
246 worktree $(pwd)
247 HEAD $ZERO_OID
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
262 test_expect_success 'linked worktrees are sorted' '
263 mkdir sorted &&
264 git init sorted/main &&
266 cd sorted/main &&
267 test_tick &&
268 test_commit new &&
269 git worktree add ../first &&
270 git worktree add ../second &&
271 git worktree list --porcelain >out &&
272 grep ^worktree out >actual
273 ) &&
274 cat >expected <<-EOF &&
275 worktree $(pwd)/sorted/main
276 worktree $(pwd)/sorted/first
277 worktree $(pwd)/sorted/second
279 test_cmp expected sorted/main/actual
282 test_expect_success 'worktree path when called in .git directory' '
283 git worktree list >list1 &&
284 git -C .git worktree list >list2 &&
285 test_cmp list1 list2
288 test_done