3 test_description
='git branch display tests'
5 .
"$TEST_DIRECTORY"/lib-terminal.sh
7 test_expect_success
'make commits' '
12 echo content >>file &&
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
32 test_expect_success
'git branch shows local branches' '
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
46 test_expect_success
'git branch --list pattern shows matching local branches' '
47 git branch --list branch* >actual &&
48 test_cmp expect actual
52 origin/HEAD -> origin/branch-one
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
73 remotes/origin/HEAD -> origin/branch-one
74 remotes/origin/branch-one
75 remotes/origin/branch-two
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
95 test_expect_success
'git branch -v shows branch summaries' '
97 awk "{print \$NF}" <tmp >actual &&
98 test_cmp expect actual
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 &&
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 &&
140 git checkout branch-one
141 git branch -D branch-and-tag-name
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 &&
155 git checkout branch-one &&
157 git worktree remove worktree_dir
159 git worktree add worktree_dir branch-two &&
161 git branch --show-current &&
162 git -C worktree_dir branch --show-current
164 test_cmp expect actual
167 test_expect_success
'git branch shows detached HEAD properly' '
169 * (HEAD detached at $(git rev-parse --short HEAD^0))
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' '
182 * (HEAD detached at $(git rev-parse --short HEAD^0))
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' '
194 * (HEAD detached from $(git rev-parse --short HEAD))
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' '
206 * (HEAD detached at fromtag)
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' '
219 * (HEAD detached from fromtag)
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)
236 git branch --sort=objectsize >actual &&
237 test_cmp expect actual &&
239 cat >expect <<-\EOF &&
240 * (HEAD detached from fromtag)
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)
256 git branch --sort=type >actual &&
257 test_cmp expect actual &&
259 cat >expect <<-\EOF &&
260 * (HEAD detached from fromtag)
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)
276 git branch --sort=version:refname >actual &&
277 test_cmp expect actual &&
279 cat >expect <<-\EOF &&
280 * (HEAD detached from fromtag)
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 &&
294 git branch --points-at=branch-one >actual &&
295 test_cmp expect actual
298 test_expect_success
'ambiguous branch/tag not marked' '
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 &&
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 &&
331 test_cmp expected actual &&
332 git branch --list -i | awk "{print \$NF}" >actual &&
333 cat >expected <<-\EOF &&
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
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>
403 + <CYAN>branch-two<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
447 + branch-two $one ($(pwd)/worktree_dir) one
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