Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / t / t6300-for-each-ref.sh
blobdcaab7265f5c75a8be444db3e2d1766732192f8d
1 #!/bin/sh
3 # Copyright (c) 2007 Andy Parkins
6 test_description='for-each-ref test'
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-gpg.sh
10 . "$TEST_DIRECTORY"/lib-terminal.sh
12 # Mon Jul 3 23:18:43 2006 +0000
13 datestamp=1151968723
14 setdate_and_increment () {
15 GIT_COMMITTER_DATE="$datestamp +0200"
16 datestamp=$(expr "$datestamp" + 1)
17 GIT_AUTHOR_DATE="$datestamp +0200"
18 datestamp=$(expr "$datestamp" + 1)
19 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
22 test_expect_success setup '
23 test_oid_cache <<-EOF &&
24 disklen sha1:138
25 disklen sha256:154
26 EOF
27 setdate_and_increment &&
28 echo "Using $datestamp" > one &&
29 git add one &&
30 git commit -m "Initial" &&
31 git branch -M main &&
32 setdate_and_increment &&
33 git tag -a -m "Tagging at $datestamp" testtag &&
34 git update-ref refs/remotes/origin/main main &&
35 git remote add origin nowhere &&
36 git config branch.main.remote origin &&
37 git config branch.main.merge refs/heads/main &&
38 git remote add myfork elsewhere &&
39 git config remote.pushdefault myfork &&
40 git config push.default current
43 test_atom() {
44 case "$1" in
45 head) ref=refs/heads/main ;;
46 tag) ref=refs/tags/testtag ;;
47 sym) ref=refs/heads/sym ;;
48 *) ref=$1 ;;
49 esac
50 printf '%s\n' "$3" >expected
51 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
52 git for-each-ref --format='%($2)' $ref >actual &&
53 sanitize_pgp <actual >actual.clean &&
54 test_cmp expected actual.clean
56 # Automatically test "contents:size" atom after testing "contents"
57 if test "$2" = "contents"
58 then
59 # for commit leg, $3 is changed there
60 expect=$(printf '%s' "$3" | wc -c)
61 test_expect_${4:-success} $PREREQ "basic atom: $1 contents:size" '
62 type=$(git cat-file -t "$ref") &&
63 case $type in
64 tag)
65 # We cannot use $3 as it expects sanitize_pgp to run
66 git cat-file tag $ref >out &&
67 expect=$(tail -n +6 out | wc -c) &&
68 rm -f out ;;
69 tree | blob)
70 expect="" ;;
71 commit)
72 : "use the calculated expect" ;;
74 BUG "unknown object type" ;;
75 esac &&
76 # Leave $expect unquoted to lose possible leading whitespaces
77 echo $expect >expected &&
78 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
79 test_cmp expected actual
84 hexlen=$(test_oid hexsz)
85 disklen=$(test_oid disklen)
87 test_atom head refname refs/heads/main
88 test_atom head refname: refs/heads/main
89 test_atom head refname:short main
90 test_atom head refname:lstrip=1 heads/main
91 test_atom head refname:lstrip=2 main
92 test_atom head refname:lstrip=-1 main
93 test_atom head refname:lstrip=-2 heads/main
94 test_atom head refname:rstrip=1 refs/heads
95 test_atom head refname:rstrip=2 refs
96 test_atom head refname:rstrip=-1 refs
97 test_atom head refname:rstrip=-2 refs/heads
98 test_atom head refname:strip=1 heads/main
99 test_atom head refname:strip=2 main
100 test_atom head refname:strip=-1 main
101 test_atom head refname:strip=-2 heads/main
102 test_atom head upstream refs/remotes/origin/main
103 test_atom head upstream:short origin/main
104 test_atom head upstream:lstrip=2 origin/main
105 test_atom head upstream:lstrip=-2 origin/main
106 test_atom head upstream:rstrip=2 refs/remotes
107 test_atom head upstream:rstrip=-2 refs/remotes
108 test_atom head upstream:strip=2 origin/main
109 test_atom head upstream:strip=-2 origin/main
110 test_atom head push refs/remotes/myfork/main
111 test_atom head push:short myfork/main
112 test_atom head push:lstrip=1 remotes/myfork/main
113 test_atom head push:lstrip=-1 main
114 test_atom head push:rstrip=1 refs/remotes/myfork
115 test_atom head push:rstrip=-1 refs
116 test_atom head push:strip=1 remotes/myfork/main
117 test_atom head push:strip=-1 main
118 test_atom head objecttype commit
119 test_atom head objectsize $((131 + hexlen))
120 test_atom head objectsize:disk $disklen
121 test_atom head deltabase $ZERO_OID
122 test_atom head objectname $(git rev-parse refs/heads/main)
123 test_atom head objectname:short $(git rev-parse --short refs/heads/main)
124 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
125 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
126 test_atom head tree $(git rev-parse refs/heads/main^{tree})
127 test_atom head tree:short $(git rev-parse --short refs/heads/main^{tree})
128 test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/main^{tree})
129 test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/main^{tree})
130 test_atom head parent ''
131 test_atom head parent:short ''
132 test_atom head parent:short=1 ''
133 test_atom head parent:short=10 ''
134 test_atom head numparent 0
135 test_atom head object ''
136 test_atom head type ''
137 test_atom head raw "$(git cat-file commit refs/heads/main)
139 test_atom head '*objectname' ''
140 test_atom head '*objecttype' ''
141 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
142 test_atom head authorname 'A U Thor'
143 test_atom head authoremail '<author@example.com>'
144 test_atom head authoremail:trim 'author@example.com'
145 test_atom head authoremail:localpart 'author'
146 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
147 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
148 test_atom head committername 'C O Mitter'
149 test_atom head committeremail '<committer@example.com>'
150 test_atom head committeremail:trim 'committer@example.com'
151 test_atom head committeremail:localpart 'committer'
152 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
153 test_atom head tag ''
154 test_atom head tagger ''
155 test_atom head taggername ''
156 test_atom head taggeremail ''
157 test_atom head taggeremail:trim ''
158 test_atom head taggeremail:localpart ''
159 test_atom head taggerdate ''
160 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
161 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
162 test_atom head subject 'Initial'
163 test_atom head subject:sanitize 'Initial'
164 test_atom head contents:subject 'Initial'
165 test_atom head body ''
166 test_atom head contents:body ''
167 test_atom head contents:signature ''
168 test_atom head contents 'Initial
170 test_atom head HEAD '*'
172 test_atom tag refname refs/tags/testtag
173 test_atom tag refname:short testtag
174 test_atom tag upstream ''
175 test_atom tag push ''
176 test_atom tag objecttype tag
177 test_atom tag objectsize $((114 + hexlen))
178 test_atom tag objectsize:disk $disklen
179 test_atom tag '*objectsize:disk' $disklen
180 test_atom tag deltabase $ZERO_OID
181 test_atom tag '*deltabase' $ZERO_OID
182 test_atom tag objectname $(git rev-parse refs/tags/testtag)
183 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
184 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
185 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
186 test_atom tag tree ''
187 test_atom tag tree:short ''
188 test_atom tag tree:short=1 ''
189 test_atom tag tree:short=10 ''
190 test_atom tag parent ''
191 test_atom tag parent:short ''
192 test_atom tag parent:short=1 ''
193 test_atom tag parent:short=10 ''
194 test_atom tag numparent ''
195 test_atom tag object $(git rev-parse refs/tags/testtag^0)
196 test_atom tag type 'commit'
197 test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
198 test_atom tag '*objecttype' 'commit'
199 test_atom tag author ''
200 test_atom tag authorname ''
201 test_atom tag authoremail ''
202 test_atom tag authoremail:trim ''
203 test_atom tag authoremail:localpart ''
204 test_atom tag authordate ''
205 test_atom tag committer ''
206 test_atom tag committername ''
207 test_atom tag committeremail ''
208 test_atom tag committeremail:trim ''
209 test_atom tag committeremail:localpart ''
210 test_atom tag committerdate ''
211 test_atom tag tag 'testtag'
212 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
213 test_atom tag taggername 'C O Mitter'
214 test_atom tag taggeremail '<committer@example.com>'
215 test_atom tag taggeremail:trim 'committer@example.com'
216 test_atom tag taggeremail:localpart 'committer'
217 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
218 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
219 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
220 test_atom tag subject 'Tagging at 1151968727'
221 test_atom tag subject:sanitize 'Tagging-at-1151968727'
222 test_atom tag contents:subject 'Tagging at 1151968727'
223 test_atom tag body ''
224 test_atom tag contents:body ''
225 test_atom tag contents:signature ''
226 test_atom tag contents 'Tagging at 1151968727
228 test_atom tag HEAD ' '
230 test_expect_success 'basic atom: refs/tags/testtag *raw' '
231 git cat-file commit refs/tags/testtag^{} >expected &&
232 git for-each-ref --format="%(*raw)" refs/tags/testtag >actual &&
233 sanitize_pgp <expected >expected.clean &&
234 echo >>expected.clean &&
235 sanitize_pgp <actual >actual.clean &&
236 test_cmp expected.clean actual.clean
239 test_expect_success 'Check invalid atoms names are errors' '
240 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
243 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
244 git for-each-ref --format="%(authordate)" refs/heads &&
245 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
246 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
247 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
250 test_expect_success 'Check valid format specifiers for date fields' '
251 git for-each-ref --format="%(authordate:default)" refs/heads &&
252 git for-each-ref --format="%(authordate:relative)" refs/heads &&
253 git for-each-ref --format="%(authordate:short)" refs/heads &&
254 git for-each-ref --format="%(authordate:local)" refs/heads &&
255 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
256 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
259 test_expect_success 'Check invalid format specifiers are errors' '
260 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
263 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
264 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
265 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
266 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
269 test_date () {
270 f=$1 &&
271 committer_date=$2 &&
272 author_date=$3 &&
273 tagger_date=$4 &&
274 cat >expected <<-EOF &&
275 'refs/heads/main' '$committer_date' '$author_date'
276 'refs/tags/testtag' '$tagger_date'
279 git for-each-ref --shell \
280 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
281 refs/heads &&
282 git for-each-ref --shell \
283 --format="%(refname) %(taggerdate${f:+:$f})" \
284 refs/tags
285 ) >actual &&
286 test_cmp expected actual
289 test_expect_success 'Check unformatted date fields output' '
290 test_date "" \
291 "Tue Jul 4 01:18:43 2006 +0200" \
292 "Tue Jul 4 01:18:44 2006 +0200" \
293 "Tue Jul 4 01:18:45 2006 +0200"
296 test_expect_success 'Check format "default" formatted date fields output' '
297 test_date default \
298 "Tue Jul 4 01:18:43 2006 +0200" \
299 "Tue Jul 4 01:18:44 2006 +0200" \
300 "Tue Jul 4 01:18:45 2006 +0200"
303 test_expect_success 'Check format "default-local" date fields output' '
304 test_date default-local "Mon Jul 3 23:18:43 2006" "Mon Jul 3 23:18:44 2006" "Mon Jul 3 23:18:45 2006"
307 # Don't know how to do relative check because I can't know when this script
308 # is going to be run and can't fake the current time to git, and hence can't
309 # provide expected output. Instead, I'll just make sure that "relative"
310 # doesn't exit in error
311 test_expect_success 'Check format "relative" date fields output' '
312 f=relative &&
313 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
314 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
317 # We just check that this is the same as "relative" for now.
318 test_expect_success 'Check format "relative-local" date fields output' '
319 test_date relative-local \
320 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
321 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
322 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
325 test_expect_success 'Check format "short" date fields output' '
326 test_date short 2006-07-04 2006-07-04 2006-07-04
329 test_expect_success 'Check format "short-local" date fields output' '
330 test_date short-local 2006-07-03 2006-07-03 2006-07-03
333 test_expect_success 'Check format "local" date fields output' '
334 test_date local \
335 "Mon Jul 3 23:18:43 2006" \
336 "Mon Jul 3 23:18:44 2006" \
337 "Mon Jul 3 23:18:45 2006"
340 test_expect_success 'Check format "iso8601" date fields output' '
341 test_date iso8601 \
342 "2006-07-04 01:18:43 +0200" \
343 "2006-07-04 01:18:44 +0200" \
344 "2006-07-04 01:18:45 +0200"
347 test_expect_success 'Check format "iso8601-local" date fields output' '
348 test_date iso8601-local "2006-07-03 23:18:43 +0000" "2006-07-03 23:18:44 +0000" "2006-07-03 23:18:45 +0000"
351 test_expect_success 'Check format "rfc2822" date fields output' '
352 test_date rfc2822 \
353 "Tue, 4 Jul 2006 01:18:43 +0200" \
354 "Tue, 4 Jul 2006 01:18:44 +0200" \
355 "Tue, 4 Jul 2006 01:18:45 +0200"
358 test_expect_success 'Check format "rfc2822-local" date fields output' '
359 test_date rfc2822-local "Mon, 3 Jul 2006 23:18:43 +0000" "Mon, 3 Jul 2006 23:18:44 +0000" "Mon, 3 Jul 2006 23:18:45 +0000"
362 test_expect_success 'Check format "raw" date fields output' '
363 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
366 test_expect_success 'Check format "raw-local" date fields output' '
367 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
370 test_expect_success 'Check format of strftime date fields' '
371 echo "my date is 2006-07-04" >expected &&
372 git for-each-ref \
373 --format="%(authordate:format:my date is %Y-%m-%d)" \
374 refs/heads >actual &&
375 test_cmp expected actual
378 test_expect_success 'Check format of strftime-local date fields' '
379 echo "my date is 2006-07-03" >expected &&
380 git for-each-ref \
381 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
382 refs/heads >actual &&
383 test_cmp expected actual
386 test_expect_success 'exercise strftime with odd fields' '
387 echo >expected &&
388 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
389 test_cmp expected actual &&
390 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
391 echo $long >expected &&
392 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
393 test_cmp expected actual
396 cat >expected <<\EOF
397 refs/heads/main
398 refs/remotes/origin/main
399 refs/tags/testtag
402 test_expect_success 'Verify ascending sort' '
403 git for-each-ref --format="%(refname)" --sort=refname >actual &&
404 test_cmp expected actual
408 cat >expected <<\EOF
409 refs/tags/testtag
410 refs/remotes/origin/main
411 refs/heads/main
414 test_expect_success 'Verify descending sort' '
415 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
416 test_cmp expected actual
419 test_expect_success 'Give help even with invalid sort atoms' '
420 test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
421 grep "^usage: git for-each-ref" actual
424 cat >expected <<\EOF
425 refs/tags/testtag
426 refs/tags/testtag-2
429 test_expect_success 'exercise patterns with prefixes' '
430 git tag testtag-2 &&
431 test_when_finished "git tag -d testtag-2" &&
432 git for-each-ref --format="%(refname)" \
433 refs/tags/testtag refs/tags/testtag-2 >actual &&
434 test_cmp expected actual
437 cat >expected <<\EOF
438 refs/tags/testtag
439 refs/tags/testtag-2
442 test_expect_success 'exercise glob patterns with prefixes' '
443 git tag testtag-2 &&
444 test_when_finished "git tag -d testtag-2" &&
445 git for-each-ref --format="%(refname)" \
446 refs/tags/testtag "refs/tags/testtag-*" >actual &&
447 test_cmp expected actual
450 cat >expected <<\EOF
451 'refs/heads/main'
452 'refs/remotes/origin/main'
453 'refs/tags/testtag'
456 test_expect_success 'Quoting style: shell' '
457 git for-each-ref --shell --format="%(refname)" >actual &&
458 test_cmp expected actual
461 test_expect_success 'Quoting style: perl' '
462 git for-each-ref --perl --format="%(refname)" >actual &&
463 test_cmp expected actual
466 test_expect_success 'Quoting style: python' '
467 git for-each-ref --python --format="%(refname)" >actual &&
468 test_cmp expected actual
471 cat >expected <<\EOF
472 "refs/heads/main"
473 "refs/remotes/origin/main"
474 "refs/tags/testtag"
477 test_expect_success 'Quoting style: tcl' '
478 git for-each-ref --tcl --format="%(refname)" >actual &&
479 test_cmp expected actual
482 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
483 test_expect_success "more than one quoting style: $i" "
484 test_must_fail git for-each-ref $i 2>err &&
485 grep '^error: more than one quoting style' err
487 done
489 test_expect_success 'setup for upstream:track[short]' '
490 test_commit two
493 test_atom head upstream:track '[ahead 1]'
494 test_atom head upstream:trackshort '>'
495 test_atom head upstream:track,nobracket 'ahead 1'
496 test_atom head upstream:nobracket,track 'ahead 1'
498 test_expect_success 'setup for push:track[short]' '
499 test_commit third &&
500 git update-ref refs/remotes/myfork/main main &&
501 git reset main~1
504 test_atom head push:track '[behind 1]'
505 test_atom head push:trackshort '<'
507 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
508 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
509 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
512 test_expect_success 'Check that :track[short] works when upstream is invalid' '
513 cat >expected <<-\EOF &&
514 [gone]
517 test_when_finished "git config branch.main.merge refs/heads/main" &&
518 git config branch.main.merge refs/heads/does-not-exist &&
519 git for-each-ref \
520 --format="%(upstream:track)$LF%(upstream:trackshort)" \
521 refs/heads >actual &&
522 test_cmp expected actual
525 test_expect_success 'Check for invalid refname format' '
526 test_must_fail git for-each-ref --format="%(refname:INVALID)"
529 test_expect_success 'set up color tests' '
530 cat >expected.color <<-EOF &&
531 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
532 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
533 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
534 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
535 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
536 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
538 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
539 color_format="%(objectname:short) %(color:green)%(refname:short)"
542 test_expect_success TTY '%(color) shows color with a tty' '
543 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
544 test_decode_color <actual.raw >actual &&
545 test_cmp expected.color actual
548 test_expect_success '%(color) does not show color without tty' '
549 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
550 test_cmp expected.bare actual
553 test_expect_success '--color can override tty check' '
554 git for-each-ref --color --format="$color_format" >actual.raw &&
555 test_decode_color <actual.raw >actual &&
556 test_cmp expected.color actual
559 test_expect_success 'color.ui=always does not override tty check' '
560 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
561 test_cmp expected.bare actual
564 cat >expected <<\EOF
565 heads/main
566 tags/main
569 test_expect_success 'Check ambiguous head and tag refs (strict)' '
570 git config --bool core.warnambiguousrefs true &&
571 git checkout -b newtag &&
572 echo "Using $datestamp" > one &&
573 git add one &&
574 git commit -m "Branch" &&
575 setdate_and_increment &&
576 git tag -m "Tagging at $datestamp" main &&
577 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
578 test_cmp expected actual
581 cat >expected <<\EOF
582 heads/main
583 main
586 test_expect_success 'Check ambiguous head and tag refs (loose)' '
587 git config --bool core.warnambiguousrefs false &&
588 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
589 test_cmp expected actual
592 cat >expected <<\EOF
593 heads/ambiguous
594 ambiguous
597 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
598 git checkout main &&
599 git tag ambiguous testtag^0 &&
600 git branch ambiguous testtag^0 &&
601 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
602 test_cmp expected actual
605 test_expect_success 'create tag without tagger' '
606 git tag -a -m "Broken tag" taggerless &&
607 git tag -f taggerless $(git cat-file tag taggerless |
608 sed -e "/^tagger /d" |
609 git hash-object --stdin -w -t tag)
612 test_atom refs/tags/taggerless type 'commit'
613 test_atom refs/tags/taggerless tag 'taggerless'
614 test_atom refs/tags/taggerless tagger ''
615 test_atom refs/tags/taggerless taggername ''
616 test_atom refs/tags/taggerless taggeremail ''
617 test_atom refs/tags/taggerless taggeremail:trim ''
618 test_atom refs/tags/taggerless taggeremail:localpart ''
619 test_atom refs/tags/taggerless taggerdate ''
620 test_atom refs/tags/taggerless committer ''
621 test_atom refs/tags/taggerless committername ''
622 test_atom refs/tags/taggerless committeremail ''
623 test_atom refs/tags/taggerless committeremail:trim ''
624 test_atom refs/tags/taggerless committeremail:localpart ''
625 test_atom refs/tags/taggerless committerdate ''
626 test_atom refs/tags/taggerless subject 'Broken tag'
628 test_expect_success 'an unusual tag with an incomplete line' '
630 git tag -m "bogo" bogo &&
631 bogo=$(git cat-file tag bogo) &&
632 bogo=$(printf "%s" "$bogo" | git mktag) &&
633 git tag -f bogo "$bogo" &&
634 git for-each-ref --format "%(body)" refs/tags/bogo
638 test_expect_success 'create tag with subject and body content' '
639 cat >>msg <<-\EOF &&
640 the subject line
642 first body line
643 second body line
645 git tag -F msg subject-body
647 test_atom refs/tags/subject-body subject 'the subject line'
648 test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
649 test_atom refs/tags/subject-body body 'first body line
650 second body line
652 test_atom refs/tags/subject-body contents 'the subject line
654 first body line
655 second body line
658 test_expect_success 'create tag with multiline subject' '
659 cat >msg <<-\EOF &&
660 first subject line
661 second subject line
663 first body line
664 second body line
666 git tag -F msg multiline
668 test_atom refs/tags/multiline subject 'first subject line second subject line'
669 test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
670 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
671 test_atom refs/tags/multiline body 'first body line
672 second body line
674 test_atom refs/tags/multiline contents:body 'first body line
675 second body line
677 test_atom refs/tags/multiline contents:signature ''
678 test_atom refs/tags/multiline contents 'first subject line
679 second subject line
681 first body line
682 second body line
685 test_expect_success GPG 'create signed tags' '
686 git tag -s -m "" signed-empty &&
687 git tag -s -m "subject line" signed-short &&
688 cat >msg <<-\EOF &&
689 subject line
691 body contents
693 git tag -s -F msg signed-long
696 sig='-----BEGIN PGP SIGNATURE-----
697 -----END PGP SIGNATURE-----
700 PREREQ=GPG
701 test_atom refs/tags/signed-empty subject ''
702 test_atom refs/tags/signed-empty subject:sanitize ''
703 test_atom refs/tags/signed-empty contents:subject ''
704 test_atom refs/tags/signed-empty body "$sig"
705 test_atom refs/tags/signed-empty contents:body ''
706 test_atom refs/tags/signed-empty contents:signature "$sig"
707 test_atom refs/tags/signed-empty contents "$sig"
709 test_expect_success GPG 'basic atom: refs/tags/signed-empty raw' '
710 git cat-file tag refs/tags/signed-empty >expected &&
711 git for-each-ref --format="%(raw)" refs/tags/signed-empty >actual &&
712 sanitize_pgp <expected >expected.clean &&
713 echo >>expected.clean &&
714 sanitize_pgp <actual >actual.clean &&
715 test_cmp expected.clean actual.clean
718 test_atom refs/tags/signed-short subject 'subject line'
719 test_atom refs/tags/signed-short subject:sanitize 'subject-line'
720 test_atom refs/tags/signed-short contents:subject 'subject line'
721 test_atom refs/tags/signed-short body "$sig"
722 test_atom refs/tags/signed-short contents:body ''
723 test_atom refs/tags/signed-short contents:signature "$sig"
724 test_atom refs/tags/signed-short contents "subject line
725 $sig"
727 test_expect_success GPG 'basic atom: refs/tags/signed-short raw' '
728 git cat-file tag refs/tags/signed-short >expected &&
729 git for-each-ref --format="%(raw)" refs/tags/signed-short >actual &&
730 sanitize_pgp <expected >expected.clean &&
731 echo >>expected.clean &&
732 sanitize_pgp <actual >actual.clean &&
733 test_cmp expected.clean actual.clean
736 test_atom refs/tags/signed-long subject 'subject line'
737 test_atom refs/tags/signed-long subject:sanitize 'subject-line'
738 test_atom refs/tags/signed-long contents:subject 'subject line'
739 test_atom refs/tags/signed-long body "body contents
740 $sig"
741 test_atom refs/tags/signed-long contents:body 'body contents
743 test_atom refs/tags/signed-long contents:signature "$sig"
744 test_atom refs/tags/signed-long contents "subject line
746 body contents
747 $sig"
749 test_expect_success GPG 'basic atom: refs/tags/signed-long raw' '
750 git cat-file tag refs/tags/signed-long >expected &&
751 git for-each-ref --format="%(raw)" refs/tags/signed-long >actual &&
752 sanitize_pgp <expected >expected.clean &&
753 echo >>expected.clean &&
754 sanitize_pgp <actual >actual.clean &&
755 test_cmp expected.clean actual.clean
758 test_expect_success 'set up refs pointing to tree and blob' '
759 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
760 git update-ref refs/myblobs/first refs/heads/main:one
763 test_atom refs/mytrees/first subject ""
764 test_atom refs/mytrees/first contents:subject ""
765 test_atom refs/mytrees/first body ""
766 test_atom refs/mytrees/first contents:body ""
767 test_atom refs/mytrees/first contents:signature ""
768 test_atom refs/mytrees/first contents ""
770 test_expect_success 'basic atom: refs/mytrees/first raw' '
771 git cat-file tree refs/mytrees/first >expected &&
772 echo >>expected &&
773 git for-each-ref --format="%(raw)" refs/mytrees/first >actual &&
774 test_cmp expected actual &&
775 git cat-file -s refs/mytrees/first >expected &&
776 git for-each-ref --format="%(raw:size)" refs/mytrees/first >actual &&
777 test_cmp expected actual
780 test_atom refs/myblobs/first subject ""
781 test_atom refs/myblobs/first contents:subject ""
782 test_atom refs/myblobs/first body ""
783 test_atom refs/myblobs/first contents:body ""
784 test_atom refs/myblobs/first contents:signature ""
785 test_atom refs/myblobs/first contents ""
787 test_expect_success 'basic atom: refs/myblobs/first raw' '
788 git cat-file blob refs/myblobs/first >expected &&
789 echo >>expected &&
790 git for-each-ref --format="%(raw)" refs/myblobs/first >actual &&
791 test_cmp expected actual &&
792 git cat-file -s refs/myblobs/first >expected &&
793 git for-each-ref --format="%(raw:size)" refs/myblobs/first >actual &&
794 test_cmp expected actual
797 test_expect_success 'set up refs pointing to binary blob' '
798 printf "a\0b\0c" >blob1 &&
799 printf "a\0c\0b" >blob2 &&
800 printf "\0a\0b\0c" >blob3 &&
801 printf "abc" >blob4 &&
802 printf "\0 \0 \0 " >blob5 &&
803 printf "\0 \0a\0 " >blob6 &&
804 printf " " >blob7 &&
805 >blob8 &&
806 obj=$(git hash-object -w blob1) &&
807 git update-ref refs/myblobs/blob1 "$obj" &&
808 obj=$(git hash-object -w blob2) &&
809 git update-ref refs/myblobs/blob2 "$obj" &&
810 obj=$(git hash-object -w blob3) &&
811 git update-ref refs/myblobs/blob3 "$obj" &&
812 obj=$(git hash-object -w blob4) &&
813 git update-ref refs/myblobs/blob4 "$obj" &&
814 obj=$(git hash-object -w blob5) &&
815 git update-ref refs/myblobs/blob5 "$obj" &&
816 obj=$(git hash-object -w blob6) &&
817 git update-ref refs/myblobs/blob6 "$obj" &&
818 obj=$(git hash-object -w blob7) &&
819 git update-ref refs/myblobs/blob7 "$obj" &&
820 obj=$(git hash-object -w blob8) &&
821 git update-ref refs/myblobs/blob8 "$obj"
824 test_expect_success 'Verify sorts with raw' '
825 cat >expected <<-EOF &&
826 refs/myblobs/blob8
827 refs/myblobs/blob5
828 refs/myblobs/blob6
829 refs/myblobs/blob3
830 refs/myblobs/blob7
831 refs/mytrees/first
832 refs/myblobs/first
833 refs/myblobs/blob1
834 refs/myblobs/blob2
835 refs/myblobs/blob4
836 refs/heads/main
838 git for-each-ref --format="%(refname)" --sort=raw \
839 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
840 test_cmp expected actual
843 test_expect_success 'Verify sorts with raw:size' '
844 cat >expected <<-EOF &&
845 refs/myblobs/blob8
846 refs/myblobs/first
847 refs/myblobs/blob7
848 refs/heads/main
849 refs/myblobs/blob4
850 refs/myblobs/blob1
851 refs/myblobs/blob2
852 refs/myblobs/blob3
853 refs/myblobs/blob5
854 refs/myblobs/blob6
855 refs/mytrees/first
857 git for-each-ref --format="%(refname)" --sort=raw:size \
858 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
859 test_cmp expected actual
862 test_expect_success 'validate raw atom with %(if:equals)' '
863 cat >expected <<-EOF &&
864 not equals
865 not equals
866 not equals
867 not equals
868 not equals
869 not equals
870 refs/myblobs/blob4
871 not equals
872 not equals
873 not equals
874 not equals
875 not equals
877 git for-each-ref --format="%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)" \
878 refs/myblobs/ refs/heads/ >actual &&
879 test_cmp expected actual
882 test_expect_success 'validate raw atom with %(if:notequals)' '
883 cat >expected <<-EOF &&
884 refs/heads/ambiguous
885 refs/heads/main
886 refs/heads/newtag
887 refs/myblobs/blob1
888 refs/myblobs/blob2
889 refs/myblobs/blob3
890 equals
891 refs/myblobs/blob5
892 refs/myblobs/blob6
893 refs/myblobs/blob7
894 refs/myblobs/blob8
895 refs/myblobs/first
897 git for-each-ref --format="%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)" \
898 refs/myblobs/ refs/heads/ >actual &&
899 test_cmp expected actual
902 test_expect_success 'empty raw refs with %(if)' '
903 cat >expected <<-EOF &&
904 refs/myblobs/blob1 not empty
905 refs/myblobs/blob2 not empty
906 refs/myblobs/blob3 not empty
907 refs/myblobs/blob4 not empty
908 refs/myblobs/blob5 not empty
909 refs/myblobs/blob6 not empty
910 refs/myblobs/blob7 empty
911 refs/myblobs/blob8 empty
912 refs/myblobs/first not empty
914 git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
915 refs/myblobs/ >actual &&
916 test_cmp expected actual
919 test_expect_success '%(raw) with --python must fail' '
920 test_must_fail git for-each-ref --format="%(raw)" --python
923 test_expect_success '%(raw) with --tcl must fail' '
924 test_must_fail git for-each-ref --format="%(raw)" --tcl
927 test_expect_success '%(raw) with --perl' '
928 git for-each-ref --format="\$name= %(raw);
929 print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
930 cmp blob1 actual &&
931 git for-each-ref --format="\$name= %(raw);
932 print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
933 cmp blob3 actual &&
934 git for-each-ref --format="\$name= %(raw);
935 print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
936 cmp blob8 actual &&
937 git for-each-ref --format="\$name= %(raw);
938 print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
939 cmp one actual &&
940 git cat-file tree refs/mytrees/first > expected &&
941 git for-each-ref --format="\$name= %(raw);
942 print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
943 cmp expected actual
946 test_expect_success '%(raw) with --shell must fail' '
947 test_must_fail git for-each-ref --format="%(raw)" --shell
950 test_expect_success '%(raw) with --shell and --sort=raw must fail' '
951 test_must_fail git for-each-ref --format="%(raw)" --sort=raw --shell
954 test_expect_success '%(raw:size) with --shell' '
955 git for-each-ref --format="%(raw:size)" | sed "s/^/$SQ/;s/$/$SQ/" >expect &&
956 git for-each-ref --format="%(raw:size)" --shell >actual &&
957 test_cmp expect actual
960 test_expect_success 'for-each-ref --format compare with cat-file --batch' '
961 git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
962 git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)
963 %(raw)" refs/mytrees/first >actual &&
964 test_cmp expected actual
967 test_expect_success 'set up multiple-sort tags' '
968 for when in 100000 200000
970 for email in user1 user2
972 for ref in ref1 ref2
974 GIT_COMMITTER_DATE="@$when +0000" \
975 GIT_COMMITTER_EMAIL="$email@example.com" \
976 git tag -m "tag $ref-$when-$email" \
977 multi-$ref-$when-$email || return 1
978 done
979 done
980 done
983 test_expect_success 'Verify sort with multiple keys' '
984 cat >expected <<-\EOF &&
985 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
986 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
987 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
988 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
989 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
990 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
991 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
992 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
994 git for-each-ref \
995 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
996 --sort=-refname \
997 --sort=taggeremail \
998 --sort=taggerdate \
999 "refs/tags/multi-*" >actual &&
1000 test_cmp expected actual
1003 test_expect_success 'equivalent sorts fall back on refname' '
1004 cat >expected <<-\EOF &&
1005 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1006 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1007 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1008 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1009 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1010 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1011 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1012 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1014 git for-each-ref \
1015 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1016 --sort=taggerdate \
1017 "refs/tags/multi-*" >actual &&
1018 test_cmp expected actual
1021 test_expect_success '--no-sort cancels the previous sort keys' '
1022 cat >expected <<-\EOF &&
1023 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1024 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1025 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1026 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1027 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1028 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1029 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1030 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1032 git for-each-ref \
1033 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1034 --sort=-refname \
1035 --sort=taggeremail \
1036 --no-sort \
1037 --sort=taggerdate \
1038 "refs/tags/multi-*" >actual &&
1039 test_cmp expected actual
1042 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
1043 test_when_finished "git checkout main" &&
1044 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1045 sed -e "s/^\* / /" actual >expect &&
1046 git checkout --orphan orphaned-branch &&
1047 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1048 test_cmp expect actual
1051 cat >trailers <<EOF
1052 Reviewed-by: A U Thor <author@example.com>
1053 Signed-off-by: A U Thor <author@example.com>
1054 [ v2 updated patch description ]
1055 Acked-by: A U Thor
1056 <author@example.com>
1059 unfold () {
1060 perl -0pe 's/\n\s+/ /g'
1063 test_expect_success 'set up trailers for next test' '
1064 echo "Some contents" > two &&
1065 git add two &&
1066 git commit -F - <<-EOF
1067 trailers: this commit message has trailers
1069 Some message contents
1071 $(cat trailers)
1075 test_trailer_option () {
1076 title=$1 option=$2
1077 cat >expect
1078 test_expect_success "$title" '
1079 git for-each-ref --format="%($option)" refs/heads/main >actual &&
1080 test_cmp expect actual &&
1081 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1082 test_cmp expect actual
1086 test_trailer_option '%(trailers:unfold) unfolds trailers' \
1087 'trailers:unfold' <<-EOF
1088 $(unfold <trailers)
1092 test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
1093 'trailers:only' <<-EOF
1094 $(grep -v patch.description <trailers)
1098 test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \
1099 'trailers:only=no,only=true' <<-EOF
1100 $(grep -v patch.description <trailers)
1104 test_trailer_option '%(trailers:only=yes) shows only "key: value" trailers' \
1105 'trailers:only=yes' <<-EOF
1106 $(grep -v patch.description <trailers)
1110 test_trailer_option '%(trailers:only=no) shows all trailers' \
1111 'trailers:only=no' <<-EOF
1112 $(cat trailers)
1116 test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
1117 'trailers:only,unfold' <<-EOF
1118 $(grep -v patch.description <trailers | unfold)
1122 test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
1123 'trailers:unfold,only' <<-EOF
1124 $(grep -v patch.description <trailers | unfold)
1128 test_trailer_option '%(trailers:key=foo) shows that trailer' \
1129 'trailers:key=Signed-off-by' <<-EOF
1130 Signed-off-by: A U Thor <author@example.com>
1134 test_trailer_option '%(trailers:key=foo) is case insensitive' \
1135 'trailers:key=SiGned-oFf-bY' <<-EOF
1136 Signed-off-by: A U Thor <author@example.com>
1140 test_trailer_option '%(trailers:key=foo:) trailing colon also works' \
1141 'trailers:key=Signed-off-by:' <<-EOF
1142 Signed-off-by: A U Thor <author@example.com>
1146 test_trailer_option '%(trailers:key=foo) multiple keys' \
1147 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
1148 Reviewed-by: A U Thor <author@example.com>
1149 Signed-off-by: A U Thor <author@example.com>
1153 test_trailer_option '%(trailers:key=nonexistent) becomes empty' \
1154 'trailers:key=Shined-off-by:' <<-EOF
1158 test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \
1159 'trailers:key=Acked-by' <<-EOF
1160 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
1164 test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
1165 'trailers:key=Signed-Off-by,unfold' <<-EOF
1166 $(unfold <trailers | grep Signed-off-by)
1170 test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
1171 'trailers:key=Signed-off-by,only=no' <<-EOF
1172 Signed-off-by: A U Thor <author@example.com>
1173 $(grep patch.description <trailers)
1177 test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \
1178 'trailers:key=Signed-off-by,valueonly' <<-EOF
1179 A U Thor <author@example.com>
1183 test_trailer_option '%(trailers:separator) changes separator' \
1184 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1185 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
1188 test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \
1189 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1190 Reviewed-by,A U Thor <author@example.com>
1191 Signed-off-by,A U Thor <author@example.com>
1195 test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \
1196 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1197 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
1200 test_failing_trailer_option () {
1201 title=$1 option=$2
1202 cat >expect
1203 test_expect_success "$title" '
1204 # error message cannot be checked under i18n
1205 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
1206 test_cmp expect actual &&
1207 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
1208 test_cmp expect actual
1212 test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
1213 'trailers:unsupported' <<-\EOF
1214 fatal: unknown %(trailers) argument: unsupported
1217 test_failing_trailer_option '%(trailers:key) without value is error' \
1218 'trailers:key' <<-\EOF
1219 fatal: expected %(trailers:key=<value>)
1222 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
1223 cat >expect <<-EOF &&
1224 fatal: unrecognized %(contents) argument: trailersonly
1226 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
1227 test_cmp expect actual
1230 test_expect_success 'basic atom: head contents:trailers' '
1231 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
1232 sanitize_pgp <actual >actual.clean &&
1233 # git for-each-ref ends with a blank line
1234 cat >expect <<-EOF &&
1235 $(cat trailers)
1238 test_cmp expect actual.clean
1241 test_expect_success 'basic atom: rest must fail' '
1242 test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1245 test_expect_success 'trailer parsing not fooled by --- line' '
1246 git commit --allow-empty -F - <<-\EOF &&
1247 this is the subject
1249 This is the body. The message has a "---" line which would confuse a
1250 message+patch parser. But here we know we have only a commit message,
1251 so we get it right.
1253 trailer: wrong
1255 This is more body.
1257 trailer: right
1261 echo "trailer: right" &&
1262 echo
1263 } >expect &&
1264 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
1265 test_cmp expect actual
1268 test_expect_success 'Add symbolic ref for the following tests' '
1269 git symbolic-ref refs/heads/sym refs/heads/main
1272 cat >expected <<EOF
1273 refs/heads/main
1276 test_expect_success 'Verify usage of %(symref) atom' '
1277 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1278 test_cmp expected actual
1281 cat >expected <<EOF
1282 heads/main
1285 test_expect_success 'Verify usage of %(symref:short) atom' '
1286 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1287 test_cmp expected actual
1290 cat >expected <<EOF
1291 main
1292 heads/main
1295 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1296 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1297 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
1298 test_cmp expected actual &&
1300 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1301 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
1302 test_cmp expected actual
1305 cat >expected <<EOF
1306 refs
1307 refs/heads
1310 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1311 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1312 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1313 test_cmp expected actual
1316 test_expect_success ':remotename and :remoteref' '
1317 git init remote-tests &&
1319 cd remote-tests &&
1320 test_commit initial &&
1321 git branch -M main &&
1322 git remote add from fifth.coffee:blub &&
1323 git config branch.main.remote from &&
1324 git config branch.main.merge refs/heads/stable &&
1325 git remote add to southridge.audio:repo &&
1326 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
1327 git config branch.main.pushRemote to &&
1328 for pair in "%(upstream)=refs/remotes/from/stable" \
1329 "%(upstream:remotename)=from" \
1330 "%(upstream:remoteref)=refs/heads/stable" \
1331 "%(push)=refs/remotes/to/pushed/main" \
1332 "%(push:remotename)=to" \
1333 "%(push:remoteref)=refs/heads/pushed/main"
1335 echo "${pair#*=}" >expect &&
1336 git for-each-ref --format="${pair%=*}" \
1337 refs/heads/main >actual &&
1338 test_cmp expect actual || exit 1
1339 done &&
1340 git branch push-simple &&
1341 git config branch.push-simple.pushRemote from &&
1342 actual="$(git for-each-ref \
1343 --format="%(push:remotename),%(push:remoteref)" \
1344 refs/heads/push-simple)" &&
1345 test from, = "$actual"
1349 test_expect_success 'for-each-ref --ignore-case ignores case' '
1350 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
1351 test_must_be_empty actual &&
1353 echo refs/heads/main >expect &&
1354 git for-each-ref --format="%(refname)" --ignore-case \
1355 refs/heads/MAIN >actual &&
1356 test_cmp expect actual
1359 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1360 # name refs numerically to avoid case-insensitive filesystem conflicts
1361 nr=0 &&
1362 for email in a A b B
1364 for subject in a A b B
1366 GIT_COMMITTER_EMAIL="$email@example.com" \
1367 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1368 nr=$((nr+1))||
1369 return 1
1370 done
1371 done &&
1372 git for-each-ref --ignore-case \
1373 --format="%(taggeremail) %(subject) %(refname)" \
1374 --sort=refname \
1375 --sort=subject \
1376 --sort=taggeremail \
1377 refs/tags/icase-* >actual &&
1378 cat >expect <<-\EOF &&
1379 <a@example.com> tag a refs/tags/icase-00
1380 <a@example.com> tag A refs/tags/icase-01
1381 <A@example.com> tag a refs/tags/icase-04
1382 <A@example.com> tag A refs/tags/icase-05
1383 <a@example.com> tag b refs/tags/icase-02
1384 <a@example.com> tag B refs/tags/icase-03
1385 <A@example.com> tag b refs/tags/icase-06
1386 <A@example.com> tag B refs/tags/icase-07
1387 <b@example.com> tag a refs/tags/icase-08
1388 <b@example.com> tag A refs/tags/icase-09
1389 <B@example.com> tag a refs/tags/icase-12
1390 <B@example.com> tag A refs/tags/icase-13
1391 <b@example.com> tag b refs/tags/icase-10
1392 <b@example.com> tag B refs/tags/icase-11
1393 <B@example.com> tag b refs/tags/icase-14
1394 <B@example.com> tag B refs/tags/icase-15
1396 test_cmp expect actual
1399 test_expect_success 'for-each-ref reports broken tags' '
1400 git tag -m "good tag" broken-tag-good HEAD &&
1401 git cat-file tag broken-tag-good >good &&
1402 sed s/commit/blob/ <good >bad &&
1403 bad=$(git hash-object -w -t tag bad) &&
1404 git update-ref refs/tags/broken-tag-bad $bad &&
1405 test_must_fail git for-each-ref --format="%(*objectname)" \
1406 refs/tags/broken-tag-*
1409 test_done