]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3203-branch-output.sh
commit-graph: avoid leaking topo_levels slab in write_commit_graph()
[thirdparty/git.git] / t / t3203-branch-output.sh
1 #!/bin/sh
2
3 test_description='git branch display tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
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 git branch -M main &&
15 echo content >>file &&
16 git commit -a -m two
17 '
18
19 test_expect_success 'make branches' '
20 git branch branch-one &&
21 git branch branch-two HEAD^
22 '
23
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
28 '
29
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
38 '
39
40 test_expect_success 'git branch --list shows local branches' '
41 git branch --list >actual &&
42 test_cmp expect actual
43 '
44
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
52 '
53
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
62 '
63
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
75 '
76
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
86 '
87
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
96 '
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
101 '
102
103 test_expect_success 'git branch -v pattern does not show branch summaries' '
104 test_must_fail git branch -v branch*
105 '
106
107 test_expect_success 'git branch `--show-current` shows current branch' '
108 cat >expect <<-\EOF &&
109 branch-two
110 EOF
111 git checkout branch-two &&
112 git branch --show-current >actual &&
113 test_cmp expect actual
114 '
115
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
120 '
121
122 test_expect_success 'git branch `--show-current` works properly when tag exists' '
123 cat >expect <<-\EOF &&
124 branch-and-tag-name
125 EOF
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
135 '
136
137 test_expect_success 'git branch `--show-current` works properly with worktrees' '
138 cat >expect <<-\EOF &&
139 branch-one
140 branch-two
141 EOF
142 git checkout branch-one &&
143 test_when_finished "
144 git worktree remove worktree_dir
145 " &&
146 git worktree add worktree_dir branch-two &&
147 {
148 git branch --show-current &&
149 git -C worktree_dir branch --show-current
150 } >actual &&
151 test_cmp expect actual
152 '
153
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
160 EOF
161 git checkout HEAD^0 &&
162 git branch >actual &&
163 test_i18ncmp expect actual
164 '
165
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
173 EOF
174 git checkout --detach &&
175 git branch >actual &&
176 test_i18ncmp expect actual
177 '
178
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
185 EOF
186 git reset --hard HEAD^1 &&
187 git branch >actual &&
188 test_i18ncmp expect actual
189 '
190
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
197 EOF
198 git tag fromtag main &&
199 git checkout fromtag &&
200 git branch >actual &&
201 test_i18ncmp expect actual
202 '
203
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
210 EOF
211 git reset --hard HEAD^1 &&
212 git branch >actual &&
213 test_i18ncmp expect actual
214 '
215
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
222 EOF
223 git branch --sort=objectsize >actual &&
224 test_i18ncmp expect actual &&
225
226 cat >expect <<-\EOF &&
227 * (HEAD detached from fromtag)
228 branch-one
229 main
230 branch-two
231 EOF
232 git branch --sort=-objectsize >actual &&
233 test_i18ncmp expect actual
234 '
235
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
242 EOF
243 git branch --sort=type >actual &&
244 test_i18ncmp expect actual &&
245
246 cat >expect <<-\EOF &&
247 * (HEAD detached from fromtag)
248 branch-one
249 branch-two
250 main
251 EOF
252 git branch --sort=-type >actual &&
253 test_i18ncmp expect actual
254 '
255
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
262 EOF
263 git branch --sort=version:refname >actual &&
264 test_i18ncmp expect actual &&
265
266 cat >expect <<-\EOF &&
267 * (HEAD detached from fromtag)
268 main
269 branch-two
270 branch-one
271 EOF
272 git branch --sort=-version:refname >actual &&
273 test_i18ncmp expect actual
274 '
275
276 test_expect_success 'git branch --points-at option' '
277 cat >expect <<-\EOF &&
278 branch-one
279 main
280 EOF
281 git branch --points-at=branch-one >actual &&
282 test_cmp expect actual
283 '
284
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
291 '
292
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
299 EOF
300 git branch >actual.raw &&
301 grep ref-to <actual.raw >actual &&
302 test_cmp expect actual
303 '
304
305 test_expect_success 'sort branches, ignore case' '
306 (
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
317 EOF
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
324 EOF
325 test_cmp expected actual
326 )
327 '
328
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
338 EOF
339 git branch --format="Refname is %(refname)" >actual &&
340 test_i18ncmp expect actual
341 '
342
343 test_expect_success 'worktree colors correct' '
344 cat >expect <<-EOF &&
345 * <GREEN>(HEAD detached from fromtag)<RESET>
346 ambiguous<RESET>
347 branch-one<RESET>
348 + <CYAN>branch-two<RESET>
349 main<RESET>
350 ref-to-branch<RESET> -> branch-one
351 ref-to-remote<RESET> -> origin/branch-one
352 EOF
353 git worktree add worktree_dir branch-two &&
354 git branch --color >actual.raw &&
355 rm -r worktree_dir &&
356 git worktree prune &&
357 test_decode_color <actual.raw >actual &&
358 test_i18ncmp expect actual
359 '
360
361 test_expect_success "set up color tests" '
362 echo "<RED>main<RESET>" >expect.color &&
363 echo "main" >expect.bare &&
364 color_args="--format=%(color:red)%(refname:short) --list main"
365 '
366
367 test_expect_success '%(color) omitted without tty' '
368 TERM=vt100 git branch $color_args >actual.raw &&
369 test_decode_color <actual.raw >actual &&
370 test_cmp expect.bare actual
371 '
372
373 test_expect_success TTY '%(color) present with tty' '
374 test_terminal git branch $color_args >actual.raw &&
375 test_decode_color <actual.raw >actual &&
376 test_cmp expect.color actual
377 '
378
379 test_expect_success '--color overrides auto-color' '
380 git branch --color $color_args >actual.raw &&
381 test_decode_color <actual.raw >actual &&
382 test_cmp expect.color actual
383 '
384
385 test_expect_success 'verbose output lists worktree path' '
386 one=$(git rev-parse --short HEAD) &&
387 two=$(git rev-parse --short main) &&
388 cat >expect <<-EOF &&
389 * (HEAD detached from fromtag) $one one
390 ambiguous $one one
391 branch-one $two two
392 + branch-two $one ($(pwd)/worktree_dir) one
393 main $two two
394 ref-to-branch $two two
395 ref-to-remote $two two
396 EOF
397 git worktree add worktree_dir branch-two &&
398 git branch -vv >actual &&
399 rm -r worktree_dir &&
400 git worktree prune &&
401 test_i18ncmp expect actual
402 '
403
404 test_done