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' '
12 git checkout -b side &&
14 git tag -m "An annotated tag" annotated-tag &&
15 git tag -m "Annonated doubly" doubly-annotated-tag annotated-tag &&
17 # Note that these "signed" tags might not actually be signed.
18 # Tests which care about the distinction should be marked
19 # with the GPG prereq.
20 if test_have_prereq GPG
26 git tag $sign -m "A signed tag" signed-tag &&
27 git tag $sign -m "Signed doubly" doubly-signed-tag signed-tag &&
29 git checkout master &&
30 git update-ref refs/odd/spot master
33 test_expect_success 'filtering with --points-at' '
34 cat >expect <<-\EOF &&
39 git for-each-ref --format="%(refname)" --points-at=master >actual &&
40 test_cmp expect actual
43 test_expect_success 'check signed tags with --points-at' '
44 sed -e "s/Z$//" >expect <<-\EOF &&
46 refs/tags/annotated-tag four
48 refs/tags/signed-tag four
50 git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
51 test_cmp expect actual
54 test_expect_success 'filtering with --merged' '
55 cat >expect <<-\EOF &&
62 git for-each-ref --format="%(refname)" --merged=master >actual &&
63 test_cmp expect actual
66 test_expect_success 'filtering with --no-merged' '
67 cat >expect <<-\EOF &&
69 refs/tags/annotated-tag
70 refs/tags/doubly-annotated-tag
71 refs/tags/doubly-signed-tag
75 git for-each-ref --format="%(refname)" --no-merged=master >actual &&
76 test_cmp expect actual
79 test_expect_success 'filtering with --contains' '
80 cat >expect <<-\EOF &&
84 refs/tags/annotated-tag
85 refs/tags/doubly-annotated-tag
86 refs/tags/doubly-signed-tag
92 git for-each-ref --format="%(refname)" --contains=two >actual &&
93 test_cmp expect actual
96 test_expect_success 'filtering with --no-contains' '
97 cat >expect <<-\EOF &&
100 git for-each-ref --format="%(refname)" --no-contains=two >actual &&
101 test_cmp expect actual
104 test_expect_success 'filtering with --contains and --no-contains' '
105 cat >expect <<-\EOF &&
108 git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
109 test_cmp expect actual
112 test_expect_success '%(color) must fail' '
113 test_must_fail git for-each-ref --format="%(color)%(refname)"
116 test_expect_success 'left alignment is default' '
117 cat >expect <<-\EOF &&
118 refname is refs/heads/master |refs/heads/master
119 refname is refs/heads/side |refs/heads/side
120 refname is refs/odd/spot |refs/odd/spot
121 refname is refs/tags/annotated-tag|refs/tags/annotated-tag
122 refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
123 refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
124 refname is refs/tags/four |refs/tags/four
125 refname is refs/tags/one |refs/tags/one
126 refname is refs/tags/signed-tag|refs/tags/signed-tag
127 refname is refs/tags/three |refs/tags/three
128 refname is refs/tags/two |refs/tags/two
130 git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
131 test_cmp expect actual
134 test_expect_success 'middle alignment' '
135 cat >expect <<-\EOF &&
136 | refname is refs/heads/master |refs/heads/master
137 | refname is refs/heads/side |refs/heads/side
138 | refname is refs/odd/spot |refs/odd/spot
139 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
140 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
141 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
142 | refname is refs/tags/four |refs/tags/four
143 | refname is refs/tags/one |refs/tags/one
144 |refname is refs/tags/signed-tag|refs/tags/signed-tag
145 | refname is refs/tags/three |refs/tags/three
146 | refname is refs/tags/two |refs/tags/two
148 git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
149 test_cmp expect actual
152 test_expect_success 'right alignment' '
153 cat >expect <<-\EOF &&
154 | refname is refs/heads/master|refs/heads/master
155 | refname is refs/heads/side|refs/heads/side
156 | refname is refs/odd/spot|refs/odd/spot
157 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
158 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
159 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
160 | refname is refs/tags/four|refs/tags/four
161 | refname is refs/tags/one|refs/tags/one
162 |refname is refs/tags/signed-tag|refs/tags/signed-tag
163 | refname is refs/tags/three|refs/tags/three
164 | refname is refs/tags/two|refs/tags/two
166 git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
167 test_cmp expect actual
171 | refname is refs/heads/master |refs/heads/master
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
184 test_align_permutations() {
187 test_expect_success "align:$option" '
188 git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
189 test_cmp expect actual
194 test_align_permutations <<-\EOF
201 position=middle,width=42
202 width=42,position=middle
205 # Last one wins (silently) when multiple arguments of the same type are given
207 test_align_permutations <<-\EOF
210 width=42,position=right,middle
211 42,right,position=middle
214 # Individual atoms inside %(align:...) and %(end) must not be quoted.
216 test_expect_success 'alignment with format quote' "
217 cat >expect <<-\EOF &&
218 |' '\''master| A U Thor'\'' '|
219 |' '\''side| A U Thor'\'' '|
220 |' '\''odd/spot| A U Thor'\'' '|
221 |' '\''annotated-tag| '\'' '|
222 |' '\''doubly-annotated-tag| '\'' '|
223 |' '\''doubly-signed-tag| '\'' '|
224 |' '\''four| A U Thor'\'' '|
225 |' '\''one| A U Thor'\'' '|
226 |' '\''signed-tag| '\'' '|
227 |' '\''three| A U Thor'\'' '|
228 |' '\''two| A U Thor'\'' '|
230 git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
231 test_cmp expect actual
234 test_expect_success 'nested alignment with quote formatting' "
235 cat >expect <<-\EOF &&
240 |'doubly-annotated-tag '|
241 |'doubly-signed-tag '|
248 git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
249 test_cmp expect actual
252 test_expect_success 'check `%(contents:lines=1)`' '
253 cat >expect <<-\EOF &&
257 annotated-tag |An annotated tag
258 doubly-annotated-tag |Annonated doubly
259 doubly-signed-tag |Signed doubly
262 signed-tag |A signed tag
266 git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
267 test_cmp expect actual
270 test_expect_success 'check `%(contents:lines=0)`' '
271 cat >expect <<-\EOF &&
276 doubly-annotated-tag |
284 git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
285 test_cmp expect actual
288 test_expect_success 'check `%(contents:lines=99999)`' '
289 cat >expect <<-\EOF &&
293 annotated-tag |An annotated tag
294 doubly-annotated-tag |Annonated doubly
295 doubly-signed-tag |Signed doubly
298 signed-tag |A signed tag
302 git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
303 test_cmp expect actual
306 test_expect_success '`%(contents:lines=-1)` should fail' '
307 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
310 test_expect_success 'setup for version sort' '
311 test_commit foo1.3 &&
312 test_commit foo1.6 &&
316 test_expect_success 'version sort' '
317 git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
318 cat >expect <<-\EOF &&
323 test_cmp expect actual
326 test_expect_success 'version sort (shortened)' '
327 git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
328 cat >expect <<-\EOF &&
333 test_cmp expect actual
336 test_expect_success 'reverse version sort' '
337 git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
338 cat >expect <<-\EOF &&
343 test_cmp expect actual
346 test_expect_success 'improper usage of %(if), %(then), %(else) and %(end) atoms' '
347 test_must_fail git for-each-ref --format="%(if)" &&
348 test_must_fail git for-each-ref --format="%(then) %(end)" &&
349 test_must_fail git for-each-ref --format="%(else) %(end)" &&
350 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
351 test_must_fail git for-each-ref --format="%(if) %(then) %(then) %(end)" &&
352 test_must_fail git for-each-ref --format="%(then) %(else) %(end)" &&
353 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
354 test_must_fail git for-each-ref --format="%(if) %(then) %(else)" &&
355 test_must_fail git for-each-ref --format="%(if) %(else) %(then) %(end)" &&
356 test_must_fail git for-each-ref --format="%(if) %(then) %(else) %(else) %(end)" &&
357 test_must_fail git for-each-ref --format="%(if) %(end)"
360 test_expect_success 'check %(if)...%(then)...%(end) atoms' '
361 git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Author: %(authorname)%(end)" >actual &&
362 cat >expect <<-\EOF &&
363 refs/heads/master Author: A U Thor
364 refs/heads/side Author: A U Thor
365 refs/odd/spot Author: A U Thor
366 refs/tags/annotated-tag
367 refs/tags/doubly-annotated-tag
368 refs/tags/doubly-signed-tag
369 refs/tags/foo1.10 Author: A U Thor
370 refs/tags/foo1.3 Author: A U Thor
371 refs/tags/foo1.6 Author: A U Thor
372 refs/tags/four Author: A U Thor
373 refs/tags/one Author: A U Thor
375 refs/tags/three Author: A U Thor
376 refs/tags/two Author: A U Thor
378 test_cmp expect actual
381 test_expect_success 'check %(if)...%(then)...%(else)...%(end) atoms' '
382 git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(else)No author%(end): %(refname)" >actual &&
383 cat >expect <<-\EOF &&
384 A U Thor: refs/heads/master
385 A U Thor: refs/heads/side
386 A U Thor: refs/odd/spot
387 No author: refs/tags/annotated-tag
388 No author: refs/tags/doubly-annotated-tag
389 No author: refs/tags/doubly-signed-tag
390 A U Thor: refs/tags/foo1.10
391 A U Thor: refs/tags/foo1.3
392 A U Thor: refs/tags/foo1.6
393 A U Thor: refs/tags/four
394 A U Thor: refs/tags/one
395 No author: refs/tags/signed-tag
396 A U Thor: refs/tags/three
397 A U Thor: refs/tags/two
399 test_cmp expect actual
401 test_expect_success 'ignore spaces in %(if) atom usage' '
402 git for-each-ref --format="%(refname:short): %(if)%(HEAD)%(then)Head ref%(else)Not Head ref%(end)" >actual &&
403 cat >expect <<-\EOF &&
406 odd/spot: Not Head ref
407 annotated-tag: Not Head ref
408 doubly-annotated-tag: Not Head ref
409 doubly-signed-tag: Not Head ref
410 foo1.10: Not Head ref
415 signed-tag: Not Head ref
419 test_cmp expect actual
422 test_expect_success 'check %(if:equals=<string>)' '
423 git for-each-ref --format="%(if:equals=master)%(refname:short)%(then)Found master%(else)Not master%(end)" refs/heads/ >actual &&
424 cat >expect <<-\EOF &&
428 test_cmp expect actual
431 test_expect_success 'check %(if:notequals=<string>)' '
432 git for-each-ref --format="%(if:notequals=master)%(refname:short)%(then)Not master%(else)Found master%(end)" refs/heads/ >actual &&
433 cat >expect <<-\EOF &&
437 test_cmp expect actual
440 test_expect_success '--merged is incompatible with --no-merged' '
441 test_must_fail git for-each-ref --merged HEAD --no-merged HEAD
444 test_expect_success 'validate worktree atom' '
445 cat >expect <<-EOF &&
447 master_worktree: $(pwd)/worktree_dir
448 side: not checked out
450 git worktree add -b master_worktree worktree_dir master &&
451 git for-each-ref --format="%(refname:short): %(if)%(worktreepath)%(then)%(worktreepath)%(else)not checked out%(end)" refs/heads/ >actual &&
452 rm -r worktree_dir &&
453 git worktree prune &&
454 test_cmp expect actual