3 test_description='git branch display tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . "$TEST_DIRECTORY"/lib-terminal.sh
10 test_expect_success 'make commits' '
15 echo content >>file &&
19 test_expect_success 'make branches' '
20 git branch branch-one &&
21 git branch branch-two HEAD^
24 test_expect_success 'make remote branches' '
25 git update-ref refs/remotes/origin/branch-one branch-one &&
26 git update-ref refs/remotes/origin/branch-two branch-two &&
27 git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
35 test_expect_success 'git branch shows local branches' '
37 test_cmp expect actual
40 test_expect_success 'git branch --list shows local branches' '
41 git branch --list >actual &&
42 test_cmp expect actual
49 test_expect_success 'git branch --list pattern shows matching local branches' '
50 git branch --list branch* >actual &&
51 test_cmp expect actual
55 origin/HEAD -> origin/branch-one
59 test_expect_success 'git branch -r shows remote branches' '
60 git branch -r >actual &&
61 test_cmp expect actual
68 remotes/origin/HEAD -> origin/branch-one
69 remotes/origin/branch-one
70 remotes/origin/branch-two
72 test_expect_success 'git branch -a shows local and remote branches' '
73 git branch -a >actual &&
74 test_cmp expect actual
82 test_expect_success 'git branch -v shows branch summaries' '
84 awk "{print \$NF}" <tmp >actual &&
85 test_cmp expect actual
92 test_expect_success 'git branch --list -v pattern shows branch summaries' '
93 git branch --list -v branch* >tmp &&
94 awk "{print \$NF}" <tmp >actual &&
95 test_cmp expect actual
97 test_expect_success 'git branch --ignore-case --list -v pattern shows branch summaries' '
98 git branch --list --ignore-case -v BRANCH* >tmp &&
99 awk "{print \$NF}" <tmp >actual &&
100 test_cmp expect actual
103 test_expect_success 'git branch -v pattern does not show branch summaries' '
104 test_must_fail git branch -v branch*
107 test_expect_success 'git branch `--show-current` shows current branch' '
108 cat >expect <<-\EOF &&
111 git checkout branch-two &&
112 git branch --show-current >actual &&
113 test_cmp expect actual
116 test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
117 git checkout HEAD^0 &&
118 git branch --show-current >actual &&
119 test_must_be_empty actual
122 test_expect_success 'git branch `--show-current` works properly when tag exists' '
123 cat >expect <<-\EOF &&
127 git checkout branch-one
128 git branch -D branch-and-tag-name
130 git checkout -b branch-and-tag-name &&
131 test_when_finished "git tag -d branch-and-tag-name" &&
132 git tag branch-and-tag-name &&
133 git branch --show-current >actual &&
134 test_cmp expect actual
137 test_expect_success 'git branch `--show-current` works properly with worktrees' '
138 cat >expect <<-\EOF &&
142 git checkout branch-one &&
144 git worktree remove worktree_dir
146 git worktree add worktree_dir branch-two &&
148 git branch --show-current &&
149 git -C worktree_dir branch --show-current
151 test_cmp expect actual
154 test_expect_success 'git branch shows detached HEAD properly' '
156 * (HEAD detached at $(git rev-parse --short HEAD^0))
161 git checkout HEAD^0 &&
162 git branch >actual &&
163 test_cmp expect actual
166 test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
169 * (HEAD detached at $(git rev-parse --short HEAD^0))
174 git checkout --detach &&
175 git branch >actual &&
176 test_cmp expect actual
179 test_expect_success 'git branch shows detached HEAD properly after moving' '
181 * (HEAD detached from $(git rev-parse --short HEAD))
186 git reset --hard HEAD^1 &&
187 git branch >actual &&
188 test_cmp expect actual
191 test_expect_success 'git branch shows detached HEAD properly from tag' '
193 * (HEAD detached at fromtag)
198 git tag fromtag main &&
199 git checkout fromtag &&
200 git branch >actual &&
201 test_cmp expect actual
204 test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
206 * (HEAD detached from fromtag)
211 git reset --hard HEAD^1 &&
212 git branch >actual &&
213 test_cmp expect actual
216 test_expect_success 'git branch `--sort=[-]objectsize` option' '
217 cat >expect <<-\EOF &&
218 * (HEAD detached from fromtag)
223 git branch --sort=objectsize >actual &&
224 test_cmp expect actual &&
226 cat >expect <<-\EOF &&
227 * (HEAD detached from fromtag)
232 git branch --sort=-objectsize >actual &&
233 test_cmp expect actual
236 test_expect_success 'git branch `--sort=[-]type` option' '
237 cat >expect <<-\EOF &&
238 * (HEAD detached from fromtag)
243 git branch --sort=type >actual &&
244 test_cmp expect actual &&
246 cat >expect <<-\EOF &&
247 * (HEAD detached from fromtag)
252 git branch --sort=-type >actual &&
253 test_cmp expect actual
256 test_expect_success 'git branch `--sort=[-]version:refname` option' '
257 cat >expect <<-\EOF &&
258 * (HEAD detached from fromtag)
263 git branch --sort=version:refname >actual &&
264 test_cmp expect actual &&
266 cat >expect <<-\EOF &&
267 * (HEAD detached from fromtag)
272 git branch --sort=-version:refname >actual &&
273 test_cmp expect actual
276 test_expect_success 'git branch --points-at option' '
277 cat >expect <<-\EOF &&
281 git branch --points-at=branch-one >actual &&
282 test_cmp expect actual
285 test_expect_success 'ambiguous branch/tag not marked' '
287 git branch ambiguous &&
288 echo " ambiguous" >expect &&
289 git branch --list ambiguous >actual &&
290 test_cmp expect actual
293 test_expect_success 'local-branch symrefs shortened properly' '
294 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
295 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
296 cat >expect <<-\EOF &&
297 ref-to-branch -> branch-one
298 ref-to-remote -> origin/branch-one
300 git branch >actual.raw &&
301 grep ref-to <actual.raw >actual &&
302 test_cmp expect actual
305 test_expect_success 'sort branches, ignore case' '
307 git init -b main sort-icase &&
309 test_commit initial &&
310 git branch branch-one &&
311 git branch BRANCH-two &&
312 git branch --list | awk "{print \$NF}" >actual &&
313 cat >expected <<-\EOF &&
318 test_cmp expected actual &&
319 git branch --list -i | awk "{print \$NF}" >actual &&
320 cat >expected <<-\EOF &&
325 test_cmp expected actual
329 test_expect_success 'git branch --format option' '
330 cat >expect <<-\EOF &&
331 Refname is (HEAD detached from fromtag)
332 Refname is refs/heads/ambiguous
333 Refname is refs/heads/branch-one
334 Refname is refs/heads/branch-two
335 Refname is refs/heads/main
336 Refname is refs/heads/ref-to-branch
337 Refname is refs/heads/ref-to-remote
339 git branch --format="Refname is %(refname)" >actual &&
340 test_cmp expect actual
343 test_expect_success 'worktree colors correct' '
344 cat >expect <<-EOF &&
345 * <GREEN>(HEAD detached from fromtag)<RESET>
348 + <CYAN>branch-two<RESET>
350 ref-to-branch<RESET> -> branch-one
351 ref-to-remote<RESET> -> origin/branch-one
353 git worktree add worktree_dir branch-two &&
354 git branch --color >actual.raw &&
355 rm -r worktree_dir &&
356 git worktree prune &&
357 test_decode_color <actual.raw >actual &&
358 test_cmp expect actual
361 test_expect_success "set up color tests" '
362 echo "<RED>main<RESET>" >expect.color &&
363 echo "main" >expect.bare &&
364 color_args="--format=%(color:red)%(refname:short) --list main"
367 test_expect_success '%(color) omitted without tty' '
368 TERM=vt100 git branch $color_args >actual.raw &&
369 test_decode_color <actual.raw >actual &&
370 test_cmp expect.bare actual
373 test_expect_success TTY '%(color) present with tty' '
374 test_terminal git branch $color_args >actual.raw &&
375 test_decode_color <actual.raw >actual &&
376 test_cmp expect.color actual
379 test_expect_success '--color overrides auto-color' '
380 git branch --color $color_args >actual.raw &&
381 test_decode_color <actual.raw >actual &&
382 test_cmp expect.color actual
385 test_expect_success 'verbose output lists worktree path' '
386 one=$(git rev-parse --short HEAD) &&
387 two=$(git rev-parse --short main) &&
388 cat >expect <<-EOF &&
389 * (HEAD detached from fromtag) $one one
392 + branch-two $one ($(pwd)/worktree_dir) one
394 ref-to-branch $two two
395 ref-to-remote $two two
397 git worktree add worktree_dir branch-two &&
398 git branch -vv >actual &&
399 rm -r worktree_dir &&
400 git worktree prune &&
401 test_cmp expect actual