]> git.ipfire.org Git - thirdparty/git.git/blame - t/t6302-for-each-ref-filter.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t6302-for-each-ref-filter.sh
CommitLineData
af83bafa
KN
1#!/bin/sh
2
3test_description='test for-each-refs usage of ref-filter APIs'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY"/lib-gpg.sh
7
af83bafa
KN
8test_expect_success 'setup some history and refs' '
9 test_commit one &&
2dbd00a7 10 git branch -M main &&
af83bafa
KN
11 test_commit two &&
12 test_commit three &&
13 git checkout -b side &&
14 test_commit four &&
7bc734ee
ES
15 git tag -m "An annotated tag" annotated-tag &&
16 git tag -m "Annonated doubly" doubly-annotated-tag annotated-tag &&
f3072180
JK
17
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.
618310a3
ES
21 if test_have_prereq GPG
22 then
f3072180
JK
23 sign=-s
24 else
25 sign=
618310a3 26 fi &&
f3072180
JK
27 git tag $sign -m "A signed tag" signed-tag &&
28 git tag $sign -m "Signed doubly" doubly-signed-tag signed-tag &&
29
2dbd00a7
JS
30 git checkout main &&
31 git update-ref refs/odd/spot main
af83bafa
KN
32'
33
33d15b54
KN
34test_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
53'
54
55test_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
63'
64
d325406e
KN
65test_expect_success 'filtering with --points-at' '
66 cat >expect <<-\EOF &&
2dbd00a7 67 refs/heads/main
d325406e
KN
68 refs/odd/spot
69 refs/tags/three
70 EOF
2dbd00a7 71 git for-each-ref --format="%(refname)" --points-at=main >actual &&
d325406e
KN
72 test_cmp expect actual
73'
74
75test_expect_success 'check signed tags with --points-at' '
f3072180 76 sed -e "s/Z$//" >expect <<-\EOF &&
d325406e 77 refs/heads/side Z
7bc734ee 78 refs/tags/annotated-tag four
188782ec
VD
79 refs/tags/doubly-annotated-tag four
80 refs/tags/doubly-signed-tag four
d325406e
KN
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
86'
87
7c328348
KN
88test_expect_success 'filtering with --merged' '
89 cat >expect <<-\EOF &&
2dbd00a7 90 refs/heads/main
7c328348
KN
91 refs/odd/spot
92 refs/tags/one
93 refs/tags/three
94 refs/tags/two
95 EOF
2dbd00a7 96 git for-each-ref --format="%(refname)" --merged=main >actual &&
7c328348
KN
97 test_cmp expect actual
98'
99
100test_expect_success 'filtering with --no-merged' '
f3072180 101 cat >expect <<-\EOF &&
7c328348 102 refs/heads/side
7bc734ee
ES
103 refs/tags/annotated-tag
104 refs/tags/doubly-annotated-tag
ef93c7db 105 refs/tags/doubly-signed-tag
7c328348
KN
106 refs/tags/four
107 refs/tags/signed-tag
108 EOF
2dbd00a7 109 git for-each-ref --format="%(refname)" --no-merged=main >actual &&
7c328348
KN
110 test_cmp expect actual
111'
112
4a71109a 113test_expect_success 'filtering with --contains' '
f3072180 114 cat >expect <<-\EOF &&
2dbd00a7 115 refs/heads/main
4a71109a
KN
116 refs/heads/side
117 refs/odd/spot
7bc734ee
ES
118 refs/tags/annotated-tag
119 refs/tags/doubly-annotated-tag
ef93c7db 120 refs/tags/doubly-signed-tag
4a71109a
KN
121 refs/tags/four
122 refs/tags/signed-tag
123 refs/tags/three
124 refs/tags/two
125 EOF
126 git for-each-ref --format="%(refname)" --contains=two >actual &&
127 test_cmp expect actual
128'
129
ac3f5a34
ÆAB
130test_expect_success 'filtering with --no-contains' '
131 cat >expect <<-\EOF &&
132 refs/tags/one
133 EOF
134 git for-each-ref --format="%(refname)" --no-contains=two >actual &&
135 test_cmp expect actual
136'
137
138test_expect_success 'filtering with --contains and --no-contains' '
139 cat >expect <<-\EOF &&
140 refs/tags/two
141 EOF
142 git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
143 test_cmp expect actual
144'
145
40a7551d
KN
146test_expect_success '%(color) must fail' '
147 test_must_fail git for-each-ref --format="%(color)%(refname)"
148'
149
1e1c4c5e
ZH
150test_expect_success '%(color:#aa22ac) must succeed' '
151 test_when_finished rm -rf test &&
152 git init test &&
153 (
154 cd test &&
155 test_commit initial &&
156 git branch -M main &&
157 cat >expect <<-\EOF &&
158 refs/heads/main
159 refs/tags/initial
160 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
166 )
167'
168
2dbd00a7 169test_expect_success 'left alignment is default' '
f3072180 170 cat >expect <<-\EOF &&
66713e84 171 refname is refs/heads/main |refs/heads/main
ce592082
KN
172 refname is refs/heads/side |refs/heads/side
173 refname is refs/odd/spot |refs/odd/spot
7bc734ee
ES
174 refname is refs/tags/annotated-tag|refs/tags/annotated-tag
175 refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
ef93c7db 176 refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
ce592082
KN
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
182 EOF
183 git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
184 test_cmp expect actual
185'
186
2dbd00a7 187test_expect_success 'middle alignment' '
f3072180 188 cat >expect <<-\EOF &&
66713e84 189 | refname is refs/heads/main |refs/heads/main
ce592082
KN
190 | refname is refs/heads/side |refs/heads/side
191 | refname is refs/odd/spot |refs/odd/spot
7bc734ee
ES
192 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
193 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
ef93c7db 194 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
ce592082
KN
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
200 EOF
201 git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
202 test_cmp expect actual
203'
204
2dbd00a7 205test_expect_success 'right alignment' '
f3072180 206 cat >expect <<-\EOF &&
66713e84 207 | refname is refs/heads/main|refs/heads/main
ce592082
KN
208 | refname is refs/heads/side|refs/heads/side
209 | refname is refs/odd/spot|refs/odd/spot
7bc734ee
ES
210 |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
211 |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
ef93c7db 212 |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
ce592082
KN
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
218 EOF
219 git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
220 test_cmp expect actual
221'
222
f3072180 223cat >expect <<-\EOF
66713e84 224| refname is refs/heads/main |refs/heads/main
395fb8f9
KN
225| refname is refs/heads/side |refs/heads/side
226| refname is refs/odd/spot |refs/odd/spot
7bc734ee
ES
227| refname is refs/tags/annotated-tag |refs/tags/annotated-tag
228|refname is refs/tags/doubly-annotated-tag |refs/tags/doubly-annotated-tag
ef93c7db 229| refname is refs/tags/doubly-signed-tag |refs/tags/doubly-signed-tag
395fb8f9
KN
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
235EOF
236
237test_align_permutations() {
238 while read -r option
239 do
2dbd00a7 240 test_expect_success "align:$option" '
395fb8f9
KN
241 git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
242 test_cmp expect actual
243 '
244 done
245}
246
247test_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
256EOF
257
258# Last one wins (silently) when multiple arguments of the same type are given
259
260test_align_permutations <<-\EOF
261 32,width=42,middle
262 width=30,42,middle
263 width=42,position=right,middle
264 42,right,position=middle
265EOF
266
ce592082
KN
267# Individual atoms inside %(align:...) and %(end) must not be quoted.
268
2dbd00a7 269test_expect_success 'alignment with format quote' "
f3072180 270 cat >expect <<-\EOF &&
66713e84 271 |' '\''main| A U Thor'\'' '|
ce592082
KN
272 |' '\''side| A U Thor'\'' '|
273 |' '\''odd/spot| A U Thor'\'' '|
7bc734ee
ES
274 |' '\''annotated-tag| '\'' '|
275 |' '\''doubly-annotated-tag| '\'' '|
ef93c7db 276 |' '\''doubly-signed-tag| '\'' '|
ce592082
KN
277 |' '\''four| A U Thor'\'' '|
278 |' '\''one| A U Thor'\'' '|
279 |' '\''signed-tag| '\'' '|
280 |' '\''three| A U Thor'\'' '|
281 |' '\''two| A U Thor'\'' '|
282 EOF
283 git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
284 test_cmp expect actual
285"
286
2dbd00a7 287test_expect_success 'nested alignment with quote formatting' "
f3072180 288 cat >expect <<-\EOF &&
66713e84 289 |' main '|
ce592082
KN
290 |' side '|
291 |' odd/spot '|
7bc734ee
ES
292 |' annotated-tag '|
293 |'doubly-annotated-tag '|
ef93c7db 294 |'doubly-signed-tag '|
ce592082
KN
295 |' four '|
296 |' one '|
297 |' signed-tag '|
298 |' three '|
299 |' two '|
300 EOF
301 git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
302 test_cmp expect actual
303"
304
1bb38e5a 305test_expect_success 'check `%(contents:lines=1)`' '
f3072180 306 cat >expect <<-\EOF &&
2dbd00a7 307 main |three
1bb38e5a
KN
308 side |four
309 odd/spot |three
7bc734ee
ES
310 annotated-tag |An annotated tag
311 doubly-annotated-tag |Annonated doubly
ef93c7db 312 doubly-signed-tag |Signed doubly
1bb38e5a
KN
313 four |four
314 one |one
ef93c7db 315 signed-tag |A signed tag
1bb38e5a
KN
316 three |three
317 two |two
318 EOF
319 git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
320 test_cmp expect actual
321'
322
323test_expect_success 'check `%(contents:lines=0)`' '
f3072180 324 cat >expect <<-\EOF &&
2dbd00a7 325 main |
1bb38e5a
KN
326 side |
327 odd/spot |
7bc734ee
ES
328 annotated-tag |
329 doubly-annotated-tag |
ef93c7db 330 doubly-signed-tag |
1bb38e5a
KN
331 four |
332 one |
333 signed-tag |
334 three |
335 two |
336 EOF
337 git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
338 test_cmp expect actual
339'
340
341test_expect_success 'check `%(contents:lines=99999)`' '
f3072180 342 cat >expect <<-\EOF &&
2dbd00a7 343 main |three
1bb38e5a
KN
344 side |four
345 odd/spot |three
7bc734ee
ES
346 annotated-tag |An annotated tag
347 doubly-annotated-tag |Annonated doubly
ef93c7db 348 doubly-signed-tag |Signed doubly
1bb38e5a
KN
349 four |four
350 one |one
ef93c7db 351 signed-tag |A signed tag
1bb38e5a
KN
352 three |three
353 two |two
354 EOF
355 git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
356 test_cmp expect actual
357'
358
359test_expect_success '`%(contents:lines=-1)` should fail' '
360 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
361'
362
90c00408
KN
363test_expect_success 'setup for version sort' '
364 test_commit foo1.3 &&
365 test_commit foo1.6 &&
366 test_commit foo1.10
367'
368
369test_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
375 EOF
376 test_cmp expect actual
377'
378
379test_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
385 EOF
386 test_cmp expect actual
387'
388
389test_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
395 EOF
396 test_cmp expect actual
397'
398
c58492d4
KN
399test_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)"
411'
412
413test_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 &&
2dbd00a7 416 refs/heads/main Author: A U Thor
c58492d4
KN
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
430 EOF
431 test_cmp expect actual
432'
433
434test_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 &&
2dbd00a7 437 A U Thor: refs/heads/main
c58492d4
KN
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
451 EOF
452 test_cmp expect actual
453'
454test_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 &&
2dbd00a7 457 main: Head ref
c58492d4
KN
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
471 EOF
472 test_cmp expect actual
473'
474
4f3e3b37 475test_expect_success 'check %(if:equals=<string>)' '
2dbd00a7 476 git for-each-ref --format="%(if:equals=main)%(refname:short)%(then)Found main%(else)Not main%(end)" refs/heads/ >actual &&
4f3e3b37 477 cat >expect <<-\EOF &&
2dbd00a7
JS
478 Found main
479 Not main
4f3e3b37
KN
480 EOF
481 test_cmp expect actual
482'
483
484test_expect_success 'check %(if:notequals=<string>)' '
2dbd00a7 485 git for-each-ref --format="%(if:notequals=main)%(refname:short)%(then)Not main%(else)Found main%(end)" refs/heads/ >actual &&
4f3e3b37 486 cat >expect <<-\EOF &&
2dbd00a7
JS
487 Found main
488 Not main
4f3e3b37
KN
489 EOF
490 test_cmp expect actual
491'
492
21bf9339
AL
493test_expect_success '--merged is compatible with --no-merged' '
494 git for-each-ref --merged HEAD --no-merged HEAD
17d6c744
ÆAB
495'
496
2582083f
NB
497test_expect_success 'validate worktree atom' '
498 cat >expect <<-EOF &&
2dbd00a7
JS
499 main: $(pwd)
500 main_worktree: $(pwd)/worktree_dir
2582083f
NB
501 side: not checked out
502 EOF
2dbd00a7 503 git worktree add -b main_worktree worktree_dir main &&
2582083f
NB
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
508'
509
af83bafa 510test_done