3 test_description
='test for-each-refs usage of ref-filter APIs'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9 .
"$TEST_DIRECTORY"/lib-gpg.sh
11 test_expect_success
'setup some history and refs' '
16 git checkout -b side &&
18 git tag -m "An annotated tag" annotated-tag &&
19 git tag -m "Annonated doubly" doubly-annotated-tag annotated-tag &&
21 # Note that these "signed" tags might not actually be signed.
22 # Tests which care about the distinction should be marked
23 # with the GPG prereq.
24 if test_have_prereq GPG
30 git tag $sign -m "A signed tag" signed-tag &&
31 git tag $sign -m "Signed doubly" doubly-signed-tag signed-tag &&
34 git update-ref refs/odd/spot main
37 test_expect_success
'filtering with --points-at' '
38 cat >expect <<-\EOF &&
43 git for-each-ref --format="%(refname)" --points-at=main >actual &&
44 test_cmp expect actual
47 test_expect_success
'check signed tags with --points-at' '
48 sed -e "s/Z$//" >expect <<-\EOF &&
50 refs/tags/annotated-tag four
52 refs/tags/signed-tag four
54 git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
55 test_cmp expect actual
58 test_expect_success
'filtering with --merged' '
59 cat >expect <<-\EOF &&
66 git for-each-ref --format="%(refname)" --merged=main >actual &&
67 test_cmp expect actual
70 test_expect_success
'filtering with --no-merged' '
71 cat >expect <<-\EOF &&
73 refs/tags/annotated-tag
74 refs/tags/doubly-annotated-tag
75 refs/tags/doubly-signed-tag
79 git for-each-ref --format="%(refname)" --no-merged=main >actual &&
80 test_cmp expect actual
83 test_expect_success
'filtering with --contains' '
84 cat >expect <<-\EOF &&
88 refs/tags/annotated-tag
89 refs/tags/doubly-annotated-tag
90 refs/tags/doubly-signed-tag
96 git for-each-ref --format="%(refname)" --contains=two >actual &&
97 test_cmp expect actual
100 test_expect_success
'filtering with --no-contains' '
101 cat >expect <<-\EOF &&
104 git for-each-ref --format="%(refname)" --no-contains=two >actual &&
105 test_cmp expect actual
108 test_expect_success
'filtering with --contains and --no-contains' '
109 cat >expect <<-\EOF &&
112 git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
113 test_cmp expect actual
116 test_expect_success
'%(color) must fail' '
117 test_must_fail git for-each-ref --format="%(color)%(refname)"
120 test_expect_success
'%(color:#aa22ac) must succeed' '
121 test_when_finished rm -rf test &&
125 test_commit initial &&
126 git branch -M main &&
127 cat >expect <<-\EOF &&
131 git remote add origin nowhere &&
132 git config branch.main.remote origin &&
133 git config branch.main.merge refs/heads/main &&
134 git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
135 test_cmp expect actual
139 test_expect_success
'left alignment is default' '
140 cat >expect <<-\EOF &&
141 refname is refs/heads/main |refs/heads/main
142 refname is refs/heads/side |refs/heads/side
143 refname is refs/odd/spot |refs/odd/spot
144 refname is refs/tags/annotated-tag|refs/tags/annotated-tag
145 refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
146 refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
147 refname is refs/tags/four |refs/tags/four
148 refname is refs/tags/one |refs/tags/one
149 refname is refs/tags/signed-tag|refs/tags/signed-tag
150 refname is refs/tags/three |refs/tags/three
151 refname is refs/tags/two |refs/tags/two
153 git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
154 test_cmp expect actual
157 test_expect_success
'middle alignment' '
158 cat >expect <<-\EOF &&
159 | refname is refs/heads/main |refs/heads/main
160 | refname is refs/heads/side |refs/heads/side
161 | refname is refs/odd/spot |refs/odd/spot
162 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
163 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
164 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
165 | refname is refs/tags/four |refs/tags/four
166 | refname is refs/tags/one |refs/tags/one
167 |refname is refs/tags/signed-tag|refs/tags/signed-tag
168 | refname is refs/tags/three |refs/tags/three
169 | refname is refs/tags/two |refs/tags/two
171 git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
172 test_cmp expect actual
175 test_expect_success
'right alignment' '
176 cat >expect <<-\EOF &&
177 | refname is refs/heads/main|refs/heads/main
178 | refname is refs/heads/side|refs/heads/side
179 | refname is refs/odd/spot|refs/odd/spot
180 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
181 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
182 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
183 | refname is refs/tags/four|refs/tags/four
184 | refname is refs/tags/one|refs/tags/one
185 |refname is refs/tags/signed-tag|refs/tags/signed-tag
186 | refname is refs/tags/three|refs/tags/three
187 | refname is refs/tags/two|refs/tags/two
189 git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
190 test_cmp expect actual
194 | refname is refs/heads/main |refs/heads/main
195 | refname is refs/heads/side |refs/heads/side
196 | refname is refs/odd/spot |refs/odd/spot
197 | refname is refs/tags/annotated-tag |refs/tags/annotated-tag
198 |refname is refs/tags/doubly-annotated-tag |refs/tags/doubly-annotated-tag
199 | refname is refs/tags/doubly-signed-tag |refs/tags/doubly-signed-tag
200 | refname is refs/tags/four |refs/tags/four
201 | refname is refs/tags/one |refs/tags/one
202 | refname is refs/tags/signed-tag |refs/tags/signed-tag
203 | refname is refs/tags/three |refs/tags/three
204 | refname is refs/tags/two |refs/tags/two
207 test_align_permutations() {
210 test_expect_success "align:$option" '
211 git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
212 test_cmp expect actual
217 test_align_permutations <<-\
EOF
224 position=middle,width=42
225 width=42,position=middle
228 # Last one wins (silently) when multiple arguments of the same type are given
230 test_align_permutations <<-\EOF
233 width=42,position=right,middle
234 42,right,position=middle
237 # Individual atoms inside %(align:...) and %(end) must not be quoted.
239 test_expect_success 'alignment with format quote' "
240 cat >expect <<-\EOF &&
241 |' '\''main| A U Thor'\'' '|
242 |' '\''side| A U Thor'\'' '|
243 |' '\''odd/spot| A U Thor'\'' '|
244 |' '\''annotated-tag| '\'' '|
245 |' '\''doubly-annotated-tag| '\'' '|
246 |' '\''doubly-signed-tag| '\'' '|
247 |' '\''four| A U Thor'\'' '|
248 |' '\''one| A U Thor'\'' '|
249 |' '\''signed-tag| '\'' '|
250 |' '\''three| A U Thor'\'' '|
251 |' '\''two| A U Thor'\'' '|
253 git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
254 test_cmp expect actual
257 test_expect_success 'nested alignment with quote formatting' "
258 cat >expect <<-\EOF &&
263 |'doubly-annotated-tag '|
264 |'doubly-signed-tag '|
271 git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
272 test_cmp expect actual
275 test_expect_success 'check `%(contents:lines=1)`' '
276 cat >expect <<-\EOF &&
280 annotated-tag |An annotated tag
281 doubly-annotated-tag |Annonated doubly
282 doubly-signed-tag |Signed doubly
285 signed-tag |A signed tag
289 git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
290 test_cmp expect actual
293 test_expect_success 'check `%(contents:lines=0)`' '
294 cat >expect <<-\EOF &&
299 doubly-annotated-tag |
307 git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
308 test_cmp expect actual
311 test_expect_success 'check `%(contents:lines=99999)`' '
312 cat >expect <<-\EOF &&
316 annotated-tag |An annotated tag
317 doubly-annotated-tag |Annonated doubly
318 doubly-signed-tag |Signed doubly
321 signed-tag |A signed tag
325 git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
326 test_cmp expect actual
329 test_expect_success '`%(contents:lines=-1)` should fail' '
330 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
333 test_expect_success 'setup for version sort' '
334 test_commit foo1.3 &&
335 test_commit foo1.6 &&
339 test_expect_success 'version sort' '
340 git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
341 cat >expect <<-\EOF &&
346 test_cmp expect actual
349 test_expect_success 'version sort (shortened)' '
350 git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
351 cat >expect <<-\EOF &&
356 test_cmp expect actual
359 test_expect_success 'reverse version sort' '
360 git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
361 cat >expect <<-\EOF &&
366 test_cmp expect actual
369 test_expect_success 'improper usage of %(if), %(then), %(else) and %(end) atoms' '
370 test_must_fail git for-each-ref --format="%(if)" &&
371 test_must_fail git for-each-ref --format="%(then) %(end)" &&
372 test_must_fail git for-each-ref --format="%(else) %(end)" &&
373 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
374 test_must_fail git for-each-ref --format="%(if) %(then) %(then) %(end)" &&
375 test_must_fail git for-each-ref --format="%(then) %(else) %(end)" &&
376 test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
377 test_must_fail git for-each-ref --format="%(if) %(then) %(else)" &&
378 test_must_fail git for-each-ref --format="%(if) %(else) %(then) %(end)" &&
379 test_must_fail git for-each-ref --format="%(if) %(then) %(else) %(else) %(end)" &&
380 test_must_fail git for-each-ref --format="%(if) %(end)"
383 test_expect_success 'check %(if)...%(then)...%(end) atoms' '
384 git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Author: %(authorname)%(end)" >actual &&
385 cat >expect <<-\EOF &&
386 refs/heads/main Author: A U Thor
387 refs/heads/side Author: A U Thor
388 refs/odd/spot Author: A U Thor
389 refs/tags/annotated-tag
390 refs/tags/doubly-annotated-tag
391 refs/tags/doubly-signed-tag
392 refs/tags/foo1.10 Author: A U Thor
393 refs/tags/foo1.3 Author: A U Thor
394 refs/tags/foo1.6 Author: A U Thor
395 refs/tags/four Author: A U Thor
396 refs/tags/one Author: A U Thor
398 refs/tags/three Author: A U Thor
399 refs/tags/two Author: A U Thor
401 test_cmp expect actual
404 test_expect_success 'check %(if)...%(then)...%(else)...%(end) atoms' '
405 git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(else)No author%(end): %(refname)" >actual &&
406 cat >expect <<-\EOF &&
407 A U Thor: refs/heads/main
408 A U Thor: refs/heads/side
409 A U Thor: refs/odd/spot
410 No author: refs/tags/annotated-tag
411 No author: refs/tags/doubly-annotated-tag
412 No author: refs/tags/doubly-signed-tag
413 A U Thor: refs/tags/foo1.10
414 A U Thor: refs/tags/foo1.3
415 A U Thor: refs/tags/foo1.6
416 A U Thor: refs/tags/four
417 A U Thor: refs/tags/one
418 No author: refs/tags/signed-tag
419 A U Thor: refs/tags/three
420 A U Thor: refs/tags/two
422 test_cmp expect actual
424 test_expect_success 'ignore spaces in %(if) atom usage' '
425 git for-each-ref --format="%(refname:short): %(if)%(HEAD)%(then)Head ref%(else)Not Head ref%(end)" >actual &&
426 cat >expect <<-\EOF &&
429 odd/spot: Not Head ref
430 annotated-tag: Not Head ref
431 doubly-annotated-tag: Not Head ref
432 doubly-signed-tag: Not Head ref
433 foo1.10: Not Head ref
438 signed-tag: Not Head ref
442 test_cmp expect actual
445 test_expect_success 'check %(if:equals=<string>)' '
446 git for-each-ref --format="%(if:equals=main)%(refname:short)%(then)Found main%(else)Not main%(end)" refs/heads/ >actual &&
447 cat >expect <<-\EOF &&
451 test_cmp expect actual
454 test_expect_success 'check %(if:notequals=<string>)' '
455 git for-each-ref --format="%(if:notequals=main)%(refname:short)%(then)Not main%(else)Found main%(end)" refs/heads/ >actual &&
456 cat >expect <<-\EOF &&
460 test_cmp expect actual
463 test_expect_success '--merged is compatible with --no-merged' '
464 git for-each-ref --merged HEAD --no-merged HEAD
467 test_expect_success 'validate worktree atom' '
468 cat >expect <<-EOF &&
470 main_worktree: $(pwd)/worktree_dir
471 side: not checked out
473 git worktree add -b main_worktree worktree_dir main &&
474 git for-each-ref --format="%(refname:short): %(if)%(worktreepath)%(then)%(worktreepath)%(else)not checked out%(end)" refs/heads/ >actual &&
475 rm -r worktree_dir &&
476 git worktree prune &&
477 test_cmp expect actual