3 test_description='git branch display tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . "$TEST_DIRECTORY"/lib-terminal.sh
10 test_expect_success 'make commits' '
14 echo content >>file &&
18 test_expect_success 'make branches' '
19 git branch branch-one &&
20 git branch branch-two HEAD^
23 test_expect_success 'make remote branches' '
24 git update-ref refs/remotes/origin/branch-one branch-one &&
25 git update-ref refs/remotes/origin/branch-two branch-two &&
26 git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
34 test_expect_success 'git branch shows local branches' '
36 test_cmp expect actual
39 test_expect_success 'git branch --list shows local branches' '
40 git branch --list >actual &&
41 test_cmp expect actual
48 test_expect_success 'git branch --list pattern shows matching local branches' '
49 git branch --list branch* >actual &&
50 test_cmp expect actual
54 origin/HEAD -> origin/branch-one
58 test_expect_success 'git branch -r shows remote branches' '
59 git branch -r >actual &&
60 test_cmp expect actual
67 remotes/origin/HEAD -> origin/branch-one
68 remotes/origin/branch-one
69 remotes/origin/branch-two
71 test_expect_success 'git branch -a shows local and remote branches' '
72 git branch -a >actual &&
73 test_cmp expect actual
81 test_expect_success 'git branch -v shows branch summaries' '
83 awk "{print \$NF}" <tmp >actual &&
84 test_cmp expect actual
91 test_expect_success 'git branch --list -v pattern shows branch summaries' '
92 git branch --list -v branch* >tmp &&
93 awk "{print \$NF}" <tmp >actual &&
94 test_cmp expect actual
96 test_expect_success 'git branch --ignore-case --list -v pattern shows branch summaries' '
97 git branch --list --ignore-case -v BRANCH* >tmp &&
98 awk "{print \$NF}" <tmp >actual &&
99 test_cmp expect actual
102 test_expect_success 'git branch -v pattern does not show branch summaries' '
103 test_must_fail git branch -v branch*
106 test_expect_success 'git branch `--show-current` shows current branch' '
107 cat >expect <<-\EOF &&
110 git checkout branch-two &&
111 git branch --show-current >actual &&
112 test_cmp expect actual
115 test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
116 git checkout HEAD^0 &&
117 git branch --show-current >actual &&
118 test_must_be_empty actual
121 test_expect_success 'git branch `--show-current` works properly when tag exists' '
122 cat >expect <<-\EOF &&
126 git checkout branch-one
127 git branch -D branch-and-tag-name
129 git checkout -b branch-and-tag-name &&
130 test_when_finished "git tag -d branch-and-tag-name" &&
131 git tag branch-and-tag-name &&
132 git branch --show-current >actual &&
133 test_cmp expect actual
136 test_expect_success 'git branch `--show-current` works properly with worktrees' '
137 cat >expect <<-\EOF &&
141 git checkout branch-one &&
143 git worktree remove worktree_dir
145 git worktree add worktree_dir branch-two &&
147 git branch --show-current &&
148 git -C worktree_dir branch --show-current
150 test_cmp expect actual
153 test_expect_success 'git branch shows detached HEAD properly' '
155 * (HEAD detached at $(git rev-parse --short HEAD^0))
160 git checkout HEAD^0 &&
161 git branch >actual &&
162 test_i18ncmp expect actual
165 test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
166 git checkout master &&
168 * (HEAD detached at $(git rev-parse --short HEAD^0))
173 git checkout --detach &&
174 git branch >actual &&
175 test_i18ncmp expect actual
178 test_expect_success 'git branch shows detached HEAD properly after moving' '
180 * (HEAD detached from $(git rev-parse --short HEAD))
185 git reset --hard HEAD^1 &&
186 git branch >actual &&
187 test_i18ncmp expect actual
190 test_expect_success 'git branch shows detached HEAD properly from tag' '
192 * (HEAD detached at fromtag)
197 git tag fromtag master &&
198 git checkout fromtag &&
199 git branch >actual &&
200 test_i18ncmp expect actual
203 test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
205 * (HEAD detached from fromtag)
210 git reset --hard HEAD^1 &&
211 git branch >actual &&
212 test_i18ncmp expect actual
215 test_expect_success 'git branch `--sort` option' '
216 cat >expect <<-\EOF &&
217 * (HEAD detached from fromtag)
222 git branch --sort=objectsize >actual &&
223 test_i18ncmp expect actual
226 test_expect_success 'git branch --points-at option' '
227 cat >expect <<-\EOF &&
231 git branch --points-at=branch-one >actual &&
232 test_cmp expect actual
235 test_expect_success 'ambiguous branch/tag not marked' '
237 git branch ambiguous &&
238 echo " ambiguous" >expect &&
239 git branch --list ambiguous >actual &&
240 test_cmp expect actual
243 test_expect_success 'local-branch symrefs shortened properly' '
244 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
245 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
246 cat >expect <<-\EOF &&
247 ref-to-branch -> branch-one
248 ref-to-remote -> origin/branch-one
250 git branch >actual.raw &&
251 grep ref-to <actual.raw >actual &&
252 test_cmp expect actual
255 test_expect_success 'sort branches, ignore case' '
257 git init sort-icase &&
259 test_commit initial &&
260 git branch branch-one &&
261 git branch BRANCH-two &&
262 git branch --list | awk "{print \$NF}" >actual &&
263 cat >expected <<-\EOF &&
268 test_cmp expected actual &&
269 git branch --list -i | awk "{print \$NF}" >actual &&
270 cat >expected <<-\EOF &&
275 test_cmp expected actual
279 test_expect_success 'git branch --format option' '
280 cat >expect <<-\EOF &&
281 Refname is (HEAD detached from fromtag)
282 Refname is refs/heads/ambiguous
283 Refname is refs/heads/branch-one
284 Refname is refs/heads/branch-two
285 Refname is refs/heads/master
286 Refname is refs/heads/ref-to-branch
287 Refname is refs/heads/ref-to-remote
289 git branch --format="Refname is %(refname)" >actual &&
290 test_i18ncmp expect actual
293 test_expect_success 'worktree colors correct' '
294 cat >expect <<-EOF &&
295 * <GREEN>(HEAD detached from fromtag)<RESET>
298 + <CYAN>branch-two<RESET>
300 ref-to-branch<RESET> -> branch-one
301 ref-to-remote<RESET> -> origin/branch-one
303 git worktree add worktree_dir branch-two &&
304 git branch --color >actual.raw &&
305 rm -r worktree_dir &&
306 git worktree prune &&
307 test_decode_color <actual.raw >actual &&
308 test_i18ncmp expect actual
311 test_expect_success "set up color tests" '
312 echo "<RED>master<RESET>" >expect.color &&
313 echo "master" >expect.bare &&
314 color_args="--format=%(color:red)%(refname:short) --list master"
317 test_expect_success '%(color) omitted without tty' '
318 TERM=vt100 git branch $color_args >actual.raw &&
319 test_decode_color <actual.raw >actual &&
320 test_cmp expect.bare actual
323 test_expect_success TTY '%(color) present with tty' '
324 test_terminal git branch $color_args >actual.raw &&
325 test_decode_color <actual.raw >actual &&
326 test_cmp expect.color actual
329 test_expect_success '--color overrides auto-color' '
330 git branch --color $color_args >actual.raw &&
331 test_decode_color <actual.raw >actual &&
332 test_cmp expect.color actual
335 test_expect_success PREPARE_FOR_MAIN_BRANCH 'verbose output lists worktree path' '
336 one=$(git rev-parse --short HEAD) &&
337 two=$(git rev-parse --short master) &&
338 cat >expect <<-EOF &&
339 * (HEAD detached from fromtag) $one one
342 + branch-two $one ($(pwd)/worktree_dir) one
344 ref-to-branch $two two
345 ref-to-remote $two two
347 git worktree add worktree_dir branch-two &&
348 git branch -vv >actual &&
349 rm -r worktree_dir &&
350 git worktree prune &&
351 test_i18ncmp expect actual