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
'filtering with --points-at' '
35 cat >expect <<-\EOF &&
40 git for-each-ref --format="%(refname)" --points-at=main >actual &&
41 test_cmp expect actual
44 test_expect_success
'check signed tags with --points-at' '
45 sed -e "s/Z$//" >expect <<-\EOF &&
47 refs/tags/annotated-tag four
48 refs/tags/doubly-annotated-tag four
49 refs/tags/doubly-signed-tag four
51 refs/tags/signed-tag four
53 git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
54 test_cmp expect actual
57 test_expect_success
'filtering with --merged' '
58 cat >expect <<-\EOF &&
65 git for-each-ref --format="%(refname)" --merged=main >actual &&
66 test_cmp expect actual
69 test_expect_success
'filtering with --no-merged' '
70 cat >expect <<-\EOF &&
72 refs/tags/annotated-tag
73 refs/tags/doubly-annotated-tag
74 refs/tags/doubly-signed-tag
78 git for-each-ref --format="%(refname)" --no-merged=main >actual &&
79 test_cmp expect actual
82 test_expect_success
'filtering with --contains' '
83 cat >expect <<-\EOF &&
87 refs/tags/annotated-tag
88 refs/tags/doubly-annotated-tag
89 refs/tags/doubly-signed-tag
95 git for-each-ref --format="%(refname)" --contains=two >actual &&
96 test_cmp expect actual
99 test_expect_success
'filtering with --no-contains' '
100 cat >expect <<-\EOF &&
103 git for-each-ref --format="%(refname)" --no-contains=two >actual &&
104 test_cmp expect actual
107 test_expect_success
'filtering with --contains and --no-contains' '
108 cat >expect <<-\EOF &&
111 git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
112 test_cmp expect actual
115 test_expect_success
'%(color) must fail' '
116 test_must_fail git for-each-ref --format="%(color)%(refname)"
119 test_expect_success
'%(color:#aa22ac) must succeed' '
120 test_when_finished rm -rf test &&
124 test_commit initial &&
125 git branch -M main &&
126 cat >expect <<-\EOF &&
130 git remote add origin nowhere &&
131 git config branch.main.remote origin &&
132 git config branch.main.merge refs/heads/main &&
133 git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
134 test_cmp expect actual
138 test_expect_success
'left alignment is default' '
139 cat >expect <<-\EOF &&
140 refname is refs/heads/main |refs/heads/main
141 refname is refs/heads/side |refs/heads/side
142 refname is refs/odd/spot |refs/odd/spot
143 refname is refs/tags/annotated-tag|refs/tags/annotated-tag
144 refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
145 refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
146 refname is refs/tags/four |refs/tags/four
147 refname is refs/tags/one |refs/tags/one
148 refname is refs/tags/signed-tag|refs/tags/signed-tag
149 refname is refs/tags/three |refs/tags/three
150 refname is refs/tags/two |refs/tags/two
152 git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
153 test_cmp expect actual
156 test_expect_success
'middle alignment' '
157 cat >expect <<-\EOF &&
158 | refname is refs/heads/main |refs/heads/main
159 | refname is refs/heads/side |refs/heads/side
160 | refname is refs/odd/spot |refs/odd/spot
161 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
162 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
163 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
164 | refname is refs/tags/four |refs/tags/four
165 | refname is refs/tags/one |refs/tags/one
166 |refname is refs/tags/signed-tag|refs/tags/signed-tag
167 | refname is refs/tags/three |refs/tags/three
168 | refname is refs/tags/two |refs/tags/two
170 git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
171 test_cmp expect actual
174 test_expect_success
'right alignment' '
175 cat >expect <<-\EOF &&
176 | refname is refs/heads/main|refs/heads/main
177 | refname is refs/heads/side|refs/heads/side
178 | refname is refs/odd/spot|refs/odd/spot
179 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
180 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
181 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
182 | refname is refs/tags/four|refs/tags/four
183 | refname is refs/tags/one|refs/tags/one
184 |refname is refs/tags/signed-tag|refs/tags/signed-tag
185 | refname is refs/tags/three|refs/tags/three
186 | refname is refs/tags/two|refs/tags/two
188 git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
189 test_cmp expect actual
193 | refname is refs/heads/main |refs/heads/main
194 | refname is refs/heads/side |refs/heads/side
195 | refname is refs/odd/spot |refs/odd/spot
196 | refname is refs/tags/annotated-tag |refs/tags/annotated-tag
197 |refname is refs/tags/doubly-annotated-tag |refs/tags/doubly-annotated-tag
198 | refname is refs/tags/doubly-signed-tag |refs/tags/doubly-signed-tag
199 | refname is refs/tags/four |refs/tags/four
200 | refname is refs/tags/one |refs/tags/one
201 | refname is refs/tags/signed-tag |refs/tags/signed-tag
202 | refname is refs/tags/three |refs/tags/three
203 | refname is refs/tags/two |refs/tags/two
206 test_align_permutations() {
209 test_expect_success "align:$option" '
210 git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
211 test_cmp expect actual
216 test_align_permutations <<-\
EOF
223 position=middle,width=42
224 width=42,position=middle
227 # Last one wins (silently) when multiple arguments of the same type are given
229 test_align_permutations <<-\EOF
232 width=42,position=right,middle
233 42,right,position=middle
236 # Individual atoms inside %(align:...) and %(end) must not be quoted.
238 test_expect_success 'alignment with format quote' "
239 cat >expect <<-\EOF &&
240 |' '\''main| A U Thor'\'' '|
241 |' '\''side| A U Thor'\'' '|
242 |' '\''odd/spot| A U Thor'\'' '|
243 |' '\''annotated-tag| '\'' '|
244 |' '\''doubly-annotated-tag| '\'' '|
245 |' '\''doubly-signed-tag| '\'' '|
246 |' '\''four| A U Thor'\'' '|
247 |' '\''one| A U Thor'\'' '|
248 |' '\''signed-tag| '\'' '|
249 |' '\''three| A U Thor'\'' '|
250 |' '\''two| A U Thor'\'' '|
252 git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
253 test_cmp expect actual
256 test_expect_success 'nested alignment with quote formatting' "
257 cat >expect <<-\EOF &&
262 |'doubly-annotated-tag '|
263 |'doubly-signed-tag '|
270 git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
271 test_cmp expect actual
274 test_expect_success 'check `%(contents:lines=1)`' '
275 cat >expect <<-\EOF &&
279 annotated-tag |An annotated tag
280 doubly-annotated-tag |Annonated doubly
281 doubly-signed-tag |Signed doubly
284 signed-tag |A signed tag
288 git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
289 test_cmp expect actual
292 test_expect_success 'check `%(contents:lines=0)`' '
293 cat >expect <<-\EOF &&
298 doubly-annotated-tag |
306 git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
307 test_cmp expect actual
310 test_expect_success 'check `%(contents:lines=99999)`' '
311 cat >expect <<-\EOF &&
315 annotated-tag |An annotated tag
316 doubly-annotated-tag |Annonated doubly
317 doubly-signed-tag |Signed doubly
320 signed-tag |A signed tag
324 git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
325 test_cmp expect actual
328 test_expect_success '`%(contents:lines=-1)` should fail' '
329 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
332 test_expect_success 'setup for version sort' '
333 test_commit foo1.3 &&
334 test_commit foo1.6 &&
338 test_expect_success 'version sort' '
339 git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
340 cat >expect <<-\EOF &&
345 test_cmp expect actual
348 test_expect_success 'version sort (shortened)' '
349 git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
350 cat >expect <<-\EOF &&
355 test_cmp expect actual
358 test_expect_success 'reverse version sort' '
359 git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
360 cat >expect <<-\EOF &&
365 test_cmp expect actual
368 test_expect_success 'improper usage of %(if), %(then), %(else) and %(end) atoms' '
369 test_must_fail git for-each-ref --format="%(if)" &&
370 test_must_fail git for-each-ref --format="%(then) %(end)" &&
371 test_must_fail git for-each-ref --format="%(else) %(end)" &&
372 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
373 test_must_fail git for-each-ref --format="%(if) %(then) %(then) %(end)" &&
374 test_must_fail git for-each-ref --format="%(then) %(else) %(end)" &&
375 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
376 test_must_fail git for-each-ref --format="%(if) %(then) %(else)" &&
377 test_must_fail git for-each-ref --format="%(if) %(else) %(then) %(end)" &&
378 test_must_fail git for-each-ref --format="%(if) %(then) %(else) %(else) %(end)" &&
379 test_must_fail git for-each-ref --format="%(if) %(end)"
382 test_expect_success 'check %(if)...%(then)...%(end) atoms' '
383 git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Author: %(authorname)%(end)" >actual &&
384 cat >expect <<-\EOF &&
385 refs/heads/main Author: A U Thor
386 refs/heads/side Author: A U Thor
387 refs/odd/spot Author: A U Thor
388 refs/tags/annotated-tag
389 refs/tags/doubly-annotated-tag
390 refs/tags/doubly-signed-tag
391 refs/tags/foo1.10 Author: A U Thor
392 refs/tags/foo1.3 Author: A U Thor
393 refs/tags/foo1.6 Author: A U Thor
394 refs/tags/four Author: A U Thor
395 refs/tags/one Author: A U Thor
397 refs/tags/three Author: A U Thor
398 refs/tags/two Author: A U Thor
400 test_cmp expect actual
403 test_expect_success 'check %(if)...%(then)...%(else)...%(end) atoms' '
404 git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(else)No author%(end): %(refname)" >actual &&
405 cat >expect <<-\EOF &&
406 A U Thor: refs/heads/main
407 A U Thor: refs/heads/side
408 A U Thor: refs/odd/spot
409 No author: refs/tags/annotated-tag
410 No author: refs/tags/doubly-annotated-tag
411 No author: refs/tags/doubly-signed-tag
412 A U Thor: refs/tags/foo1.10
413 A U Thor: refs/tags/foo1.3
414 A U Thor: refs/tags/foo1.6
415 A U Thor: refs/tags/four
416 A U Thor: refs/tags/one
417 No author: refs/tags/signed-tag
418 A U Thor: refs/tags/three
419 A U Thor: refs/tags/two
421 test_cmp expect actual
423 test_expect_success 'ignore spaces in %(if) atom usage' '
424 git for-each-ref --format="%(refname:short): %(if)%(HEAD)%(then)Head ref%(else)Not Head ref%(end)" >actual &&
425 cat >expect <<-\EOF &&
428 odd/spot: Not Head ref
429 annotated-tag: Not Head ref
430 doubly-annotated-tag: Not Head ref
431 doubly-signed-tag: Not Head ref
432 foo1.10: Not Head ref
437 signed-tag: Not Head ref
441 test_cmp expect actual
444 test_expect_success 'check %(if:equals=<string>)' '
445 git for-each-ref --format="%(if:equals=main)%(refname:short)%(then)Found main%(else)Not main%(end)" refs/heads/ >actual &&
446 cat >expect <<-\EOF &&
450 test_cmp expect actual
453 test_expect_success 'check %(if:notequals=<string>)' '
454 git for-each-ref --format="%(if:notequals=main)%(refname:short)%(then)Not main%(else)Found main%(end)" refs/heads/ >actual &&
455 cat >expect <<-\EOF &&
459 test_cmp expect actual
462 test_expect_success '--merged is compatible with --no-merged' '
463 git for-each-ref --merged HEAD --no-merged HEAD
466 test_expect_success 'validate worktree atom' '
467 cat >expect <<-EOF &&
469 main_worktree: $(pwd)/worktree_dir
470 side: not checked out
472 git worktree add -b main_worktree worktree_dir main &&
473 git for-each-ref --format="%(refname:short): %(if)%(worktreepath)%(then)%(worktreepath)%(else)not checked out%(end)" refs/heads/ >actual &&
474 rm -r worktree_dir &&
475 git worktree prune &&
476 test_cmp expect actual