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_trailer_option () {
820 test_expect_success "$title" '
821 git for-each-ref --format="%($option)" refs/heads/main >actual &&
822 test_cmp expect actual &&
823 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
824 test_cmp expect actual
828 test_trailer_option '%(trailers:unfold) unfolds trailers' \
829 'trailers:unfold' <<-EOF
834 test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
835 'trailers:only' <<-EOF
836 $(grep -v patch.description <trailers)
840 test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \
841 'trailers:only=no,only=true' <<-EOF
842 $(grep -v patch.description <trailers)
846 test_trailer_option '%(trailers:only=yes) shows only "key: value" trailers' \
847 'trailers:only=yes' <<-EOF
848 $(grep -v patch.description <trailers)
852 test_trailer_option '%(trailers:only=no) shows all trailers' \
853 'trailers:only=no' <<-EOF
858 test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
859 'trailers:only,unfold' <<-EOF
860 $(grep -v patch.description <trailers | unfold)
864 test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
865 'trailers:unfold,only' <<-EOF
866 $(grep -v patch.description <trailers | unfold)
870 test_trailer_option '%(trailers:key=foo) shows that trailer' \
871 'trailers:key=Signed-off-by' <<-EOF
872 Signed-off-by: A U Thor <author@example.com>
876 test_trailer_option '%(trailers:key=foo) is case insensitive' \
877 'trailers:key=SiGned-oFf-bY' <<-EOF
878 Signed-off-by: A U Thor <author@example.com>
882 test_trailer_option '%(trailers:key=foo:) trailing colon also works' \
883 'trailers:key=Signed-off-by:' <<-EOF
884 Signed-off-by: A U Thor <author@example.com>
888 test_trailer_option '%(trailers:key=foo) multiple keys' \
889 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
890 Reviewed-by: A U Thor <author@example.com>
891 Signed-off-by: A U Thor <author@example.com>
895 test_trailer_option '%(trailers:key=nonexistent) becomes empty' \
896 'trailers:key=Shined-off-by:' <<-EOF
900 test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \
901 'trailers:key=Acked-by' <<-EOF
902 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
906 test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
907 'trailers:key=Signed-Off-by,unfold' <<-EOF
908 $(unfold <trailers | grep Signed-off-by)
912 test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
913 'trailers:key=Signed-off-by,only=no' <<-EOF
914 Signed-off-by: A U Thor <author@example.com>
915 $(grep patch.description <trailers)
919 test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \
920 'trailers:key=Signed-off-by,valueonly' <<-EOF
921 A U Thor <author@example.com>
925 test_trailer_option '%(trailers:separator) changes separator' \
926 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
927 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
930 test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \
931 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
932 Reviewed-by,A U Thor <author@example.com>
933 Signed-off-by,A U Thor <author@example.com>
937 test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \
938 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
939 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
942 test_failing_trailer_option () {
945 test_expect_success "$title" '
946 # error message cannot be checked under i18n
947 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
948 test_cmp expect actual &&
949 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
950 test_cmp expect actual
954 test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
955 'trailers:unsupported' <<-\EOF
956 fatal: unknown %(trailers) argument: unsupported
959 test_failing_trailer_option '%(trailers:key) without value is error' \
960 'trailers:key' <<-\EOF
961 fatal: expected %(trailers:key=<value>)
964 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
965 cat >expect <<-EOF &&
966 fatal: unrecognized %(contents) argument: trailersonly
968 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
969 test_cmp expect actual
972 test_expect_success 'basic atom: head contents:trailers' '
973 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
974 sanitize_pgp <actual >actual.clean &&
975 # git for-each-ref ends with a blank line
976 cat >expect <<-EOF &&
980 test_cmp expect actual.clean
983 test_expect_success 'trailer parsing not fooled by --- line' '
984 git commit --allow-empty -F - <<-\EOF &&
987 This is the body. The message has a "---" line which would confuse a
988 message+patch parser. But here we know we have only a commit message,
999 echo "trailer: right" &&
1002 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
1003 test_cmp expect actual
1006 test_expect_success 'Add symbolic ref for the following tests' '
1007 git symbolic-ref refs/heads/sym refs/heads/main
1014 test_expect_success 'Verify usage of %(symref) atom' '
1015 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1016 test_cmp expected actual
1023 test_expect_success 'Verify usage of %(symref:short) atom' '
1024 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1025 test_cmp expected actual
1033 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1034 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1035 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
1036 test_cmp expected actual &&
1038 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1039 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
1040 test_cmp expected actual
1048 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1049 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1050 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1051 test_cmp expected actual
1054 test_expect_success ':remotename and :remoteref' '
1055 git init remote-tests &&
1058 test_commit initial &&
1059 git branch -M main &&
1060 git remote add from fifth.coffee:blub &&
1061 git config branch.main.remote from &&
1062 git config branch.main.merge refs/heads/stable &&
1063 git remote add to southridge.audio:repo &&
1064 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
1065 git config branch.main.pushRemote to &&
1066 for pair in "%(upstream)=refs/remotes/from/stable" \
1067 "%(upstream:remotename)=from" \
1068 "%(upstream:remoteref)=refs/heads/stable" \
1069 "%(push)=refs/remotes/to/pushed/main" \
1070 "%(push:remotename)=to" \
1071 "%(push:remoteref)=refs/heads/pushed/main"
1073 echo "${pair#*=}" >expect &&
1074 git for-each-ref --format="${pair%=*}" \
1075 refs/heads/main >actual &&
1076 test_cmp expect actual
1078 git branch push-simple &&
1079 git config branch.push-simple.pushRemote from &&
1080 actual="$(git for-each-ref \
1081 --format="%(push:remotename),%(push:remoteref)" \
1082 refs/heads/push-simple)" &&
1083 test from, = "$actual"
1087 test_expect_success 'for-each-ref --ignore-case ignores case' '
1088 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
1089 test_must_be_empty actual &&
1091 echo refs/heads/main >expect &&
1092 git for-each-ref --format="%(refname)" --ignore-case \
1093 refs/heads/MAIN >actual &&
1094 test_cmp expect actual
1097 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1098 # name refs numerically to avoid case-insensitive filesystem conflicts
1100 for email in a A b B
1102 for subject in a A b B
1104 GIT_COMMITTER_EMAIL="$email@example.com" \
1105 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1110 git for-each-ref --ignore-case \
1111 --format="%(taggeremail) %(subject) %(refname)" \
1114 --sort=taggeremail \
1115 refs/tags/icase-* >actual &&
1116 cat >expect <<-\EOF &&
1117 <a@example.com> tag a refs/tags/icase-00
1118 <a@example.com> tag A refs/tags/icase-01
1119 <A@example.com> tag a refs/tags/icase-04
1120 <A@example.com> tag A refs/tags/icase-05
1121 <a@example.com> tag b refs/tags/icase-02
1122 <a@example.com> tag B refs/tags/icase-03
1123 <A@example.com> tag b refs/tags/icase-06
1124 <A@example.com> tag B refs/tags/icase-07
1125 <b@example.com> tag a refs/tags/icase-08
1126 <b@example.com> tag A refs/tags/icase-09
1127 <B@example.com> tag a refs/tags/icase-12
1128 <B@example.com> tag A refs/tags/icase-13
1129 <b@example.com> tag b refs/tags/icase-10
1130 <b@example.com> tag B refs/tags/icase-11
1131 <B@example.com> tag b refs/tags/icase-14
1132 <B@example.com> tag B refs/tags/icase-15
1134 test_cmp expect actual
1137 test_expect_success 'for-each-ref reports broken tags' '
1138 git tag -m "good tag" broken-tag-good HEAD &&
1139 git cat-file tag broken-tag-good >good &&
1140 sed s/commit/blob/ <good >bad &&
1141 bad=$(git hash-object -w -t tag bad) &&
1142 git update-ref refs/tags/broken-tag-bad $bad &&
1143 test_must_fail git for-each-ref --format="%(*objectname)" \
1144 refs/tags/broken-tag-*