]>
Commit | Line | Data |
---|---|---|
0afc3044 JK |
1 | #!/bin/sh |
2 | ||
3 | test_description='git branch display tests' | |
d6c6b108 | 4 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
5 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
6 | ||
0afc3044 | 7 | . ./test-lib.sh |
11b087ad | 8 | . "$TEST_DIRECTORY"/lib-terminal.sh |
0afc3044 JK |
9 | |
10 | test_expect_success 'make commits' ' | |
11 | echo content >file && | |
12 | git add file && | |
13 | git commit -m one && | |
1eee0a42 | 14 | git branch -M main && |
0afc3044 JK |
15 | echo content >>file && |
16 | git commit -a -m two | |
17 | ' | |
18 | ||
19 | test_expect_success 'make branches' ' | |
a48fcd83 | 20 | git branch branch-one && |
0afc3044 JK |
21 | git branch branch-two HEAD^ |
22 | ' | |
23 | ||
24 | test_expect_success 'make remote branches' ' | |
a48fcd83 JN |
25 | git update-ref refs/remotes/origin/branch-one branch-one && |
26 | git update-ref refs/remotes/origin/branch-two branch-two && | |
0afc3044 JK |
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 | |
1eee0a42 | 33 | * main |
0afc3044 JK |
34 | EOF |
35 | test_expect_success 'git branch shows local branches' ' | |
36 | git branch >actual && | |
37 | test_cmp expect actual | |
38 | ' | |
39 | ||
cddd127b MG |
40 | test_expect_success 'git branch --list shows local branches' ' |
41 | git branch --list >actual && | |
42 | test_cmp expect actual | |
43 | ' | |
44 | ||
d8d33736 MG |
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 | ||
0afc3044 JK |
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 | |
1eee0a42 | 67 | * main |
0afc3044 JK |
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 | ||
d8d33736 MG |
88 | cat >expect <<'EOF' |
89 | two | |
90 | one | |
91 | EOF | |
7b787599 MG |
92 | test_expect_success 'git branch --list -v pattern shows branch summaries' ' |
93 | git branch --list -v branch* >tmp && | |
d8d33736 MG |
94 | awk "{print \$NF}" <tmp >actual && |
95 | test_cmp expect actual | |
96 | ' | |
3bb16a8b NTND |
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 | ' | |
d8d33736 | 102 | |
7b787599 MG |
103 | test_expect_success 'git branch -v pattern does not show branch summaries' ' |
104 | test_must_fail git branch -v branch* | |
105 | ' | |
106 | ||
0ecb1fc7 DU |
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 && | |
99eea645 JH |
143 | test_when_finished " |
144 | git worktree remove worktree_dir | |
145 | " && | |
ab313814 | 146 | git worktree add worktree_dir branch-two && |
0ecb1fc7 DU |
147 | { |
148 | git branch --show-current && | |
ab313814 | 149 | git -C worktree_dir branch --show-current |
0ecb1fc7 DU |
150 | } >actual && |
151 | test_cmp expect actual | |
152 | ' | |
153 | ||
c8183cd2 NTND |
154 | test_expect_success 'git branch shows detached HEAD properly' ' |
155 | cat >expect <<EOF && | |
4b063186 | 156 | * (HEAD detached at $(git rev-parse --short HEAD^0)) |
0afc3044 JK |
157 | branch-one |
158 | branch-two | |
1eee0a42 | 159 | main |
0afc3044 | 160 | EOF |
0afc3044 JK |
161 | git checkout HEAD^0 && |
162 | git branch >actual && | |
1108cea7 | 163 | test_cmp expect actual |
0afc3044 JK |
164 | ' |
165 | ||
0eb8548f | 166 | test_expect_success 'git branch shows detached HEAD properly after checkout --detach' ' |
1eee0a42 | 167 | git checkout main && |
9cb07d81 MM |
168 | cat >expect <<EOF && |
169 | * (HEAD detached at $(git rev-parse --short HEAD^0)) | |
170 | branch-one | |
171 | branch-two | |
1eee0a42 | 172 | main |
9cb07d81 MM |
173 | EOF |
174 | git checkout --detach && | |
175 | git branch >actual && | |
1108cea7 | 176 | test_cmp expect actual |
9cb07d81 MM |
177 | ' |
178 | ||
4b063186 MG |
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 | |
1eee0a42 | 184 | main |
4b063186 MG |
185 | EOF |
186 | git reset --hard HEAD^1 && | |
187 | git branch >actual && | |
1108cea7 | 188 | test_cmp expect actual |
4b063186 MG |
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 | |
1eee0a42 | 196 | main |
4b063186 | 197 | EOF |
1eee0a42 | 198 | git tag fromtag main && |
4b063186 MG |
199 | git checkout fromtag && |
200 | git branch >actual && | |
1108cea7 | 201 | test_cmp expect actual |
4b063186 MG |
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 | |
1eee0a42 | 209 | main |
4b063186 MG |
210 | EOF |
211 | git reset --hard HEAD^1 && | |
212 | git branch >actual && | |
1108cea7 | 213 | test_cmp expect actual |
4b063186 MG |
214 | ' |
215 | ||
08bf6a8b | 216 | test_expect_success 'git branch `--sort=[-]objectsize` option' ' |
aedcb7dc | 217 | cat >expect <<-\EOF && |
aedcb7dc | 218 | * (HEAD detached from fromtag) |
9e468334 | 219 | branch-two |
aedcb7dc | 220 | branch-one |
1eee0a42 | 221 | main |
aedcb7dc KN |
222 | EOF |
223 | git branch --sort=objectsize >actual && | |
1108cea7 | 224 | test_cmp expect actual && |
08bf6a8b ÆAB |
225 | |
226 | cat >expect <<-\EOF && | |
4045f659 | 227 | * (HEAD detached from fromtag) |
08bf6a8b ÆAB |
228 | branch-one |
229 | main | |
08bf6a8b ÆAB |
230 | branch-two |
231 | EOF | |
232 | git branch --sort=-objectsize >actual && | |
1108cea7 | 233 | test_cmp expect actual |
08bf6a8b ÆAB |
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 && | |
1108cea7 | 244 | test_cmp expect actual && |
08bf6a8b ÆAB |
245 | |
246 | cat >expect <<-\EOF && | |
4045f659 | 247 | * (HEAD detached from fromtag) |
08bf6a8b ÆAB |
248 | branch-one |
249 | branch-two | |
250 | main | |
251 | EOF | |
252 | git branch --sort=-type >actual && | |
1108cea7 | 253 | test_cmp expect actual |
08bf6a8b ÆAB |
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 && | |
1108cea7 | 264 | test_cmp expect actual && |
08bf6a8b ÆAB |
265 | |
266 | cat >expect <<-\EOF && | |
4045f659 | 267 | * (HEAD detached from fromtag) |
08bf6a8b ÆAB |
268 | main |
269 | branch-two | |
270 | branch-one | |
08bf6a8b ÆAB |
271 | EOF |
272 | git branch --sort=-version:refname >actual && | |
1108cea7 | 273 | test_cmp expect actual |
aedcb7dc KN |
274 | ' |
275 | ||
aa3bc55e KN |
276 | test_expect_success 'git branch --points-at option' ' |
277 | cat >expect <<-\EOF && | |
278 | branch-one | |
1eee0a42 | 279 | main |
aa3bc55e KN |
280 | EOF |
281 | git branch --points-at=branch-one >actual && | |
282 | test_cmp expect actual | |
283 | ' | |
284 | ||
0571979b JK |
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 | ||
95c38fb0 JK |
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 | |
949af068 | 298 | ref-to-remote -> origin/branch-one |
95c38fb0 JK |
299 | EOF |
300 | git branch >actual.raw && | |
301 | grep ref-to <actual.raw >actual && | |
302 | test_cmp expect actual | |
303 | ' | |
304 | ||
3bb16a8b NTND |
305 | test_expect_success 'sort branches, ignore case' ' |
306 | ( | |
1eee0a42 | 307 | git init -b main sort-icase && |
3bb16a8b NTND |
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 | |
1eee0a42 | 316 | main |
3bb16a8b NTND |
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 | |
1eee0a42 | 323 | main |
3bb16a8b NTND |
324 | EOF |
325 | test_cmp expected actual | |
326 | ) | |
327 | ' | |
328 | ||
3d9e4ce3 KN |
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 | |
1eee0a42 | 335 | Refname is refs/heads/main |
3d9e4ce3 KN |
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 && | |
1108cea7 | 340 | test_cmp expect actual |
3d9e4ce3 KN |
341 | ' |
342 | ||
ab313814 NB |
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> | |
1eee0a42 | 349 | main<RESET> |
ab313814 NB |
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 && | |
1108cea7 | 358 | test_cmp expect actual |
ab313814 NB |
359 | ' |
360 | ||
11b087ad | 361 | test_expect_success "set up color tests" ' |
1eee0a42 JS |
362 | echo "<RED>main<RESET>" >expect.color && |
363 | echo "main" >expect.bare && | |
364 | color_args="--format=%(color:red)%(refname:short) --list main" | |
11b087ad JK |
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' ' | |
e433749d | 374 | test_terminal git branch $color_args >actual.raw && |
11b087ad JK |
375 | test_decode_color <actual.raw >actual && |
376 | test_cmp expect.color actual | |
377 | ' | |
378 | ||
11b087ad JK |
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 | ||
1eee0a42 | 385 | test_expect_success 'verbose output lists worktree path' ' |
6e938146 | 386 | one=$(git rev-parse --short HEAD) && |
1eee0a42 | 387 | two=$(git rev-parse --short main) && |
6e938146 NB |
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 | |
66713e84 | 393 | main $two two |
6e938146 NB |
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 && | |
1108cea7 | 401 | test_cmp expect actual |
6e938146 NB |
402 | ' |
403 | ||
0afc3044 | 404 | test_done |