blob: 79e0fce2d90fb9e947a76052c32e41d20519e6bd [file] [log] [blame]
Michael Rappazzobb9c03b2015-10-08 13:01:05 -04001#!/bin/sh
2
3test_description='test git worktree list'
4
Johannes Schindelin883b98e2020-11-18 23:44:22 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Michael Rappazzobb9c03b2015-10-08 13:01:05 -04008. ./test-lib.sh
9
10test_expect_success 'setup' '
11 test_commit init
12'
13
Johannes Schindelin098aa862017-02-17 17:59:06 +010014test_expect_success 'rev-parse --git-common-dir on main worktree' '
Nguyễn Thái Ngọc Duy17f13652016-02-12 11:31:45 +070015 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 &&
Michael Rappazzo5de8a542017-02-17 17:59:02 +010020 echo ../.git >expected2 &&
Nguyễn Thái Ngọc Duy17f13652016-02-12 11:31:45 +070021 test_cmp expected2 actual2
22'
23
Johannes Schindelin098aa862017-02-17 17:59:06 +010024test_expect_success 'rev-parse --git-path objects linked worktree' '
Michael Rappazzo5de8a542017-02-17 17:59:02 +010025 echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +053026 test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000027 git worktree add --detach linked-tree main &&
Michael Rappazzo5de8a542017-02-17 17:59:02 +010028 git -C linked-tree rev-parse --git-path objects >actual &&
29 test_cmp expect actual
30'
31
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040032test_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 &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +053034 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000035 git worktree add --detach here main &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040036 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +053037 git worktree list >out &&
38 sed "s/ */ /g" <out >actual &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040039 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 &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +053044 test_when_finished "rm -rf here out actual expect && git worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000045 git worktree add --detach here main &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040046 echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +053047 git -C here worktree list >out &&
48 sed "s/ */ /g" <out >actual &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040049 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 &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +053057 test_when_finished "rm -rf here actual expect && git worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000058 git worktree add --detach here main &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -040059 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
Phillip Woodd97eb302022-03-31 16:21:28 +000067test_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
Johannes Schindelin8d889312020-11-03 11:48:07 +000086test_expect_success '"list" all worktrees with locked annotation' '
Rafael Silvac57b3362020-10-11 10:11:52 +000087 test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +000088 git worktree add --detach locked main &&
89 git worktree add --detach unlocked main &&
Rafael Silvac57b3362020-10-11 10:11:52 +000090 git worktree lock locked &&
Rafael Silva47409e72021-01-27 09:03:07 +010091 test_when_finished "git worktree unlock locked" &&
Rafael Silvac57b3362020-10-11 10:11:52 +000092 git worktree list >out &&
93 grep "/locked *[0-9a-f].* locked$" out &&
94 ! grep "/unlocked *[0-9a-f].* locked$" out
95'
96
Rafael Silva862c7232021-01-27 09:03:08 +010097test_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
Rafael Silva9b19a582021-01-27 09:03:09 +0100129test_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_i18ngrep "^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 &&
Eric Sunshineda8fb6b2021-12-02 22:44:19 -0500156 git worktree prune --verbose 2>out &&
Rafael Silva9b19a582021-01-27 09:03:09 +0100157 test_i18ngrep "^Removing worktrees/prunable" out &&
158 test_i18ngrep ! "^Removing worktrees/unprunable" out
159'
160
Rafael Silva076b4442021-01-27 09:03:10 +0100161test_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 &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +0100189 test_cmp actual expect
Rafael Silva076b4442021-01-27 09:03:10 +0100190'
191
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400192test_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" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000197 git push bare1 main &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400198 git reset --hard HEAD^
199'
200
201test_expect_success '"list" all worktrees from bare main' '
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530202 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000203 git -C bare1 worktree add --detach ../there main &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400204 echo "$(pwd)/bare1 (bare)" >expect &&
205 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530206 git -C bare1 worktree list >out &&
207 sed "s/ */ /g" <out >actual &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400208 test_cmp expect actual
209'
210
211test_expect_success '"list" all worktrees --porcelain from bare main' '
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530212 test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000213 git -C bare1 worktree add --detach ../there main &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400214 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' '
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530226 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
Johannes Schindelin883b98e2020-11-18 23:44:22 +0000227 git -C bare1 worktree add --detach ../there main &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400228 echo "$(pwd)/bare1 (bare)" >expect &&
229 echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530230 git -C there worktree list >out &&
231 sed "s/ */ /g" <out >actual &&
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400232 test_cmp expect actual
233'
234
235test_expect_success 'bare repo cleanup' '
236 rm -rf bare1
237'
238
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700239test_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)
brian m. carlson8125a582018-05-13 02:24:13 +0000247 HEAD $ZERO_OID
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700248
249 EOF
250 cd linked &&
251 echo "worktree $(pwd)" >expected &&
Han-Wen Nienhuys100ac472021-08-02 16:53:30 +0000252 (cd ../ && test-tool ref-store main create-symref HEAD .broken ) &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530253 git worktree list --porcelain >out &&
254 head -n 3 out >actual &&
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700255 test_cmp ../expected actual &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530256 git worktree list >out &&
257 head -n 1 out >actual.2 &&
Nguyễn Thái Ngọc Duya2345632016-11-28 16:36:54 +0700258 grep -F "(error)" actual.2
259 )
260'
261
Nguyễn Thái Ngọc Duy4df1d4d2016-11-28 16:36:56 +0700262test_expect_success 'linked worktrees are sorted' '
263 mkdir sorted &&
264 git init sorted/main &&
265 (
266 cd sorted/main &&
267 test_tick &&
268 test_commit new &&
269 git worktree add ../first &&
270 git worktree add ../second &&
Prathamesh Chavan210e5db2017-04-04 03:05:57 +0530271 git worktree list --porcelain >out &&
272 grep ^worktree out >actual
Nguyễn Thái Ngọc Duy4df1d4d2016-11-28 16:36:56 +0700273 ) &&
274 cat >expected <<-EOF &&
275 worktree $(pwd)/sorted/main
276 worktree $(pwd)/sorted/first
277 worktree $(pwd)/sorted/second
278 EOF
279 test_cmp expected sorted/main/actual
280'
281
Hariom Verma4d864892020-03-04 07:00:00 +0000282test_expect_success 'worktree path when called in .git directory' '
Andrei Rybak64d10222020-03-22 22:14:22 +0100283 git worktree list >list1 &&
Hariom Verma4d864892020-03-04 07:00:00 +0000284 git -C .git worktree list >list2 &&
285 test_cmp list1 list2
286'
287
Michael Rappazzobb9c03b2015-10-08 13:01:05 -0400288test_done