3 # Copyright (c) 2007 Andy Parkins
6 test_description
='for-each-ref test'
9 GNUPGHOME_NOT_USED
=$GNUPGHOME
10 .
"$TEST_DIRECTORY"/lib-gpg.sh
11 .
"$TEST_DIRECTORY"/lib-terminal.sh
13 # Mon Jul 3 23:18:43 2006 +0000
15 setdate_and_increment
() {
16 GIT_COMMITTER_DATE
="$datestamp +0200"
17 datestamp
=$
(expr "$datestamp" + 1)
18 GIT_AUTHOR_DATE
="$datestamp +0200"
19 datestamp
=$
(expr "$datestamp" + 1)
20 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
23 test_expect_success setup
'
24 test_oid_cache <<-EOF &&
28 setdate_and_increment &&
29 echo "Using $datestamp" > one &&
31 git commit -m "Initial" &&
33 setdate_and_increment &&
34 git tag -a -m "Tagging at $datestamp" testtag &&
35 git update-ref refs/remotes/origin/main main &&
36 git remote add origin nowhere &&
37 git config branch.main.remote origin &&
38 git config branch.main.merge refs/heads/main &&
39 git remote add myfork elsewhere &&
40 git config remote.pushdefault myfork &&
41 git config push.default current
46 head) ref
=refs
/heads
/main
;;
47 tag
) ref
=refs
/tags
/testtag
;;
48 sym
) ref
=refs
/heads
/sym
;;
51 printf '%s\n' "$3" >expected
52 test_expect_
${4:-success} $PREREQ "basic atom: $1 $2" "
53 git for-each-ref --format='%($2)' $ref >actual &&
54 sanitize_pgp <actual >actual.clean &&
55 test_cmp expected actual.clean
57 # Automatically test "contents:size" atom after testing "contents"
58 if test "$2" = "contents"
60 # for commit leg, $3 is changed there
61 expect
=$
(printf '%s' "$3" |
wc -c)
62 test_expect_
${4:-success} $PREREQ "basic atom: $1 contents:size" '
63 type=$(git cat-file -t "$ref") &&
66 # We cannot use $3 as it expects sanitize_pgp to run
67 git cat-file tag $ref >out &&
68 expect=$(tail -n +6 out | wc -c) &&
73 : "use the calculated expect" ;;
75 BUG "unknown object type" ;;
77 # Leave $expect unquoted to lose possible leading whitespaces
78 echo $expect >expected &&
79 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
80 test_cmp expected actual
85 hexlen
=$
(test_oid hexsz
)
86 disklen
=$
(test_oid disklen
)
88 test_atom
head refname refs
/heads
/main
89 test_atom
head refname
: refs
/heads
/main
90 test_atom
head refname
:short main
91 test_atom
head refname
:lstrip
=1 heads
/main
92 test_atom
head refname
:lstrip
=2 main
93 test_atom
head refname
:lstrip
=-1 main
94 test_atom
head refname
:lstrip
=-2 heads
/main
95 test_atom
head refname
:rstrip
=1 refs
/heads
96 test_atom
head refname
:rstrip
=2 refs
97 test_atom
head refname
:rstrip
=-1 refs
98 test_atom
head refname
:rstrip
=-2 refs
/heads
99 test_atom
head refname
:strip
=1 heads
/main
100 test_atom
head refname
:strip
=2 main
101 test_atom
head refname
:strip
=-1 main
102 test_atom
head refname
:strip
=-2 heads
/main
103 test_atom
head upstream refs
/remotes
/origin
/main
104 test_atom
head upstream
:short origin
/main
105 test_atom
head upstream
:lstrip
=2 origin
/main
106 test_atom
head upstream
:lstrip
=-2 origin
/main
107 test_atom
head upstream
:rstrip
=2 refs
/remotes
108 test_atom
head upstream
:rstrip
=-2 refs
/remotes
109 test_atom
head upstream
:strip
=2 origin
/main
110 test_atom
head upstream
:strip
=-2 origin
/main
111 test_atom
head push refs
/remotes
/myfork
/main
112 test_atom
head push
:short myfork
/main
113 test_atom
head push
:lstrip
=1 remotes
/myfork
/main
114 test_atom
head push
:lstrip
=-1 main
115 test_atom
head push
:rstrip
=1 refs
/remotes
/myfork
116 test_atom
head push
:rstrip
=-1 refs
117 test_atom
head push
:strip
=1 remotes
/myfork
/main
118 test_atom
head push
:strip
=-1 main
119 test_atom
head objecttype commit
120 test_atom
head objectsize $
((131 + hexlen
))
121 test_atom
head objectsize
:disk
$disklen
122 test_atom
head deltabase
$ZERO_OID
123 test_atom
head objectname $
(git rev-parse refs
/heads
/main
)
124 test_atom
head objectname
:short $
(git rev-parse
--short refs
/heads
/main
)
125 test_atom
head objectname
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/main
)
126 test_atom
head objectname
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/main
)
127 test_atom
head tree $
(git rev-parse refs
/heads
/main^
{tree
})
128 test_atom
head tree
:short $
(git rev-parse
--short refs
/heads
/main^
{tree
})
129 test_atom
head tree
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/main^
{tree
})
130 test_atom
head tree
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/main^
{tree
})
131 test_atom
head parent
''
132 test_atom
head parent
:short
''
133 test_atom
head parent
:short
=1 ''
134 test_atom
head parent
:short
=10 ''
135 test_atom
head numparent
0
136 test_atom
head object
''
137 test_atom
head type ''
138 test_atom
head raw
"$(git cat-file commit refs/heads/main)
140 test_atom
head '*objectname' ''
141 test_atom
head '*objecttype' ''
142 test_atom
head author
'A U Thor <author@example.com> 1151968724 +0200'
143 test_atom
head authorname
'A U Thor'
144 test_atom
head authoremail
'<author@example.com>'
145 test_atom
head authoremail
:trim
'author@example.com'
146 test_atom
head authoremail
:localpart
'author'
147 test_atom
head authordate
'Tue Jul 4 01:18:44 2006 +0200'
148 test_atom
head committer
'C O Mitter <committer@example.com> 1151968723 +0200'
149 test_atom
head committername
'C O Mitter'
150 test_atom
head committeremail
'<committer@example.com>'
151 test_atom
head committeremail
:trim
'committer@example.com'
152 test_atom
head committeremail
:localpart
'committer'
153 test_atom
head committerdate
'Tue Jul 4 01:18:43 2006 +0200'
154 test_atom
head tag
''
155 test_atom
head tagger
''
156 test_atom
head taggername
''
157 test_atom
head taggeremail
''
158 test_atom
head taggeremail
:trim
''
159 test_atom
head taggeremail
:localpart
''
160 test_atom
head taggerdate
''
161 test_atom
head creator
'C O Mitter <committer@example.com> 1151968723 +0200'
162 test_atom
head creatordate
'Tue Jul 4 01:18:43 2006 +0200'
163 test_atom
head subject
'Initial'
164 test_atom
head subject
:sanitize
'Initial'
165 test_atom
head contents
:subject
'Initial'
166 test_atom
head body
''
167 test_atom
head contents
:body
''
168 test_atom
head contents
:signature
''
169 test_atom
head contents
'Initial
171 test_atom
head HEAD
'*'
173 test_atom tag refname refs
/tags
/testtag
174 test_atom tag refname
:short testtag
175 test_atom tag upstream
''
176 test_atom tag push
''
177 test_atom tag objecttype tag
178 test_atom tag objectsize $
((114 + hexlen
))
179 test_atom tag objectsize
:disk
$disklen
180 test_atom tag
'*objectsize:disk' $disklen
181 test_atom tag deltabase
$ZERO_OID
182 test_atom tag
'*deltabase' $ZERO_OID
183 test_atom tag objectname $
(git rev-parse refs
/tags
/testtag
)
184 test_atom tag objectname
:short $
(git rev-parse
--short refs
/tags
/testtag
)
185 test_atom
head objectname
:short
=1 $
(git rev-parse
--short=1 refs
/heads
/main
)
186 test_atom
head objectname
:short
=10 $
(git rev-parse
--short=10 refs
/heads
/main
)
187 test_atom tag tree
''
188 test_atom tag tree
:short
''
189 test_atom tag tree
:short
=1 ''
190 test_atom tag tree
:short
=10 ''
191 test_atom tag parent
''
192 test_atom tag parent
:short
''
193 test_atom tag parent
:short
=1 ''
194 test_atom tag parent
:short
=10 ''
195 test_atom tag numparent
''
196 test_atom tag object $
(git rev-parse refs
/tags
/testtag^
0)
197 test_atom tag
type 'commit'
198 test_atom tag
'*objectname' $
(git rev-parse refs
/tags
/testtag^
{})
199 test_atom tag
'*objecttype' 'commit'
200 test_atom tag author
''
201 test_atom tag authorname
''
202 test_atom tag authoremail
''
203 test_atom tag authoremail
:trim
''
204 test_atom tag authoremail
:localpart
''
205 test_atom tag authordate
''
206 test_atom tag committer
''
207 test_atom tag committername
''
208 test_atom tag committeremail
''
209 test_atom tag committeremail
:trim
''
210 test_atom tag committeremail
:localpart
''
211 test_atom tag committerdate
''
212 test_atom tag tag
'testtag'
213 test_atom tag tagger
'C O Mitter <committer@example.com> 1151968725 +0200'
214 test_atom tag taggername
'C O Mitter'
215 test_atom tag taggeremail
'<committer@example.com>'
216 test_atom tag taggeremail
:trim
'committer@example.com'
217 test_atom tag taggeremail
:localpart
'committer'
218 test_atom tag taggerdate
'Tue Jul 4 01:18:45 2006 +0200'
219 test_atom tag creator
'C O Mitter <committer@example.com> 1151968725 +0200'
220 test_atom tag creatordate
'Tue Jul 4 01:18:45 2006 +0200'
221 test_atom tag subject
'Tagging at 1151968727'
222 test_atom tag subject
:sanitize
'Tagging-at-1151968727'
223 test_atom tag contents
:subject
'Tagging at 1151968727'
224 test_atom tag body
''
225 test_atom tag contents
:body
''
226 test_atom tag contents
:signature
''
227 test_atom tag contents
'Tagging at 1151968727
229 test_atom tag HEAD
' '
231 test_expect_success
'basic atom: refs/tags/testtag *raw' '
232 git cat-file commit refs/tags/testtag^{} >expected &&
233 git for-each-ref --format="%(*raw)" refs/tags/testtag >actual &&
234 sanitize_pgp <expected >expected.clean &&
235 echo >>expected.clean &&
236 sanitize_pgp <actual >actual.clean &&
237 test_cmp expected.clean actual.clean
240 test_expect_success
'Check invalid atoms names are errors' '
241 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
244 test_expect_success
'Check format specifiers are ignored in naming date atoms' '
245 git for-each-ref --format="%(authordate)" refs/heads &&
246 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
247 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
248 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
251 test_expect_success
'Check valid format specifiers for date fields' '
252 git for-each-ref --format="%(authordate:default)" refs/heads &&
253 git for-each-ref --format="%(authordate:relative)" refs/heads &&
254 git for-each-ref --format="%(authordate:short)" refs/heads &&
255 git for-each-ref --format="%(authordate:local)" refs/heads &&
256 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
257 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
260 test_expect_success
'Check invalid format specifiers are errors' '
261 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
264 test_expect_success
'arguments to %(objectname:short=) must be positive integers' '
265 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
266 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
267 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
275 cat >expected
<<-EOF &&
276 'refs/heads/main' '$committer_date' '$author_date'
277 'refs/tags/testtag' '$tagger_date'
280 git for-each-ref
--shell \
281 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
283 git for-each-ref
--shell \
284 --format="%(refname) %(taggerdate${f:+:$f})" \
287 test_cmp expected actual
290 test_expect_success
'Check unformatted date fields output' '
292 "Tue Jul 4 01:18:43 2006 +0200" \
293 "Tue Jul 4 01:18:44 2006 +0200" \
294 "Tue Jul 4 01:18:45 2006 +0200"
297 test_expect_success
'Check format "default" formatted date fields output' '
299 "Tue Jul 4 01:18:43 2006 +0200" \
300 "Tue Jul 4 01:18:44 2006 +0200" \
301 "Tue Jul 4 01:18:45 2006 +0200"
304 test_expect_success
'Check format "default-local" date fields output' '
305 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"
308 # Don't know how to do relative check because I can't know when this script
309 # is going to be run and can't fake the current time to git, and hence can't
310 # provide expected output. Instead, I'll just make sure that "relative"
311 # doesn't exit in error
312 test_expect_success
'Check format "relative" date fields output' '
314 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
315 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
318 # We just check that this is the same as "relative" for now.
319 test_expect_success
'Check format "relative-local" date fields output' '
320 test_date relative-local \
321 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
322 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
323 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
326 test_expect_success
'Check format "short" date fields output' '
327 test_date short 2006-07-04 2006-07-04 2006-07-04
330 test_expect_success
'Check format "short-local" date fields output' '
331 test_date short-local 2006-07-03 2006-07-03 2006-07-03
334 test_expect_success
'Check format "local" date fields output' '
336 "Mon Jul 3 23:18:43 2006" \
337 "Mon Jul 3 23:18:44 2006" \
338 "Mon Jul 3 23:18:45 2006"
341 test_expect_success
'Check format "iso8601" date fields output' '
343 "2006-07-04 01:18:43 +0200" \
344 "2006-07-04 01:18:44 +0200" \
345 "2006-07-04 01:18:45 +0200"
348 test_expect_success
'Check format "iso8601-local" date fields output' '
349 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"
352 test_expect_success
'Check format "rfc2822" date fields output' '
354 "Tue, 4 Jul 2006 01:18:43 +0200" \
355 "Tue, 4 Jul 2006 01:18:44 +0200" \
356 "Tue, 4 Jul 2006 01:18:45 +0200"
359 test_expect_success
'Check format "rfc2822-local" date fields output' '
360 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"
363 test_expect_success
'Check format "raw" date fields output' '
364 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
367 test_expect_success
'Check format "raw-local" date fields output' '
368 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
371 test_expect_success
'Check format of strftime date fields' '
372 echo "my date is 2006-07-04" >expected &&
374 --format="%(authordate:format:my date is %Y-%m-%d)" \
375 refs/heads >actual &&
376 test_cmp expected actual
379 test_expect_success
'Check format of strftime-local date fields' '
380 echo "my date is 2006-07-03" >expected &&
382 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
383 refs/heads >actual &&
384 test_cmp expected actual
387 test_expect_success
'exercise strftime with odd fields' '
389 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
390 test_cmp expected actual &&
391 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
392 echo $long >expected &&
393 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
394 test_cmp expected actual
399 refs
/remotes
/origin
/main
403 test_expect_success
'Verify ascending sort' '
404 git for-each-ref --format="%(refname)" --sort=refname >actual &&
405 test_cmp expected actual
411 refs
/remotes
/origin
/main
415 test_expect_success
'Verify descending sort' '
416 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
417 test_cmp expected actual
420 test_expect_success
'Give help even with invalid sort atoms' '
421 test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
422 grep "^usage: git for-each-ref" actual
430 test_expect_success
'exercise patterns with prefixes' '
432 test_when_finished "git tag -d testtag-2" &&
433 git for-each-ref --format="%(refname)" \
434 refs/tags/testtag refs/tags/testtag-2 >actual &&
435 test_cmp expected actual
443 test_expect_success
'exercise glob patterns with prefixes' '
445 test_when_finished "git tag -d testtag-2" &&
446 git for-each-ref --format="%(refname)" \
447 refs/tags/testtag "refs/tags/testtag-*" >actual &&
448 test_cmp expected actual
457 test_expect_success
'exercise patterns with prefix exclusions' '
458 for tag in foo/one foo/two foo/three bar baz
460 git tag "$tag" || return 1
462 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
463 git for-each-ref --format="%(refname)" \
464 refs/tags/ --exclude=refs/tags/foo >actual &&
465 test_cmp expected actual
475 test_expect_success
'exercise patterns with pattern exclusions' '
476 for tag in foo/one foo/two foo/three bar baz
478 git tag "$tag" || return 1
480 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
481 git for-each-ref --format="%(refname)" \
482 refs/tags/ --exclude="refs/tags/foo/t*" >actual &&
483 test_cmp expected actual
488 'refs/remotes/origin/main'
492 test_expect_success
'Quoting style: shell' '
493 git for-each-ref --shell --format="%(refname)" >actual &&
494 test_cmp expected actual
497 test_expect_success
'Quoting style: perl' '
498 git for-each-ref --perl --format="%(refname)" >actual &&
499 test_cmp expected actual
502 test_expect_success
'Quoting style: python' '
503 git for-each-ref --python --format="%(refname)" >actual &&
504 test_cmp expected actual
509 "refs/remotes/origin/main"
513 test_expect_success
'Quoting style: tcl' '
514 git for-each-ref --tcl --format="%(refname)" >actual &&
515 test_cmp expected actual
518 for i
in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
519 test_expect_success
"more than one quoting style: $i" "
520 test_must_fail git for-each-ref $i 2>err &&
521 grep '^error: more than one quoting style' err
525 test_expect_success
'setup for upstream:track[short]' '
529 test_atom
head upstream
:track
'[ahead 1]'
530 test_atom
head upstream
:trackshort
'>'
531 test_atom
head upstream
:track
,nobracket
'ahead 1'
532 test_atom
head upstream
:nobracket
,track
'ahead 1'
534 test_expect_success
'setup for push:track[short]' '
536 git update-ref refs/remotes/myfork/main main &&
540 test_atom
head push
:track
'[behind 1]'
541 test_atom
head push
:trackshort
'<'
543 test_expect_success
'Check that :track[short] cannot be used with other atoms' '
544 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
545 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
548 test_expect_success
'Check that :track[short] works when upstream is invalid' '
549 cat >expected <<-\EOF &&
553 test_when_finished "git config branch.main.merge refs/heads/main" &&
554 git config branch.main.merge refs/heads/does-not-exist &&
556 --format="%(upstream:track)$LF%(upstream:trackshort)" \
557 refs/heads >actual &&
558 test_cmp expected actual
561 test_expect_success
'Check for invalid refname format' '
562 test_must_fail git for-each-ref --format="%(refname:INVALID)"
565 test_expect_success
'set up color tests' '
566 cat >expected.color <<-EOF &&
567 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
568 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
569 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
570 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
571 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
572 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
574 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
575 color_format="%(objectname:short) %(color:green)%(refname:short)"
578 test_expect_success TTY
'%(color) shows color with a tty' '
579 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
580 test_decode_color <actual.raw >actual &&
581 test_cmp expected.color actual
584 test_expect_success
'%(color) does not show color without tty' '
585 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
586 test_cmp expected.bare actual
589 test_expect_success
'--color can override tty check' '
590 git for-each-ref --color --format="$color_format" >actual.raw &&
591 test_decode_color <actual.raw >actual &&
592 test_cmp expected.color actual
595 test_expect_success
'color.ui=always does not override tty check' '
596 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
597 test_cmp expected.bare actual
600 test_expect_success
'setup for describe atom tests' '
601 git init -b master describe-repo &&
605 test_commit --no-tag one &&
608 test_commit --no-tag two &&
609 git tag -a -m "tag two" tagtwo
613 test_expect_success
'describe atom vs git describe' '
617 git for-each-ref --format="%(objectname)" \
621 if desc=$(git describe $hash)
623 : >expect-contains-good
625 : >expect-contains-bad
627 echo "$hash $desc" || return 1
629 test_path_exists expect-contains-good &&
630 test_path_exists expect-contains-bad &&
632 git for-each-ref --format="%(objectname) %(describe)" \
633 refs/tags/ >actual 2>err &&
634 test_cmp expect actual &&
635 test_must_be_empty err
639 test_expect_success
'describe:tags vs describe --tags' '
642 git describe --tags >expect &&
643 git for-each-ref --format="%(describe:tags)" \
644 refs/heads/master >actual &&
645 test_cmp expect actual
649 test_expect_success
'describe:abbrev=... vs describe --abbrev=...' '
653 # Case 1: We have commits between HEAD and the most
654 # recent tag reachable from it
655 test_commit --no-tag file &&
656 git describe --abbrev=14 >expect &&
657 git for-each-ref --format="%(describe:abbrev=14)" \
658 refs/heads/master >actual &&
659 test_cmp expect actual &&
661 # Make sure the hash used is atleast 14 digits long
662 sed -e "s/^.*-g\([0-9a-f]*\)$/\1/" <actual >hexpart &&
663 test 15 -le $(wc -c <hexpart) &&
665 # Case 2: We have a tag at HEAD, describe directly gives
666 # the name of the tag
667 git tag -a -m tagged tagname &&
668 git describe --abbrev=14 >expect &&
669 git for-each-ref --format="%(describe:abbrev=14)" \
670 refs/heads/master >actual &&
671 test_cmp expect actual &&
672 test tagname = $(cat actual)
676 test_expect_success
'describe:match=... vs describe --match ...' '
679 git tag -a -m "tag foo" tag-foo &&
680 git describe --match "*-foo" >expect &&
681 git for-each-ref --format="%(describe:match="*-foo")" \
682 refs/heads/master >actual &&
683 test_cmp expect actual
687 test_expect_success
'describe:exclude:... vs describe --exclude ...' '
690 git tag -a -m "tag bar" tag-bar &&
691 git describe --exclude "*-bar" >expect &&
692 git for-each-ref --format="%(describe:exclude="*-bar")" \
693 refs/heads/master >actual &&
694 test_cmp expect actual
698 test_expect_success
'deref with describe atom' '
701 cat >expect <<-\EOF &&
709 git for-each-ref --format="%(*describe)" >actual &&
710 test_cmp expect actual
714 test_expect_success
'err on bad describe atom arg' '
718 # The bad arg is the only arg passed to describe atom
719 cat >expect <<-\EOF &&
720 fatal: unrecognized %(describe) argument: baz
722 test_must_fail git for-each-ref --format="%(describe:baz)" \
723 refs/heads/master 2>actual &&
724 test_cmp expect actual &&
726 # The bad arg is in the middle of the option string
727 # passed to the describe atom
728 cat >expect <<-\EOF &&
729 fatal: unrecognized %(describe) argument: qux=1,abbrev=14
731 test_must_fail git for-each-ref \
732 --format="%(describe:tags,qux=1,abbrev=14)" \
733 ref/heads/master 2>actual &&
734 test_cmp expect actual
743 test_expect_success
'Check ambiguous head and tag refs (strict)' '
744 git config --bool core.warnambiguousrefs true &&
745 git checkout -b newtag &&
746 echo "Using $datestamp" > one &&
748 git commit -m "Branch" &&
749 setdate_and_increment &&
750 git tag -m "Tagging at $datestamp" main &&
751 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
752 test_cmp expected actual
760 test_expect_success
'Check ambiguous head and tag refs (loose)' '
761 git config --bool core.warnambiguousrefs false &&
762 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
763 test_cmp expected actual
771 test_expect_success
'Check ambiguous head and tag refs II (loose)' '
773 git tag ambiguous testtag^0 &&
774 git branch ambiguous testtag^0 &&
775 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
776 test_cmp expected actual
779 test_expect_success
'create tag without tagger' '
780 git tag -a -m "Broken tag" taggerless &&
781 git tag -f taggerless $(git cat-file tag taggerless |
782 sed -e "/^tagger /d" |
783 git hash-object --literally --stdin -w -t tag)
786 test_atom refs
/tags
/taggerless
type 'commit'
787 test_atom refs
/tags
/taggerless tag
'taggerless'
788 test_atom refs
/tags
/taggerless tagger
''
789 test_atom refs
/tags
/taggerless taggername
''
790 test_atom refs
/tags
/taggerless taggeremail
''
791 test_atom refs
/tags
/taggerless taggeremail
:trim
''
792 test_atom refs
/tags
/taggerless taggeremail
:localpart
''
793 test_atom refs
/tags
/taggerless taggerdate
''
794 test_atom refs
/tags
/taggerless committer
''
795 test_atom refs
/tags
/taggerless committername
''
796 test_atom refs
/tags
/taggerless committeremail
''
797 test_atom refs
/tags
/taggerless committeremail
:trim
''
798 test_atom refs
/tags
/taggerless committeremail
:localpart
''
799 test_atom refs
/tags
/taggerless committerdate
''
800 test_atom refs
/tags
/taggerless subject
'Broken tag'
802 test_expect_success
'an unusual tag with an incomplete line' '
804 git tag -m "bogo" bogo &&
805 bogo=$(git cat-file tag bogo) &&
806 bogo=$(printf "%s" "$bogo" | git mktag) &&
807 git tag -f bogo "$bogo" &&
808 git for-each-ref --format "%(body)" refs/tags/bogo
812 test_expect_success
'create tag with subject and body content' '
819 git tag -F msg subject-body
821 test_atom refs
/tags
/subject-body subject
'the subject line'
822 test_atom refs
/tags
/subject-body subject
:sanitize
'the-subject-line'
823 test_atom refs
/tags
/subject-body body
'first body line
826 test_atom refs
/tags
/subject-body contents
'the subject line
832 test_expect_success
'create tag with multiline subject' '
840 git tag -F msg multiline
842 test_atom refs
/tags
/multiline subject
'first subject line second subject line'
843 test_atom refs
/tags
/multiline subject
:sanitize
'first-subject-line-second-subject-line'
844 test_atom refs
/tags
/multiline contents
:subject
'first subject line second subject line'
845 test_atom refs
/tags
/multiline body
'first body line
848 test_atom refs
/tags
/multiline contents
:body
'first body line
851 test_atom refs
/tags
/multiline contents
:signature
''
852 test_atom refs
/tags
/multiline contents
'first subject line
859 test_expect_success GPG
'create signed tags' '
860 git tag -s -m "" signed-empty &&
861 git tag -s -m "subject line" signed-short &&
867 git tag -s -F msg signed-long
870 sig
='-----BEGIN PGP SIGNATURE-----
871 -----END PGP SIGNATURE-----
875 test_atom refs
/tags
/signed-empty subject
''
876 test_atom refs
/tags
/signed-empty subject
:sanitize
''
877 test_atom refs
/tags
/signed-empty contents
:subject
''
878 test_atom refs
/tags
/signed-empty body
"$sig"
879 test_atom refs
/tags
/signed-empty contents
:body
''
880 test_atom refs
/tags
/signed-empty contents
:signature
"$sig"
881 test_atom refs
/tags
/signed-empty contents
"$sig"
883 test_expect_success GPG
'basic atom: refs/tags/signed-empty raw' '
884 git cat-file tag refs/tags/signed-empty >expected &&
885 git for-each-ref --format="%(raw)" refs/tags/signed-empty >actual &&
886 sanitize_pgp <expected >expected.clean &&
887 echo >>expected.clean &&
888 sanitize_pgp <actual >actual.clean &&
889 test_cmp expected.clean actual.clean
892 test_atom refs
/tags
/signed-short subject
'subject line'
893 test_atom refs
/tags
/signed-short subject
:sanitize
'subject-line'
894 test_atom refs
/tags
/signed-short contents
:subject
'subject line'
895 test_atom refs
/tags
/signed-short body
"$sig"
896 test_atom refs
/tags
/signed-short contents
:body
''
897 test_atom refs
/tags
/signed-short contents
:signature
"$sig"
898 test_atom refs
/tags
/signed-short contents
"subject line
901 test_expect_success GPG
'basic atom: refs/tags/signed-short raw' '
902 git cat-file tag refs/tags/signed-short >expected &&
903 git for-each-ref --format="%(raw)" refs/tags/signed-short >actual &&
904 sanitize_pgp <expected >expected.clean &&
905 echo >>expected.clean &&
906 sanitize_pgp <actual >actual.clean &&
907 test_cmp expected.clean actual.clean
910 test_atom refs
/tags
/signed-long subject
'subject line'
911 test_atom refs
/tags
/signed-long subject
:sanitize
'subject-line'
912 test_atom refs
/tags
/signed-long contents
:subject
'subject line'
913 test_atom refs
/tags
/signed-long body
"body contents
915 test_atom refs
/tags
/signed-long contents
:body
'body contents
917 test_atom refs
/tags
/signed-long contents
:signature
"$sig"
918 test_atom refs
/tags
/signed-long contents
"subject line
923 test_expect_success GPG
'basic atom: refs/tags/signed-long raw' '
924 git cat-file tag refs/tags/signed-long >expected &&
925 git for-each-ref --format="%(raw)" refs/tags/signed-long >actual &&
926 sanitize_pgp <expected >expected.clean &&
927 echo >>expected.clean &&
928 sanitize_pgp <actual >actual.clean &&
929 test_cmp expected.clean actual.clean
932 test_expect_success
'set up refs pointing to tree and blob' '
933 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
934 git update-ref refs/myblobs/first refs/heads/main:one
937 test_atom refs
/mytrees
/first subject
""
938 test_atom refs
/mytrees
/first contents
:subject
""
939 test_atom refs
/mytrees
/first body
""
940 test_atom refs
/mytrees
/first contents
:body
""
941 test_atom refs
/mytrees
/first contents
:signature
""
942 test_atom refs
/mytrees
/first contents
""
944 test_expect_success
'basic atom: refs/mytrees/first raw' '
945 git cat-file tree refs/mytrees/first >expected &&
947 git for-each-ref --format="%(raw)" refs/mytrees/first >actual &&
948 test_cmp expected actual &&
949 git cat-file -s refs/mytrees/first >expected &&
950 git for-each-ref --format="%(raw:size)" refs/mytrees/first >actual &&
951 test_cmp expected actual
954 test_atom refs
/myblobs
/first subject
""
955 test_atom refs
/myblobs
/first contents
:subject
""
956 test_atom refs
/myblobs
/first body
""
957 test_atom refs
/myblobs
/first contents
:body
""
958 test_atom refs
/myblobs
/first contents
:signature
""
959 test_atom refs
/myblobs
/first contents
""
961 test_expect_success
'basic atom: refs/myblobs/first raw' '
962 git cat-file blob refs/myblobs/first >expected &&
964 git for-each-ref --format="%(raw)" refs/myblobs/first >actual &&
965 test_cmp expected actual &&
966 git cat-file -s refs/myblobs/first >expected &&
967 git for-each-ref --format="%(raw:size)" refs/myblobs/first >actual &&
968 test_cmp expected actual
971 test_expect_success
'set up refs pointing to binary blob' '
972 printf "a\0b\0c" >blob1 &&
973 printf "a\0c\0b" >blob2 &&
974 printf "\0a\0b\0c" >blob3 &&
975 printf "abc" >blob4 &&
976 printf "\0 \0 \0 " >blob5 &&
977 printf "\0 \0a\0 " >blob6 &&
980 obj=$(git hash-object -w blob1) &&
981 git update-ref refs/myblobs/blob1 "$obj" &&
982 obj=$(git hash-object -w blob2) &&
983 git update-ref refs/myblobs/blob2 "$obj" &&
984 obj=$(git hash-object -w blob3) &&
985 git update-ref refs/myblobs/blob3 "$obj" &&
986 obj=$(git hash-object -w blob4) &&
987 git update-ref refs/myblobs/blob4 "$obj" &&
988 obj=$(git hash-object -w blob5) &&
989 git update-ref refs/myblobs/blob5 "$obj" &&
990 obj=$(git hash-object -w blob6) &&
991 git update-ref refs/myblobs/blob6 "$obj" &&
992 obj=$(git hash-object -w blob7) &&
993 git update-ref refs/myblobs/blob7 "$obj" &&
994 obj=$(git hash-object -w blob8) &&
995 git update-ref refs/myblobs/blob8 "$obj"
998 test_expect_success
'Verify sorts with raw' '
999 cat >expected <<-EOF &&
1012 git for-each-ref --format="%(refname)" --sort=raw \
1013 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1014 test_cmp expected actual
1017 test_expect_success
'Verify sorts with raw:size' '
1018 cat >expected <<-EOF &&
1031 git for-each-ref --format="%(refname)" --sort=raw:size \
1032 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1033 test_cmp expected actual
1036 test_expect_success
'validate raw atom with %(if:equals)' '
1037 cat >expected <<-EOF &&
1051 git for-each-ref --format="%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)" \
1052 refs/myblobs/ refs/heads/ >actual &&
1053 test_cmp expected actual
1056 test_expect_success
'validate raw atom with %(if:notequals)' '
1057 cat >expected <<-EOF &&
1058 refs/heads/ambiguous
1071 git for-each-ref --format="%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)" \
1072 refs/myblobs/ refs/heads/ >actual &&
1073 test_cmp expected actual
1076 test_expect_success
'empty raw refs with %(if)' '
1077 cat >expected <<-EOF &&
1078 refs/myblobs/blob1 not empty
1079 refs/myblobs/blob2 not empty
1080 refs/myblobs/blob3 not empty
1081 refs/myblobs/blob4 not empty
1082 refs/myblobs/blob5 not empty
1083 refs/myblobs/blob6 not empty
1084 refs/myblobs/blob7 empty
1085 refs/myblobs/blob8 empty
1086 refs/myblobs/first not empty
1088 git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
1089 refs/myblobs/ >actual &&
1090 test_cmp expected actual
1093 test_expect_success
'%(raw) with --python must fail' '
1094 test_must_fail git for-each-ref --format="%(raw)" --python
1097 test_expect_success
'%(raw) with --tcl must fail' '
1098 test_must_fail git for-each-ref --format="%(raw)" --tcl
1101 test_expect_success
'%(raw) with --perl' '
1102 git for-each-ref --format="\$name= %(raw);
1103 print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
1105 git for-each-ref --format="\$name= %(raw);
1106 print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
1108 git for-each-ref --format="\$name= %(raw);
1109 print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
1111 git for-each-ref --format="\$name= %(raw);
1112 print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
1114 git cat-file tree refs/mytrees/first > expected &&
1115 git for-each-ref --format="\$name= %(raw);
1116 print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
1120 test_expect_success
'%(raw) with --shell must fail' '
1121 test_must_fail git for-each-ref --format="%(raw)" --shell
1124 test_expect_success
'%(raw) with --shell and --sort=raw must fail' '
1125 test_must_fail git for-each-ref --format="%(raw)" --sort=raw --shell
1128 test_expect_success
'%(raw:size) with --shell' '
1129 git for-each-ref --format="%(raw:size)" | sed "s/^/$SQ/;s/$/$SQ/" >expect &&
1130 git for-each-ref --format="%(raw:size)" --shell >actual &&
1131 test_cmp expect actual
1134 test_expect_success
'for-each-ref --format compare with cat-file --batch' '
1135 git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
1136 git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)
1137 %(raw)" refs/mytrees/first >actual &&
1138 test_cmp expected actual
1141 test_expect_success
'verify sorts with contents:size' '
1142 cat >expect <<-\EOF &&
1145 refs/heads/ambiguous
1147 git for-each-ref --format="%(refname)" \
1148 --sort=contents:size refs/heads/ >actual &&
1149 test_cmp expect actual
1152 test_expect_success
'set up multiple-sort tags' '
1153 for when in 100000 200000
1155 for email in user1 user2
1157 for ref in ref1 ref2
1159 GIT_COMMITTER_DATE="@$when +0000" \
1160 GIT_COMMITTER_EMAIL="$email@example.com" \
1161 git tag -m "tag $ref-$when-$email" \
1162 multi-$ref-$when-$email || return 1
1168 test_expect_success
'Verify sort with multiple keys' '
1169 cat >expected <<-\EOF &&
1170 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1171 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1172 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1173 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1174 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1175 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1176 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1177 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1180 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1182 --sort=taggeremail \
1184 "refs/tags/multi-*" >actual &&
1185 test_cmp expected actual
1188 test_expect_success
'equivalent sorts fall back on refname' '
1189 cat >expected <<-\EOF &&
1190 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1191 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1192 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1193 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1194 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1195 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1196 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1197 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1200 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1202 "refs/tags/multi-*" >actual &&
1203 test_cmp expected actual
1206 test_expect_success
'--no-sort cancels the previous sort keys' '
1207 cat >expected <<-\EOF &&
1208 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1209 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1210 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1211 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1212 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1213 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1214 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1215 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1218 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1220 --sort=taggeremail \
1223 "refs/tags/multi-*" >actual &&
1224 test_cmp expected actual
1227 test_expect_success
'do not dereference NULL upon %(HEAD) on unborn branch' '
1228 test_when_finished "git checkout main" &&
1229 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1230 sed -e "s/^\* / /" actual >expect &&
1231 git checkout --orphan orphaned-branch &&
1232 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1233 test_cmp expect actual
1237 Reviewed-by: A U Thor <author@example.com>
1238 Signed-off-by: A U Thor <author@example.com>
1239 [ v2 updated patch description ]
1241 <author@example.com>
1245 perl
-0pe 's/\n\s+/ /g'
1248 test_expect_success
'set up trailers for next test' '
1249 echo "Some contents" > two &&
1251 git commit -F - <<-EOF
1252 trailers: this commit message has trailers
1254 Some message contents
1260 test_trailer_option
() {
1263 test_expect_success
"$title" '
1264 git for-each-ref --format="%($option)" refs/heads/main >actual &&
1265 test_cmp expect actual &&
1266 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1267 test_cmp expect actual
1271 test_trailer_option
'%(trailers:unfold) unfolds trailers' \
1272 'trailers:unfold' <<-EOF
1277 test_trailer_option
'%(trailers:only) shows only "key: value" trailers' \
1278 'trailers:only' <<-EOF
1279 $(grep -v patch.description <trailers)
1283 test_trailer_option
'%(trailers:only=no,only=true) shows only "key: value" trailers' \
1284 'trailers:only=no,only=true' <<-EOF
1285 $(grep -v patch.description <trailers)
1289 test_trailer_option
'%(trailers:only=yes) shows only "key: value" trailers' \
1290 'trailers:only=yes' <<-EOF
1291 $(grep -v patch.description <trailers)
1295 test_trailer_option
'%(trailers:only=no) shows all trailers' \
1296 'trailers:only=no' <<-EOF
1301 test_trailer_option
'%(trailers:only) and %(trailers:unfold) work together' \
1302 'trailers:only,unfold' <<-EOF
1303 $(grep -v patch.description <trailers | unfold)
1307 test_trailer_option
'%(trailers:unfold) and %(trailers:only) work together' \
1308 'trailers:unfold,only' <<-EOF
1309 $(grep -v patch.description <trailers | unfold)
1313 test_trailer_option
'%(trailers:key=foo) shows that trailer' \
1314 'trailers:key=Signed-off-by' <<-EOF
1315 Signed-off-by: A U Thor <author@example.com>
1319 test_trailer_option
'%(trailers:key=foo) is case insensitive' \
1320 'trailers:key=SiGned-oFf-bY' <<-EOF
1321 Signed-off-by: A U Thor <author@example.com>
1325 test_trailer_option
'%(trailers:key=foo:) trailing colon also works' \
1326 'trailers:key=Signed-off-by:' <<-EOF
1327 Signed-off-by: A U Thor <author@example.com>
1331 test_trailer_option
'%(trailers:key=foo) multiple keys' \
1332 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
1333 Reviewed-by: A U Thor <author@example.com>
1334 Signed-off-by: A U Thor <author@example.com>
1338 test_trailer_option
'%(trailers:key=nonexistent) becomes empty' \
1339 'trailers:key=Shined-off-by:' <<-EOF
1343 test_trailer_option
'%(trailers:key=foo) handles multiple lines even if folded' \
1344 'trailers:key=Acked-by' <<-EOF
1345 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
1349 test_trailer_option
'%(trailers:key=foo,unfold) properly unfolds' \
1350 'trailers:key=Signed-Off-by,unfold' <<-EOF
1351 $(unfold <trailers | grep Signed-off-by)
1355 test_trailer_option
'%(trailers:key=foo,only=no) also includes nontrailer lines' \
1356 'trailers:key=Signed-off-by,only=no' <<-EOF
1357 Signed-off-by: A U Thor <author@example.com>
1358 $(grep patch.description <trailers)
1362 test_trailer_option
'%(trailers:key=foo,valueonly) shows only value' \
1363 'trailers:key=Signed-off-by,valueonly' <<-EOF
1364 A U Thor <author@example.com>
1368 test_trailer_option
'%(trailers:separator) changes separator' \
1369 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1370 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
1373 test_trailer_option
'%(trailers:key_value_separator) changes key-value separator' \
1374 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1375 Reviewed-by,A U Thor <author@example.com>
1376 Signed-off-by,A U Thor <author@example.com>
1380 test_trailer_option
'%(trailers:separator,key_value_separator) changes both separators' \
1381 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1382 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
1385 test_failing_trailer_option
() {
1388 test_expect_success
"$title" '
1389 # error message cannot be checked under i18n
1390 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
1391 test_cmp expect actual &&
1392 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
1393 test_cmp expect actual
1397 test_failing_trailer_option
'%(trailers) rejects unknown trailers arguments' \
1398 'trailers:unsupported' <<-\EOF
1399 fatal: unknown %(trailers) argument: unsupported
1402 test_failing_trailer_option '%(trailers:key) without value is error' \
1403 'trailers:key' <<-\
EOF
1404 fatal: expected %(trailers:key=<value>)
1407 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
1408 cat >expect <<-EOF &&
1409 fatal: unrecognized %(contents) argument: trailersonly
1411 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
1412 test_cmp expect actual
1415 test_expect_success 'basic atom: head contents:trailers' '
1416 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
1417 sanitize_pgp <actual >actual.clean &&
1418 # git for-each-ref ends with a blank line
1419 cat >expect <<-EOF &&
1423 test_cmp expect actual.clean
1426 test_expect_success 'basic atom: rest must fail' '
1427 test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1430 test_expect_success 'HEAD atom does not take arguments' '
1431 test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
1432 echo "fatal: %(HEAD) does not take arguments" >expect &&
1436 test_expect_success 'subject atom rejects unknown arguments' '
1437 test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
1438 echo "fatal: unrecognized %(subject) argument: foo" >expect &&
1442 test_expect_success 'refname atom rejects unknown arguments' '
1443 test_must_fail git for-each-ref --format="%(refname:foo)" 2>err &&
1444 echo "fatal: unrecognized %(refname) argument: foo" >expect &&
1448 test_expect_success 'trailer parsing not fooled by --- line' '
1449 git commit --allow-empty -F - <<-\EOF &&
1452 This is the body. The message has a "---" line which would confuse a
1453 message+patch parser. But here we know we have only a commit message,
1464 echo "trailer: right" &&
1467 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
1468 test_cmp expect actual
1471 test_expect_success 'Add symbolic ref for the following tests' '
1472 git symbolic-ref refs/heads/sym refs/heads/main
1479 test_expect_success 'Verify usage of %(symref) atom' '
1480 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1481 test_cmp expected actual
1488 test_expect_success 'Verify usage of %(symref:short) atom' '
1489 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1490 test_cmp expected actual
1498 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1499 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1500 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
1501 test_cmp expected actual &&
1503 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1504 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
1505 test_cmp expected actual
1513 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1514 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1515 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1516 test_cmp expected actual
1519 test_expect_success ':remotename and :remoteref' '
1520 git init remote-tests &&
1523 test_commit initial &&
1524 git branch -M main &&
1525 git remote add from fifth.coffee:blub &&
1526 git config branch.main.remote from &&
1527 git config branch.main.merge refs/heads/stable &&
1528 git remote add to southridge.audio:repo &&
1529 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
1530 git config branch.main.pushRemote to &&
1531 for pair in "%(upstream)=refs/remotes/from/stable" \
1532 "%(upstream:remotename)=from" \
1533 "%(upstream:remoteref)=refs/heads/stable" \
1534 "%(push)=refs/remotes/to/pushed/main" \
1535 "%(push:remotename)=to" \
1536 "%(push:remoteref)=refs/heads/pushed/main"
1538 echo "${pair#*=}" >expect &&
1539 git for-each-ref --format="${pair%=*}" \
1540 refs/heads/main >actual &&
1541 test_cmp expect actual || exit 1
1543 git branch push-simple &&
1544 git config branch.push-simple.pushRemote from &&
1545 actual="$(git for-each-ref \
1546 --format="%(push:remotename),%(push:remoteref)" \
1547 refs/heads/push-simple)" &&
1548 test from, = "$actual"
1552 test_expect_success 'for-each-ref --ignore-case ignores case' '
1553 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
1554 test_must_be_empty actual &&
1556 echo refs/heads/main >expect &&
1557 git for-each-ref --format="%(refname)" --ignore-case \
1558 refs/heads/MAIN >actual &&
1559 test_cmp expect actual
1562 test_expect_success 'for-each-ref --omit-empty works' '
1563 git for-each-ref --format="%(refname)" >actual &&
1564 test_line_count -gt 1 actual &&
1565 git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
1566 echo refs/heads/main >expect &&
1567 test_cmp expect actual
1570 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1571 # name refs numerically to avoid case-insensitive filesystem conflicts
1573 for email in a A b B
1575 for subject in a A b B
1577 GIT_COMMITTER_EMAIL="$email@example.com" \
1578 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1583 git for-each-ref --ignore-case \
1584 --format="%(taggeremail) %(subject) %(refname)" \
1587 --sort=taggeremail \
1588 refs/tags/icase-* >actual &&
1589 cat >expect <<-\
EOF &&
1590 <a@example.com> tag a refs/tags/icase-00
1591 <a@example.com> tag A refs/tags/icase-01
1592 <A@example.com> tag a refs/tags/icase-04
1593 <A@example.com> tag A refs/tags/icase-05
1594 <a@example.com> tag b refs/tags/icase-02
1595 <a@example.com> tag B refs/tags/icase-03
1596 <A@example.com> tag b refs/tags/icase-06
1597 <A@example.com> tag B refs/tags/icase-07
1598 <b@example.com> tag a refs/tags/icase-08
1599 <b@example.com> tag A refs/tags/icase-09
1600 <B@example.com> tag a refs/tags/icase-12
1601 <B@example.com> tag A refs/tags/icase-13
1602 <b@example.com> tag b refs/tags/icase-10
1603 <b@example.com> tag B refs/tags/icase-11
1604 <B@example.com> tag b refs/tags/icase-14
1605 <B@example.com> tag B refs/tags/icase-15
1607 test_cmp expect actual
1610 test_expect_success 'for-each-ref reports broken tags' '
1611 git tag -m "good tag" broken-tag-good HEAD &&
1612 git cat-file tag broken-tag-good >good &&
1613 sed s/commit/blob/ <good >bad &&
1614 bad=$(git hash-object -w -t tag bad) &&
1615 git update-ref refs/tags/broken-tag-bad $bad &&
1616 test_must_fail git for-each-ref --format="%(*objectname)" \
1617 refs/tags/broken-tag-*
1620 test_expect_success 'set up tag with signature and no blank lines' '
1621 git tag -F - fake-sig-no-blanks
<<-\EOF
1623 -----BEGIN PGP SIGNATURE-----
1624 not a real signature, but we just care about the
1625 subject/body parsing. It is important here that
1626 there are no blank lines in the signature.
1627 -----END PGP SIGNATURE-----
1631 test_atom refs/tags/fake-sig-no-blanks contents:subject 'this is the subject'
1632 test_atom refs/tags/fake-sig-no-blanks contents:body ''
1633 test_atom refs/tags/fake-sig-no-blanks contents:signature "$sig"
1635 test_expect_success 'set up tag with CRLF signature' '
1638 -----BEGIN PGP SIGNATURE-----
1640 not a real signature, but we just care about
1641 the subject/body parsing. It is important here
1642 that there is a blank line separating this
1643 from the signature header.
1644 -----END PGP SIGNATURE-----
1646 git tag -F - --cleanup=verbatim fake-sig-crlf
1649 test_atom refs/tags/fake-sig-crlf contents:subject 'this is the subject
'
1650 test_atom refs/tags/fake-sig-crlf contents:body ''
1652 # CRLF is retained in the signature, so we have to pass our expected value
1653 # through append_cr. But test_atom requires a shell string, which means command
1654 # substitution, and the shell will strip trailing newlines from the output of
1655 # the substitution. Hack around it by adding and then removing a dummy line.
1656 sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
1657 sig_crlf=${sig_crlf%dummy}
1658 test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
1660 test_expect_success 'git for-each-ref
--stdin: empty
' '
1662 git for-each-ref
--format="%(refname)" --stdin <in >actual
&&
1663 git for-each-ref
--format="%(refname)" >expect
&&
1664 test_cmp expect actual
1667 test_expect_success 'git for-each-ref
--stdin: fails
if extra args
' '
1669 test_must_fail git for-each-ref
--format="%(refname)" \
1670 --stdin refs
/heads
/extra
<in 2>err
&&
1671 grep "unknown arguments supplied with --stdin" err
1674 test_expect_success 'git for-each-ref
--stdin: matches
' '
1680 cat >expect
<<-EOF &&
1681 refs/heads/ambiguous
1682 refs/tags/multi-ref1-100000-user1
1683 refs/tags/multi-ref1-100000-user2
1684 refs/tags/multi-ref1-200000-user1
1685 refs/tags/multi-ref1-200000-user2
1686 refs/tags/multi-ref2-100000-user1
1687 refs/tags/multi-ref2-100000-user2
1688 refs/tags/multi-ref2-200000-user1
1689 refs/tags/multi-ref2-200000-user2
1693 git for-each-ref
--format="%(refname)" --stdin <in >actual
&&
1694 test_cmp expect actual
1697 test_expect_success 'git for-each-ref with non-existing refs
' '
1699 refs/heads/this-ref-does-not-exist
1703 git for-each-ref
--format="%(refname)" --stdin <in >actual
&&
1704 test_must_be_empty actual
&&
1706 xargs git for-each-ref
--format="%(refname)" <in >actual
&&
1707 test_must_be_empty actual
1710 GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1711 TRUSTLEVEL_FORMAT="%(signature:trustlevel)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1713 test_expect_success GPG 'setup
for signature atom using gpg
' '
1714 git checkout
-b signed
&&
1716 test_when_finished
"test_unconfig commit.gpgSign" &&
1721 git commit
-S -m "file: 1" &&
1722 git tag first-signed
&&
1726 git commit
-a -m "file: 2" &&
1727 git tag second-unsigned
&&
1729 git config commit.gpgSign
1 &&
1732 git commit
-a --no-gpg-sign -m "file: 3" &&
1733 git tag third-unsigned
&&
1736 git rebase
-f HEAD^^
&& git tag second-signed HEAD^
&&
1737 git tag third-signed
&&
1741 git commit
-a -SB7227189 -m "file: 4" &&
1742 git tag fourth-signed
&&
1746 git commit
-a --no-gpg-sign -m "file: 5" &&
1747 git tag fifth-unsigned
&&
1751 git commit
-a --no-gpg-sign -m "file: 6" &&
1754 git rebase
-f HEAD^^
&&
1755 git tag fifth-signed HEAD^
&&
1756 git tag sixth-signed
&&
1760 git commit
-a --no-gpg-sign -m "file: 7" &&
1761 git tag seventh-unsigned
1764 test_expect_success GPGSSH 'setup
for signature atom using
ssh' '
1765 test_when_finished
"test_unconfig gpg.format user.signingkey" &&
1767 test_config gpg.format
ssh &&
1768 test_config user.signingkey
"${GPGSSH_KEY_PRIMARY}" &&
1772 git commit
-S -m "file: 8" &&
1773 git tag eighth-signed-ssh
1776 test_expect_success GPG2 'bare signature atom
' '
1777 git verify-commit first-signed
2>expect
&&
1779 git for-each-ref refs
/tags
/first-signed \
1780 --format="%(signature)" >actual
&&
1781 test_cmp expect actual
1784 test_expect_success GPG 'show good signature with custom format
' '
1785 git verify-commit first-signed
&&
1786 cat >expect
<<-\EOF &&
1789 C O Mitter <committer@example.com>
1790 73D758744BE721698EC54E8713B6F51ECDDE430D
1791 73D758744BE721698EC54E8713B6F51ECDDE430D
1793 git for-each-ref refs/tags/first-signed \
1794 --format="$GRADE_FORMAT" >actual &&
1795 test_cmp expect actual
1797 test_expect_success GPGSSH 'show good signature with custom format
1799 test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
1800 FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
1801 cat >expect.tmpl <<-\
EOF &&
1804 principal with number 1
1808 sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
1809 git for-each-ref refs/tags/eighth-signed-ssh \
1810 --format="$GRADE_FORMAT" >actual &&
1811 test_cmp expect actual
1814 test_expect_success GPG 'signature atom with grade option and bad signature' '
1815 git cat-file commit third-signed >raw &&
1816 sed -e "s/^file: 3/file: 3 forged/" raw >forged1 &&
1817 FORGED1=$(git hash-object -w -t commit forged1) &&
1818 git update-ref refs/tags/third-signed "$FORGED1" &&
1819 test_must_fail git verify-commit "$FORGED1" &&
1821 cat >expect <<-\EOF &&
1824 C O Mitter <committer@example.com>
1828 git for-each-ref refs/tags/third-signed \
1829 --format="$GRADE_FORMAT" >actual &&
1830 test_cmp expect actual
1833 test_expect_success GPG 'show untrusted signature with custom format' '
1834 cat >expect <<-\EOF &&
1837 Eris Discordia <discord@example.net>
1838 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1839 D4BE22311AD3131E5EDA29A461092E85B7227189
1841 git for-each-ref refs/tags/fourth-signed \
1842 --format="$GRADE_FORMAT" >actual &&
1843 test_cmp expect actual
1846 test_expect_success GPG 'show untrusted signature with undefined trust level' '
1847 cat >expect <<-\EOF &&
1850 Eris Discordia <discord@example.net>
1851 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1852 D4BE22311AD3131E5EDA29A461092E85B7227189
1854 git for-each-ref refs/tags/fourth-signed \
1855 --format="$TRUSTLEVEL_FORMAT" >actual &&
1856 test_cmp expect actual
1859 test_expect_success GPG 'show untrusted signature with ultimate trust level' '
1860 cat >expect <<-\EOF &&
1863 C O Mitter <committer@example.com>
1864 73D758744BE721698EC54E8713B6F51ECDDE430D
1865 73D758744BE721698EC54E8713B6F51ECDDE430D
1867 git for-each-ref refs/tags/sixth-signed \
1868 --format="$TRUSTLEVEL_FORMAT" >actual &&
1869 test_cmp expect actual
1872 test_expect_success GPG 'show unknown signature with custom format' '
1873 cat >expect <<-\EOF &&
1880 GNUPGHOME="$GNUPGHOME_NOT_USED" git for-each-ref \
1881 refs/tags/sixth-signed --format="$GRADE_FORMAT" >actual &&
1882 test_cmp expect actual
1885 test_expect_success GPG 'show lack of signature with custom format' '
1886 cat >expect <<-\EOF &&
1893 git for-each-ref refs/tags/seventh-unsigned \
1894 --format="$GRADE_FORMAT" >actual &&
1895 test_cmp expect actual