t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / t / t6302-for-each-ref-filter.sh
blob7f44d3c3f22898369d87a3169cca75ab0f827622
1 #!/bin/sh
3 test_description='test for-each-refs usage of ref-filter APIs'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-gpg.sh
9 test_expect_success 'setup some history and refs' '
10 test_commit one &&
11 git branch -M main &&
12 test_commit two &&
13 test_commit three &&
14 git checkout -b side &&
15 test_commit four &&
16 git tag -m "An annotated tag" annotated-tag &&
17 git tag -m "Annonated doubly" doubly-annotated-tag annotated-tag &&
19 # Note that these "signed" tags might not actually be signed.
20 # Tests which care about the distinction should be marked
21 # with the GPG prereq.
22 if test_have_prereq GPG
23 then
24 sign=-s
25 else
26 sign=
27 fi &&
28 git tag $sign -m "A signed tag" signed-tag &&
29 git tag $sign -m "Signed doubly" doubly-signed-tag signed-tag &&
31 git checkout main &&
32 git update-ref refs/odd/spot main
35 test_expect_success '--include-root-refs pattern prints pseudorefs' '
36 cat >expect <<-\EOF &&
37 HEAD
38 ORIG_HEAD
39 refs/heads/main
40 refs/heads/side
41 refs/odd/spot
42 refs/tags/annotated-tag
43 refs/tags/doubly-annotated-tag
44 refs/tags/doubly-signed-tag
45 refs/tags/four
46 refs/tags/one
47 refs/tags/signed-tag
48 refs/tags/three
49 refs/tags/two
50 EOF
51 git update-ref ORIG_HEAD main &&
52 git for-each-ref --format="%(refname)" --include-root-refs >actual &&
53 test_cmp expect actual
56 test_expect_success '--include-root-refs pattern does not print special refs' '
57 test_when_finished "rm -rf repo" &&
58 git init repo &&
60 cd repo &&
61 test_commit initial &&
62 git rev-parse HEAD >.git/MERGE_HEAD &&
63 git for-each-ref --format="%(refname)" --include-root-refs >actual &&
64 cat >expect <<-EOF &&
65 HEAD
66 $(git symbolic-ref HEAD)
67 refs/tags/initial
68 EOF
69 test_cmp expect actual
73 test_expect_success '--include-root-refs with other patterns' '
74 cat >expect <<-\EOF &&
75 HEAD
76 ORIG_HEAD
77 EOF
78 git update-ref ORIG_HEAD main &&
79 git for-each-ref --format="%(refname)" --include-root-refs "*HEAD" >actual &&
80 test_cmp expect actual
83 test_expect_success '--include-root-refs omits dangling symrefs' '
84 test_when_finished "rm -rf repo" &&
85 git init repo &&
87 cd repo &&
88 test_commit initial &&
89 git symbolic-ref DANGLING_HEAD refs/heads/missing &&
90 cat >expect <<-EOF &&
91 HEAD
92 $(git symbolic-ref HEAD)
93 refs/tags/initial
94 EOF
95 git for-each-ref --format="%(refname)" --include-root-refs >actual &&
96 test_cmp expect actual
100 test_expect_success 'filtering with --points-at' '
101 cat >expect <<-\EOF &&
102 refs/heads/main
103 refs/odd/spot
104 refs/tags/three
106 git for-each-ref --format="%(refname)" --points-at=main >actual &&
107 test_cmp expect actual
110 test_expect_success 'check signed tags with --points-at' '
111 sed -e "s/Z$//" >expect <<-\EOF &&
112 refs/heads/side Z
113 refs/tags/annotated-tag four
114 refs/tags/doubly-annotated-tag four
115 refs/tags/doubly-signed-tag four
116 refs/tags/four Z
117 refs/tags/signed-tag four
119 git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
120 test_cmp expect actual
123 test_expect_success 'filtering with --merged' '
124 cat >expect <<-\EOF &&
125 refs/heads/main
126 refs/odd/spot
127 refs/tags/one
128 refs/tags/three
129 refs/tags/two
131 git for-each-ref --format="%(refname)" --merged=main >actual &&
132 test_cmp expect actual
135 test_expect_success 'filtering with --no-merged' '
136 cat >expect <<-\EOF &&
137 refs/heads/side
138 refs/tags/annotated-tag
139 refs/tags/doubly-annotated-tag
140 refs/tags/doubly-signed-tag
141 refs/tags/four
142 refs/tags/signed-tag
144 git for-each-ref --format="%(refname)" --no-merged=main >actual &&
145 test_cmp expect actual
148 test_expect_success 'filtering with --contains' '
149 cat >expect <<-\EOF &&
150 refs/heads/main
151 refs/heads/side
152 refs/odd/spot
153 refs/tags/annotated-tag
154 refs/tags/doubly-annotated-tag
155 refs/tags/doubly-signed-tag
156 refs/tags/four
157 refs/tags/signed-tag
158 refs/tags/three
159 refs/tags/two
161 git for-each-ref --format="%(refname)" --contains=two >actual &&
162 test_cmp expect actual
165 test_expect_success 'filtering with --no-contains' '
166 cat >expect <<-\EOF &&
167 refs/tags/one
169 git for-each-ref --format="%(refname)" --no-contains=two >actual &&
170 test_cmp expect actual
173 test_expect_success 'filtering with --contains and --no-contains' '
174 cat >expect <<-\EOF &&
175 refs/tags/two
177 git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
178 test_cmp expect actual
181 test_expect_success '%(color) must fail' '
182 test_must_fail git for-each-ref --format="%(color)%(refname)"
185 test_expect_success '%(color:#aa22ac) must succeed' '
186 test_when_finished rm -rf test &&
187 git init test &&
189 cd test &&
190 test_commit initial &&
191 git branch -M main &&
192 cat >expect <<-\EOF &&
193 refs/heads/main
194 refs/tags/initial
196 git remote add origin nowhere &&
197 git config branch.main.remote origin &&
198 git config branch.main.merge refs/heads/main &&
199 git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
200 test_cmp expect actual
204 test_expect_success 'left alignment is default' '
205 cat >expect <<-\EOF &&
206 refname is refs/heads/main |refs/heads/main
207 refname is refs/heads/side |refs/heads/side
208 refname is refs/odd/spot |refs/odd/spot
209 refname is refs/tags/annotated-tag|refs/tags/annotated-tag
210 refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
211 refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
212 refname is refs/tags/four |refs/tags/four
213 refname is refs/tags/one |refs/tags/one
214 refname is refs/tags/signed-tag|refs/tags/signed-tag
215 refname is refs/tags/three |refs/tags/three
216 refname is refs/tags/two |refs/tags/two
218 git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
219 test_cmp expect actual
222 test_expect_success 'middle alignment' '
223 cat >expect <<-\EOF &&
224 | refname is refs/heads/main |refs/heads/main
225 | refname is refs/heads/side |refs/heads/side
226 | refname is refs/odd/spot |refs/odd/spot
227 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
228 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
229 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
230 | refname is refs/tags/four |refs/tags/four
231 | refname is refs/tags/one |refs/tags/one
232 |refname is refs/tags/signed-tag|refs/tags/signed-tag
233 | refname is refs/tags/three |refs/tags/three
234 | refname is refs/tags/two |refs/tags/two
236 git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
237 test_cmp expect actual
240 test_expect_success 'right alignment' '
241 cat >expect <<-\EOF &&
242 | refname is refs/heads/main|refs/heads/main
243 | refname is refs/heads/side|refs/heads/side
244 | refname is refs/odd/spot|refs/odd/spot
245 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
246 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
247 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
248 | refname is refs/tags/four|refs/tags/four
249 | refname is refs/tags/one|refs/tags/one
250 |refname is refs/tags/signed-tag|refs/tags/signed-tag
251 | refname is refs/tags/three|refs/tags/three
252 | refname is refs/tags/two|refs/tags/two
254 git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
255 test_cmp expect actual
258 cat >expect <<-\EOF
259 | refname is refs/heads/main |refs/heads/main
260 | refname is refs/heads/side |refs/heads/side
261 | refname is refs/odd/spot |refs/odd/spot
262 | refname is refs/tags/annotated-tag |refs/tags/annotated-tag
263 |refname is refs/tags/doubly-annotated-tag |refs/tags/doubly-annotated-tag
264 | refname is refs/tags/doubly-signed-tag |refs/tags/doubly-signed-tag
265 | refname is refs/tags/four |refs/tags/four
266 | refname is refs/tags/one |refs/tags/one
267 | refname is refs/tags/signed-tag |refs/tags/signed-tag
268 | refname is refs/tags/three |refs/tags/three
269 | refname is refs/tags/two |refs/tags/two
272 test_align_permutations() {
273 while read -r option
275 test_expect_success "align:$option" '
276 git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
277 test_cmp expect actual
279 done
282 test_align_permutations <<-\EOF
283 middle,42
284 42,middle
285 position=middle,42
286 42,position=middle
287 middle,width=42
288 width=42,middle
289 position=middle,width=42
290 width=42,position=middle
293 # Last one wins (silently) when multiple arguments of the same type are given
295 test_align_permutations <<-\EOF
296 32,width=42,middle
297 width=30,42,middle
298 width=42,position=right,middle
299 42,right,position=middle
302 # Individual atoms inside %(align:...) and %(end) must not be quoted.
304 test_expect_success 'alignment with format quote' "
305 cat >expect <<-\EOF &&
306 |' '\''main| A U Thor'\'' '|
307 |' '\''side| A U Thor'\'' '|
308 |' '\''odd/spot| A U Thor'\'' '|
309 |' '\''annotated-tag| '\'' '|
310 |' '\''doubly-annotated-tag| '\'' '|
311 |' '\''doubly-signed-tag| '\'' '|
312 |' '\''four| A U Thor'\'' '|
313 |' '\''one| A U Thor'\'' '|
314 |' '\''signed-tag| '\'' '|
315 |' '\''three| A U Thor'\'' '|
316 |' '\''two| A U Thor'\'' '|
318 git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
319 test_cmp expect actual
322 test_expect_success 'nested alignment with quote formatting' "
323 cat >expect <<-\EOF &&
324 |' main '|
325 |' side '|
326 |' odd/spot '|
327 |' annotated-tag '|
328 |'doubly-annotated-tag '|
329 |'doubly-signed-tag '|
330 |' four '|
331 |' one '|
332 |' signed-tag '|
333 |' three '|
334 |' two '|
336 git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
337 test_cmp expect actual
340 test_expect_success 'check `%(contents:lines=1)`' '
341 cat >expect <<-\EOF &&
342 main |three
343 side |four
344 odd/spot |three
345 annotated-tag |An annotated tag
346 doubly-annotated-tag |Annonated doubly
347 doubly-signed-tag |Signed doubly
348 four |four
349 one |one
350 signed-tag |A signed tag
351 three |three
352 two |two
354 git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
355 test_cmp expect actual
358 test_expect_success 'check `%(contents:lines=0)`' '
359 cat >expect <<-\EOF &&
360 main |
361 side |
362 odd/spot |
363 annotated-tag |
364 doubly-annotated-tag |
365 doubly-signed-tag |
366 four |
367 one |
368 signed-tag |
369 three |
370 two |
372 git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
373 test_cmp expect actual
376 test_expect_success 'check `%(contents:lines=99999)`' '
377 cat >expect <<-\EOF &&
378 main |three
379 side |four
380 odd/spot |three
381 annotated-tag |An annotated tag
382 doubly-annotated-tag |Annonated doubly
383 doubly-signed-tag |Signed doubly
384 four |four
385 one |one
386 signed-tag |A signed tag
387 three |three
388 two |two
390 git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
391 test_cmp expect actual
394 test_expect_success '`%(contents:lines=-1)` should fail' '
395 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
398 test_expect_success 'setup for version sort' '
399 test_commit foo1.3 &&
400 test_commit foo1.6 &&
401 test_commit foo1.10
404 test_expect_success 'version sort' '
405 git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
406 cat >expect <<-\EOF &&
407 foo1.3
408 foo1.6
409 foo1.10
411 test_cmp expect actual
414 test_expect_success 'version sort (shortened)' '
415 git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
416 cat >expect <<-\EOF &&
417 foo1.3
418 foo1.6
419 foo1.10
421 test_cmp expect actual
424 test_expect_success 'reverse version sort' '
425 git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
426 cat >expect <<-\EOF &&
427 foo1.10
428 foo1.6
429 foo1.3
431 test_cmp expect actual
434 test_expect_success 'improper usage of %(if), %(then), %(else) and %(end) atoms' '
435 test_must_fail git for-each-ref --format="%(if)" &&
436 test_must_fail git for-each-ref --format="%(then) %(end)" &&
437 test_must_fail git for-each-ref --format="%(else) %(end)" &&
438 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
439 test_must_fail git for-each-ref --format="%(if) %(then) %(then) %(end)" &&
440 test_must_fail git for-each-ref --format="%(then) %(else) %(end)" &&
441 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
442 test_must_fail git for-each-ref --format="%(if) %(then) %(else)" &&
443 test_must_fail git for-each-ref --format="%(if) %(else) %(then) %(end)" &&
444 test_must_fail git for-each-ref --format="%(if) %(then) %(else) %(else) %(end)" &&
445 test_must_fail git for-each-ref --format="%(if) %(end)"
448 test_expect_success 'check %(if)...%(then)...%(end) atoms' '
449 git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Author: %(authorname)%(end)" >actual &&
450 cat >expect <<-\EOF &&
451 refs/heads/main Author: A U Thor
452 refs/heads/side Author: A U Thor
453 refs/odd/spot Author: A U Thor
454 refs/tags/annotated-tag
455 refs/tags/doubly-annotated-tag
456 refs/tags/doubly-signed-tag
457 refs/tags/foo1.10 Author: A U Thor
458 refs/tags/foo1.3 Author: A U Thor
459 refs/tags/foo1.6 Author: A U Thor
460 refs/tags/four Author: A U Thor
461 refs/tags/one Author: A U Thor
462 refs/tags/signed-tag
463 refs/tags/three Author: A U Thor
464 refs/tags/two Author: A U Thor
466 test_cmp expect actual
469 test_expect_success 'check %(if)...%(then)...%(else)...%(end) atoms' '
470 git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(else)No author%(end): %(refname)" >actual &&
471 cat >expect <<-\EOF &&
472 A U Thor: refs/heads/main
473 A U Thor: refs/heads/side
474 A U Thor: refs/odd/spot
475 No author: refs/tags/annotated-tag
476 No author: refs/tags/doubly-annotated-tag
477 No author: refs/tags/doubly-signed-tag
478 A U Thor: refs/tags/foo1.10
479 A U Thor: refs/tags/foo1.3
480 A U Thor: refs/tags/foo1.6
481 A U Thor: refs/tags/four
482 A U Thor: refs/tags/one
483 No author: refs/tags/signed-tag
484 A U Thor: refs/tags/three
485 A U Thor: refs/tags/two
487 test_cmp expect actual
489 test_expect_success 'ignore spaces in %(if) atom usage' '
490 git for-each-ref --format="%(refname:short): %(if)%(HEAD)%(then)Head ref%(else)Not Head ref%(end)" >actual &&
491 cat >expect <<-\EOF &&
492 main: Head ref
493 side: Not Head ref
494 odd/spot: Not Head ref
495 annotated-tag: Not Head ref
496 doubly-annotated-tag: Not Head ref
497 doubly-signed-tag: Not Head ref
498 foo1.10: Not Head ref
499 foo1.3: Not Head ref
500 foo1.6: Not Head ref
501 four: Not Head ref
502 one: Not Head ref
503 signed-tag: Not Head ref
504 three: Not Head ref
505 two: Not Head ref
507 test_cmp expect actual
510 test_expect_success 'check %(if:equals=<string>)' '
511 git for-each-ref --format="%(if:equals=main)%(refname:short)%(then)Found main%(else)Not main%(end)" refs/heads/ >actual &&
512 cat >expect <<-\EOF &&
513 Found main
514 Not main
516 test_cmp expect actual
519 test_expect_success 'check %(if:notequals=<string>)' '
520 git for-each-ref --format="%(if:notequals=main)%(refname:short)%(then)Not main%(else)Found main%(end)" refs/heads/ >actual &&
521 cat >expect <<-\EOF &&
522 Found main
523 Not main
525 test_cmp expect actual
528 test_expect_success '--merged is compatible with --no-merged' '
529 git for-each-ref --merged HEAD --no-merged HEAD
532 test_expect_success 'validate worktree atom' '
533 cat >expect <<-EOF &&
534 main: $(pwd)
535 main_worktree: $(pwd)/worktree_dir
536 side: not checked out
538 git worktree add -b main_worktree worktree_dir main &&
539 git for-each-ref --format="%(refname:short): %(if)%(worktreepath)%(then)%(worktreepath)%(else)not checked out%(end)" refs/heads/ >actual &&
540 rm -r worktree_dir &&
541 git worktree prune &&
542 test_cmp expect actual
545 test_done