3 test_description
='git branch display tests'
5 .
"$TEST_DIRECTORY"/lib-terminal.sh
7 test_expect_success
'make commits' '
11 echo content >>file &&
15 test_expect_success
'make branches' '
16 git branch branch-one &&
17 git branch branch-two HEAD^
20 test_expect_success
'make remote branches' '
21 git update-ref refs/remotes/origin/branch-one branch-one &&
22 git update-ref refs/remotes/origin/branch-two branch-two &&
23 git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
31 test_expect_success
'git branch shows local branches' '
33 test_cmp expect actual
36 test_expect_success
'git branch --list shows local branches' '
37 git branch --list >actual &&
38 test_cmp expect actual
45 test_expect_success
'git branch --list pattern shows matching local branches' '
46 git branch --list branch* >actual &&
47 test_cmp expect actual
51 origin/HEAD -> origin/branch-one
55 test_expect_success
'git branch -r shows remote branches' '
56 git branch -r >actual &&
57 test_cmp expect actual
64 remotes/origin/HEAD -> origin/branch-one
65 remotes/origin/branch-one
66 remotes/origin/branch-two
68 test_expect_success
'git branch -a shows local and remote branches' '
69 git branch -a >actual &&
70 test_cmp expect actual
78 test_expect_success
'git branch -v shows branch summaries' '
80 awk "{print \$NF}" <tmp >actual &&
81 test_cmp expect actual
88 test_expect_success
'git branch --list -v pattern shows branch summaries' '
89 git branch --list -v branch* >tmp &&
90 awk "{print \$NF}" <tmp >actual &&
91 test_cmp expect actual
93 test_expect_success
'git branch --ignore-case --list -v pattern shows branch summaries' '
94 git branch --list --ignore-case -v BRANCH* >tmp &&
95 awk "{print \$NF}" <tmp >actual &&
96 test_cmp expect actual
99 test_expect_success
'git branch -v pattern does not show branch summaries' '
100 test_must_fail git branch -v branch*
103 test_expect_success
'git branch `--show-current` shows current branch' '
104 cat >expect <<-\EOF &&
107 git checkout branch-two &&
108 git branch --show-current >actual &&
109 test_cmp expect actual
112 test_expect_success
'git branch `--show-current` is silent when detached HEAD' '
113 git checkout HEAD^0 &&
114 git branch --show-current >actual &&
115 test_must_be_empty actual
118 test_expect_success
'git branch `--show-current` works properly when tag exists' '
119 cat >expect <<-\EOF &&
123 git checkout branch-one
124 git branch -D branch-and-tag-name
126 git checkout -b branch-and-tag-name &&
127 test_when_finished "git tag -d branch-and-tag-name" &&
128 git tag branch-and-tag-name &&
129 git branch --show-current >actual &&
130 test_cmp expect actual
133 test_expect_success
'git branch `--show-current` works properly with worktrees' '
134 cat >expect <<-\EOF &&
138 git checkout branch-one &&
140 git worktree remove worktree_dir
142 git worktree add worktree_dir branch-two &&
144 git branch --show-current &&
145 git -C worktree_dir branch --show-current
147 test_cmp expect actual
150 test_expect_success
'git branch shows detached HEAD properly' '
152 * (HEAD detached at $(git rev-parse --short HEAD^0))
157 git checkout HEAD^0 &&
158 git branch >actual &&
159 test_i18ncmp expect actual
162 test_expect_success
'git branch shows detached HEAD properly after checkout --detach' '
163 git checkout master &&
165 * (HEAD detached at $(git rev-parse --short HEAD^0))
170 git checkout --detach &&
171 git branch >actual &&
172 test_i18ncmp expect actual
175 test_expect_success
'git branch shows detached HEAD properly after moving' '
177 * (HEAD detached from $(git rev-parse --short HEAD))
182 git reset --hard HEAD^1 &&
183 git branch >actual &&
184 test_i18ncmp expect actual
187 test_expect_success
'git branch shows detached HEAD properly from tag' '
189 * (HEAD detached at fromtag)
194 git tag fromtag master &&
195 git checkout fromtag &&
196 git branch >actual &&
197 test_i18ncmp expect actual
200 test_expect_success
'git branch shows detached HEAD properly after moving from tag' '
202 * (HEAD detached from fromtag)
207 git reset --hard HEAD^1 &&
208 git branch >actual &&
209 test_i18ncmp expect actual
212 test_expect_success
'git branch `--sort` option' '
213 cat >expect <<-\EOF &&
214 * (HEAD detached from fromtag)
219 git branch --sort=objectsize >actual &&
220 test_i18ncmp expect actual
223 test_expect_success
'git branch --points-at option' '
224 cat >expect <<-\EOF &&
228 git branch --points-at=branch-one >actual &&
229 test_cmp expect actual
232 test_expect_success
'ambiguous branch/tag not marked' '
234 git branch ambiguous &&
235 echo " ambiguous" >expect &&
236 git branch --list ambiguous >actual &&
237 test_cmp expect actual
240 test_expect_success
'local-branch symrefs shortened properly' '
241 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
242 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
243 cat >expect <<-\EOF &&
244 ref-to-branch -> branch-one
245 ref-to-remote -> origin/branch-one
247 git branch >actual.raw &&
248 grep ref-to <actual.raw >actual &&
249 test_cmp expect actual
252 test_expect_success
'sort branches, ignore case' '
254 git init sort-icase &&
256 test_commit initial &&
257 git branch branch-one &&
258 git branch BRANCH-two &&
259 git branch --list | awk "{print \$NF}" >actual &&
260 cat >expected <<-\EOF &&
265 test_cmp expected actual &&
266 git branch --list -i | awk "{print \$NF}" >actual &&
267 cat >expected <<-\EOF &&
272 test_cmp expected actual
276 test_expect_success
'git branch --format option' '
277 cat >expect <<-\EOF &&
278 Refname is (HEAD detached from fromtag)
279 Refname is refs/heads/ambiguous
280 Refname is refs/heads/branch-one
281 Refname is refs/heads/branch-two
282 Refname is refs/heads/master
283 Refname is refs/heads/ref-to-branch
284 Refname is refs/heads/ref-to-remote
286 git branch --format="Refname is %(refname)" >actual &&
287 test_i18ncmp expect actual
290 test_expect_success
'worktree colors correct' '
291 cat >expect <<-EOF &&
292 * <GREEN>(HEAD detached from fromtag)<RESET>
295 + <CYAN>branch-two<RESET>
297 ref-to-branch<RESET> -> branch-one
298 ref-to-remote<RESET> -> origin/branch-one
300 git worktree add worktree_dir branch-two &&
301 git branch --color >actual.raw &&
302 rm -r worktree_dir &&
303 git worktree prune &&
304 test_decode_color <actual.raw >actual &&
305 test_i18ncmp expect actual
308 test_expect_success
"set up color tests" '
309 echo "<RED>master<RESET>" >expect.color &&
310 echo "master" >expect.bare &&
311 color_args="--format=%(color:red)%(refname:short) --list master"
314 test_expect_success
'%(color) omitted without tty' '
315 TERM=vt100 git branch $color_args >actual.raw &&
316 test_decode_color <actual.raw >actual &&
317 test_cmp expect.bare actual
320 test_expect_success TTY
'%(color) present with tty' '
321 test_terminal git branch $color_args >actual.raw &&
322 test_decode_color <actual.raw >actual &&
323 test_cmp expect.color actual
326 test_expect_success
'--color overrides auto-color' '
327 git branch --color $color_args >actual.raw &&
328 test_decode_color <actual.raw >actual &&
329 test_cmp expect.color actual
332 test_expect_success
'verbose output lists worktree path' '
333 one=$(git rev-parse --short HEAD) &&
334 two=$(git rev-parse --short master) &&
335 cat >expect <<-EOF &&
336 * (HEAD detached from fromtag) $one one
339 + branch-two $one ($(pwd)/worktree_dir) one
341 ref-to-branch $two two
342 ref-to-remote $two two
344 git worktree add worktree_dir branch-two &&
345 git branch -vv >actual &&
346 rm -r worktree_dir &&
347 git worktree prune &&
348 test_i18ncmp expect actual