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