Git 2.43.4
[git.git] / t / t3203-branch-output.sh
blob758963b189d8d9fc7ad50f4fa06bf431c4fe6e80
1 #!/bin/sh
3 test_description='git branch display tests'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-terminal.sh
7 test_expect_success 'make commits' '
8 echo content >file &&
9 git add file &&
10 git commit -m one &&
11 git branch -M main &&
12 echo content >>file &&
13 git commit -a -m two
16 test_expect_success 'make branches' '
17 git branch branch-one &&
18 git branch branch-two HEAD^
21 test_expect_success 'make remote branches' '
22 git update-ref refs/remotes/origin/branch-one branch-one &&
23 git update-ref refs/remotes/origin/branch-two branch-two &&
24 git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
27 cat >expect <<'EOF'
28 branch-one
29 branch-two
30 * main
31 EOF
32 test_expect_success 'git branch shows local branches' '
33 git branch >actual &&
34 test_cmp expect actual
37 test_expect_success 'git branch --list shows local branches' '
38 git branch --list >actual &&
39 test_cmp expect actual
42 cat >expect <<'EOF'
43 branch-one
44 branch-two
45 EOF
46 test_expect_success 'git branch --list pattern shows matching local branches' '
47 git branch --list branch* >actual &&
48 test_cmp expect actual
51 cat >expect <<'EOF'
52 origin/HEAD -> origin/branch-one
53 origin/branch-one
54 origin/branch-two
55 EOF
56 test_expect_success 'git branch -r shows remote branches' '
57 git branch -r >actual &&
58 test_cmp expect actual &&
60 git branch --remotes >actual &&
61 test_cmp expect actual
64 test_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
69 cat >expect <<'EOF'
70 branch-one
71 branch-two
72 * main
73 remotes/origin/HEAD -> origin/branch-one
74 remotes/origin/branch-one
75 remotes/origin/branch-two
76 EOF
77 test_expect_success 'git branch -a shows local and remote branches' '
78 git branch -a >actual &&
79 test_cmp expect actual &&
81 git branch --all >actual &&
82 test_cmp expect actual
85 test_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
90 cat >expect <<'EOF'
91 two
92 one
93 two
94 EOF
95 test_expect_success 'git branch -v shows branch summaries' '
96 git branch -v >tmp &&
97 awk "{print \$NF}" <tmp >actual &&
98 test_cmp expect actual
101 cat >expect <<'EOF'
105 test_expect_success 'git branch --list -v pattern shows branch summaries' '
106 git branch --list -v branch* >tmp &&
107 awk "{print \$NF}" <tmp >actual &&
108 test_cmp expect actual
110 test_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
116 test_expect_success 'git branch -v pattern does not show branch summaries' '
117 test_must_fail git branch -v branch*
120 test_expect_success 'git branch `--show-current` shows current branch' '
121 cat >expect <<-\EOF &&
122 branch-two
124 git checkout branch-two &&
125 git branch --show-current >actual &&
126 test_cmp expect actual
129 test_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
135 test_expect_success 'git branch `--show-current` works properly when tag exists' '
136 cat >expect <<-\EOF &&
137 branch-and-tag-name
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
150 test_expect_success 'git branch `--show-current` works properly with worktrees' '
151 cat >expect <<-\EOF &&
152 branch-one
153 branch-two
155 git checkout branch-one &&
156 test_when_finished "
157 git worktree remove worktree_dir
158 " &&
159 git worktree add worktree_dir branch-two &&
161 git branch --show-current &&
162 git -C worktree_dir branch --show-current
163 } >actual &&
164 test_cmp expect actual
167 test_expect_success 'git branch shows detached HEAD properly' '
168 cat >expect <<EOF &&
169 * (HEAD detached at $(git rev-parse --short HEAD^0))
170 branch-one
171 branch-two
172 main
174 git checkout HEAD^0 &&
175 git branch >actual &&
176 test_cmp expect actual
179 test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
180 git checkout main &&
181 cat >expect <<EOF &&
182 * (HEAD detached at $(git rev-parse --short HEAD^0))
183 branch-one
184 branch-two
185 main
187 git checkout --detach &&
188 git branch >actual &&
189 test_cmp expect actual
192 test_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
197 main
199 git reset --hard HEAD^1 &&
200 git branch >actual &&
201 test_cmp expect actual
204 test_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
209 main
211 git tag fromtag main &&
212 git checkout fromtag &&
213 git branch >actual &&
214 test_cmp expect actual
217 test_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
222 main
224 git reset --hard HEAD^1 &&
225 git branch >actual &&
226 test_cmp expect actual
229 test_expect_success 'git branch `--sort=[-]objectsize` option' '
230 cat >expect <<-\EOF &&
231 * (HEAD detached from fromtag)
232 branch-two
233 branch-one
234 main
236 git branch --sort=objectsize >actual &&
237 test_cmp expect actual &&
239 cat >expect <<-\EOF &&
240 * (HEAD detached from fromtag)
241 branch-one
242 main
243 branch-two
245 git branch --sort=-objectsize >actual &&
246 test_cmp expect actual
249 test_expect_success 'git branch `--sort=[-]type` option' '
250 cat >expect <<-\EOF &&
251 * (HEAD detached from fromtag)
252 branch-one
253 branch-two
254 main
256 git branch --sort=type >actual &&
257 test_cmp expect actual &&
259 cat >expect <<-\EOF &&
260 * (HEAD detached from fromtag)
261 branch-one
262 branch-two
263 main
265 git branch --sort=-type >actual &&
266 test_cmp expect actual
269 test_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
276 git branch --sort=version:refname >actual &&
277 test_cmp expect actual &&
279 cat >expect <<-\EOF &&
280 * (HEAD detached from fromtag)
281 main
282 branch-two
283 branch-one
285 git branch --sort=-version:refname >actual &&
286 test_cmp expect actual
289 test_expect_success 'git branch --points-at option' '
290 cat >expect <<-\EOF &&
291 branch-one
292 main
294 git branch --points-at=branch-one >actual &&
295 test_cmp expect actual
298 test_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
306 test_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
311 ref-to-remote -> origin/branch-one
313 git branch >actual.raw &&
314 grep ref-to <actual.raw >actual &&
315 test_cmp expect actual
318 test_expect_success 'sort branches, ignore case' '
320 git init -b main sort-icase &&
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
329 main
331 test_cmp expected actual &&
332 git branch --list -i | awk "{print \$NF}" >actual &&
333 cat >expected <<-\EOF &&
334 branch-one
335 BRANCH-two
336 main
338 test_cmp expected actual
342 test_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
348 Refname is refs/heads/main
349 Refname is refs/heads/ref-to-branch
350 Refname is refs/heads/ref-to-remote
352 git branch --format="Refname is %(refname)" >actual &&
353 test_cmp expect actual
356 test_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
366 git branch --format="%(refname) %(ahead-behind:HEAD)" >actual &&
367 test_cmp expect actual
370 test_expect_success 'git branch with --format=%(rest) must fail' '
371 test_must_fail git branch --format="%(rest)" >actual
374 test_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
381 Refname is refs/heads/ref-to-branch
382 Refname is refs/heads/ref-to-remote
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
394 git branch --omit-empty --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
395 test_cmp expect actual
398 test_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>
404 main<RESET>
405 ref-to-branch<RESET> -> branch-one
406 ref-to-remote<RESET> -> origin/branch-one
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 &&
413 test_cmp expect actual
416 test_expect_success "set up color tests" '
417 echo "<RED>main<RESET>" >expect.color &&
418 echo "main" >expect.bare &&
419 color_args="--format=%(color:red)%(refname:short) --list main"
422 test_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
428 test_expect_success TTY '%(color) present with tty' '
429 test_terminal git branch $color_args >actual.raw &&
430 test_decode_color <actual.raw >actual &&
431 test_cmp expect.color actual
434 test_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
440 test_expect_success 'verbose output lists worktree path' '
441 one=$(git rev-parse --short HEAD) &&
442 two=$(git rev-parse --short main) &&
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
448 main $two two
449 ref-to-branch $two two
450 ref-to-remote $two two
452 git worktree add worktree_dir branch-two &&
453 git branch -vv >actual &&
454 rm -r worktree_dir &&
455 git worktree prune &&
456 test_cmp expect actual
459 test_done