1#!/bin/sh
2
3test_description='git branch display tests'
4. ./test-lib.sh
5. "$TEST_DIRECTORY"/lib-terminal.sh
6
7test_expect_success 'make commits' '
8 echo content >file &&
9 git add file &&
10 git commit -m one &&
11 echo content >>file &&
12 git commit -a -m two
13'
14
15test_expect_success 'make branches' '
16 git branch branch-one &&
17 git branch branch-two HEAD^
18'
19
20test_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
24'
25
26cat >expect <<'EOF'
27 branch-one
28 branch-two
29* master
30EOF
31test_expect_success 'git branch shows local branches' '
32 git branch >actual &&
33 test_cmp expect actual
34'
35
36test_expect_success 'git branch --list shows local branches' '
37 git branch --list >actual &&
38 test_cmp expect actual
39'
40
41cat >expect <<'EOF'
42 branch-one
43 branch-two
44EOF
45test_expect_success 'git branch --list pattern shows matching local branches' '
46 git branch --list branch* >actual &&
47 test_cmp expect actual
48'
49
50cat >expect <<'EOF'
51 origin/HEAD -> origin/branch-one
52 origin/branch-one
53 origin/branch-two
54EOF
55test_expect_success 'git branch -r shows remote branches' '
56 git branch -r >actual &&
57 test_cmp expect actual
58'
59
60cat >expect <<'EOF'
61 branch-one
62 branch-two
63* master
64 remotes/origin/HEAD -> origin/branch-one
65 remotes/origin/branch-one
66 remotes/origin/branch-two
67EOF
68test_expect_success 'git branch -a shows local and remote branches' '
69 git branch -a >actual &&
70 test_cmp expect actual
71'
72
73cat >expect <<'EOF'
74two
75one
76two
77EOF
78test_expect_success 'git branch -v shows branch summaries' '
79 git branch -v >tmp &&
80 awk "{print \$NF}" <tmp >actual &&
81 test_cmp expect actual
82'
83
84cat >expect <<'EOF'
85two
86one
87EOF
88test_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
92'
93test_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
97'
98
99test_expect_success 'git branch -v pattern does not show branch summaries' '
100 test_must_fail git branch -v branch*
101'
102
103test_expect_success 'git branch `--show-current` shows current branch' '
104 cat >expect <<-\EOF &&
105 branch-two
106 EOF
107 git checkout branch-two &&
108 git branch --show-current >actual &&
109 test_cmp expect actual
110'
111
112test_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
116'
117
118test_expect_success 'git branch `--show-current` works properly when tag exists' '
119 cat >expect <<-\EOF &&
120 branch-and-tag-name
121 EOF
122 test_when_finished "
123 git checkout branch-one
124 git branch -D branch-and-tag-name
125 " &&
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
131'
132
133test_expect_success 'git branch `--show-current` works properly with worktrees' '
134 cat >expect <<-\EOF &&
135 branch-one
136 branch-two
137 EOF
138 git checkout branch-one &&
139 git worktree add worktree_dir branch-two &&
140 {
141 git branch --show-current &&
142 git -C worktree_dir branch --show-current
143 } >actual &&
144 rm -r worktree_dir &&
145 git worktree prune &&
146 test_cmp expect actual
147'
148
149test_expect_success 'git branch shows detached HEAD properly' '
150 cat >expect <<EOF &&
151* (HEAD detached at $(git rev-parse --short HEAD^0))
152 branch-one
153 branch-two
154 master
155EOF
156 git checkout HEAD^0 &&
157 git branch >actual &&
158 test_i18ncmp expect actual
159'
160
161test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
162 git checkout master &&
163 cat >expect <<EOF &&
164* (HEAD detached at $(git rev-parse --short HEAD^0))
165 branch-one
166 branch-two
167 master
168EOF
169 git checkout --detach &&
170 git branch >actual &&
171 test_i18ncmp expect actual
172'
173
174test_expect_success 'git branch shows detached HEAD properly after moving' '
175 cat >expect <<EOF &&
176* (HEAD detached from $(git rev-parse --short HEAD))
177 branch-one
178 branch-two
179 master
180EOF
181 git reset --hard HEAD^1 &&
182 git branch >actual &&
183 test_i18ncmp expect actual
184'
185
186test_expect_success 'git branch shows detached HEAD properly from tag' '
187 cat >expect <<EOF &&
188* (HEAD detached at fromtag)
189 branch-one
190 branch-two
191 master
192EOF
193 git tag fromtag master &&
194 git checkout fromtag &&
195 git branch >actual &&
196 test_i18ncmp expect actual
197'
198
199test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
200 cat >expect <<EOF &&
201* (HEAD detached from fromtag)
202 branch-one
203 branch-two
204 master
205EOF
206 git reset --hard HEAD^1 &&
207 git branch >actual &&
208 test_i18ncmp expect actual
209'
210
211test_expect_success 'git branch `--sort` option' '
212 cat >expect <<-\EOF &&
213 * (HEAD detached from fromtag)
214 branch-two
215 branch-one
216 master
217 EOF
218 git branch --sort=objectsize >actual &&
219 test_i18ncmp expect actual
220'
221
222test_expect_success 'git branch --points-at option' '
223 cat >expect <<-\EOF &&
224 branch-one
225 master
226 EOF
227 git branch --points-at=branch-one >actual &&
228 test_cmp expect actual
229'
230
231test_expect_success 'ambiguous branch/tag not marked' '
232 git tag ambiguous &&
233 git branch ambiguous &&
234 echo " ambiguous" >expect &&
235 git branch --list ambiguous >actual &&
236 test_cmp expect actual
237'
238
239test_expect_success 'local-branch symrefs shortened properly' '
240 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
241 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
242 cat >expect <<-\EOF &&
243 ref-to-branch -> branch-one
244 ref-to-remote -> origin/branch-one
245 EOF
246 git branch >actual.raw &&
247 grep ref-to <actual.raw >actual &&
248 test_cmp expect actual
249'
250
251test_expect_success 'sort branches, ignore case' '
252 (
253 git init sort-icase &&
254 cd sort-icase &&
255 test_commit initial &&
256 git branch branch-one &&
257 git branch BRANCH-two &&
258 git branch --list | awk "{print \$NF}" >actual &&
259 cat >expected <<-\EOF &&
260 BRANCH-two
261 branch-one
262 master
263 EOF
264 test_cmp expected actual &&
265 git branch --list -i | awk "{print \$NF}" >actual &&
266 cat >expected <<-\EOF &&
267 branch-one
268 BRANCH-two
269 master
270 EOF
271 test_cmp expected actual
272 )
273'
274
275test_expect_success 'git branch --format option' '
276 cat >expect <<-\EOF &&
277 Refname is (HEAD detached from fromtag)
278 Refname is refs/heads/ambiguous
279 Refname is refs/heads/branch-one
280 Refname is refs/heads/branch-two
281 Refname is refs/heads/master
282 Refname is refs/heads/ref-to-branch
283 Refname is refs/heads/ref-to-remote
284 EOF
285 git branch --format="Refname is %(refname)" >actual &&
286 test_i18ncmp expect actual
287'
288
289test_expect_success 'worktree colors correct' '
290 cat >expect <<-EOF &&
291 * <GREEN>(HEAD detached from fromtag)<RESET>
292 ambiguous<RESET>
293 branch-one<RESET>
294 + <CYAN>branch-two<RESET>
295 master<RESET>
296 ref-to-branch<RESET> -> branch-one
297 ref-to-remote<RESET> -> origin/branch-one
298 EOF
299 git worktree add worktree_dir branch-two &&
300 git branch --color >actual.raw &&
301 rm -r worktree_dir &&
302 git worktree prune &&
303 test_decode_color <actual.raw >actual &&
304 test_i18ncmp expect actual
305'
306
307test_expect_success "set up color tests" '
308 echo "<RED>master<RESET>" >expect.color &&
309 echo "master" >expect.bare &&
310 color_args="--format=%(color:red)%(refname:short) --list master"
311'
312
313test_expect_success '%(color) omitted without tty' '
314 TERM=vt100 git branch $color_args >actual.raw &&
315 test_decode_color <actual.raw >actual &&
316 test_cmp expect.bare actual
317'
318
319test_expect_success TTY '%(color) present with tty' '
320 test_terminal git branch $color_args >actual.raw &&
321 test_decode_color <actual.raw >actual &&
322 test_cmp expect.color actual
323'
324
325test_expect_success '--color overrides auto-color' '
326 git branch --color $color_args >actual.raw &&
327 test_decode_color <actual.raw >actual &&
328 test_cmp expect.color actual
329'
330
331test_expect_success 'verbose output lists worktree path' '
332 one=$(git rev-parse --short HEAD) &&
333 two=$(git rev-parse --short master) &&
334 cat >expect <<-EOF &&
335 * (HEAD detached from fromtag) $one one
336 ambiguous $one one
337 branch-one $two two
338 + branch-two $one ($(pwd)/worktree_dir) one
339 master $two two
340 ref-to-branch $two two
341 ref-to-remote $two two
342 EOF
343 git worktree add worktree_dir branch-two &&
344 git branch -vv >actual &&
345 rm -r worktree_dir &&
346 git worktree prune &&
347 test_i18ncmp expect actual
348'
349
350test_done