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
65 remotes/origin/HEAD -> origin/branch-one
66 remotes/origin/branch-one
67 remotes/origin/branch-two
69 test_expect_success
'git branch -a shows local and remote branches' '
70 git branch -a >actual &&
71 test_cmp expect actual
79 test_expect_success
'git branch -v shows branch summaries' '
81 awk "{print \$NF}" <tmp >actual &&
82 test_cmp expect actual
89 test_expect_success
'git branch --list -v pattern shows branch summaries' '
90 git branch --list -v branch* >tmp &&
91 awk "{print \$NF}" <tmp >actual &&
92 test_cmp expect actual
94 test_expect_success
'git branch --ignore-case --list -v pattern shows branch summaries' '
95 git branch --list --ignore-case -v BRANCH* >tmp &&
96 awk "{print \$NF}" <tmp >actual &&
97 test_cmp expect actual
100 test_expect_success
'git branch -v pattern does not show branch summaries' '
101 test_must_fail git branch -v branch*
104 test_expect_success
'git branch `--show-current` shows current branch' '
105 cat >expect <<-\EOF &&
108 git checkout branch-two &&
109 git branch --show-current >actual &&
110 test_cmp expect actual
113 test_expect_success
'git branch `--show-current` is silent when detached HEAD' '
114 git checkout HEAD^0 &&
115 git branch --show-current >actual &&
116 test_must_be_empty actual
119 test_expect_success
'git branch `--show-current` works properly when tag exists' '
120 cat >expect <<-\EOF &&
124 git checkout branch-one
125 git branch -D branch-and-tag-name
127 git checkout -b branch-and-tag-name &&
128 test_when_finished "git tag -d branch-and-tag-name" &&
129 git tag branch-and-tag-name &&
130 git branch --show-current >actual &&
131 test_cmp expect actual
134 test_expect_success
'git branch `--show-current` works properly with worktrees' '
135 cat >expect <<-\EOF &&
139 git checkout branch-one &&
141 git worktree remove worktree_dir
143 git worktree add worktree_dir branch-two &&
145 git branch --show-current &&
146 git -C worktree_dir branch --show-current
148 test_cmp expect actual
151 test_expect_success
'git branch shows detached HEAD properly' '
153 * (HEAD detached at $(git rev-parse --short HEAD^0))
158 git checkout HEAD^0 &&
159 git branch >actual &&
160 test_cmp expect actual
163 test_expect_success
'git branch shows detached HEAD properly after checkout --detach' '
166 * (HEAD detached at $(git rev-parse --short HEAD^0))
171 git checkout --detach &&
172 git branch >actual &&
173 test_cmp expect actual
176 test_expect_success
'git branch shows detached HEAD properly after moving' '
178 * (HEAD detached from $(git rev-parse --short HEAD))
183 git reset --hard HEAD^1 &&
184 git branch >actual &&
185 test_cmp expect actual
188 test_expect_success
'git branch shows detached HEAD properly from tag' '
190 * (HEAD detached at fromtag)
195 git tag fromtag main &&
196 git checkout fromtag &&
197 git branch >actual &&
198 test_cmp expect actual
201 test_expect_success
'git branch shows detached HEAD properly after moving from tag' '
203 * (HEAD detached from fromtag)
208 git reset --hard HEAD^1 &&
209 git branch >actual &&
210 test_cmp expect actual
213 test_expect_success
'git branch `--sort=[-]objectsize` option' '
214 cat >expect <<-\EOF &&
215 * (HEAD detached from fromtag)
220 git branch --sort=objectsize >actual &&
221 test_cmp expect actual &&
223 cat >expect <<-\EOF &&
224 * (HEAD detached from fromtag)
229 git branch --sort=-objectsize >actual &&
230 test_cmp expect actual
233 test_expect_success
'git branch `--sort=[-]type` option' '
234 cat >expect <<-\EOF &&
235 * (HEAD detached from fromtag)
240 git branch --sort=type >actual &&
241 test_cmp expect actual &&
243 cat >expect <<-\EOF &&
244 * (HEAD detached from fromtag)
249 git branch --sort=-type >actual &&
250 test_cmp expect actual
253 test_expect_success
'git branch `--sort=[-]version:refname` option' '
254 cat >expect <<-\EOF &&
255 * (HEAD detached from fromtag)
260 git branch --sort=version:refname >actual &&
261 test_cmp expect actual &&
263 cat >expect <<-\EOF &&
264 * (HEAD detached from fromtag)
269 git branch --sort=-version:refname >actual &&
270 test_cmp expect actual
273 test_expect_success
'git branch --points-at option' '
274 cat >expect <<-\EOF &&
278 git branch --points-at=branch-one >actual &&
279 test_cmp expect actual
282 test_expect_success
'ambiguous branch/tag not marked' '
284 git branch ambiguous &&
285 echo " ambiguous" >expect &&
286 git branch --list ambiguous >actual &&
287 test_cmp expect actual
290 test_expect_success
'local-branch symrefs shortened properly' '
291 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
292 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
293 cat >expect <<-\EOF &&
294 ref-to-branch -> branch-one
295 ref-to-remote -> origin/branch-one
297 git branch >actual.raw &&
298 grep ref-to <actual.raw >actual &&
299 test_cmp expect actual
302 test_expect_success
'sort branches, ignore case' '
304 git init -b main sort-icase &&
306 test_commit initial &&
307 git branch branch-one &&
308 git branch BRANCH-two &&
309 git branch --list | awk "{print \$NF}" >actual &&
310 cat >expected <<-\EOF &&
315 test_cmp expected actual &&
316 git branch --list -i | awk "{print \$NF}" >actual &&
317 cat >expected <<-\EOF &&
322 test_cmp expected actual
326 test_expect_success
'git branch --format option' '
327 cat >expect <<-\EOF &&
328 Refname is (HEAD detached from fromtag)
329 Refname is refs/heads/ambiguous
330 Refname is refs/heads/branch-one
331 Refname is refs/heads/branch-two
332 Refname is refs/heads/main
333 Refname is refs/heads/ref-to-branch
334 Refname is refs/heads/ref-to-remote
336 git branch --format="Refname is %(refname)" >actual &&
337 test_cmp expect actual
340 test_expect_success
'git branch with --format=%(rest) must fail' '
341 test_must_fail git branch --format="%(rest)" >actual
344 test_expect_success
'worktree colors correct' '
345 cat >expect <<-EOF &&
346 * <GREEN>(HEAD detached from fromtag)<RESET>
349 + <CYAN>branch-two<RESET>
351 ref-to-branch<RESET> -> branch-one
352 ref-to-remote<RESET> -> origin/branch-one
354 git worktree add worktree_dir branch-two &&
355 git branch --color >actual.raw &&
356 rm -r worktree_dir &&
357 git worktree prune &&
358 test_decode_color <actual.raw >actual &&
359 test_cmp expect actual
362 test_expect_success
"set up color tests" '
363 echo "<RED>main<RESET>" >expect.color &&
364 echo "main" >expect.bare &&
365 color_args="--format=%(color:red)%(refname:short) --list main"
368 test_expect_success
'%(color) omitted without tty' '
369 TERM=vt100 git branch $color_args >actual.raw &&
370 test_decode_color <actual.raw >actual &&
371 test_cmp expect.bare actual
374 test_expect_success TTY
'%(color) present with tty' '
375 test_terminal git branch $color_args >actual.raw &&
376 test_decode_color <actual.raw >actual &&
377 test_cmp expect.color actual
380 test_expect_success
'--color overrides auto-color' '
381 git branch --color $color_args >actual.raw &&
382 test_decode_color <actual.raw >actual &&
383 test_cmp expect.color actual
386 test_expect_success
'verbose output lists worktree path' '
387 one=$(git rev-parse --short HEAD) &&
388 two=$(git rev-parse --short main) &&
389 cat >expect <<-EOF &&
390 * (HEAD detached from fromtag) $one one
393 + branch-two $one ($(pwd)/worktree_dir) one
395 ref-to-branch $two two
396 ref-to-remote $two two
398 git worktree add worktree_dir branch-two &&
399 git branch -vv >actual &&
400 rm -r worktree_dir &&
401 git worktree prune &&
402 test_cmp expect actual