Merge branch 'ab/ci-updates'
[git/debian.git] / t / t3203-branch-output.sh
blob6e94c6db7b5aa987c29364f32fee8c0d52538adf
1 #!/bin/sh
3 test_description='git branch display tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 . ./test-lib.sh
8 . "$TEST_DIRECTORY"/lib-terminal.sh
10 test_expect_success 'make commits' '
11 echo content >file &&
12 git add file &&
13 git commit -m one &&
14 git branch -M main &&
15 echo content >>file &&
16 git commit -a -m two
19 test_expect_success 'make branches' '
20 git branch branch-one &&
21 git branch branch-two HEAD^
24 test_expect_success 'make remote branches' '
25 git update-ref refs/remotes/origin/branch-one branch-one &&
26 git update-ref refs/remotes/origin/branch-two branch-two &&
27 git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
30 cat >expect <<'EOF'
31 branch-one
32 branch-two
33 * main
34 EOF
35 test_expect_success 'git branch shows local branches' '
36 git branch >actual &&
37 test_cmp expect actual
40 test_expect_success 'git branch --list shows local branches' '
41 git branch --list >actual &&
42 test_cmp expect actual
45 cat >expect <<'EOF'
46 branch-one
47 branch-two
48 EOF
49 test_expect_success 'git branch --list pattern shows matching local branches' '
50 git branch --list branch* >actual &&
51 test_cmp expect actual
54 cat >expect <<'EOF'
55 origin/HEAD -> origin/branch-one
56 origin/branch-one
57 origin/branch-two
58 EOF
59 test_expect_success 'git branch -r shows remote branches' '
60 git branch -r >actual &&
61 test_cmp expect actual
64 cat >expect <<'EOF'
65 branch-one
66 branch-two
67 * main
68 remotes/origin/HEAD -> origin/branch-one
69 remotes/origin/branch-one
70 remotes/origin/branch-two
71 EOF
72 test_expect_success 'git branch -a shows local and remote branches' '
73 git branch -a >actual &&
74 test_cmp expect actual
77 cat >expect <<'EOF'
78 two
79 one
80 two
81 EOF
82 test_expect_success 'git branch -v shows branch summaries' '
83 git branch -v >tmp &&
84 awk "{print \$NF}" <tmp >actual &&
85 test_cmp expect actual
88 cat >expect <<'EOF'
89 two
90 one
91 EOF
92 test_expect_success 'git branch --list -v pattern shows branch summaries' '
93 git branch --list -v branch* >tmp &&
94 awk "{print \$NF}" <tmp >actual &&
95 test_cmp expect actual
97 test_expect_success 'git branch --ignore-case --list -v pattern shows branch summaries' '
98 git branch --list --ignore-case -v BRANCH* >tmp &&
99 awk "{print \$NF}" <tmp >actual &&
100 test_cmp expect actual
103 test_expect_success 'git branch -v pattern does not show branch summaries' '
104 test_must_fail git branch -v branch*
107 test_expect_success 'git branch `--show-current` shows current branch' '
108 cat >expect <<-\EOF &&
109 branch-two
111 git checkout branch-two &&
112 git branch --show-current >actual &&
113 test_cmp expect actual
116 test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
117 git checkout HEAD^0 &&
118 git branch --show-current >actual &&
119 test_must_be_empty actual
122 test_expect_success 'git branch `--show-current` works properly when tag exists' '
123 cat >expect <<-\EOF &&
124 branch-and-tag-name
126 test_when_finished "
127 git checkout branch-one
128 git branch -D branch-and-tag-name
129 " &&
130 git checkout -b branch-and-tag-name &&
131 test_when_finished "git tag -d branch-and-tag-name" &&
132 git tag branch-and-tag-name &&
133 git branch --show-current >actual &&
134 test_cmp expect actual
137 test_expect_success 'git branch `--show-current` works properly with worktrees' '
138 cat >expect <<-\EOF &&
139 branch-one
140 branch-two
142 git checkout branch-one &&
143 test_when_finished "
144 git worktree remove worktree_dir
145 " &&
146 git worktree add worktree_dir branch-two &&
148 git branch --show-current &&
149 git -C worktree_dir branch --show-current
150 } >actual &&
151 test_cmp expect actual
154 test_expect_success 'git branch shows detached HEAD properly' '
155 cat >expect <<EOF &&
156 * (HEAD detached at $(git rev-parse --short HEAD^0))
157 branch-one
158 branch-two
159 main
161 git checkout HEAD^0 &&
162 git branch >actual &&
163 test_cmp expect actual
166 test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
167 git checkout main &&
168 cat >expect <<EOF &&
169 * (HEAD detached at $(git rev-parse --short HEAD^0))
170 branch-one
171 branch-two
172 main
174 git checkout --detach &&
175 git branch >actual &&
176 test_cmp expect actual
179 test_expect_success 'git branch shows detached HEAD properly after moving' '
180 cat >expect <<EOF &&
181 * (HEAD detached from $(git rev-parse --short HEAD))
182 branch-one
183 branch-two
184 main
186 git reset --hard HEAD^1 &&
187 git branch >actual &&
188 test_cmp expect actual
191 test_expect_success 'git branch shows detached HEAD properly from tag' '
192 cat >expect <<EOF &&
193 * (HEAD detached at fromtag)
194 branch-one
195 branch-two
196 main
198 git tag fromtag main &&
199 git checkout fromtag &&
200 git branch >actual &&
201 test_cmp expect actual
204 test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
205 cat >expect <<EOF &&
206 * (HEAD detached from fromtag)
207 branch-one
208 branch-two
209 main
211 git reset --hard HEAD^1 &&
212 git branch >actual &&
213 test_cmp expect actual
216 test_expect_success 'git branch `--sort=[-]objectsize` option' '
217 cat >expect <<-\EOF &&
218 * (HEAD detached from fromtag)
219 branch-two
220 branch-one
221 main
223 git branch --sort=objectsize >actual &&
224 test_cmp expect actual &&
226 cat >expect <<-\EOF &&
227 * (HEAD detached from fromtag)
228 branch-one
229 main
230 branch-two
232 git branch --sort=-objectsize >actual &&
233 test_cmp expect actual
236 test_expect_success 'git branch `--sort=[-]type` option' '
237 cat >expect <<-\EOF &&
238 * (HEAD detached from fromtag)
239 branch-one
240 branch-two
241 main
243 git branch --sort=type >actual &&
244 test_cmp expect actual &&
246 cat >expect <<-\EOF &&
247 * (HEAD detached from fromtag)
248 branch-one
249 branch-two
250 main
252 git branch --sort=-type >actual &&
253 test_cmp expect actual
256 test_expect_success 'git branch `--sort=[-]version:refname` option' '
257 cat >expect <<-\EOF &&
258 * (HEAD detached from fromtag)
259 branch-one
260 branch-two
261 main
263 git branch --sort=version:refname >actual &&
264 test_cmp expect actual &&
266 cat >expect <<-\EOF &&
267 * (HEAD detached from fromtag)
268 main
269 branch-two
270 branch-one
272 git branch --sort=-version:refname >actual &&
273 test_cmp expect actual
276 test_expect_success 'git branch --points-at option' '
277 cat >expect <<-\EOF &&
278 branch-one
279 main
281 git branch --points-at=branch-one >actual &&
282 test_cmp expect actual
285 test_expect_success 'ambiguous branch/tag not marked' '
286 git tag ambiguous &&
287 git branch ambiguous &&
288 echo " ambiguous" >expect &&
289 git branch --list ambiguous >actual &&
290 test_cmp expect actual
293 test_expect_success 'local-branch symrefs shortened properly' '
294 git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
295 git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
296 cat >expect <<-\EOF &&
297 ref-to-branch -> branch-one
298 ref-to-remote -> origin/branch-one
300 git branch >actual.raw &&
301 grep ref-to <actual.raw >actual &&
302 test_cmp expect actual
305 test_expect_success 'sort branches, ignore case' '
307 git init -b main sort-icase &&
308 cd sort-icase &&
309 test_commit initial &&
310 git branch branch-one &&
311 git branch BRANCH-two &&
312 git branch --list | awk "{print \$NF}" >actual &&
313 cat >expected <<-\EOF &&
314 BRANCH-two
315 branch-one
316 main
318 test_cmp expected actual &&
319 git branch --list -i | awk "{print \$NF}" >actual &&
320 cat >expected <<-\EOF &&
321 branch-one
322 BRANCH-two
323 main
325 test_cmp expected actual
329 test_expect_success 'git branch --format option' '
330 cat >expect <<-\EOF &&
331 Refname is (HEAD detached from fromtag)
332 Refname is refs/heads/ambiguous
333 Refname is refs/heads/branch-one
334 Refname is refs/heads/branch-two
335 Refname is refs/heads/main
336 Refname is refs/heads/ref-to-branch
337 Refname is refs/heads/ref-to-remote
339 git branch --format="Refname is %(refname)" >actual &&
340 test_cmp expect actual
343 test_expect_success 'git branch with --format=%(rest) must fail' '
344 test_must_fail git branch --format="%(rest)" >actual
347 test_expect_success 'worktree colors correct' '
348 cat >expect <<-EOF &&
349 * <GREEN>(HEAD detached from fromtag)<RESET>
350 ambiguous<RESET>
351 branch-one<RESET>
352 + <CYAN>branch-two<RESET>
353 main<RESET>
354 ref-to-branch<RESET> -> branch-one
355 ref-to-remote<RESET> -> origin/branch-one
357 git worktree add worktree_dir branch-two &&
358 git branch --color >actual.raw &&
359 rm -r worktree_dir &&
360 git worktree prune &&
361 test_decode_color <actual.raw >actual &&
362 test_cmp expect actual
365 test_expect_success "set up color tests" '
366 echo "<RED>main<RESET>" >expect.color &&
367 echo "main" >expect.bare &&
368 color_args="--format=%(color:red)%(refname:short) --list main"
371 test_expect_success '%(color) omitted without tty' '
372 TERM=vt100 git branch $color_args >actual.raw &&
373 test_decode_color <actual.raw >actual &&
374 test_cmp expect.bare actual
377 test_expect_success TTY '%(color) present with tty' '
378 test_terminal git branch $color_args >actual.raw &&
379 test_decode_color <actual.raw >actual &&
380 test_cmp expect.color actual
383 test_expect_success '--color overrides auto-color' '
384 git branch --color $color_args >actual.raw &&
385 test_decode_color <actual.raw >actual &&
386 test_cmp expect.color actual
389 test_expect_success 'verbose output lists worktree path' '
390 one=$(git rev-parse --short HEAD) &&
391 two=$(git rev-parse --short main) &&
392 cat >expect <<-EOF &&
393 * (HEAD detached from fromtag) $one one
394 ambiguous $one one
395 branch-one $two two
396 + branch-two $one ($(pwd)/worktree_dir) one
397 main $two two
398 ref-to-branch $two two
399 ref-to-remote $two two
401 git worktree add worktree_dir branch-two &&
402 git branch -vv >actual &&
403 rm -r worktree_dir &&
404 git worktree prune &&
405 test_cmp expect actual
408 test_done