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