Merge branch 'kz/merge-fail-early-upon-refresh-failure'
[git.git] / t / t6302-for-each-ref-filter.sh
blob163c378cfd16577a00199dc1696c136662854004
1 #!/bin/sh
3 test_description='test for-each-refs usage of ref-filter APIs'
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-gpg.sh
8 test_expect_success 'setup some history and refs' '
9 test_commit one &&
10 git branch -M main &&
11 test_commit two &&
12 test_commit three &&
13 git checkout -b side &&
14 test_commit four &&
15 git tag -m "An annotated tag" annotated-tag &&
16 git tag -m "Annonated doubly" doubly-annotated-tag annotated-tag &&
18 # Note that these "signed" tags might not actually be signed.
19 # Tests which care about the distinction should be marked
20 # with the GPG prereq.
21 if test_have_prereq GPG
22 then
23 sign=-s
24 else
25 sign=
26 fi &&
27 git tag $sign -m "A signed tag" signed-tag &&
28 git tag $sign -m "Signed doubly" doubly-signed-tag signed-tag &&
30 git checkout main &&
31 git update-ref refs/odd/spot main
34 test_expect_success '--include-root-refs pattern prints pseudorefs' '
35 cat >expect <<-\EOF &&
36 HEAD
37 ORIG_HEAD
38 refs/heads/main
39 refs/heads/side
40 refs/odd/spot
41 refs/tags/annotated-tag
42 refs/tags/doubly-annotated-tag
43 refs/tags/doubly-signed-tag
44 refs/tags/four
45 refs/tags/one
46 refs/tags/signed-tag
47 refs/tags/three
48 refs/tags/two
49 EOF
50 git update-ref ORIG_HEAD main &&
51 git for-each-ref --format="%(refname)" --include-root-refs >actual &&
52 test_cmp expect actual
55 test_expect_success '--include-root-refs pattern does not print special refs' '
56 test_when_finished "rm -rf repo" &&
57 git init repo &&
59 cd repo &&
60 test_commit initial &&
61 git rev-parse HEAD >.git/MERGE_HEAD &&
62 git for-each-ref --format="%(refname)" --include-root-refs >actual &&
63 cat >expect <<-EOF &&
64 HEAD
65 $(git symbolic-ref HEAD)
66 refs/tags/initial
67 EOF
68 test_cmp expect actual
72 test_expect_success '--include-root-refs with other patterns' '
73 cat >expect <<-\EOF &&
74 HEAD
75 ORIG_HEAD
76 EOF
77 git update-ref ORIG_HEAD main &&
78 git for-each-ref --format="%(refname)" --include-root-refs "*HEAD" >actual &&
79 test_cmp expect actual
82 test_expect_success '--include-root-refs omits dangling symrefs' '
83 test_when_finished "rm -rf repo" &&
84 git init repo &&
86 cd repo &&
87 test_commit initial &&
88 git symbolic-ref DANGLING_HEAD refs/heads/missing &&
89 cat >expect <<-EOF &&
90 HEAD
91 $(git symbolic-ref HEAD)
92 refs/tags/initial
93 EOF
94 git for-each-ref --format="%(refname)" --include-root-refs >actual &&
95 test_cmp expect actual
99 test_expect_success 'filtering with --points-at' '
100 cat >expect <<-\EOF &&
101 refs/heads/main
102 refs/odd/spot
103 refs/tags/three
105 git for-each-ref --format="%(refname)" --points-at=main >actual &&
106 test_cmp expect actual
109 test_expect_success 'check signed tags with --points-at' '
110 sed -e "s/Z$//" >expect <<-\EOF &&
111 refs/heads/side Z
112 refs/tags/annotated-tag four
113 refs/tags/doubly-annotated-tag four
114 refs/tags/doubly-signed-tag four
115 refs/tags/four Z
116 refs/tags/signed-tag four
118 git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
119 test_cmp expect actual
122 test_expect_success 'filtering with --merged' '
123 cat >expect <<-\EOF &&
124 refs/heads/main
125 refs/odd/spot
126 refs/tags/one
127 refs/tags/three
128 refs/tags/two
130 git for-each-ref --format="%(refname)" --merged=main >actual &&
131 test_cmp expect actual
134 test_expect_success 'filtering with --no-merged' '
135 cat >expect <<-\EOF &&
136 refs/heads/side
137 refs/tags/annotated-tag
138 refs/tags/doubly-annotated-tag
139 refs/tags/doubly-signed-tag
140 refs/tags/four
141 refs/tags/signed-tag
143 git for-each-ref --format="%(refname)" --no-merged=main >actual &&
144 test_cmp expect actual
147 test_expect_success 'filtering with --contains' '
148 cat >expect <<-\EOF &&
149 refs/heads/main
150 refs/heads/side
151 refs/odd/spot
152 refs/tags/annotated-tag
153 refs/tags/doubly-annotated-tag
154 refs/tags/doubly-signed-tag
155 refs/tags/four
156 refs/tags/signed-tag
157 refs/tags/three
158 refs/tags/two
160 git for-each-ref --format="%(refname)" --contains=two >actual &&
161 test_cmp expect actual
164 test_expect_success 'filtering with --no-contains' '
165 cat >expect <<-\EOF &&
166 refs/tags/one
168 git for-each-ref --format="%(refname)" --no-contains=two >actual &&
169 test_cmp expect actual
172 test_expect_success 'filtering with --contains and --no-contains' '
173 cat >expect <<-\EOF &&
174 refs/tags/two
176 git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
177 test_cmp expect actual
180 test_expect_success '%(color) must fail' '
181 test_must_fail git for-each-ref --format="%(color)%(refname)"
184 test_expect_success '%(color:#aa22ac) must succeed' '
185 test_when_finished rm -rf test &&
186 git init test &&
188 cd test &&
189 test_commit initial &&
190 git branch -M main &&
191 cat >expect <<-\EOF &&
192 refs/heads/main
193 refs/tags/initial
195 git remote add origin nowhere &&
196 git config branch.main.remote origin &&
197 git config branch.main.merge refs/heads/main &&
198 git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
199 test_cmp expect actual
203 test_expect_success 'left alignment is default' '
204 cat >expect <<-\EOF &&
205 refname is refs/heads/main |refs/heads/main
206 refname is refs/heads/side |refs/heads/side
207 refname is refs/odd/spot |refs/odd/spot
208 refname is refs/tags/annotated-tag|refs/tags/annotated-tag
209 refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
210 refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
211 refname is refs/tags/four |refs/tags/four
212 refname is refs/tags/one |refs/tags/one
213 refname is refs/tags/signed-tag|refs/tags/signed-tag
214 refname is refs/tags/three |refs/tags/three
215 refname is refs/tags/two |refs/tags/two
217 git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
218 test_cmp expect actual
221 test_expect_success 'middle alignment' '
222 cat >expect <<-\EOF &&
223 | refname is refs/heads/main |refs/heads/main
224 | refname is refs/heads/side |refs/heads/side
225 | refname is refs/odd/spot |refs/odd/spot
226 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
227 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
228 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
229 | refname is refs/tags/four |refs/tags/four
230 | refname is refs/tags/one |refs/tags/one
231 |refname is refs/tags/signed-tag|refs/tags/signed-tag
232 | refname is refs/tags/three |refs/tags/three
233 | refname is refs/tags/two |refs/tags/two
235 git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
236 test_cmp expect actual
239 test_expect_success 'right alignment' '
240 cat >expect <<-\EOF &&
241 | refname is refs/heads/main|refs/heads/main
242 | refname is refs/heads/side|refs/heads/side
243 | refname is refs/odd/spot|refs/odd/spot
244 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
245 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
246 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
247 | refname is refs/tags/four|refs/tags/four
248 | refname is refs/tags/one|refs/tags/one
249 |refname is refs/tags/signed-tag|refs/tags/signed-tag
250 | refname is refs/tags/three|refs/tags/three
251 | refname is refs/tags/two|refs/tags/two
253 git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
254 test_cmp expect actual
257 cat >expect <<-\EOF
258 | refname is refs/heads/main |refs/heads/main
259 | refname is refs/heads/side |refs/heads/side
260 | refname is refs/odd/spot |refs/odd/spot
261 | refname is refs/tags/annotated-tag |refs/tags/annotated-tag
262 |refname is refs/tags/doubly-annotated-tag |refs/tags/doubly-annotated-tag
263 | refname is refs/tags/doubly-signed-tag |refs/tags/doubly-signed-tag
264 | refname is refs/tags/four |refs/tags/four
265 | refname is refs/tags/one |refs/tags/one
266 | refname is refs/tags/signed-tag |refs/tags/signed-tag
267 | refname is refs/tags/three |refs/tags/three
268 | refname is refs/tags/two |refs/tags/two
271 test_align_permutations() {
272 while read -r option
274 test_expect_success "align:$option" '
275 git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
276 test_cmp expect actual
278 done
281 test_align_permutations <<-\EOF
282 middle,42
283 42,middle
284 position=middle,42
285 42,position=middle
286 middle,width=42
287 width=42,middle
288 position=middle,width=42
289 width=42,position=middle
292 # Last one wins (silently) when multiple arguments of the same type are given
294 test_align_permutations <<-\EOF
295 32,width=42,middle
296 width=30,42,middle
297 width=42,position=right,middle
298 42,right,position=middle
301 # Individual atoms inside %(align:...) and %(end) must not be quoted.
303 test_expect_success 'alignment with format quote' "
304 cat >expect <<-\EOF &&
305 |' '\''main| A U Thor'\'' '|
306 |' '\''side| A U Thor'\'' '|
307 |' '\''odd/spot| A U Thor'\'' '|
308 |' '\''annotated-tag| '\'' '|
309 |' '\''doubly-annotated-tag| '\'' '|
310 |' '\''doubly-signed-tag| '\'' '|
311 |' '\''four| A U Thor'\'' '|
312 |' '\''one| A U Thor'\'' '|
313 |' '\''signed-tag| '\'' '|
314 |' '\''three| A U Thor'\'' '|
315 |' '\''two| A U Thor'\'' '|
317 git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
318 test_cmp expect actual
321 test_expect_success 'nested alignment with quote formatting' "
322 cat >expect <<-\EOF &&
323 |' main '|
324 |' side '|
325 |' odd/spot '|
326 |' annotated-tag '|
327 |'doubly-annotated-tag '|
328 |'doubly-signed-tag '|
329 |' four '|
330 |' one '|
331 |' signed-tag '|
332 |' three '|
333 |' two '|
335 git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
336 test_cmp expect actual
339 test_expect_success 'check `%(contents:lines=1)`' '
340 cat >expect <<-\EOF &&
341 main |three
342 side |four
343 odd/spot |three
344 annotated-tag |An annotated tag
345 doubly-annotated-tag |Annonated doubly
346 doubly-signed-tag |Signed doubly
347 four |four
348 one |one
349 signed-tag |A signed tag
350 three |three
351 two |two
353 git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
354 test_cmp expect actual
357 test_expect_success 'check `%(contents:lines=0)`' '
358 cat >expect <<-\EOF &&
359 main |
360 side |
361 odd/spot |
362 annotated-tag |
363 doubly-annotated-tag |
364 doubly-signed-tag |
365 four |
366 one |
367 signed-tag |
368 three |
369 two |
371 git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
372 test_cmp expect actual
375 test_expect_success 'check `%(contents:lines=99999)`' '
376 cat >expect <<-\EOF &&
377 main |three
378 side |four
379 odd/spot |three
380 annotated-tag |An annotated tag
381 doubly-annotated-tag |Annonated doubly
382 doubly-signed-tag |Signed doubly
383 four |four
384 one |one
385 signed-tag |A signed tag
386 three |three
387 two |two
389 git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
390 test_cmp expect actual
393 test_expect_success '`%(contents:lines=-1)` should fail' '
394 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
397 test_expect_success 'setup for version sort' '
398 test_commit foo1.3 &&
399 test_commit foo1.6 &&
400 test_commit foo1.10
403 test_expect_success 'version sort' '
404 git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
405 cat >expect <<-\EOF &&
406 foo1.3
407 foo1.6
408 foo1.10
410 test_cmp expect actual
413 test_expect_success 'version sort (shortened)' '
414 git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
415 cat >expect <<-\EOF &&
416 foo1.3
417 foo1.6
418 foo1.10
420 test_cmp expect actual
423 test_expect_success 'reverse version sort' '
424 git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
425 cat >expect <<-\EOF &&
426 foo1.10
427 foo1.6
428 foo1.3
430 test_cmp expect actual
433 test_expect_success 'improper usage of %(if), %(then), %(else) and %(end) atoms' '
434 test_must_fail git for-each-ref --format="%(if)" &&
435 test_must_fail git for-each-ref --format="%(then) %(end)" &&
436 test_must_fail git for-each-ref --format="%(else) %(end)" &&
437 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
438 test_must_fail git for-each-ref --format="%(if) %(then) %(then) %(end)" &&
439 test_must_fail git for-each-ref --format="%(then) %(else) %(end)" &&
440 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
441 test_must_fail git for-each-ref --format="%(if) %(then) %(else)" &&
442 test_must_fail git for-each-ref --format="%(if) %(else) %(then) %(end)" &&
443 test_must_fail git for-each-ref --format="%(if) %(then) %(else) %(else) %(end)" &&
444 test_must_fail git for-each-ref --format="%(if) %(end)"
447 test_expect_success 'check %(if)...%(then)...%(end) atoms' '
448 git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Author: %(authorname)%(end)" >actual &&
449 cat >expect <<-\EOF &&
450 refs/heads/main Author: A U Thor
451 refs/heads/side Author: A U Thor
452 refs/odd/spot Author: A U Thor
453 refs/tags/annotated-tag
454 refs/tags/doubly-annotated-tag
455 refs/tags/doubly-signed-tag
456 refs/tags/foo1.10 Author: A U Thor
457 refs/tags/foo1.3 Author: A U Thor
458 refs/tags/foo1.6 Author: A U Thor
459 refs/tags/four Author: A U Thor
460 refs/tags/one Author: A U Thor
461 refs/tags/signed-tag
462 refs/tags/three Author: A U Thor
463 refs/tags/two Author: A U Thor
465 test_cmp expect actual
468 test_expect_success 'check %(if)...%(then)...%(else)...%(end) atoms' '
469 git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(else)No author%(end): %(refname)" >actual &&
470 cat >expect <<-\EOF &&
471 A U Thor: refs/heads/main
472 A U Thor: refs/heads/side
473 A U Thor: refs/odd/spot
474 No author: refs/tags/annotated-tag
475 No author: refs/tags/doubly-annotated-tag
476 No author: refs/tags/doubly-signed-tag
477 A U Thor: refs/tags/foo1.10
478 A U Thor: refs/tags/foo1.3
479 A U Thor: refs/tags/foo1.6
480 A U Thor: refs/tags/four
481 A U Thor: refs/tags/one
482 No author: refs/tags/signed-tag
483 A U Thor: refs/tags/three
484 A U Thor: refs/tags/two
486 test_cmp expect actual
488 test_expect_success 'ignore spaces in %(if) atom usage' '
489 git for-each-ref --format="%(refname:short): %(if)%(HEAD)%(then)Head ref%(else)Not Head ref%(end)" >actual &&
490 cat >expect <<-\EOF &&
491 main: Head ref
492 side: Not Head ref
493 odd/spot: Not Head ref
494 annotated-tag: Not Head ref
495 doubly-annotated-tag: Not Head ref
496 doubly-signed-tag: Not Head ref
497 foo1.10: Not Head ref
498 foo1.3: Not Head ref
499 foo1.6: Not Head ref
500 four: Not Head ref
501 one: Not Head ref
502 signed-tag: Not Head ref
503 three: Not Head ref
504 two: Not Head ref
506 test_cmp expect actual
509 test_expect_success 'check %(if:equals=<string>)' '
510 git for-each-ref --format="%(if:equals=main)%(refname:short)%(then)Found main%(else)Not main%(end)" refs/heads/ >actual &&
511 cat >expect <<-\EOF &&
512 Found main
513 Not main
515 test_cmp expect actual
518 test_expect_success 'check %(if:notequals=<string>)' '
519 git for-each-ref --format="%(if:notequals=main)%(refname:short)%(then)Not main%(else)Found main%(end)" refs/heads/ >actual &&
520 cat >expect <<-\EOF &&
521 Found main
522 Not main
524 test_cmp expect actual
527 test_expect_success '--merged is compatible with --no-merged' '
528 git for-each-ref --merged HEAD --no-merged HEAD
531 test_expect_success 'validate worktree atom' '
532 cat >expect <<-EOF &&
533 main: $(pwd)
534 main_worktree: $(pwd)/worktree_dir
535 side: not checked out
537 git worktree add -b main_worktree worktree_dir main &&
538 git for-each-ref --format="%(refname:short): %(if)%(worktreepath)%(then)%(worktreepath)%(else)not checked out%(end)" refs/heads/ >actual &&
539 rm -r worktree_dir &&
540 git worktree prune &&
541 test_cmp expect actual
544 test_done