3 # Copyright (c) 2007 Andy Parkins
6 test_description='for-each-ref test'
9 . "$TEST_DIRECTORY"/lib-gpg.sh
10 . "$TEST_DIRECTORY"/lib-terminal.sh
12 # Mon Jul 3 23:18:43 2006 +0000
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 &&
27 setdate_and_increment &&
28 echo "Using $datestamp" > one &&
30 git commit -m "Initial" &&
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
45 head) ref=refs/heads/main ;;
46 tag) ref=refs/tags/testtag ;;
47 sym) ref=refs/heads/sym ;;
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"
59 case $(git cat-file -t "$ref") in
61 # We cannot use $3 as it expects sanitize_pgp to run
62 expect=$(git cat-file tag $ref | tail -n +6 | wc -c) ;;
66 expect=$(printf '%s' "$3" | wc -c) ;;
68 # Leave $expect unquoted to lose possible leading whitespaces
69 echo $expect >expected
70 test_expect_${4:-success} $PREREQ "basic atom: $1 contents:size" '
71 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
72 test_cmp expected actual
77 hexlen=$(test_oid hexsz)
78 disklen=$(test_oid disklen)
80 test_atom head refname refs/heads/main
81 test_atom head refname: refs/heads/main
82 test_atom head refname:short main
83 test_atom head refname:lstrip=1 heads/main
84 test_atom head refname:lstrip=2 main
85 test_atom head refname:lstrip=-1 main
86 test_atom head refname:lstrip=-2 heads/main
87 test_atom head refname:rstrip=1 refs/heads
88 test_atom head refname:rstrip=2 refs
89 test_atom head refname:rstrip=-1 refs
90 test_atom head refname:rstrip=-2 refs/heads
91 test_atom head refname:strip=1 heads/main
92 test_atom head refname:strip=2 main
93 test_atom head refname:strip=-1 main
94 test_atom head refname:strip=-2 heads/main
95 test_atom head upstream refs/remotes/origin/main
96 test_atom head upstream:short origin/main
97 test_atom head upstream:lstrip=2 origin/main
98 test_atom head upstream:lstrip=-2 origin/main
99 test_atom head upstream:rstrip=2 refs/remotes
100 test_atom head upstream:rstrip=-2 refs/remotes
101 test_atom head upstream:strip=2 origin/main
102 test_atom head upstream:strip=-2 origin/main
103 test_atom head push refs/remotes/myfork/main
104 test_atom head push:short myfork/main
105 test_atom head push:lstrip=1 remotes/myfork/main
106 test_atom head push:lstrip=-1 main
107 test_atom head push:rstrip=1 refs/remotes/myfork
108 test_atom head push:rstrip=-1 refs
109 test_atom head push:strip=1 remotes/myfork/main
110 test_atom head push:strip=-1 main
111 test_atom head objecttype commit
112 test_atom head objectsize $((131 + hexlen))
113 test_atom head objectsize:disk $disklen
114 test_atom head deltabase $ZERO_OID
115 test_atom head objectname $(git rev-parse refs/heads/main)
116 test_atom head objectname:short $(git rev-parse --short refs/heads/main)
117 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
118 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
119 test_atom head tree $(git rev-parse refs/heads/main^{tree})
120 test_atom head tree:short $(git rev-parse --short refs/heads/main^{tree})
121 test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/main^{tree})
122 test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/main^{tree})
123 test_atom head parent ''
124 test_atom head parent:short ''
125 test_atom head parent:short=1 ''
126 test_atom head parent:short=10 ''
127 test_atom head numparent 0
128 test_atom head object ''
129 test_atom head type ''
130 test_atom head '*objectname' ''
131 test_atom head '*objecttype' ''
132 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
133 test_atom head authorname 'A U Thor'
134 test_atom head authoremail '<author@example.com>'
135 test_atom head authoremail:trim 'author@example.com'
136 test_atom head authoremail:localpart 'author'
137 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
138 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
139 test_atom head committername 'C O Mitter'
140 test_atom head committeremail '<committer@example.com>'
141 test_atom head committeremail:trim 'committer@example.com'
142 test_atom head committeremail:localpart 'committer'
143 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
144 test_atom head tag ''
145 test_atom head tagger ''
146 test_atom head taggername ''
147 test_atom head taggeremail ''
148 test_atom head taggeremail:trim ''
149 test_atom head taggeremail:localpart ''
150 test_atom head taggerdate ''
151 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
152 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
153 test_atom head subject 'Initial'
154 test_atom head subject:sanitize 'Initial'
155 test_atom head contents:subject 'Initial'
156 test_atom head body ''
157 test_atom head contents:body ''
158 test_atom head contents:signature ''
159 test_atom head contents 'Initial
161 test_atom head HEAD '*'
163 test_atom tag refname refs/tags/testtag
164 test_atom tag refname:short testtag
165 test_atom tag upstream ''
166 test_atom tag push ''
167 test_atom tag objecttype tag
168 test_atom tag objectsize $((114 + hexlen))
169 test_atom tag objectsize:disk $disklen
170 test_atom tag '*objectsize:disk' $disklen
171 test_atom tag deltabase $ZERO_OID
172 test_atom tag '*deltabase' $ZERO_OID
173 test_atom tag objectname $(git rev-parse refs/tags/testtag)
174 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
175 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
176 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
177 test_atom tag tree ''
178 test_atom tag tree:short ''
179 test_atom tag tree:short=1 ''
180 test_atom tag tree:short=10 ''
181 test_atom tag parent ''
182 test_atom tag parent:short ''
183 test_atom tag parent:short=1 ''
184 test_atom tag parent:short=10 ''
185 test_atom tag numparent ''
186 test_atom tag object $(git rev-parse refs/tags/testtag^0)
187 test_atom tag type 'commit'
188 test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
189 test_atom tag '*objecttype' 'commit'
190 test_atom tag author ''
191 test_atom tag authorname ''
192 test_atom tag authoremail ''
193 test_atom tag authoremail:trim ''
194 test_atom tag authoremail:localpart ''
195 test_atom tag authordate ''
196 test_atom tag committer ''
197 test_atom tag committername ''
198 test_atom tag committeremail ''
199 test_atom tag committeremail:trim ''
200 test_atom tag committeremail:localpart ''
201 test_atom tag committerdate ''
202 test_atom tag tag 'testtag'
203 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
204 test_atom tag taggername 'C O Mitter'
205 test_atom tag taggeremail '<committer@example.com>'
206 test_atom tag taggeremail:trim 'committer@example.com'
207 test_atom tag taggeremail:localpart 'committer'
208 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
209 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
210 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
211 test_atom tag subject 'Tagging at 1151968727'
212 test_atom tag subject:sanitize 'Tagging-at-1151968727'
213 test_atom tag contents:subject 'Tagging at 1151968727'
214 test_atom tag body ''
215 test_atom tag contents:body ''
216 test_atom tag contents:signature ''
217 test_atom tag contents 'Tagging at 1151968727
219 test_atom tag HEAD ' '
221 test_expect_success 'Check invalid atoms names are errors' '
222 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
225 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
226 git for-each-ref --format="%(authordate)" refs/heads &&
227 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
228 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
229 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
232 test_expect_success 'Check valid format specifiers for date fields' '
233 git for-each-ref --format="%(authordate:default)" refs/heads &&
234 git for-each-ref --format="%(authordate:relative)" refs/heads &&
235 git for-each-ref --format="%(authordate:short)" refs/heads &&
236 git for-each-ref --format="%(authordate:local)" refs/heads &&
237 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
238 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
241 test_expect_success 'Check invalid format specifiers are errors' '
242 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
245 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
246 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
247 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
248 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
256 cat >expected <<-EOF &&
257 'refs/heads/main' '$committer_date' '$author_date'
258 'refs/tags/testtag' '$tagger_date'
261 git for-each-ref --shell \
262 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
264 git for-each-ref --shell \
265 --format="%(refname) %(taggerdate${f:+:$f})" \
268 test_cmp expected actual
271 test_expect_success 'Check unformatted date fields output' '
273 "Tue Jul 4 01:18:43 2006 +0200" \
274 "Tue Jul 4 01:18:44 2006 +0200" \
275 "Tue Jul 4 01:18:45 2006 +0200"
278 test_expect_success 'Check format "default" formatted date fields output' '
280 "Tue Jul 4 01:18:43 2006 +0200" \
281 "Tue Jul 4 01:18:44 2006 +0200" \
282 "Tue Jul 4 01:18:45 2006 +0200"
285 test_expect_success 'Check format "default-local" date fields output' '
286 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"
289 # Don't know how to do relative check because I can't know when this script
290 # is going to be run and can't fake the current time to git, and hence can't
291 # provide expected output. Instead, I'll just make sure that "relative"
292 # doesn't exit in error
293 test_expect_success 'Check format "relative" date fields output' '
295 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
296 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
299 # We just check that this is the same as "relative" for now.
300 test_expect_success 'Check format "relative-local" date fields output' '
301 test_date relative-local \
302 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
303 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
304 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
307 test_expect_success 'Check format "short" date fields output' '
308 test_date short 2006-07-04 2006-07-04 2006-07-04
311 test_expect_success 'Check format "short-local" date fields output' '
312 test_date short-local 2006-07-03 2006-07-03 2006-07-03
315 test_expect_success 'Check format "local" date fields output' '
317 "Mon Jul 3 23:18:43 2006" \
318 "Mon Jul 3 23:18:44 2006" \
319 "Mon Jul 3 23:18:45 2006"
322 test_expect_success 'Check format "iso8601" date fields output' '
324 "2006-07-04 01:18:43 +0200" \
325 "2006-07-04 01:18:44 +0200" \
326 "2006-07-04 01:18:45 +0200"
329 test_expect_success 'Check format "iso8601-local" date fields output' '
330 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"
333 test_expect_success 'Check format "rfc2822" date fields output' '
335 "Tue, 4 Jul 2006 01:18:43 +0200" \
336 "Tue, 4 Jul 2006 01:18:44 +0200" \
337 "Tue, 4 Jul 2006 01:18:45 +0200"
340 test_expect_success 'Check format "rfc2822-local" date fields output' '
341 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"
344 test_expect_success 'Check format "raw" date fields output' '
345 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
348 test_expect_success 'Check format "raw-local" date fields output' '
349 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
352 test_expect_success 'Check format of strftime date fields' '
353 echo "my date is 2006-07-04" >expected &&
355 --format="%(authordate:format:my date is %Y-%m-%d)" \
356 refs/heads >actual &&
357 test_cmp expected actual
360 test_expect_success 'Check format of strftime-local date fields' '
361 echo "my date is 2006-07-03" >expected &&
363 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
364 refs/heads >actual &&
365 test_cmp expected actual
368 test_expect_success 'exercise strftime with odd fields' '
370 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
371 test_cmp expected actual &&
372 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
373 echo $long >expected &&
374 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
375 test_cmp expected actual
380 refs/remotes/origin/main
384 test_expect_success 'Verify ascending sort' '
385 git for-each-ref --format="%(refname)" --sort=refname >actual &&
386 test_cmp expected actual
392 refs/remotes/origin/main
396 test_expect_success 'Verify descending sort' '
397 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
398 test_cmp expected actual
406 test_expect_success 'exercise patterns with prefixes' '
408 test_when_finished "git tag -d testtag-2" &&
409 git for-each-ref --format="%(refname)" \
410 refs/tags/testtag refs/tags/testtag-2 >actual &&
411 test_cmp expected actual
419 test_expect_success 'exercise glob patterns with prefixes' '
421 test_when_finished "git tag -d testtag-2" &&
422 git for-each-ref --format="%(refname)" \
423 refs/tags/testtag "refs/tags/testtag-*" >actual &&
424 test_cmp expected actual
429 'refs/remotes/origin/main'
433 test_expect_success 'Quoting style: shell' '
434 git for-each-ref --shell --format="%(refname)" >actual &&
435 test_cmp expected actual
438 test_expect_success 'Quoting style: perl' '
439 git for-each-ref --perl --format="%(refname)" >actual &&
440 test_cmp expected actual
443 test_expect_success 'Quoting style: python' '
444 git for-each-ref --python --format="%(refname)" >actual &&
445 test_cmp expected actual
450 "refs/remotes/origin/main"
454 test_expect_success 'Quoting style: tcl' '
455 git for-each-ref --tcl --format="%(refname)" >actual &&
456 test_cmp expected actual
459 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
460 test_expect_success "more than one quoting style: $i" "
461 test_must_fail git for-each-ref $i 2>err &&
462 grep '^error: more than one quoting style' err
466 test_expect_success 'setup for upstream:track[short]' '
470 test_atom head upstream:track '[ahead 1]'
471 test_atom head upstream:trackshort '>'
472 test_atom head upstream:track,nobracket 'ahead 1'
473 test_atom head upstream:nobracket,track 'ahead 1'
475 test_expect_success 'setup for push:track[short]' '
477 git update-ref refs/remotes/myfork/main main &&
481 test_atom head push:track '[behind 1]'
482 test_atom head push:trackshort '<'
484 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
485 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
486 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
489 test_expect_success 'Check that :track[short] works when upstream is invalid' '
490 cat >expected <<-\EOF &&
494 test_when_finished "git config branch.main.merge refs/heads/main" &&
495 git config branch.main.merge refs/heads/does-not-exist &&
497 --format="%(upstream:track)$LF%(upstream:trackshort)" \
498 refs/heads >actual &&
499 test_cmp expected actual
502 test_expect_success 'Check for invalid refname format' '
503 test_must_fail git for-each-ref --format="%(refname:INVALID)"
506 test_expect_success 'set up color tests' '
507 cat >expected.color <<-EOF &&
508 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
509 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
510 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
511 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
512 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
513 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
515 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
516 color_format="%(objectname:short) %(color:green)%(refname:short)"
519 test_expect_success TTY '%(color) shows color with a tty' '
520 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
521 test_decode_color <actual.raw >actual &&
522 test_cmp expected.color actual
525 test_expect_success '%(color) does not show color without tty' '
526 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
527 test_cmp expected.bare actual
530 test_expect_success '--color can override tty check' '
531 git for-each-ref --color --format="$color_format" >actual.raw &&
532 test_decode_color <actual.raw >actual &&
533 test_cmp expected.color actual
536 test_expect_success 'color.ui=always does not override tty check' '
537 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
538 test_cmp expected.bare actual
546 test_expect_success 'Check ambiguous head and tag refs (strict)' '
547 git config --bool core.warnambiguousrefs true &&
548 git checkout -b newtag &&
549 echo "Using $datestamp" > one &&
551 git commit -m "Branch" &&
552 setdate_and_increment &&
553 git tag -m "Tagging at $datestamp" main &&
554 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
555 test_cmp expected actual
563 test_expect_success 'Check ambiguous head and tag refs (loose)' '
564 git config --bool core.warnambiguousrefs false &&
565 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
566 test_cmp expected actual
574 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
576 git tag ambiguous testtag^0 &&
577 git branch ambiguous testtag^0 &&
578 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
579 test_cmp expected actual
582 test_expect_success 'create tag without tagger' '
583 git tag -a -m "Broken tag" taggerless &&
584 git tag -f taggerless $(git cat-file tag taggerless |
585 sed -e "/^tagger /d" |
586 git hash-object --stdin -w -t tag)
589 test_atom refs/tags/taggerless type 'commit'
590 test_atom refs/tags/taggerless tag 'taggerless'
591 test_atom refs/tags/taggerless tagger ''
592 test_atom refs/tags/taggerless taggername ''
593 test_atom refs/tags/taggerless taggeremail ''
594 test_atom refs/tags/taggerless taggeremail:trim ''
595 test_atom refs/tags/taggerless taggeremail:localpart ''
596 test_atom refs/tags/taggerless taggerdate ''
597 test_atom refs/tags/taggerless committer ''
598 test_atom refs/tags/taggerless committername ''
599 test_atom refs/tags/taggerless committeremail ''
600 test_atom refs/tags/taggerless committeremail:trim ''
601 test_atom refs/tags/taggerless committeremail:localpart ''
602 test_atom refs/tags/taggerless committerdate ''
603 test_atom refs/tags/taggerless subject 'Broken tag'
605 test_expect_success 'an unusual tag with an incomplete line' '
607 git tag -m "bogo" bogo &&
608 bogo=$(git cat-file tag bogo) &&
609 bogo=$(printf "%s" "$bogo" | git mktag) &&
610 git tag -f bogo "$bogo" &&
611 git for-each-ref --format "%(body)" refs/tags/bogo
615 test_expect_success 'create tag with subject and body content' '
622 git tag -F msg subject-body
624 test_atom refs/tags/subject-body subject 'the subject line'
625 test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
626 test_atom refs/tags/subject-body body 'first body line
629 test_atom refs/tags/subject-body contents 'the subject line
635 test_expect_success 'create tag with multiline subject' '
643 git tag -F msg multiline
645 test_atom refs/tags/multiline subject 'first subject line second subject line'
646 test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
647 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
648 test_atom refs/tags/multiline body 'first body line
651 test_atom refs/tags/multiline contents:body 'first body line
654 test_atom refs/tags/multiline contents:signature ''
655 test_atom refs/tags/multiline contents 'first subject line
662 test_expect_success GPG 'create signed tags' '
663 git tag -s -m "" signed-empty &&
664 git tag -s -m "subject line" signed-short &&
670 git tag -s -F msg signed-long
673 sig='-----BEGIN PGP SIGNATURE-----
674 -----END PGP SIGNATURE-----
678 test_atom refs/tags/signed-empty subject ''
679 test_atom refs/tags/signed-empty subject:sanitize ''
680 test_atom refs/tags/signed-empty contents:subject ''
681 test_atom refs/tags/signed-empty body "$sig"
682 test_atom refs/tags/signed-empty contents:body ''
683 test_atom refs/tags/signed-empty contents:signature "$sig"
684 test_atom refs/tags/signed-empty contents "$sig"
686 test_atom refs/tags/signed-short subject 'subject line'
687 test_atom refs/tags/signed-short subject:sanitize 'subject-line'
688 test_atom refs/tags/signed-short contents:subject 'subject line'
689 test_atom refs/tags/signed-short body "$sig"
690 test_atom refs/tags/signed-short contents:body ''
691 test_atom refs/tags/signed-short contents:signature "$sig"
692 test_atom refs/tags/signed-short contents "subject line
695 test_atom refs/tags/signed-long subject 'subject line'
696 test_atom refs/tags/signed-long subject:sanitize 'subject-line'
697 test_atom refs/tags/signed-long contents:subject 'subject line'
698 test_atom refs/tags/signed-long body "body contents
700 test_atom refs/tags/signed-long contents:body 'body contents
702 test_atom refs/tags/signed-long contents:signature "$sig"
703 test_atom refs/tags/signed-long contents "subject line
708 test_expect_success 'set up refs pointing to tree and blob' '
709 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
710 git update-ref refs/myblobs/first refs/heads/main:one
713 test_atom refs/mytrees/first subject ""
714 test_atom refs/mytrees/first contents:subject ""
715 test_atom refs/mytrees/first body ""
716 test_atom refs/mytrees/first contents:body ""
717 test_atom refs/mytrees/first contents:signature ""
718 test_atom refs/mytrees/first contents ""
720 test_atom refs/myblobs/first subject ""
721 test_atom refs/myblobs/first contents:subject ""
722 test_atom refs/myblobs/first body ""
723 test_atom refs/myblobs/first contents:body ""
724 test_atom refs/myblobs/first contents:signature ""
725 test_atom refs/myblobs/first contents ""
727 test_expect_success 'set up multiple-sort tags' '
728 for when in 100000 200000
730 for email in user1 user2
734 GIT_COMMITTER_DATE="@$when +0000" \
735 GIT_COMMITTER_EMAIL="$email@example.com" \
736 git tag -m "tag $ref-$when-$email" \
737 multi-$ref-$when-$email || return 1
743 test_expect_success 'Verify sort with multiple keys' '
744 cat >expected <<-\EOF &&
745 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
746 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
747 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
748 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
749 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
750 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
751 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
752 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
755 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
759 "refs/tags/multi-*" >actual &&
760 test_cmp expected actual
763 test_expect_success 'equivalent sorts fall back on refname' '
764 cat >expected <<-\EOF &&
765 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
766 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
767 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
768 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
769 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
770 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
771 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
772 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
775 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
777 "refs/tags/multi-*" >actual &&
778 test_cmp expected actual
781 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
782 test_when_finished "git checkout main" &&
783 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
784 sed -e "s/^\* / /" actual >expect &&
785 git checkout --orphan orphaned-branch &&
786 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
787 test_cmp expect actual
791 Reviewed-by: A U Thor <author@example.com>
792 Signed-off-by: A U Thor <author@example.com>
793 [ v2 updated patch description ]
799 perl -0pe 's/\n\s+/ /g'
802 test_expect_success 'set up trailers for next test' '
803 echo "Some contents" > two &&
805 git commit -F - <<-EOF
806 trailers: this commit message has trailers
808 Some message contents
814 test_expect_success '%(trailers:unfold) unfolds trailers' '
819 git for-each-ref --format="%(trailers:unfold)" refs/heads/main >actual &&
820 test_cmp expect actual &&
821 git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/main >actual &&
822 test_cmp expect actual
825 test_expect_success '%(trailers:only) shows only "key: value" trailers' '
827 grep -v patch.description <trailers &&
830 git for-each-ref --format="%(trailers:only)" refs/heads/main >actual &&
831 test_cmp expect actual &&
832 git for-each-ref --format="%(contents:trailers:only)" refs/heads/main >actual &&
833 test_cmp expect actual
836 test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
838 grep -v patch.description <trailers | unfold &&
841 git for-each-ref --format="%(trailers:only,unfold)" refs/heads/main >actual &&
842 test_cmp expect actual &&
843 git for-each-ref --format="%(trailers:unfold,only)" refs/heads/main >actual &&
844 test_cmp actual actual &&
845 git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/main >actual &&
846 test_cmp expect actual &&
847 git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/main >actual &&
848 test_cmp actual actual
851 test_expect_success '%(trailers) rejects unknown trailers arguments' '
852 # error message cannot be checked under i18n
853 cat >expect <<-EOF &&
854 fatal: unknown %(trailers) argument: unsupported
856 test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
857 test_i18ncmp expect actual &&
858 test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
859 test_i18ncmp expect actual
862 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
863 cat >expect <<-EOF &&
864 fatal: unrecognized %(contents) argument: trailersonly
866 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
867 test_i18ncmp expect actual
870 test_expect_success 'basic atom: head contents:trailers' '
871 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
872 sanitize_pgp <actual >actual.clean &&
873 # git for-each-ref ends with a blank line
874 cat >expect <<-EOF &&
878 test_cmp expect actual.clean
881 test_expect_success 'trailer parsing not fooled by --- line' '
882 git commit --allow-empty -F - <<-\EOF &&
885 This is the body. The message has a "---" line which would confuse a
886 message+patch parser. But here we know we have only a commit message,
897 echo "trailer: right" &&
900 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
901 test_cmp expect actual
904 test_expect_success 'Add symbolic ref for the following tests' '
905 git symbolic-ref refs/heads/sym refs/heads/main
912 test_expect_success 'Verify usage of %(symref) atom' '
913 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
914 test_cmp expected actual
921 test_expect_success 'Verify usage of %(symref:short) atom' '
922 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
923 test_cmp expected actual
931 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
932 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
933 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
934 test_cmp expected actual &&
936 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
937 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
938 test_cmp expected actual
946 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
947 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
948 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
949 test_cmp expected actual
952 test_expect_success ':remotename and :remoteref' '
953 git init remote-tests &&
956 test_commit initial &&
957 git branch -M main &&
958 git remote add from fifth.coffee:blub &&
959 git config branch.main.remote from &&
960 git config branch.main.merge refs/heads/stable &&
961 git remote add to southridge.audio:repo &&
962 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
963 git config branch.main.pushRemote to &&
964 for pair in "%(upstream)=refs/remotes/from/stable" \
965 "%(upstream:remotename)=from" \
966 "%(upstream:remoteref)=refs/heads/stable" \
967 "%(push)=refs/remotes/to/pushed/main" \
968 "%(push:remotename)=to" \
969 "%(push:remoteref)=refs/heads/pushed/main"
971 echo "${pair#*=}" >expect &&
972 git for-each-ref --format="${pair%=*}" \
973 refs/heads/main >actual &&
974 test_cmp expect actual
976 git branch push-simple &&
977 git config branch.push-simple.pushRemote from &&
978 actual="$(git for-each-ref \
979 --format="%(push:remotename),%(push:remoteref)" \
980 refs/heads/push-simple)" &&
981 test from, = "$actual"
985 test_expect_success 'for-each-ref --ignore-case ignores case' '
986 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
987 test_must_be_empty actual &&
989 echo refs/heads/main >expect &&
990 git for-each-ref --format="%(refname)" --ignore-case \
991 refs/heads/MAIN >actual &&
992 test_cmp expect actual
995 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
996 # name refs numerically to avoid case-insensitive filesystem conflicts
1000 for subject in a A b B
1002 GIT_COMMITTER_EMAIL="$email@example.com" \
1003 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1008 git for-each-ref --ignore-case \
1009 --format="%(taggeremail) %(subject) %(refname)" \
1012 --sort=taggeremail \
1013 refs/tags/icase-* >actual &&
1014 cat >expect <<-\EOF &&
1015 <a@example.com> tag a refs/tags/icase-00
1016 <a@example.com> tag A refs/tags/icase-01
1017 <A@example.com> tag a refs/tags/icase-04
1018 <A@example.com> tag A refs/tags/icase-05
1019 <a@example.com> tag b refs/tags/icase-02
1020 <a@example.com> tag B refs/tags/icase-03
1021 <A@example.com> tag b refs/tags/icase-06
1022 <A@example.com> tag B refs/tags/icase-07
1023 <b@example.com> tag a refs/tags/icase-08
1024 <b@example.com> tag A refs/tags/icase-09
1025 <B@example.com> tag a refs/tags/icase-12
1026 <B@example.com> tag A refs/tags/icase-13
1027 <b@example.com> tag b refs/tags/icase-10
1028 <b@example.com> tag B refs/tags/icase-11
1029 <B@example.com> tag b refs/tags/icase-14
1030 <B@example.com> tag B refs/tags/icase-15
1032 test_cmp expect actual