]>
Commit | Line | Data |
---|---|---|
0afc3044 JK |
1 | #!/bin/sh |
2 | ||
3 | test_description='git branch display tests' | |
4 | . ./test-lib.sh | |
11b087ad | 5 | . "$TEST_DIRECTORY"/lib-terminal.sh |
0afc3044 JK |
6 | |
7 | test_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 | ||
16 | test_expect_success 'make branches' ' | |
a48fcd83 | 17 | git branch branch-one && |
0afc3044 JK |
18 | git branch branch-two HEAD^ |
19 | ' | |
20 | ||
21 | test_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 | ||
27 | cat >expect <<'EOF' | |
28 | branch-one | |
29 | branch-two | |
1eee0a42 | 30 | * main |
0afc3044 JK |
31 | EOF |
32 | test_expect_success 'git branch shows local branches' ' | |
33 | git branch >actual && | |
34 | test_cmp expect actual | |
35 | ' | |
36 | ||
cddd127b MG |
37 | test_expect_success 'git branch --list shows local branches' ' |
38 | git branch --list >actual && | |
39 | test_cmp expect actual | |
40 | ' | |
41 | ||
d8d33736 MG |
42 | cat >expect <<'EOF' |
43 | branch-one | |
44 | branch-two | |
45 | EOF | |
46 | test_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 |
51 | cat >expect <<'EOF' |
52 | origin/HEAD -> origin/branch-one | |
53 | origin/branch-one | |
54 | origin/branch-two | |
55 | EOF | |
56 | test_expect_success 'git branch -r shows remote branches' ' | |
57 | git branch -r >actual && | |
58 | test_cmp expect actual | |
59 | ' | |
60 | ||
61 | cat >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 | |
68 | EOF | |
69 | test_expect_success 'git branch -a shows local and remote branches' ' | |
70 | git branch -a >actual && | |
71 | test_cmp expect actual | |
72 | ' | |
73 | ||
74 | cat >expect <<'EOF' | |
75 | two | |
76 | one | |
77 | two | |
78 | EOF | |
79 | test_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 |
85 | cat >expect <<'EOF' |
86 | two | |
87 | one | |
88 | EOF | |
7b787599 MG |
89 | test_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 |
94 | test_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 |
100 | test_expect_success 'git branch -v pattern does not show branch summaries' ' |
101 | test_must_fail git branch -v branch* | |
102 | ' | |
103 | ||
0ecb1fc7 DU |
104 | test_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 | ||
113 | test_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 | ||
119 | test_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 | ||
134 | test_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 |
151 | test_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 | 157 | EOF |
0afc3044 JK |
158 | git checkout HEAD^0 && |
159 | git branch >actual && | |
1108cea7 | 160 | test_cmp expect actual |
0afc3044 JK |
161 | ' |
162 | ||
0eb8548f | 163 | test_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 |
170 | EOF |
171 | git checkout --detach && | |
172 | git branch >actual && | |
1108cea7 | 173 | test_cmp expect actual |
9cb07d81 MM |
174 | ' |
175 | ||
4b063186 MG |
176 | test_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 |
182 | EOF |
183 | git reset --hard HEAD^1 && | |
184 | git branch >actual && | |
1108cea7 | 185 | test_cmp expect actual |
4b063186 MG |
186 | ' |
187 | ||
188 | test_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 | 194 | EOF |
1eee0a42 | 195 | git tag fromtag main && |
4b063186 MG |
196 | git checkout fromtag && |
197 | git branch >actual && | |
1108cea7 | 198 | test_cmp expect actual |
4b063186 MG |
199 | ' |
200 | ||
201 | test_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 |
207 | EOF |
208 | git reset --hard HEAD^1 && | |
209 | git branch >actual && | |
1108cea7 | 210 | test_cmp expect actual |
4b063186 MG |
211 | ' |
212 | ||
08bf6a8b | 213 | test_expect_success 'git branch `--sort=[-]objectsize` option' ' |
aedcb7dc | 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 && | |
1108cea7 | 221 | test_cmp expect actual && |
08bf6a8b ÆAB |
222 | |
223 | cat >expect <<-\EOF && | |
4045f659 | 224 | * (HEAD detached from fromtag) |
08bf6a8b ÆAB |
225 | branch-one |
226 | main | |
08bf6a8b ÆAB |
227 | branch-two |
228 | EOF | |
229 | git branch --sort=-objectsize >actual && | |
1108cea7 | 230 | test_cmp expect actual |
08bf6a8b ÆAB |
231 | ' |
232 | ||
233 | test_expect_success 'git branch `--sort=[-]type` option' ' | |
234 | cat >expect <<-\EOF && | |
235 | * (HEAD detached from fromtag) | |
236 | branch-one | |
237 | branch-two | |
238 | main | |
239 | EOF | |
240 | git branch --sort=type >actual && | |
1108cea7 | 241 | test_cmp expect actual && |
08bf6a8b ÆAB |
242 | |
243 | cat >expect <<-\EOF && | |
4045f659 | 244 | * (HEAD detached from fromtag) |
08bf6a8b ÆAB |
245 | branch-one |
246 | branch-two | |
247 | main | |
248 | EOF | |
249 | git branch --sort=-type >actual && | |
1108cea7 | 250 | test_cmp expect actual |
08bf6a8b ÆAB |
251 | ' |
252 | ||
253 | test_expect_success 'git branch `--sort=[-]version:refname` option' ' | |
254 | cat >expect <<-\EOF && | |
255 | * (HEAD detached from fromtag) | |
256 | branch-one | |
257 | branch-two | |
258 | main | |
259 | EOF | |
260 | git branch --sort=version:refname >actual && | |
1108cea7 | 261 | test_cmp expect actual && |
08bf6a8b ÆAB |
262 | |
263 | cat >expect <<-\EOF && | |
4045f659 | 264 | * (HEAD detached from fromtag) |
08bf6a8b ÆAB |
265 | main |
266 | branch-two | |
267 | branch-one | |
08bf6a8b ÆAB |
268 | EOF |
269 | git branch --sort=-version:refname >actual && | |
1108cea7 | 270 | test_cmp expect actual |
aedcb7dc KN |
271 | ' |
272 | ||
aa3bc55e KN |
273 | test_expect_success 'git branch --points-at option' ' |
274 | cat >expect <<-\EOF && | |
275 | branch-one | |
1eee0a42 | 276 | main |
aa3bc55e KN |
277 | EOF |
278 | git branch --points-at=branch-one >actual && | |
279 | test_cmp expect actual | |
280 | ' | |
281 | ||
0571979b JK |
282 | test_expect_success 'ambiguous branch/tag not marked' ' |
283 | git tag ambiguous && | |
284 | git branch ambiguous && | |
285 | echo " ambiguous" >expect && | |
286 | git branch --list ambiguous >actual && | |
287 | test_cmp expect actual | |
288 | ' | |
289 | ||
95c38fb0 JK |
290 | test_expect_success 'local-branch symrefs shortened properly' ' |
291 | git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one && | |
292 | git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one && | |
293 | cat >expect <<-\EOF && | |
294 | ref-to-branch -> branch-one | |
949af068 | 295 | ref-to-remote -> origin/branch-one |
95c38fb0 JK |
296 | EOF |
297 | git branch >actual.raw && | |
298 | grep ref-to <actual.raw >actual && | |
299 | test_cmp expect actual | |
300 | ' | |
301 | ||
3bb16a8b NTND |
302 | test_expect_success 'sort branches, ignore case' ' |
303 | ( | |
1eee0a42 | 304 | git init -b main sort-icase && |
3bb16a8b NTND |
305 | cd sort-icase && |
306 | test_commit initial && | |
307 | git branch branch-one && | |
308 | git branch BRANCH-two && | |
309 | git branch --list | awk "{print \$NF}" >actual && | |
310 | cat >expected <<-\EOF && | |
311 | BRANCH-two | |
312 | branch-one | |
1eee0a42 | 313 | main |
3bb16a8b NTND |
314 | EOF |
315 | test_cmp expected actual && | |
316 | git branch --list -i | awk "{print \$NF}" >actual && | |
317 | cat >expected <<-\EOF && | |
318 | branch-one | |
319 | BRANCH-two | |
1eee0a42 | 320 | main |
3bb16a8b NTND |
321 | EOF |
322 | test_cmp expected actual | |
323 | ) | |
324 | ' | |
325 | ||
3d9e4ce3 KN |
326 | test_expect_success 'git branch --format option' ' |
327 | cat >expect <<-\EOF && | |
328 | Refname is (HEAD detached from fromtag) | |
329 | Refname is refs/heads/ambiguous | |
330 | Refname is refs/heads/branch-one | |
331 | Refname is refs/heads/branch-two | |
1eee0a42 | 332 | Refname is refs/heads/main |
3d9e4ce3 KN |
333 | Refname is refs/heads/ref-to-branch |
334 | Refname is refs/heads/ref-to-remote | |
335 | EOF | |
336 | git branch --format="Refname is %(refname)" >actual && | |
1108cea7 | 337 | test_cmp expect actual |
3d9e4ce3 KN |
338 | ' |
339 | ||
49abcd21 DS |
340 | test_expect_success 'git branch --format with ahead-behind' ' |
341 | cat >expect <<-\EOF && | |
342 | (HEAD detached from fromtag) 0 0 | |
343 | refs/heads/ambiguous 0 0 | |
344 | refs/heads/branch-one 1 0 | |
345 | refs/heads/branch-two 0 0 | |
346 | refs/heads/main 1 0 | |
347 | refs/heads/ref-to-branch 1 0 | |
348 | refs/heads/ref-to-remote 1 0 | |
349 | EOF | |
350 | git branch --format="%(refname) %(ahead-behind:HEAD)" >actual && | |
351 | test_cmp expect actual | |
352 | ' | |
353 | ||
b9dee075 ZH |
354 | test_expect_success 'git branch with --format=%(rest) must fail' ' |
355 | test_must_fail git branch --format="%(rest)" >actual | |
356 | ' | |
357 | ||
aabfdc95 ØW |
358 | test_expect_success 'git branch --format --omit-empty' ' |
359 | cat >expect <<-\EOF && | |
360 | Refname is (HEAD detached from fromtag) | |
361 | Refname is refs/heads/ambiguous | |
362 | Refname is refs/heads/branch-one | |
363 | Refname is refs/heads/branch-two | |
364 | ||
365 | Refname is refs/heads/ref-to-branch | |
366 | Refname is refs/heads/ref-to-remote | |
367 | EOF | |
368 | git branch --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual && | |
369 | test_cmp expect actual && | |
370 | cat >expect <<-\EOF && | |
371 | Refname is (HEAD detached from fromtag) | |
372 | Refname is refs/heads/ambiguous | |
373 | Refname is refs/heads/branch-one | |
374 | Refname is refs/heads/branch-two | |
375 | Refname is refs/heads/ref-to-branch | |
376 | Refname is refs/heads/ref-to-remote | |
377 | EOF | |
378 | git branch --omit-empty --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual && | |
379 | test_cmp expect actual | |
380 | ' | |
381 | ||
ab313814 NB |
382 | test_expect_success 'worktree colors correct' ' |
383 | cat >expect <<-EOF && | |
384 | * <GREEN>(HEAD detached from fromtag)<RESET> | |
385 | ambiguous<RESET> | |
386 | branch-one<RESET> | |
387 | + <CYAN>branch-two<RESET> | |
1eee0a42 | 388 | main<RESET> |
ab313814 NB |
389 | ref-to-branch<RESET> -> branch-one |
390 | ref-to-remote<RESET> -> origin/branch-one | |
391 | EOF | |
392 | git worktree add worktree_dir branch-two && | |
393 | git branch --color >actual.raw && | |
394 | rm -r worktree_dir && | |
395 | git worktree prune && | |
396 | test_decode_color <actual.raw >actual && | |
1108cea7 | 397 | test_cmp expect actual |
ab313814 NB |
398 | ' |
399 | ||
11b087ad | 400 | test_expect_success "set up color tests" ' |
1eee0a42 JS |
401 | echo "<RED>main<RESET>" >expect.color && |
402 | echo "main" >expect.bare && | |
403 | color_args="--format=%(color:red)%(refname:short) --list main" | |
11b087ad JK |
404 | ' |
405 | ||
406 | test_expect_success '%(color) omitted without tty' ' | |
407 | TERM=vt100 git branch $color_args >actual.raw && | |
408 | test_decode_color <actual.raw >actual && | |
409 | test_cmp expect.bare actual | |
410 | ' | |
411 | ||
412 | test_expect_success TTY '%(color) present with tty' ' | |
e433749d | 413 | test_terminal git branch $color_args >actual.raw && |
11b087ad JK |
414 | test_decode_color <actual.raw >actual && |
415 | test_cmp expect.color actual | |
416 | ' | |
417 | ||
11b087ad JK |
418 | test_expect_success '--color overrides auto-color' ' |
419 | git branch --color $color_args >actual.raw && | |
420 | test_decode_color <actual.raw >actual && | |
421 | test_cmp expect.color actual | |
422 | ' | |
423 | ||
1eee0a42 | 424 | test_expect_success 'verbose output lists worktree path' ' |
6e938146 | 425 | one=$(git rev-parse --short HEAD) && |
1eee0a42 | 426 | two=$(git rev-parse --short main) && |
6e938146 NB |
427 | cat >expect <<-EOF && |
428 | * (HEAD detached from fromtag) $one one | |
429 | ambiguous $one one | |
430 | branch-one $two two | |
431 | + branch-two $one ($(pwd)/worktree_dir) one | |
66713e84 | 432 | main $two two |
6e938146 NB |
433 | ref-to-branch $two two |
434 | ref-to-remote $two two | |
435 | EOF | |
436 | git worktree add worktree_dir branch-two && | |
437 | git branch -vv >actual && | |
438 | rm -r worktree_dir && | |
439 | git worktree prune && | |
1108cea7 | 440 | test_cmp expect actual |
6e938146 NB |
441 | ' |
442 | ||
0afc3044 | 443 | test_done |