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 shows detached HEAD properly' '
104 cat >expect <<EOF &&
105* (HEAD detached at $(git rev-parse --short HEAD^0))
106 branch-one
107 branch-two
108 master
109EOF
110 git checkout HEAD^0 &&
111 git branch >actual &&
112 test_i18ncmp expect actual
113'
114
115test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
116 git checkout master &&
117 cat >expect <<EOF &&
118* (HEAD detached at $(git rev-parse --short HEAD^0))
119 branch-one
120 branch-two
121 master
122EOF
123 git checkout --detach &&
124 git branch >actual &&
125 test_i18ncmp expect actual
126'
127
128test_expect_success 'git branch shows detached HEAD properly after moving' '
129 cat >expect <<EOF &&
130* (HEAD detached from $(git rev-parse --short HEAD))
131 branch-one
132 branch-two
133 master
134EOF
135 git reset --hard HEAD^1 &&
136 git branch >actual &&
137 test_i18ncmp expect actual
138'
139
140test_expect_success 'git branch shows detached HEAD properly from tag' '
141 cat >expect <<EOF &&
142* (HEAD detached at fromtag)
143 branch-one
144 branch-two
145 master
146EOF
147 git tag fromtag master &&
148 git checkout fromtag &&
149 git branch >actual &&
150 test_i18ncmp expect actual
151'
152
153test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
154 cat >expect <<EOF &&
155* (HEAD detached from fromtag)
156 branch-one
157 branch-two
158 master
159EOF
160 git reset --hard HEAD^1 &&
161 git branch >actual &&
162 test_i18ncmp expect actual
163'
164
165test_expect_success 'git branch `--sort` option' '
166 cat >expect <<-\EOF &&
167 * (HEAD detached from fromtag)
168 branch-two
169 branch-one
170 master
171 EOF
172 git branch --sort=objectsize >actual &&
173 test_i18ncmp expect actual
174'
175
176test_expect_success 'git branch --points-at option' '
177 cat >expect <<-\EOF &&
178 branch-one
179 master
180 EOF
181 git branch --points-at=branch-one >actual &&
182 test_cmp expect actual
183'
184
185test_expect_success 'ambiguous branch/tag not marked' '
186 git tag ambiguous &&
187 git branch ambiguous &&
188 echo " ambiguous" >expect &&
189 git branch --list ambiguous >actual &&
190 test_cmp expect actual
191'
192
193test_expect_success 'local-branch symrefs shortened properly' '
194 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
195 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
196 cat >expect <<-\EOF &&
197 ref-to-branch -> branch-one
198 ref-to-remote -> origin/branch-one
199 EOF
200 git branch >actual.raw &&
201 grep ref-to <actual.raw >actual &&
202 test_cmp expect actual
203'
204
205test_expect_success 'sort branches, ignore case' '
206 (
207 git init sort-icase &&
208 cd sort-icase &&
209 test_commit initial &&
210 git branch branch-one &&
211 git branch BRANCH-two &&
212 git branch --list | awk "{print \$NF}" >actual &&
213 cat >expected <<-\EOF &&
214 BRANCH-two
215 branch-one
216 master
217 EOF
218 test_cmp expected actual &&
219 git branch --list -i | awk "{print \$NF}" >actual &&
220 cat >expected <<-\EOF &&
221 branch-one
222 BRANCH-two
223 master
224 EOF
225 test_cmp expected actual
226 )
227'
228
229test_expect_success 'git branch --format option' '
230 cat >expect <<-\EOF &&
231 Refname is (HEAD detached from fromtag)
232 Refname is refs/heads/ambiguous
233 Refname is refs/heads/branch-one
234 Refname is refs/heads/branch-two
235 Refname is refs/heads/master
236 Refname is refs/heads/ref-to-branch
237 Refname is refs/heads/ref-to-remote
238 EOF
239 git branch --format="Refname is %(refname)" >actual &&
240 test_i18ncmp expect actual
241'
242
243test_expect_success "set up color tests" '
244 echo "<RED>master<RESET>" >expect.color &&
245 echo "master" >expect.bare &&
246 color_args="--format=%(color:red)%(refname:short) --list master"
247'
248
249test_expect_success '%(color) omitted without tty' '
250 TERM=vt100 git branch $color_args >actual.raw &&
251 test_decode_color <actual.raw >actual &&
252 test_cmp expect.bare actual
253'
254
255test_expect_success TTY '%(color) present with tty' '
256 test_terminal env TERM=vt100 git branch $color_args >actual.raw &&
257 test_decode_color <actual.raw >actual &&
258 test_cmp expect.color actual
259'
260
261test_expect_success 'color.branch=always overrides auto-color' '
262 git -c color.branch=always branch $color_args >actual.raw &&
263 test_decode_color <actual.raw >actual &&
264 test_cmp expect.color actual
265'
266
267test_expect_success '--color overrides auto-color' '
268 git branch --color $color_args >actual.raw &&
269 test_decode_color <actual.raw >actual &&
270 test_cmp expect.color actual
271'
272
273test_done