]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3203-branch-output.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t3203-branch-output.sh
CommitLineData
0afc3044
JK
1#!/bin/sh
2
3test_description='git branch display tests'
4. ./test-lib.sh
11b087ad 5. "$TEST_DIRECTORY"/lib-terminal.sh
0afc3044
JK
6
7test_expect_success 'make commits' '
8 echo content >file &&
9 git add file &&
10 git commit -m one &&
1eee0a42 11 git branch -M main &&
0afc3044
JK
12 echo content >>file &&
13 git commit -a -m two
14'
15
16test_expect_success 'make branches' '
a48fcd83 17 git branch branch-one &&
0afc3044
JK
18 git branch branch-two HEAD^
19'
20
21test_expect_success 'make remote branches' '
a48fcd83
JN
22 git update-ref refs/remotes/origin/branch-one branch-one &&
23 git update-ref refs/remotes/origin/branch-two branch-two &&
0afc3044
JK
24 git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
25'
26
27cat >expect <<'EOF'
28 branch-one
29 branch-two
1eee0a42 30* main
0afc3044
JK
31EOF
32test_expect_success 'git branch shows local branches' '
33 git branch >actual &&
34 test_cmp expect actual
35'
36
cddd127b
MG
37test_expect_success 'git branch --list shows local branches' '
38 git branch --list >actual &&
39 test_cmp expect actual
40'
41
d8d33736
MG
42cat >expect <<'EOF'
43 branch-one
44 branch-two
45EOF
46test_expect_success 'git branch --list pattern shows matching local branches' '
47 git branch --list branch* >actual &&
48 test_cmp expect actual
49'
50
0afc3044
JK
51cat >expect <<'EOF'
52 origin/HEAD -> origin/branch-one
53 origin/branch-one
54 origin/branch-two
55EOF
56test_expect_success 'git branch -r shows remote branches' '
57 git branch -r >actual &&
e12cb98e
JH
58 test_cmp expect actual &&
59
60 git branch --remotes >actual &&
0afc3044
JK
61 test_cmp expect actual
62'
63
e12cb98e
JH
64test_expect_success 'git branch --no-remotes is rejected' '
65 test_must_fail git branch --no-remotes 2>err &&
66 grep "unknown option .no-remotes." err
67'
68
0afc3044
JK
69cat >expect <<'EOF'
70 branch-one
71 branch-two
1eee0a42 72* main
0afc3044
JK
73 remotes/origin/HEAD -> origin/branch-one
74 remotes/origin/branch-one
75 remotes/origin/branch-two
76EOF
77test_expect_success 'git branch -a shows local and remote branches' '
78 git branch -a >actual &&
e12cb98e
JH
79 test_cmp expect actual &&
80
81 git branch --all >actual &&
0afc3044
JK
82 test_cmp expect actual
83'
84
e12cb98e
JH
85test_expect_success 'git branch --no-all is rejected' '
86 test_must_fail git branch --no-all 2>err &&
87 grep "unknown option .no-all." err
88'
89
0afc3044
JK
90cat >expect <<'EOF'
91two
92one
93two
94EOF
95test_expect_success 'git branch -v shows branch summaries' '
96 git branch -v >tmp &&
97 awk "{print \$NF}" <tmp >actual &&
98 test_cmp expect actual
99'
100
d8d33736
MG
101cat >expect <<'EOF'
102two
103one
104EOF
7b787599
MG
105test_expect_success 'git branch --list -v pattern shows branch summaries' '
106 git branch --list -v branch* >tmp &&
d8d33736
MG
107 awk "{print \$NF}" <tmp >actual &&
108 test_cmp expect actual
109'
3bb16a8b
NTND
110test_expect_success 'git branch --ignore-case --list -v pattern shows branch summaries' '
111 git branch --list --ignore-case -v BRANCH* >tmp &&
112 awk "{print \$NF}" <tmp >actual &&
113 test_cmp expect actual
114'
d8d33736 115
7b787599
MG
116test_expect_success 'git branch -v pattern does not show branch summaries' '
117 test_must_fail git branch -v branch*
118'
119
0ecb1fc7
DU
120test_expect_success 'git branch `--show-current` shows current branch' '
121 cat >expect <<-\EOF &&
122 branch-two
123 EOF
124 git checkout branch-two &&
125 git branch --show-current >actual &&
126 test_cmp expect actual
127'
128
129test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
130 git checkout HEAD^0 &&
131 git branch --show-current >actual &&
132 test_must_be_empty actual
133'
134
135test_expect_success 'git branch `--show-current` works properly when tag exists' '
136 cat >expect <<-\EOF &&
137 branch-and-tag-name
138 EOF
139 test_when_finished "
140 git checkout branch-one
141 git branch -D branch-and-tag-name
142 " &&
143 git checkout -b branch-and-tag-name &&
144 test_when_finished "git tag -d branch-and-tag-name" &&
145 git tag branch-and-tag-name &&
146 git branch --show-current >actual &&
147 test_cmp expect actual
148'
149
150test_expect_success 'git branch `--show-current` works properly with worktrees' '
151 cat >expect <<-\EOF &&
152 branch-one
153 branch-two
154 EOF
155 git checkout branch-one &&
99eea645
JH
156 test_when_finished "
157 git worktree remove worktree_dir
158 " &&
ab313814 159 git worktree add worktree_dir branch-two &&
0ecb1fc7
DU
160 {
161 git branch --show-current &&
ab313814 162 git -C worktree_dir branch --show-current
0ecb1fc7
DU
163 } >actual &&
164 test_cmp expect actual
165'
166
c8183cd2
NTND
167test_expect_success 'git branch shows detached HEAD properly' '
168 cat >expect <<EOF &&
4b063186 169* (HEAD detached at $(git rev-parse --short HEAD^0))
0afc3044
JK
170 branch-one
171 branch-two
1eee0a42 172 main
0afc3044 173EOF
0afc3044
JK
174 git checkout HEAD^0 &&
175 git branch >actual &&
1108cea7 176 test_cmp expect actual
0afc3044
JK
177'
178
0eb8548f 179test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
1eee0a42 180 git checkout main &&
9cb07d81
MM
181 cat >expect <<EOF &&
182* (HEAD detached at $(git rev-parse --short HEAD^0))
183 branch-one
184 branch-two
1eee0a42 185 main
9cb07d81
MM
186EOF
187 git checkout --detach &&
188 git branch >actual &&
1108cea7 189 test_cmp expect actual
9cb07d81
MM
190'
191
4b063186
MG
192test_expect_success 'git branch shows detached HEAD properly after moving' '
193 cat >expect <<EOF &&
194* (HEAD detached from $(git rev-parse --short HEAD))
195 branch-one
196 branch-two
1eee0a42 197 main
4b063186
MG
198EOF
199 git reset --hard HEAD^1 &&
200 git branch >actual &&
1108cea7 201 test_cmp expect actual
4b063186
MG
202'
203
204test_expect_success 'git branch shows detached HEAD properly from tag' '
205 cat >expect <<EOF &&
206* (HEAD detached at fromtag)
207 branch-one
208 branch-two
1eee0a42 209 main
4b063186 210EOF
1eee0a42 211 git tag fromtag main &&
4b063186
MG
212 git checkout fromtag &&
213 git branch >actual &&
1108cea7 214 test_cmp expect actual
4b063186
MG
215'
216
217test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
218 cat >expect <<EOF &&
219* (HEAD detached from fromtag)
220 branch-one
221 branch-two
1eee0a42 222 main
4b063186
MG
223EOF
224 git reset --hard HEAD^1 &&
225 git branch >actual &&
1108cea7 226 test_cmp expect actual
4b063186
MG
227'
228
08bf6a8b 229test_expect_success 'git branch `--sort=[-]objectsize` option' '
aedcb7dc 230 cat >expect <<-\EOF &&
aedcb7dc 231 * (HEAD detached from fromtag)
9e468334 232 branch-two
aedcb7dc 233 branch-one
1eee0a42 234 main
aedcb7dc
KN
235 EOF
236 git branch --sort=objectsize >actual &&
1108cea7 237 test_cmp expect actual &&
08bf6a8b
ÆAB
238
239 cat >expect <<-\EOF &&
4045f659 240 * (HEAD detached from fromtag)
08bf6a8b
ÆAB
241 branch-one
242 main
08bf6a8b
ÆAB
243 branch-two
244 EOF
245 git branch --sort=-objectsize >actual &&
1108cea7 246 test_cmp expect actual
08bf6a8b
ÆAB
247'
248
249test_expect_success 'git branch `--sort=[-]type` option' '
250 cat >expect <<-\EOF &&
251 * (HEAD detached from fromtag)
252 branch-one
253 branch-two
254 main
255 EOF
256 git branch --sort=type >actual &&
1108cea7 257 test_cmp expect actual &&
08bf6a8b
ÆAB
258
259 cat >expect <<-\EOF &&
4045f659 260 * (HEAD detached from fromtag)
08bf6a8b
ÆAB
261 branch-one
262 branch-two
263 main
264 EOF
265 git branch --sort=-type >actual &&
1108cea7 266 test_cmp expect actual
08bf6a8b
ÆAB
267'
268
269test_expect_success 'git branch `--sort=[-]version:refname` option' '
270 cat >expect <<-\EOF &&
271 * (HEAD detached from fromtag)
272 branch-one
273 branch-two
274 main
275 EOF
276 git branch --sort=version:refname >actual &&
1108cea7 277 test_cmp expect actual &&
08bf6a8b
ÆAB
278
279 cat >expect <<-\EOF &&
4045f659 280 * (HEAD detached from fromtag)
08bf6a8b
ÆAB
281 main
282 branch-two
283 branch-one
08bf6a8b
ÆAB
284 EOF
285 git branch --sort=-version:refname >actual &&
1108cea7 286 test_cmp expect actual
aedcb7dc
KN
287'
288
aa3bc55e
KN
289test_expect_success 'git branch --points-at option' '
290 cat >expect <<-\EOF &&
291 branch-one
1eee0a42 292 main
aa3bc55e
KN
293 EOF
294 git branch --points-at=branch-one >actual &&
295 test_cmp expect actual
296'
297
0571979b
JK
298test_expect_success 'ambiguous branch/tag not marked' '
299 git tag ambiguous &&
300 git branch ambiguous &&
301 echo " ambiguous" >expect &&
302 git branch --list ambiguous >actual &&
303 test_cmp expect actual
304'
305
95c38fb0
JK
306test_expect_success 'local-branch symrefs shortened properly' '
307 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
308 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
309 cat >expect <<-\EOF &&
310 ref-to-branch -> branch-one
949af068 311 ref-to-remote -> origin/branch-one
95c38fb0
JK
312 EOF
313 git branch >actual.raw &&
314 grep ref-to <actual.raw >actual &&
315 test_cmp expect actual
316'
317
3bb16a8b
NTND
318test_expect_success 'sort branches, ignore case' '
319 (
1eee0a42 320 git init -b main sort-icase &&
3bb16a8b
NTND
321 cd sort-icase &&
322 test_commit initial &&
323 git branch branch-one &&
324 git branch BRANCH-two &&
325 git branch --list | awk "{print \$NF}" >actual &&
326 cat >expected <<-\EOF &&
327 BRANCH-two
328 branch-one
1eee0a42 329 main
3bb16a8b
NTND
330 EOF
331 test_cmp expected actual &&
332 git branch --list -i | awk "{print \$NF}" >actual &&
333 cat >expected <<-\EOF &&
334 branch-one
335 BRANCH-two
1eee0a42 336 main
3bb16a8b
NTND
337 EOF
338 test_cmp expected actual
339 )
340'
341
3d9e4ce3
KN
342test_expect_success 'git branch --format option' '
343 cat >expect <<-\EOF &&
344 Refname is (HEAD detached from fromtag)
345 Refname is refs/heads/ambiguous
346 Refname is refs/heads/branch-one
347 Refname is refs/heads/branch-two
1eee0a42 348 Refname is refs/heads/main
3d9e4ce3
KN
349 Refname is refs/heads/ref-to-branch
350 Refname is refs/heads/ref-to-remote
351 EOF
352 git branch --format="Refname is %(refname)" >actual &&
1108cea7 353 test_cmp expect actual
3d9e4ce3
KN
354'
355
49abcd21
DS
356test_expect_success 'git branch --format with ahead-behind' '
357 cat >expect <<-\EOF &&
358 (HEAD detached from fromtag) 0 0
359 refs/heads/ambiguous 0 0
360 refs/heads/branch-one 1 0
361 refs/heads/branch-two 0 0
362 refs/heads/main 1 0
363 refs/heads/ref-to-branch 1 0
364 refs/heads/ref-to-remote 1 0
365 EOF
366 git branch --format="%(refname) %(ahead-behind:HEAD)" >actual &&
367 test_cmp expect actual
368'
369
b9dee075
ZH
370test_expect_success 'git branch with --format=%(rest) must fail' '
371 test_must_fail git branch --format="%(rest)" >actual
372'
373
aabfdc95
ØW
374test_expect_success 'git branch --format --omit-empty' '
375 cat >expect <<-\EOF &&
376 Refname is (HEAD detached from fromtag)
377 Refname is refs/heads/ambiguous
378 Refname is refs/heads/branch-one
379 Refname is refs/heads/branch-two
380
381 Refname is refs/heads/ref-to-branch
382 Refname is refs/heads/ref-to-remote
383 EOF
384 git branch --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
385 test_cmp expect actual &&
386 cat >expect <<-\EOF &&
387 Refname is (HEAD detached from fromtag)
388 Refname is refs/heads/ambiguous
389 Refname is refs/heads/branch-one
390 Refname is refs/heads/branch-two
391 Refname is refs/heads/ref-to-branch
392 Refname is refs/heads/ref-to-remote
393 EOF
394 git branch --omit-empty --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
395 test_cmp expect actual
396'
397
ab313814
NB
398test_expect_success 'worktree colors correct' '
399 cat >expect <<-EOF &&
400 * <GREEN>(HEAD detached from fromtag)<RESET>
401 ambiguous<RESET>
402 branch-one<RESET>
403 + <CYAN>branch-two<RESET>
1eee0a42 404 main<RESET>
ab313814
NB
405 ref-to-branch<RESET> -> branch-one
406 ref-to-remote<RESET> -> origin/branch-one
407 EOF
408 git worktree add worktree_dir branch-two &&
409 git branch --color >actual.raw &&
410 rm -r worktree_dir &&
411 git worktree prune &&
412 test_decode_color <actual.raw >actual &&
1108cea7 413 test_cmp expect actual
ab313814
NB
414'
415
11b087ad 416test_expect_success "set up color tests" '
1eee0a42
JS
417 echo "<RED>main<RESET>" >expect.color &&
418 echo "main" >expect.bare &&
419 color_args="--format=%(color:red)%(refname:short) --list main"
11b087ad
JK
420'
421
422test_expect_success '%(color) omitted without tty' '
423 TERM=vt100 git branch $color_args >actual.raw &&
424 test_decode_color <actual.raw >actual &&
425 test_cmp expect.bare actual
426'
427
428test_expect_success TTY '%(color) present with tty' '
e433749d 429 test_terminal git branch $color_args >actual.raw &&
11b087ad
JK
430 test_decode_color <actual.raw >actual &&
431 test_cmp expect.color actual
432'
433
11b087ad
JK
434test_expect_success '--color overrides auto-color' '
435 git branch --color $color_args >actual.raw &&
436 test_decode_color <actual.raw >actual &&
437 test_cmp expect.color actual
438'
439
1eee0a42 440test_expect_success 'verbose output lists worktree path' '
6e938146 441 one=$(git rev-parse --short HEAD) &&
1eee0a42 442 two=$(git rev-parse --short main) &&
6e938146
NB
443 cat >expect <<-EOF &&
444 * (HEAD detached from fromtag) $one one
445 ambiguous $one one
446 branch-one $two two
447 + branch-two $one ($(pwd)/worktree_dir) one
66713e84 448 main $two two
6e938146
NB
449 ref-to-branch $two two
450 ref-to-remote $two two
451 EOF
452 git worktree add worktree_dir branch-two &&
453 git branch -vv >actual &&
454 rm -r worktree_dir &&
455 git worktree prune &&
1108cea7 456 test_cmp expect actual
6e938146
NB
457'
458
0afc3044 459test_done