tests: mark tests relying on the current default for `init.defaultBranch`
[git] / t / t3203-branch-output.sh
1 #!/bin/sh
2
3 test_description='git branch display tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8 . "$TEST_DIRECTORY"/lib-terminal.sh
9
10 test_expect_success 'make commits' '
11         echo content >file &&
12         git add file &&
13         git commit -m one &&
14         echo content >>file &&
15         git commit -a -m two
16 '
17
18 test_expect_success 'make branches' '
19         git branch branch-one &&
20         git branch branch-two HEAD^
21 '
22
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
27 '
28
29 cat >expect <<'EOF'
30   branch-one
31   branch-two
32 * master
33 EOF
34 test_expect_success 'git branch shows local branches' '
35         git branch >actual &&
36         test_cmp expect actual
37 '
38
39 test_expect_success 'git branch --list shows local branches' '
40         git branch --list >actual &&
41         test_cmp expect actual
42 '
43
44 cat >expect <<'EOF'
45   branch-one
46   branch-two
47 EOF
48 test_expect_success 'git branch --list pattern shows matching local branches' '
49         git branch --list branch* >actual &&
50         test_cmp expect actual
51 '
52
53 cat >expect <<'EOF'
54   origin/HEAD -> origin/branch-one
55   origin/branch-one
56   origin/branch-two
57 EOF
58 test_expect_success 'git branch -r shows remote branches' '
59         git branch -r >actual &&
60         test_cmp expect actual
61 '
62
63 cat >expect <<'EOF'
64   branch-one
65   branch-two
66 * master
67   remotes/origin/HEAD -> origin/branch-one
68   remotes/origin/branch-one
69   remotes/origin/branch-two
70 EOF
71 test_expect_success 'git branch -a shows local and remote branches' '
72         git branch -a >actual &&
73         test_cmp expect actual
74 '
75
76 cat >expect <<'EOF'
77 two
78 one
79 two
80 EOF
81 test_expect_success 'git branch -v shows branch summaries' '
82         git branch -v >tmp &&
83         awk "{print \$NF}" <tmp >actual &&
84         test_cmp expect actual
85 '
86
87 cat >expect <<'EOF'
88 two
89 one
90 EOF
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
95 '
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
100 '
101
102 test_expect_success 'git branch -v pattern does not show branch summaries' '
103         test_must_fail git branch -v branch*
104 '
105
106 test_expect_success 'git branch `--show-current` shows current branch' '
107         cat >expect <<-\EOF &&
108         branch-two
109         EOF
110         git checkout branch-two &&
111         git branch --show-current >actual &&
112         test_cmp expect actual
113 '
114
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
119 '
120
121 test_expect_success 'git branch `--show-current` works properly when tag exists' '
122         cat >expect <<-\EOF &&
123         branch-and-tag-name
124         EOF
125         test_when_finished "
126                 git checkout branch-one
127                 git branch -D branch-and-tag-name
128         " &&
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
134 '
135
136 test_expect_success 'git branch `--show-current` works properly with worktrees' '
137         cat >expect <<-\EOF &&
138         branch-one
139         branch-two
140         EOF
141         git checkout branch-one &&
142         test_when_finished "
143                 git worktree remove worktree_dir
144         " &&
145         git worktree add worktree_dir branch-two &&
146         {
147                 git branch --show-current &&
148                 git -C worktree_dir branch --show-current
149         } >actual &&
150         test_cmp expect actual
151 '
152
153 test_expect_success 'git branch shows detached HEAD properly' '
154         cat >expect <<EOF &&
155 * (HEAD detached at $(git rev-parse --short HEAD^0))
156   branch-one
157   branch-two
158   master
159 EOF
160         git checkout HEAD^0 &&
161         git branch >actual &&
162         test_i18ncmp expect actual
163 '
164
165 test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
166         git checkout master &&
167         cat >expect <<EOF &&
168 * (HEAD detached at $(git rev-parse --short HEAD^0))
169   branch-one
170   branch-two
171   master
172 EOF
173         git checkout --detach &&
174         git branch >actual &&
175         test_i18ncmp expect actual
176 '
177
178 test_expect_success 'git branch shows detached HEAD properly after moving' '
179         cat >expect <<EOF &&
180 * (HEAD detached from $(git rev-parse --short HEAD))
181   branch-one
182   branch-two
183   master
184 EOF
185         git reset --hard HEAD^1 &&
186         git branch >actual &&
187         test_i18ncmp expect actual
188 '
189
190 test_expect_success 'git branch shows detached HEAD properly from tag' '
191         cat >expect <<EOF &&
192 * (HEAD detached at fromtag)
193   branch-one
194   branch-two
195   master
196 EOF
197         git tag fromtag master &&
198         git checkout fromtag &&
199         git branch >actual &&
200         test_i18ncmp expect actual
201 '
202
203 test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
204         cat >expect <<EOF &&
205 * (HEAD detached from fromtag)
206   branch-one
207   branch-two
208   master
209 EOF
210         git reset --hard HEAD^1 &&
211         git branch >actual &&
212         test_i18ncmp expect actual
213 '
214
215 test_expect_success 'git branch `--sort` option' '
216         cat >expect <<-\EOF &&
217         * (HEAD detached from fromtag)
218           branch-two
219           branch-one
220           master
221         EOF
222         git branch --sort=objectsize >actual &&
223         test_i18ncmp expect actual
224 '
225
226 test_expect_success 'git branch --points-at option' '
227         cat >expect <<-\EOF &&
228           branch-one
229           master
230         EOF
231         git branch --points-at=branch-one >actual &&
232         test_cmp expect actual
233 '
234
235 test_expect_success 'ambiguous branch/tag not marked' '
236         git tag ambiguous &&
237         git branch ambiguous &&
238         echo "  ambiguous" >expect &&
239         git branch --list ambiguous >actual &&
240         test_cmp expect actual
241 '
242
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
249         EOF
250         git branch >actual.raw &&
251         grep ref-to <actual.raw >actual &&
252         test_cmp expect actual
253 '
254
255 test_expect_success 'sort branches, ignore case' '
256         (
257                 git init sort-icase &&
258                 cd 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 &&
264                 BRANCH-two
265                 branch-one
266                 master
267                 EOF
268                 test_cmp expected actual &&
269                 git branch --list -i | awk "{print \$NF}" >actual &&
270                 cat >expected <<-\EOF &&
271                 branch-one
272                 BRANCH-two
273                 master
274                 EOF
275                 test_cmp expected actual
276         )
277 '
278
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
288         EOF
289         git branch --format="Refname is %(refname)" >actual &&
290         test_i18ncmp expect actual
291 '
292
293 test_expect_success 'worktree colors correct' '
294         cat >expect <<-EOF &&
295         * <GREEN>(HEAD detached from fromtag)<RESET>
296           ambiguous<RESET>
297           branch-one<RESET>
298         + <CYAN>branch-two<RESET>
299           master<RESET>
300           ref-to-branch<RESET> -> branch-one
301           ref-to-remote<RESET> -> origin/branch-one
302         EOF
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
309 '
310
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"
315 '
316
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
321 '
322
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
327 '
328
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
333 '
334
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
340           ambiguous                    $one one
341           branch-one                   $two two
342         + branch-two                   $one ($(pwd)/worktree_dir) one
343           main                         $two two
344           ref-to-branch                $two two
345           ref-to-remote                $two two
346         EOF
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
352 '
353
354 test_done