3 test_description
='test for-each-refs usage of ref-filter APIs'
6 .
"$TEST_DIRECTORY"/lib-gpg.sh
8 test_expect_success
'setup some history and refs' '
13 git checkout -b side &&
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
27 git tag $sign -m "A signed tag" signed-tag &&
28 git tag $sign -m "Signed doubly" doubly-signed-tag signed-tag &&
31 git update-ref refs/odd/spot main
34 test_expect_success
'--include-root-refs pattern prints pseudorefs' '
35 cat >expect <<-\EOF &&
41 refs/tags/annotated-tag
42 refs/tags/doubly-annotated-tag
43 refs/tags/doubly-signed-tag
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 &&
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 &&
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 &&
78 refs/tags/annotated-tag four
79 refs/tags/doubly-annotated-tag four
80 refs/tags/doubly-signed-tag four
82 refs/tags/signed-tag four
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 &&
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 &&
103 refs/tags/annotated-tag
104 refs/tags/doubly-annotated-tag
105 refs/tags/doubly-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 &&
118 refs/tags/annotated-tag
119 refs/tags/doubly-annotated-tag
120 refs/tags/doubly-signed-tag
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 &&
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 &&
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 &&
155 test_commit initial &&
156 git branch -M main &&
157 cat >expect <<-\EOF &&
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
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() {
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
247 test_align_permutations <<-\
EOF
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
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 &&
293 |'doubly-annotated-tag '|
294 |'doubly-signed-tag '|
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 &&
310 annotated-tag |An annotated tag
311 doubly-annotated-tag |Annonated doubly
312 doubly-signed-tag |Signed doubly
315 signed-tag |A signed tag
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 &&
329 doubly-annotated-tag |
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 &&
346 annotated-tag |An annotated tag
347 doubly-annotated-tag |Annonated doubly
348 doubly-signed-tag |Signed doubly
351 signed-tag |A signed tag
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 &&
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 &&
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 &&
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 &&
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
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 &&
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
468 signed-tag: 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 &&
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 &&
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 &&
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