3 # Copyright (c) 2007 Andy Parkins
6 test_description='for-each-ref test'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 . "$TEST_DIRECTORY"/lib-gpg.sh
13 . "$TEST_DIRECTORY"/lib-terminal.sh
15 # Mon Jul 3 23:18:43 2006 +0000
17 setdate_and_increment () {
18 GIT_COMMITTER_DATE="$datestamp +0200"
19 datestamp=$(expr "$datestamp" + 1)
20 GIT_AUTHOR_DATE="$datestamp +0200"
21 datestamp=$(expr "$datestamp" + 1)
22 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
25 test_expect_success setup '
26 test_oid_cache <<-EOF &&
30 setdate_and_increment &&
31 echo "Using $datestamp" > one &&
33 git commit -m "Initial" &&
35 setdate_and_increment &&
36 git tag -a -m "Tagging at $datestamp" testtag &&
37 git update-ref refs/remotes/origin/main main &&
38 git remote add origin nowhere &&
39 git config branch.main.remote origin &&
40 git config branch.main.merge refs/heads/main &&
41 git remote add myfork elsewhere &&
42 git config remote.pushdefault myfork &&
43 git config push.default current
48 head) ref=refs/heads/main ;;
49 tag) ref=refs/tags/testtag ;;
50 sym) ref=refs/heads/sym ;;
53 printf '%s\n' "$3" >expected
54 test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
55 git for-each-ref --format='%($2)' $ref >actual &&
56 sanitize_pgp <actual >actual.clean &&
57 test_cmp expected actual.clean
59 # Automatically test "contents:size" atom after testing "contents"
60 if test "$2" = "contents"
62 case $(git cat-file -t "$ref") in
64 # We cannot use $3 as it expects sanitize_pgp to run
65 expect=$(git cat-file tag $ref | tail -n +6 | wc -c) ;;
69 expect=$(printf '%s' "$3" | wc -c) ;;
71 # Leave $expect unquoted to lose possible leading whitespaces
72 echo $expect >expected
73 test_expect_${4:-success} $PREREQ "basic atom: $1 contents:size" '
74 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
75 test_cmp expected actual
80 hexlen=$(test_oid hexsz)
81 disklen=$(test_oid disklen)
83 test_atom head refname refs/heads/main
84 test_atom head refname: refs/heads/main
85 test_atom head refname:short main
86 test_atom head refname:lstrip=1 heads/main
87 test_atom head refname:lstrip=2 main
88 test_atom head refname:lstrip=-1 main
89 test_atom head refname:lstrip=-2 heads/main
90 test_atom head refname:rstrip=1 refs/heads
91 test_atom head refname:rstrip=2 refs
92 test_atom head refname:rstrip=-1 refs
93 test_atom head refname:rstrip=-2 refs/heads
94 test_atom head refname:strip=1 heads/main
95 test_atom head refname:strip=2 main
96 test_atom head refname:strip=-1 main
97 test_atom head refname:strip=-2 heads/main
98 test_atom head upstream refs/remotes/origin/main
99 test_atom head upstream:short origin/main
100 test_atom head upstream:lstrip=2 origin/main
101 test_atom head upstream:lstrip=-2 origin/main
102 test_atom head upstream:rstrip=2 refs/remotes
103 test_atom head upstream:rstrip=-2 refs/remotes
104 test_atom head upstream:strip=2 origin/main
105 test_atom head upstream:strip=-2 origin/main
106 test_atom head push refs/remotes/myfork/main
107 test_atom head push:short myfork/main
108 test_atom head push:lstrip=1 remotes/myfork/main
109 test_atom head push:lstrip=-1 main
110 test_atom head push:rstrip=1 refs/remotes/myfork
111 test_atom head push:rstrip=-1 refs
112 test_atom head push:strip=1 remotes/myfork/main
113 test_atom head push:strip=-1 main
114 test_atom head objecttype commit
115 test_atom head objectsize $((131 + hexlen))
116 test_atom head objectsize:disk $disklen
117 test_atom head deltabase $ZERO_OID
118 test_atom head objectname $(git rev-parse refs/heads/main)
119 test_atom head objectname:short $(git rev-parse --short refs/heads/main)
120 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
121 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
122 test_atom head tree $(git rev-parse refs/heads/main^{tree})
123 test_atom head tree:short $(git rev-parse --short refs/heads/main^{tree})
124 test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/main^{tree})
125 test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/main^{tree})
126 test_atom head parent ''
127 test_atom head parent:short ''
128 test_atom head parent:short=1 ''
129 test_atom head parent:short=10 ''
130 test_atom head numparent 0
131 test_atom head object ''
132 test_atom head type ''
133 test_atom head '*objectname' ''
134 test_atom head '*objecttype' ''
135 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
136 test_atom head authorname 'A U Thor'
137 test_atom head authoremail '<author@example.com>'
138 test_atom head authoremail:trim 'author@example.com'
139 test_atom head authoremail:localpart 'author'
140 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
141 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
142 test_atom head committername 'C O Mitter'
143 test_atom head committeremail '<committer@example.com>'
144 test_atom head committeremail:trim 'committer@example.com'
145 test_atom head committeremail:localpart 'committer'
146 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
147 test_atom head tag ''
148 test_atom head tagger ''
149 test_atom head taggername ''
150 test_atom head taggeremail ''
151 test_atom head taggeremail:trim ''
152 test_atom head taggeremail:localpart ''
153 test_atom head taggerdate ''
154 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
155 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
156 test_atom head subject 'Initial'
157 test_atom head subject:sanitize 'Initial'
158 test_atom head contents:subject 'Initial'
159 test_atom head body ''
160 test_atom head contents:body ''
161 test_atom head contents:signature ''
162 test_atom head contents 'Initial
164 test_atom head HEAD '*'
166 test_atom tag refname refs/tags/testtag
167 test_atom tag refname:short testtag
168 test_atom tag upstream ''
169 test_atom tag push ''
170 test_atom tag objecttype tag
171 test_atom tag objectsize $((114 + hexlen))
172 test_atom tag objectsize:disk $disklen
173 test_atom tag '*objectsize:disk' $disklen
174 test_atom tag deltabase $ZERO_OID
175 test_atom tag '*deltabase' $ZERO_OID
176 test_atom tag objectname $(git rev-parse refs/tags/testtag)
177 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
178 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
179 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
180 test_atom tag tree ''
181 test_atom tag tree:short ''
182 test_atom tag tree:short=1 ''
183 test_atom tag tree:short=10 ''
184 test_atom tag parent ''
185 test_atom tag parent:short ''
186 test_atom tag parent:short=1 ''
187 test_atom tag parent:short=10 ''
188 test_atom tag numparent ''
189 test_atom tag object $(git rev-parse refs/tags/testtag^0)
190 test_atom tag type 'commit'
191 test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
192 test_atom tag '*objecttype' 'commit'
193 test_atom tag author ''
194 test_atom tag authorname ''
195 test_atom tag authoremail ''
196 test_atom tag authoremail:trim ''
197 test_atom tag authoremail:localpart ''
198 test_atom tag authordate ''
199 test_atom tag committer ''
200 test_atom tag committername ''
201 test_atom tag committeremail ''
202 test_atom tag committeremail:trim ''
203 test_atom tag committeremail:localpart ''
204 test_atom tag committerdate ''
205 test_atom tag tag 'testtag'
206 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
207 test_atom tag taggername 'C O Mitter'
208 test_atom tag taggeremail '<committer@example.com>'
209 test_atom tag taggeremail:trim 'committer@example.com'
210 test_atom tag taggeremail:localpart 'committer'
211 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
212 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
213 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
214 test_atom tag subject 'Tagging at 1151968727'
215 test_atom tag subject:sanitize 'Tagging-at-1151968727'
216 test_atom tag contents:subject 'Tagging at 1151968727'
217 test_atom tag body ''
218 test_atom tag contents:body ''
219 test_atom tag contents:signature ''
220 test_atom tag contents 'Tagging at 1151968727
222 test_atom tag HEAD ' '
224 test_expect_success 'Check invalid atoms names are errors' '
225 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
228 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
229 git for-each-ref --format="%(authordate)" refs/heads &&
230 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
231 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
232 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
235 test_expect_success 'Check valid format specifiers for date fields' '
236 git for-each-ref --format="%(authordate:default)" refs/heads &&
237 git for-each-ref --format="%(authordate:relative)" refs/heads &&
238 git for-each-ref --format="%(authordate:short)" refs/heads &&
239 git for-each-ref --format="%(authordate:local)" refs/heads &&
240 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
241 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
244 test_expect_success 'Check invalid format specifiers are errors' '
245 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
248 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
249 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
250 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
251 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
259 cat >expected <<-EOF &&
260 'refs/heads/main' '$committer_date' '$author_date'
261 'refs/tags/testtag' '$tagger_date'
264 git for-each-ref --shell \
265 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
267 git for-each-ref --shell \
268 --format="%(refname) %(taggerdate${f:+:$f})" \
271 test_cmp expected actual
274 test_expect_success 'Check unformatted date fields output' '
276 "Tue Jul 4 01:18:43 2006 +0200" \
277 "Tue Jul 4 01:18:44 2006 +0200" \
278 "Tue Jul 4 01:18:45 2006 +0200"
281 test_expect_success 'Check format "default" formatted date fields output' '
283 "Tue Jul 4 01:18:43 2006 +0200" \
284 "Tue Jul 4 01:18:44 2006 +0200" \
285 "Tue Jul 4 01:18:45 2006 +0200"
288 test_expect_success 'Check format "default-local" date fields output' '
289 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"
292 # Don't know how to do relative check because I can't know when this script
293 # is going to be run and can't fake the current time to git, and hence can't
294 # provide expected output. Instead, I'll just make sure that "relative"
295 # doesn't exit in error
296 test_expect_success 'Check format "relative" date fields output' '
298 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
299 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
302 # We just check that this is the same as "relative" for now.
303 test_expect_success 'Check format "relative-local" date fields output' '
304 test_date relative-local \
305 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
306 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
307 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
310 test_expect_success 'Check format "short" date fields output' '
311 test_date short 2006-07-04 2006-07-04 2006-07-04
314 test_expect_success 'Check format "short-local" date fields output' '
315 test_date short-local 2006-07-03 2006-07-03 2006-07-03
318 test_expect_success 'Check format "local" date fields output' '
320 "Mon Jul 3 23:18:43 2006" \
321 "Mon Jul 3 23:18:44 2006" \
322 "Mon Jul 3 23:18:45 2006"
325 test_expect_success 'Check format "iso8601" date fields output' '
327 "2006-07-04 01:18:43 +0200" \
328 "2006-07-04 01:18:44 +0200" \
329 "2006-07-04 01:18:45 +0200"
332 test_expect_success 'Check format "iso8601-local" date fields output' '
333 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"
336 test_expect_success 'Check format "rfc2822" date fields output' '
338 "Tue, 4 Jul 2006 01:18:43 +0200" \
339 "Tue, 4 Jul 2006 01:18:44 +0200" \
340 "Tue, 4 Jul 2006 01:18:45 +0200"
343 test_expect_success 'Check format "rfc2822-local" date fields output' '
344 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"
347 test_expect_success 'Check format "raw" date fields output' '
348 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
351 test_expect_success 'Check format "raw-local" date fields output' '
352 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
355 test_expect_success 'Check format of strftime date fields' '
356 echo "my date is 2006-07-04" >expected &&
358 --format="%(authordate:format:my date is %Y-%m-%d)" \
359 refs/heads >actual &&
360 test_cmp expected actual
363 test_expect_success 'Check format of strftime-local date fields' '
364 echo "my date is 2006-07-03" >expected &&
366 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
367 refs/heads >actual &&
368 test_cmp expected actual
371 test_expect_success 'exercise strftime with odd fields' '
373 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
374 test_cmp expected actual &&
375 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
376 echo $long >expected &&
377 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
378 test_cmp expected actual
383 refs/remotes/origin/main
387 test_expect_success 'Verify ascending sort' '
388 git for-each-ref --format="%(refname)" --sort=refname >actual &&
389 test_cmp expected actual
395 refs/remotes/origin/main
399 test_expect_success 'Verify descending sort' '
400 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
401 test_cmp expected actual
409 test_expect_success 'exercise patterns with prefixes' '
411 test_when_finished "git tag -d testtag-2" &&
412 git for-each-ref --format="%(refname)" \
413 refs/tags/testtag refs/tags/testtag-2 >actual &&
414 test_cmp expected actual
422 test_expect_success 'exercise glob patterns with prefixes' '
424 test_when_finished "git tag -d testtag-2" &&
425 git for-each-ref --format="%(refname)" \
426 refs/tags/testtag "refs/tags/testtag-*" >actual &&
427 test_cmp expected actual
432 'refs/remotes/origin/main'
436 test_expect_success 'Quoting style: shell' '
437 git for-each-ref --shell --format="%(refname)" >actual &&
438 test_cmp expected actual
441 test_expect_success 'Quoting style: perl' '
442 git for-each-ref --perl --format="%(refname)" >actual &&
443 test_cmp expected actual
446 test_expect_success 'Quoting style: python' '
447 git for-each-ref --python --format="%(refname)" >actual &&
448 test_cmp expected actual
453 "refs/remotes/origin/main"
457 test_expect_success 'Quoting style: tcl' '
458 git for-each-ref --tcl --format="%(refname)" >actual &&
459 test_cmp expected actual
462 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
463 test_expect_success "more than one quoting style: $i" "
464 test_must_fail git for-each-ref $i 2>err &&
465 grep '^error: more than one quoting style' err
469 test_expect_success 'setup for upstream:track[short]' '
473 test_atom head upstream:track '[ahead 1]'
474 test_atom head upstream:trackshort '>'
475 test_atom head upstream:track,nobracket 'ahead 1'
476 test_atom head upstream:nobracket,track 'ahead 1'
478 test_expect_success 'setup for push:track[short]' '
480 git update-ref refs/remotes/myfork/main main &&
484 test_atom head push:track '[behind 1]'
485 test_atom head push:trackshort '<'
487 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
488 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
489 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
492 test_expect_success 'Check that :track[short] works when upstream is invalid' '
493 cat >expected <<-\EOF &&
497 test_when_finished "git config branch.main.merge refs/heads/main" &&
498 git config branch.main.merge refs/heads/does-not-exist &&
500 --format="%(upstream:track)$LF%(upstream:trackshort)" \
501 refs/heads >actual &&
502 test_cmp expected actual
505 test_expect_success 'Check for invalid refname format' '
506 test_must_fail git for-each-ref --format="%(refname:INVALID)"
509 test_expect_success 'set up color tests' '
510 cat >expected.color <<-EOF &&
511 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
512 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
513 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
514 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
515 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
516 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
518 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
519 color_format="%(objectname:short) %(color:green)%(refname:short)"
522 test_expect_success TTY '%(color) shows color with a tty' '
523 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
524 test_decode_color <actual.raw >actual &&
525 test_cmp expected.color actual
528 test_expect_success '%(color) does not show color without tty' '
529 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
530 test_cmp expected.bare actual
533 test_expect_success '--color can override tty check' '
534 git for-each-ref --color --format="$color_format" >actual.raw &&
535 test_decode_color <actual.raw >actual &&
536 test_cmp expected.color actual
539 test_expect_success 'color.ui=always does not override tty check' '
540 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
541 test_cmp expected.bare actual
549 test_expect_success 'Check ambiguous head and tag refs (strict)' '
550 git config --bool core.warnambiguousrefs true &&
551 git checkout -b newtag &&
552 echo "Using $datestamp" > one &&
554 git commit -m "Branch" &&
555 setdate_and_increment &&
556 git tag -m "Tagging at $datestamp" main &&
557 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
558 test_cmp expected actual
566 test_expect_success 'Check ambiguous head and tag refs (loose)' '
567 git config --bool core.warnambiguousrefs false &&
568 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
569 test_cmp expected actual
577 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
579 git tag ambiguous testtag^0 &&
580 git branch ambiguous testtag^0 &&
581 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
582 test_cmp expected actual
585 test_expect_success 'create tag without tagger' '
586 git tag -a -m "Broken tag" taggerless &&
587 git tag -f taggerless $(git cat-file tag taggerless |
588 sed -e "/^tagger /d" |
589 git hash-object --stdin -w -t tag)
592 test_atom refs/tags/taggerless type 'commit'
593 test_atom refs/tags/taggerless tag 'taggerless'
594 test_atom refs/tags/taggerless tagger ''
595 test_atom refs/tags/taggerless taggername ''
596 test_atom refs/tags/taggerless taggeremail ''
597 test_atom refs/tags/taggerless taggeremail:trim ''
598 test_atom refs/tags/taggerless taggeremail:localpart ''
599 test_atom refs/tags/taggerless taggerdate ''
600 test_atom refs/tags/taggerless committer ''
601 test_atom refs/tags/taggerless committername ''
602 test_atom refs/tags/taggerless committeremail ''
603 test_atom refs/tags/taggerless committeremail:trim ''
604 test_atom refs/tags/taggerless committeremail:localpart ''
605 test_atom refs/tags/taggerless committerdate ''
606 test_atom refs/tags/taggerless subject 'Broken tag'
608 test_expect_success 'an unusual tag with an incomplete line' '
610 git tag -m "bogo" bogo &&
611 bogo=$(git cat-file tag bogo) &&
612 bogo=$(printf "%s" "$bogo" | git mktag) &&
613 git tag -f bogo "$bogo" &&
614 git for-each-ref --format "%(body)" refs/tags/bogo
618 test_expect_success 'create tag with subject and body content' '
625 git tag -F msg subject-body
627 test_atom refs/tags/subject-body subject 'the subject line'
628 test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
629 test_atom refs/tags/subject-body body 'first body line
632 test_atom refs/tags/subject-body contents 'the subject line
638 test_expect_success 'create tag with multiline subject' '
646 git tag -F msg multiline
648 test_atom refs/tags/multiline subject 'first subject line second subject line'
649 test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
650 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
651 test_atom refs/tags/multiline body 'first body line
654 test_atom refs/tags/multiline contents:body 'first body line
657 test_atom refs/tags/multiline contents:signature ''
658 test_atom refs/tags/multiline contents 'first subject line
665 test_expect_success GPG 'create signed tags' '
666 git tag -s -m "" signed-empty &&
667 git tag -s -m "subject line" signed-short &&
673 git tag -s -F msg signed-long
676 sig='-----BEGIN PGP SIGNATURE-----
677 -----END PGP SIGNATURE-----
681 test_atom refs/tags/signed-empty subject ''
682 test_atom refs/tags/signed-empty subject:sanitize ''
683 test_atom refs/tags/signed-empty contents:subject ''
684 test_atom refs/tags/signed-empty body "$sig"
685 test_atom refs/tags/signed-empty contents:body ''
686 test_atom refs/tags/signed-empty contents:signature "$sig"
687 test_atom refs/tags/signed-empty contents "$sig"
689 test_atom refs/tags/signed-short subject 'subject line'
690 test_atom refs/tags/signed-short subject:sanitize 'subject-line'
691 test_atom refs/tags/signed-short contents:subject 'subject line'
692 test_atom refs/tags/signed-short body "$sig"
693 test_atom refs/tags/signed-short contents:body ''
694 test_atom refs/tags/signed-short contents:signature "$sig"
695 test_atom refs/tags/signed-short contents "subject line
698 test_atom refs/tags/signed-long subject 'subject line'
699 test_atom refs/tags/signed-long subject:sanitize 'subject-line'
700 test_atom refs/tags/signed-long contents:subject 'subject line'
701 test_atom refs/tags/signed-long body "body contents
703 test_atom refs/tags/signed-long contents:body 'body contents
705 test_atom refs/tags/signed-long contents:signature "$sig"
706 test_atom refs/tags/signed-long contents "subject line
711 test_expect_success 'set up refs pointing to tree and blob' '
712 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
713 git update-ref refs/myblobs/first refs/heads/main:one
716 test_atom refs/mytrees/first subject ""
717 test_atom refs/mytrees/first contents:subject ""
718 test_atom refs/mytrees/first body ""
719 test_atom refs/mytrees/first contents:body ""
720 test_atom refs/mytrees/first contents:signature ""
721 test_atom refs/mytrees/first contents ""
723 test_atom refs/myblobs/first subject ""
724 test_atom refs/myblobs/first contents:subject ""
725 test_atom refs/myblobs/first body ""
726 test_atom refs/myblobs/first contents:body ""
727 test_atom refs/myblobs/first contents:signature ""
728 test_atom refs/myblobs/first contents ""
730 test_expect_success 'set up multiple-sort tags' '
731 for when in 100000 200000
733 for email in user1 user2
737 GIT_COMMITTER_DATE="@$when +0000" \
738 GIT_COMMITTER_EMAIL="$email@example.com" \
739 git tag -m "tag $ref-$when-$email" \
740 multi-$ref-$when-$email || return 1
746 test_expect_success 'Verify sort with multiple keys' '
747 cat >expected <<-\EOF &&
748 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
749 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
750 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
751 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
752 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
753 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
754 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
755 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
758 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
762 "refs/tags/multi-*" >actual &&
763 test_cmp expected actual
766 test_expect_success 'equivalent sorts fall back on refname' '
767 cat >expected <<-\EOF &&
768 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
769 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
770 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
771 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
772 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
773 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
774 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
775 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
778 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
780 "refs/tags/multi-*" >actual &&
781 test_cmp expected actual
784 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
785 test_when_finished "git checkout main" &&
786 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
787 sed -e "s/^\* / /" actual >expect &&
788 git checkout --orphan orphaned-branch &&
789 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
790 test_cmp expect actual
794 Reviewed-by: A U Thor <author@example.com>
795 Signed-off-by: A U Thor <author@example.com>
796 [ v2 updated patch description ]
802 perl -0pe 's/\n\s+/ /g'
805 test_expect_success 'set up trailers for next test' '
806 echo "Some contents" > two &&
808 git commit -F - <<-EOF
809 trailers: this commit message has trailers
811 Some message contents
817 test_expect_success '%(trailers:unfold) unfolds trailers' '
822 git for-each-ref --format="%(trailers:unfold)" refs/heads/main >actual &&
823 test_cmp expect actual &&
824 git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/main >actual &&
825 test_cmp expect actual
828 test_expect_success '%(trailers:only) shows only "key: value" trailers' '
830 grep -v patch.description <trailers &&
833 git for-each-ref --format="%(trailers:only)" refs/heads/main >actual &&
834 test_cmp expect actual &&
835 git for-each-ref --format="%(contents:trailers:only)" refs/heads/main >actual &&
836 test_cmp expect actual
839 test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
841 grep -v patch.description <trailers | unfold &&
844 git for-each-ref --format="%(trailers:only,unfold)" refs/heads/main >actual &&
845 test_cmp expect actual &&
846 git for-each-ref --format="%(trailers:unfold,only)" refs/heads/main >actual &&
847 test_cmp actual actual &&
848 git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/main >actual &&
849 test_cmp expect actual &&
850 git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/main >actual &&
851 test_cmp actual actual
854 test_expect_success '%(trailers) rejects unknown trailers arguments' '
855 # error message cannot be checked under i18n
856 cat >expect <<-EOF &&
857 fatal: unknown %(trailers) argument: unsupported
859 test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
860 test_i18ncmp expect actual &&
861 test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
862 test_i18ncmp expect actual
865 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
866 cat >expect <<-EOF &&
867 fatal: unrecognized %(contents) argument: trailersonly
869 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
870 test_i18ncmp expect actual
873 test_expect_success 'basic atom: head contents:trailers' '
874 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
875 sanitize_pgp <actual >actual.clean &&
876 # git for-each-ref ends with a blank line
877 cat >expect <<-EOF &&
881 test_cmp expect actual.clean
884 test_expect_success 'trailer parsing not fooled by --- line' '
885 git commit --allow-empty -F - <<-\EOF &&
888 This is the body. The message has a "---" line which would confuse a
889 message+patch parser. But here we know we have only a commit message,
900 echo "trailer: right" &&
903 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
904 test_cmp expect actual
907 test_expect_success 'Add symbolic ref for the following tests' '
908 git symbolic-ref refs/heads/sym refs/heads/main
915 test_expect_success 'Verify usage of %(symref) atom' '
916 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
917 test_cmp expected actual
924 test_expect_success 'Verify usage of %(symref:short) atom' '
925 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
926 test_cmp expected actual
934 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
935 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
936 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
937 test_cmp expected actual &&
939 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
940 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
941 test_cmp expected actual
949 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
950 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
951 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
952 test_cmp expected actual
955 test_expect_success ':remotename and :remoteref' '
956 git init remote-tests &&
959 test_commit initial &&
960 git branch -M main &&
961 git remote add from fifth.coffee:blub &&
962 git config branch.main.remote from &&
963 git config branch.main.merge refs/heads/stable &&
964 git remote add to southridge.audio:repo &&
965 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
966 git config branch.main.pushRemote to &&
967 for pair in "%(upstream)=refs/remotes/from/stable" \
968 "%(upstream:remotename)=from" \
969 "%(upstream:remoteref)=refs/heads/stable" \
970 "%(push)=refs/remotes/to/pushed/main" \
971 "%(push:remotename)=to" \
972 "%(push:remoteref)=refs/heads/pushed/main"
974 echo "${pair#*=}" >expect &&
975 git for-each-ref --format="${pair%=*}" \
976 refs/heads/main >actual &&
977 test_cmp expect actual
979 git branch push-simple &&
980 git config branch.push-simple.pushRemote from &&
981 actual="$(git for-each-ref \
982 --format="%(push:remotename),%(push:remoteref)" \
983 refs/heads/push-simple)" &&
984 test from, = "$actual"
988 test_expect_success 'for-each-ref --ignore-case ignores case' '
989 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
990 test_must_be_empty actual &&
992 echo refs/heads/main >expect &&
993 git for-each-ref --format="%(refname)" --ignore-case \
994 refs/heads/MAIN >actual &&
995 test_cmp expect actual
998 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
999 # name refs numerically to avoid case-insensitive filesystem conflicts
1001 for email in a A b B
1003 for subject in a A b B
1005 GIT_COMMITTER_EMAIL="$email@example.com" \
1006 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1011 git for-each-ref --ignore-case \
1012 --format="%(taggeremail) %(subject) %(refname)" \
1015 --sort=taggeremail \
1016 refs/tags/icase-* >actual &&
1017 cat >expect <<-\EOF &&
1018 <a@example.com> tag a refs/tags/icase-00
1019 <a@example.com> tag A refs/tags/icase-01
1020 <A@example.com> tag a refs/tags/icase-04
1021 <A@example.com> tag A refs/tags/icase-05
1022 <a@example.com> tag b refs/tags/icase-02
1023 <a@example.com> tag B refs/tags/icase-03
1024 <A@example.com> tag b refs/tags/icase-06
1025 <A@example.com> tag B refs/tags/icase-07
1026 <b@example.com> tag a refs/tags/icase-08
1027 <b@example.com> tag A refs/tags/icase-09
1028 <B@example.com> tag a refs/tags/icase-12
1029 <B@example.com> tag A refs/tags/icase-13
1030 <b@example.com> tag b refs/tags/icase-10
1031 <b@example.com> tag B refs/tags/icase-11
1032 <B@example.com> tag b refs/tags/icase-14
1033 <B@example.com> tag B refs/tags/icase-15
1035 test_cmp expect actual