t1010: fix unnoticed failure on Windows
[git.git] / t / t2402-worktree-list.sh
blob4012bd67b04445dd7012e75bcb9bee4057c58ec5
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 with locked annotation' '
68 test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
69 git worktree add --detach locked main &&
70 git worktree add --detach unlocked main &&
71 git worktree lock locked &&
72 test_when_finished "git worktree unlock locked" &&
73 git worktree list >out &&
74 grep "/locked *[0-9a-f].* locked$" out &&
75 ! grep "/unlocked *[0-9a-f].* locked$" out
78 test_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
95 test_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
110 test_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$"
120 test_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
129 test_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
142 test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
143 test_must_fail git worktree list --verbose --porcelain
146 test_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
162 test_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 &&
170 test_cmp actual expect
173 test_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" &&
178 git push bare1 main &&
179 git reset --hard HEAD^
182 test_expect_success '"list" all worktrees from bare main' '
183 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
184 git -C bare1 worktree add --detach ../there main &&
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 &&
187 git -C bare1 worktree list >out &&
188 sed "s/ */ /g" <out >actual &&
189 test_cmp expect actual
192 test_expect_success '"list" all worktrees --porcelain from bare main' '
193 test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
194 git -C bare1 worktree add --detach ../there main &&
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
206 test_expect_success '"list" all worktrees from linked with a bare main' '
207 test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
208 git -C bare1 worktree add --detach ../there main &&
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 &&
211 git -C there worktree list >out &&
212 sed "s/ */ /g" <out >actual &&
213 test_cmp expect actual
216 test_expect_success 'bare repo cleanup' '
217 rm -rf bare1
220 test_expect_success 'broken main worktree still at the top' '
221 git init broken-main &&
223 cd broken-main &&
224 test_commit new &&
225 git worktree add linked &&
226 cat >expected <<-EOF &&
227 worktree $(pwd)
228 HEAD $ZERO_OID
231 cd linked &&
232 echo "worktree $(pwd)" >expected &&
233 (cd ../ && test-tool ref-store main create-symref HEAD .broken ) &&
234 git worktree list --porcelain >out &&
235 head -n 3 out >actual &&
236 test_cmp ../expected actual &&
237 git worktree list >out &&
238 head -n 1 out >actual.2 &&
239 grep -F "(error)" actual.2
243 test_expect_success 'linked worktrees are sorted' '
244 mkdir sorted &&
245 git init sorted/main &&
247 cd sorted/main &&
248 test_tick &&
249 test_commit new &&
250 git worktree add ../first &&
251 git worktree add ../second &&
252 git worktree list --porcelain >out &&
253 grep ^worktree out >actual
254 ) &&
255 cat >expected <<-EOF &&
256 worktree $(pwd)/sorted/main
257 worktree $(pwd)/sorted/first
258 worktree $(pwd)/sorted/second
260 test_cmp expected sorted/main/actual
263 test_expect_success 'worktree path when called in .git directory' '
264 git worktree list >list1 &&
265 git -C .git worktree list >list2 &&
266 test_cmp list1 list2
269 test_done