The second batch
[git.git] / t / t6302-for-each-ref-filter.sh
blob948f1bb5f44e66b80004cce1b1ac2b9ee3bd4ac9
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 with other patterns' '
56 cat >expect <<-\EOF &&
57 HEAD
58 ORIG_HEAD
59 EOF
60 git update-ref ORIG_HEAD main &&
61 git for-each-ref --format="%(refname)" --include-root-refs "*HEAD" >actual &&
62 test_cmp expect actual
65 test_expect_success 'filtering with --points-at' '
66 cat >expect <<-\EOF &&
67 refs/heads/main
68 refs/odd/spot
69 refs/tags/three
70 EOF
71 git for-each-ref --format="%(refname)" --points-at=main >actual &&
72 test_cmp expect actual
75 test_expect_success 'check signed tags with --points-at' '
76 sed -e "s/Z$//" >expect <<-\EOF &&
77 refs/heads/side Z
78 refs/tags/annotated-tag four
79 refs/tags/doubly-annotated-tag four
80 refs/tags/doubly-signed-tag four
81 refs/tags/four Z
82 refs/tags/signed-tag four
83 EOF
84 git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
85 test_cmp expect actual
88 test_expect_success 'filtering with --merged' '
89 cat >expect <<-\EOF &&
90 refs/heads/main
91 refs/odd/spot
92 refs/tags/one
93 refs/tags/three
94 refs/tags/two
95 EOF
96 git for-each-ref --format="%(refname)" --merged=main >actual &&
97 test_cmp expect actual
100 test_expect_success 'filtering with --no-merged' '
101 cat >expect <<-\EOF &&
102 refs/heads/side
103 refs/tags/annotated-tag
104 refs/tags/doubly-annotated-tag
105 refs/tags/doubly-signed-tag
106 refs/tags/four
107 refs/tags/signed-tag
109 git for-each-ref --format="%(refname)" --no-merged=main >actual &&
110 test_cmp expect actual
113 test_expect_success 'filtering with --contains' '
114 cat >expect <<-\EOF &&
115 refs/heads/main
116 refs/heads/side
117 refs/odd/spot
118 refs/tags/annotated-tag
119 refs/tags/doubly-annotated-tag
120 refs/tags/doubly-signed-tag
121 refs/tags/four
122 refs/tags/signed-tag
123 refs/tags/three
124 refs/tags/two
126 git for-each-ref --format="%(refname)" --contains=two >actual &&
127 test_cmp expect actual
130 test_expect_success 'filtering with --no-contains' '
131 cat >expect <<-\EOF &&
132 refs/tags/one
134 git for-each-ref --format="%(refname)" --no-contains=two >actual &&
135 test_cmp expect actual
138 test_expect_success 'filtering with --contains and --no-contains' '
139 cat >expect <<-\EOF &&
140 refs/tags/two
142 git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
143 test_cmp expect actual
146 test_expect_success '%(color) must fail' '
147 test_must_fail git for-each-ref --format="%(color)%(refname)"
150 test_expect_success '%(color:#aa22ac) must succeed' '
151 test_when_finished rm -rf test &&
152 git init test &&
154 cd test &&
155 test_commit initial &&
156 git branch -M main &&
157 cat >expect <<-\EOF &&
158 refs/heads/main
159 refs/tags/initial
161 git remote add origin nowhere &&
162 git config branch.main.remote origin &&
163 git config branch.main.merge refs/heads/main &&
164 git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
165 test_cmp expect actual
169 test_expect_success 'left alignment is default' '
170 cat >expect <<-\EOF &&
171 refname is refs/heads/main |refs/heads/main
172 refname is refs/heads/side |refs/heads/side
173 refname is refs/odd/spot |refs/odd/spot
174 refname is refs/tags/annotated-tag|refs/tags/annotated-tag
175 refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
176 refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
177 refname is refs/tags/four |refs/tags/four
178 refname is refs/tags/one |refs/tags/one
179 refname is refs/tags/signed-tag|refs/tags/signed-tag
180 refname is refs/tags/three |refs/tags/three
181 refname is refs/tags/two |refs/tags/two
183 git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
184 test_cmp expect actual
187 test_expect_success 'middle alignment' '
188 cat >expect <<-\EOF &&
189 | refname is refs/heads/main |refs/heads/main
190 | refname is refs/heads/side |refs/heads/side
191 | refname is refs/odd/spot |refs/odd/spot
192 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
193 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
194 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
195 | refname is refs/tags/four |refs/tags/four
196 | refname is refs/tags/one |refs/tags/one
197 |refname is refs/tags/signed-tag|refs/tags/signed-tag
198 | refname is refs/tags/three |refs/tags/three
199 | refname is refs/tags/two |refs/tags/two
201 git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
202 test_cmp expect actual
205 test_expect_success 'right alignment' '
206 cat >expect <<-\EOF &&
207 | refname is refs/heads/main|refs/heads/main
208 | refname is refs/heads/side|refs/heads/side
209 | refname is refs/odd/spot|refs/odd/spot
210 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
211 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
212 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
213 | refname is refs/tags/four|refs/tags/four
214 | refname is refs/tags/one|refs/tags/one
215 |refname is refs/tags/signed-tag|refs/tags/signed-tag
216 | refname is refs/tags/three|refs/tags/three
217 | refname is refs/tags/two|refs/tags/two
219 git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
220 test_cmp expect actual
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
237 test_align_permutations() {
238 while read -r option
240 test_expect_success "align:$option" '
241 git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
242 test_cmp expect actual
244 done
247 test_align_permutations <<-\EOF
248 middle,42
249 42,middle
250 position=middle,42
251 42,position=middle
252 middle,width=42
253 width=42,middle
254 position=middle,width=42
255 width=42,position=middle
258 # Last one wins (silently) when multiple arguments of the same type are given
260 test_align_permutations <<-\EOF
261 32,width=42,middle
262 width=30,42,middle
263 width=42,position=right,middle
264 42,right,position=middle
267 # Individual atoms inside %(align:...) and %(end) must not be quoted.
269 test_expect_success 'alignment with format quote' "
270 cat >expect <<-\EOF &&
271 |' '\''main| A U Thor'\'' '|
272 |' '\''side| A U Thor'\'' '|
273 |' '\''odd/spot| A U Thor'\'' '|
274 |' '\''annotated-tag| '\'' '|
275 |' '\''doubly-annotated-tag| '\'' '|
276 |' '\''doubly-signed-tag| '\'' '|
277 |' '\''four| A U Thor'\'' '|
278 |' '\''one| A U Thor'\'' '|
279 |' '\''signed-tag| '\'' '|
280 |' '\''three| A U Thor'\'' '|
281 |' '\''two| A U Thor'\'' '|
283 git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
284 test_cmp expect actual
287 test_expect_success 'nested alignment with quote formatting' "
288 cat >expect <<-\EOF &&
289 |' main '|
290 |' side '|
291 |' odd/spot '|
292 |' annotated-tag '|
293 |'doubly-annotated-tag '|
294 |'doubly-signed-tag '|
295 |' four '|
296 |' one '|
297 |' signed-tag '|
298 |' three '|
299 |' two '|
301 git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
302 test_cmp expect actual
305 test_expect_success 'check `%(contents:lines=1)`' '
306 cat >expect <<-\EOF &&
307 main |three
308 side |four
309 odd/spot |three
310 annotated-tag |An annotated tag
311 doubly-annotated-tag |Annonated doubly
312 doubly-signed-tag |Signed doubly
313 four |four
314 one |one
315 signed-tag |A signed tag
316 three |three
317 two |two
319 git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
320 test_cmp expect actual
323 test_expect_success 'check `%(contents:lines=0)`' '
324 cat >expect <<-\EOF &&
325 main |
326 side |
327 odd/spot |
328 annotated-tag |
329 doubly-annotated-tag |
330 doubly-signed-tag |
331 four |
332 one |
333 signed-tag |
334 three |
335 two |
337 git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
338 test_cmp expect actual
341 test_expect_success 'check `%(contents:lines=99999)`' '
342 cat >expect <<-\EOF &&
343 main |three
344 side |four
345 odd/spot |three
346 annotated-tag |An annotated tag
347 doubly-annotated-tag |Annonated doubly
348 doubly-signed-tag |Signed doubly
349 four |four
350 one |one
351 signed-tag |A signed tag
352 three |three
353 two |two
355 git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
356 test_cmp expect actual
359 test_expect_success '`%(contents:lines=-1)` should fail' '
360 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
363 test_expect_success 'setup for version sort' '
364 test_commit foo1.3 &&
365 test_commit foo1.6 &&
366 test_commit foo1.10
369 test_expect_success 'version sort' '
370 git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
371 cat >expect <<-\EOF &&
372 foo1.3
373 foo1.6
374 foo1.10
376 test_cmp expect actual
379 test_expect_success 'version sort (shortened)' '
380 git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
381 cat >expect <<-\EOF &&
382 foo1.3
383 foo1.6
384 foo1.10
386 test_cmp expect actual
389 test_expect_success 'reverse version sort' '
390 git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
391 cat >expect <<-\EOF &&
392 foo1.10
393 foo1.6
394 foo1.3
396 test_cmp expect actual
399 test_expect_success 'improper usage of %(if), %(then), %(else) and %(end) atoms' '
400 test_must_fail git for-each-ref --format="%(if)" &&
401 test_must_fail git for-each-ref --format="%(then) %(end)" &&
402 test_must_fail git for-each-ref --format="%(else) %(end)" &&
403 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
404 test_must_fail git for-each-ref --format="%(if) %(then) %(then) %(end)" &&
405 test_must_fail git for-each-ref --format="%(then) %(else) %(end)" &&
406 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
407 test_must_fail git for-each-ref --format="%(if) %(then) %(else)" &&
408 test_must_fail git for-each-ref --format="%(if) %(else) %(then) %(end)" &&
409 test_must_fail git for-each-ref --format="%(if) %(then) %(else) %(else) %(end)" &&
410 test_must_fail git for-each-ref --format="%(if) %(end)"
413 test_expect_success 'check %(if)...%(then)...%(end) atoms' '
414 git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Author: %(authorname)%(end)" >actual &&
415 cat >expect <<-\EOF &&
416 refs/heads/main Author: A U Thor
417 refs/heads/side Author: A U Thor
418 refs/odd/spot Author: A U Thor
419 refs/tags/annotated-tag
420 refs/tags/doubly-annotated-tag
421 refs/tags/doubly-signed-tag
422 refs/tags/foo1.10 Author: A U Thor
423 refs/tags/foo1.3 Author: A U Thor
424 refs/tags/foo1.6 Author: A U Thor
425 refs/tags/four Author: A U Thor
426 refs/tags/one Author: A U Thor
427 refs/tags/signed-tag
428 refs/tags/three Author: A U Thor
429 refs/tags/two Author: A U Thor
431 test_cmp expect actual
434 test_expect_success 'check %(if)...%(then)...%(else)...%(end) atoms' '
435 git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(else)No author%(end): %(refname)" >actual &&
436 cat >expect <<-\EOF &&
437 A U Thor: refs/heads/main
438 A U Thor: refs/heads/side
439 A U Thor: refs/odd/spot
440 No author: refs/tags/annotated-tag
441 No author: refs/tags/doubly-annotated-tag
442 No author: refs/tags/doubly-signed-tag
443 A U Thor: refs/tags/foo1.10
444 A U Thor: refs/tags/foo1.3
445 A U Thor: refs/tags/foo1.6
446 A U Thor: refs/tags/four
447 A U Thor: refs/tags/one
448 No author: refs/tags/signed-tag
449 A U Thor: refs/tags/three
450 A U Thor: refs/tags/two
452 test_cmp expect actual
454 test_expect_success 'ignore spaces in %(if) atom usage' '
455 git for-each-ref --format="%(refname:short): %(if)%(HEAD)%(then)Head ref%(else)Not Head ref%(end)" >actual &&
456 cat >expect <<-\EOF &&
457 main: Head ref
458 side: Not Head ref
459 odd/spot: Not Head ref
460 annotated-tag: Not Head ref
461 doubly-annotated-tag: Not Head ref
462 doubly-signed-tag: Not Head ref
463 foo1.10: Not Head ref
464 foo1.3: Not Head ref
465 foo1.6: Not Head ref
466 four: Not Head ref
467 one: Not Head ref
468 signed-tag: Not Head ref
469 three: Not Head ref
470 two: Not Head ref
472 test_cmp expect actual
475 test_expect_success 'check %(if:equals=<string>)' '
476 git for-each-ref --format="%(if:equals=main)%(refname:short)%(then)Found main%(else)Not main%(end)" refs/heads/ >actual &&
477 cat >expect <<-\EOF &&
478 Found main
479 Not main
481 test_cmp expect actual
484 test_expect_success 'check %(if:notequals=<string>)' '
485 git for-each-ref --format="%(if:notequals=main)%(refname:short)%(then)Not main%(else)Found main%(end)" refs/heads/ >actual &&
486 cat >expect <<-\EOF &&
487 Found main
488 Not main
490 test_cmp expect actual
493 test_expect_success '--merged is compatible with --no-merged' '
494 git for-each-ref --merged HEAD --no-merged HEAD
497 test_expect_success 'validate worktree atom' '
498 cat >expect <<-EOF &&
499 main: $(pwd)
500 main_worktree: $(pwd)/worktree_dir
501 side: not checked out
503 git worktree add -b main_worktree worktree_dir main &&
504 git for-each-ref --format="%(refname:short): %(if)%(worktreepath)%(then)%(worktreepath)%(else)not checked out%(end)" refs/heads/ >actual &&
505 rm -r worktree_dir &&
506 git worktree prune &&
507 test_cmp expect actual
510 test_done