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
'set up multiple-sort tags' '
1142 for when in 100000 200000
1144 for email in user1 user2
1146 for ref in ref1 ref2
1148 GIT_COMMITTER_DATE="@$when +0000" \
1149 GIT_COMMITTER_EMAIL="$email@example.com" \
1150 git tag -m "tag $ref-$when-$email" \
1151 multi-$ref-$when-$email || return 1
1157 test_expect_success
'Verify sort with multiple keys' '
1158 cat >expected <<-\EOF &&
1159 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1160 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1161 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1162 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1163 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1164 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1165 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1166 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1169 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1171 --sort=taggeremail \
1173 "refs/tags/multi-*" >actual &&
1174 test_cmp expected actual
1177 test_expect_success
'equivalent sorts fall back on refname' '
1178 cat >expected <<-\EOF &&
1179 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1180 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1181 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1182 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1183 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1184 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1185 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1186 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1189 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1191 "refs/tags/multi-*" >actual &&
1192 test_cmp expected actual
1195 test_expect_success
'--no-sort cancels the previous sort keys' '
1196 cat >expected <<-\EOF &&
1197 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1198 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1199 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1200 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1201 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1202 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1203 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1204 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1207 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1209 --sort=taggeremail \
1212 "refs/tags/multi-*" >actual &&
1213 test_cmp expected actual
1216 test_expect_success
'do not dereference NULL upon %(HEAD) on unborn branch' '
1217 test_when_finished "git checkout main" &&
1218 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1219 sed -e "s/^\* / /" actual >expect &&
1220 git checkout --orphan orphaned-branch &&
1221 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1222 test_cmp expect actual
1226 Reviewed-by: A U Thor <author@example.com>
1227 Signed-off-by: A U Thor <author@example.com>
1228 [ v2 updated patch description ]
1230 <author@example.com>
1234 perl
-0pe 's/\n\s+/ /g'
1237 test_expect_success
'set up trailers for next test' '
1238 echo "Some contents" > two &&
1240 git commit -F - <<-EOF
1241 trailers: this commit message has trailers
1243 Some message contents
1249 test_trailer_option
() {
1252 test_expect_success
"$title" '
1253 git for-each-ref --format="%($option)" refs/heads/main >actual &&
1254 test_cmp expect actual &&
1255 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1256 test_cmp expect actual
1260 test_trailer_option
'%(trailers:unfold) unfolds trailers' \
1261 'trailers:unfold' <<-EOF
1266 test_trailer_option
'%(trailers:only) shows only "key: value" trailers' \
1267 'trailers:only' <<-EOF
1268 $(grep -v patch.description <trailers)
1272 test_trailer_option
'%(trailers:only=no,only=true) shows only "key: value" trailers' \
1273 'trailers:only=no,only=true' <<-EOF
1274 $(grep -v patch.description <trailers)
1278 test_trailer_option
'%(trailers:only=yes) shows only "key: value" trailers' \
1279 'trailers:only=yes' <<-EOF
1280 $(grep -v patch.description <trailers)
1284 test_trailer_option
'%(trailers:only=no) shows all trailers' \
1285 'trailers:only=no' <<-EOF
1290 test_trailer_option
'%(trailers:only) and %(trailers:unfold) work together' \
1291 'trailers:only,unfold' <<-EOF
1292 $(grep -v patch.description <trailers | unfold)
1296 test_trailer_option
'%(trailers:unfold) and %(trailers:only) work together' \
1297 'trailers:unfold,only' <<-EOF
1298 $(grep -v patch.description <trailers | unfold)
1302 test_trailer_option
'%(trailers:key=foo) shows that trailer' \
1303 'trailers:key=Signed-off-by' <<-EOF
1304 Signed-off-by: A U Thor <author@example.com>
1308 test_trailer_option
'%(trailers:key=foo) is case insensitive' \
1309 'trailers:key=SiGned-oFf-bY' <<-EOF
1310 Signed-off-by: A U Thor <author@example.com>
1314 test_trailer_option
'%(trailers:key=foo:) trailing colon also works' \
1315 'trailers:key=Signed-off-by:' <<-EOF
1316 Signed-off-by: A U Thor <author@example.com>
1320 test_trailer_option
'%(trailers:key=foo) multiple keys' \
1321 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
1322 Reviewed-by: A U Thor <author@example.com>
1323 Signed-off-by: A U Thor <author@example.com>
1327 test_trailer_option
'%(trailers:key=nonexistent) becomes empty' \
1328 'trailers:key=Shined-off-by:' <<-EOF
1332 test_trailer_option
'%(trailers:key=foo) handles multiple lines even if folded' \
1333 'trailers:key=Acked-by' <<-EOF
1334 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
1338 test_trailer_option
'%(trailers:key=foo,unfold) properly unfolds' \
1339 'trailers:key=Signed-Off-by,unfold' <<-EOF
1340 $(unfold <trailers | grep Signed-off-by)
1344 test_trailer_option
'%(trailers:key=foo,only=no) also includes nontrailer lines' \
1345 'trailers:key=Signed-off-by,only=no' <<-EOF
1346 Signed-off-by: A U Thor <author@example.com>
1347 $(grep patch.description <trailers)
1351 test_trailer_option
'%(trailers:key=foo,valueonly) shows only value' \
1352 'trailers:key=Signed-off-by,valueonly' <<-EOF
1353 A U Thor <author@example.com>
1357 test_trailer_option
'%(trailers:separator) changes separator' \
1358 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1359 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
1362 test_trailer_option
'%(trailers:key_value_separator) changes key-value separator' \
1363 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1364 Reviewed-by,A U Thor <author@example.com>
1365 Signed-off-by,A U Thor <author@example.com>
1369 test_trailer_option
'%(trailers:separator,key_value_separator) changes both separators' \
1370 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1371 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
1374 test_failing_trailer_option
() {
1377 test_expect_success
"$title" '
1378 # error message cannot be checked under i18n
1379 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
1380 test_cmp expect actual &&
1381 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
1382 test_cmp expect actual
1386 test_failing_trailer_option
'%(trailers) rejects unknown trailers arguments' \
1387 'trailers:unsupported' <<-\EOF
1388 fatal: unknown %(trailers) argument: unsupported
1391 test_failing_trailer_option '%(trailers:key) without value is error' \
1392 'trailers:key' <<-\
EOF
1393 fatal: expected %(trailers:key=<value>)
1396 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
1397 cat >expect <<-EOF &&
1398 fatal: unrecognized %(contents) argument: trailersonly
1400 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
1401 test_cmp expect actual
1404 test_expect_success 'basic atom: head contents:trailers' '
1405 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
1406 sanitize_pgp <actual >actual.clean &&
1407 # git for-each-ref ends with a blank line
1408 cat >expect <<-EOF &&
1412 test_cmp expect actual.clean
1415 test_expect_success 'basic atom: rest must fail' '
1416 test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1419 test_expect_success 'HEAD atom does not take arguments' '
1420 test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
1421 echo "fatal: %(HEAD) does not take arguments" >expect &&
1425 test_expect_success 'subject atom rejects unknown arguments' '
1426 test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
1427 echo "fatal: unrecognized %(subject) argument: foo" >expect &&
1431 test_expect_success 'refname atom rejects unknown arguments' '
1432 test_must_fail git for-each-ref --format="%(refname:foo)" 2>err &&
1433 echo "fatal: unrecognized %(refname) argument: foo" >expect &&
1437 test_expect_success 'trailer parsing not fooled by --- line' '
1438 git commit --allow-empty -F - <<-\EOF &&
1441 This is the body. The message has a "---" line which would confuse a
1442 message+patch parser. But here we know we have only a commit message,
1453 echo "trailer: right" &&
1456 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
1457 test_cmp expect actual
1460 test_expect_success 'Add symbolic ref for the following tests' '
1461 git symbolic-ref refs/heads/sym refs/heads/main
1468 test_expect_success 'Verify usage of %(symref) atom' '
1469 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1470 test_cmp expected actual
1477 test_expect_success 'Verify usage of %(symref:short) atom' '
1478 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1479 test_cmp expected actual
1487 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1488 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1489 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
1490 test_cmp expected actual &&
1492 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1493 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
1494 test_cmp expected actual
1502 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1503 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1504 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1505 test_cmp expected actual
1508 test_expect_success ':remotename and :remoteref' '
1509 git init remote-tests &&
1512 test_commit initial &&
1513 git branch -M main &&
1514 git remote add from fifth.coffee:blub &&
1515 git config branch.main.remote from &&
1516 git config branch.main.merge refs/heads/stable &&
1517 git remote add to southridge.audio:repo &&
1518 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
1519 git config branch.main.pushRemote to &&
1520 for pair in "%(upstream)=refs/remotes/from/stable" \
1521 "%(upstream:remotename)=from" \
1522 "%(upstream:remoteref)=refs/heads/stable" \
1523 "%(push)=refs/remotes/to/pushed/main" \
1524 "%(push:remotename)=to" \
1525 "%(push:remoteref)=refs/heads/pushed/main"
1527 echo "${pair#*=}" >expect &&
1528 git for-each-ref --format="${pair%=*}" \
1529 refs/heads/main >actual &&
1530 test_cmp expect actual || exit 1
1532 git branch push-simple &&
1533 git config branch.push-simple.pushRemote from &&
1534 actual="$(git for-each-ref \
1535 --format="%(push:remotename),%(push:remoteref)" \
1536 refs/heads/push-simple)" &&
1537 test from, = "$actual"
1541 test_expect_success 'for-each-ref --ignore-case ignores case' '
1542 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
1543 test_must_be_empty actual &&
1545 echo refs/heads/main >expect &&
1546 git for-each-ref --format="%(refname)" --ignore-case \
1547 refs/heads/MAIN >actual &&
1548 test_cmp expect actual
1551 test_expect_success 'for-each-ref --omit-empty works' '
1552 git for-each-ref --format="%(refname)" >actual &&
1553 test_line_count -gt 1 actual &&
1554 git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
1555 echo refs/heads/main >expect &&
1556 test_cmp expect actual
1559 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1560 # name refs numerically to avoid case-insensitive filesystem conflicts
1562 for email in a A b B
1564 for subject in a A b B
1566 GIT_COMMITTER_EMAIL="$email@example.com" \
1567 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1572 git for-each-ref --ignore-case \
1573 --format="%(taggeremail) %(subject) %(refname)" \
1576 --sort=taggeremail \
1577 refs/tags/icase-* >actual &&
1578 cat >expect <<-\
EOF &&
1579 <a@example.com> tag a refs/tags/icase-00
1580 <a@example.com> tag A refs/tags/icase-01
1581 <A@example.com> tag a refs/tags/icase-04
1582 <A@example.com> tag A refs/tags/icase-05
1583 <a@example.com> tag b refs/tags/icase-02
1584 <a@example.com> tag B refs/tags/icase-03
1585 <A@example.com> tag b refs/tags/icase-06
1586 <A@example.com> tag B refs/tags/icase-07
1587 <b@example.com> tag a refs/tags/icase-08
1588 <b@example.com> tag A refs/tags/icase-09
1589 <B@example.com> tag a refs/tags/icase-12
1590 <B@example.com> tag A refs/tags/icase-13
1591 <b@example.com> tag b refs/tags/icase-10
1592 <b@example.com> tag B refs/tags/icase-11
1593 <B@example.com> tag b refs/tags/icase-14
1594 <B@example.com> tag B refs/tags/icase-15
1596 test_cmp expect actual
1599 test_expect_success 'for-each-ref reports broken tags' '
1600 git tag -m "good tag" broken-tag-good HEAD &&
1601 git cat-file tag broken-tag-good >good &&
1602 sed s/commit/blob/ <good >bad &&
1603 bad=$(git hash-object -w -t tag bad) &&
1604 git update-ref refs/tags/broken-tag-bad $bad &&
1605 test_must_fail git for-each-ref --format="%(*objectname)" \
1606 refs/tags/broken-tag-*
1609 test_expect_success 'set up tag with signature and no blank lines' '
1610 git tag -F - fake-sig-no-blanks
<<-\EOF
1612 -----BEGIN PGP SIGNATURE-----
1613 not a real signature, but we just care about the
1614 subject/body parsing. It is important here that
1615 there are no blank lines in the signature.
1616 -----END PGP SIGNATURE-----
1620 test_atom refs/tags/fake-sig-no-blanks contents:subject 'this is the subject'
1621 test_atom refs/tags/fake-sig-no-blanks contents:body ''
1622 test_atom refs/tags/fake-sig-no-blanks contents:signature "$sig"
1624 test_expect_success 'set up tag with CRLF signature' '
1627 -----BEGIN PGP SIGNATURE-----
1629 not a real signature, but we just care about
1630 the subject/body parsing. It is important here
1631 that there is a blank line separating this
1632 from the signature header.
1633 -----END PGP SIGNATURE-----
1635 git tag -F - --cleanup=verbatim fake-sig-crlf
1638 test_atom refs/tags/fake-sig-crlf contents:subject 'this is the subject
'
1639 test_atom refs/tags/fake-sig-crlf contents:body ''
1641 # CRLF is retained in the signature, so we have to pass our expected value
1642 # through append_cr. But test_atom requires a shell string, which means command
1643 # substitution, and the shell will strip trailing newlines from the output of
1644 # the substitution. Hack around it by adding and then removing a dummy line.
1645 sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
1646 sig_crlf=${sig_crlf%dummy}
1647 test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
1649 test_expect_success 'git for-each-ref
--stdin: empty
' '
1651 git for-each-ref
--format="%(refname)" --stdin <in >actual
&&
1652 git for-each-ref
--format="%(refname)" >expect
&&
1653 test_cmp expect actual
1656 test_expect_success 'git for-each-ref
--stdin: fails
if extra args
' '
1658 test_must_fail git for-each-ref
--format="%(refname)" \
1659 --stdin refs
/heads
/extra
<in 2>err
&&
1660 grep "unknown arguments supplied with --stdin" err
1663 test_expect_success 'git for-each-ref
--stdin: matches
' '
1669 cat >expect
<<-EOF &&
1670 refs/heads/ambiguous
1671 refs/tags/multi-ref1-100000-user1
1672 refs/tags/multi-ref1-100000-user2
1673 refs/tags/multi-ref1-200000-user1
1674 refs/tags/multi-ref1-200000-user2
1675 refs/tags/multi-ref2-100000-user1
1676 refs/tags/multi-ref2-100000-user2
1677 refs/tags/multi-ref2-200000-user1
1678 refs/tags/multi-ref2-200000-user2
1682 git for-each-ref
--format="%(refname)" --stdin <in >actual
&&
1683 test_cmp expect actual
1686 test_expect_success 'git for-each-ref with non-existing refs
' '
1688 refs/heads/this-ref-does-not-exist
1692 git for-each-ref
--format="%(refname)" --stdin <in >actual
&&
1693 test_must_be_empty actual
&&
1695 xargs git for-each-ref
--format="%(refname)" <in >actual
&&
1696 test_must_be_empty actual
1699 GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1700 TRUSTLEVEL_FORMAT="%(signature:trustlevel)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1702 test_expect_success GPG 'setup
for signature atom using gpg
' '
1703 git checkout
-b signed
&&
1705 test_when_finished
"test_unconfig commit.gpgSign" &&
1710 git commit
-S -m "file: 1" &&
1711 git tag first-signed
&&
1715 git commit
-a -m "file: 2" &&
1716 git tag second-unsigned
&&
1718 git config commit.gpgSign
1 &&
1721 git commit
-a --no-gpg-sign -m "file: 3" &&
1722 git tag third-unsigned
&&
1725 git rebase
-f HEAD^^
&& git tag second-signed HEAD^
&&
1726 git tag third-signed
&&
1730 git commit
-a -SB7227189 -m "file: 4" &&
1731 git tag fourth-signed
&&
1735 git commit
-a --no-gpg-sign -m "file: 5" &&
1736 git tag fifth-unsigned
&&
1740 git commit
-a --no-gpg-sign -m "file: 6" &&
1743 git rebase
-f HEAD^^
&&
1744 git tag fifth-signed HEAD^
&&
1745 git tag sixth-signed
&&
1749 git commit
-a --no-gpg-sign -m "file: 7" &&
1750 git tag seventh-unsigned
1753 test_expect_success GPGSSH 'setup
for signature atom using
ssh' '
1754 test_when_finished
"test_unconfig gpg.format user.signingkey" &&
1756 test_config gpg.format
ssh &&
1757 test_config user.signingkey
"${GPGSSH_KEY_PRIMARY}" &&
1761 git commit
-S -m "file: 8" &&
1762 git tag eighth-signed-ssh
1765 test_expect_success GPG2 'bare signature atom
' '
1766 git verify-commit first-signed
2>out.raw
&&
1767 grep -Ev "checking the trustdb|PGP trust model" out.raw
>out
&&
1768 head -3 out
>expect
&&
1769 tail -1 out
>>expect
&&
1771 git for-each-ref refs
/tags
/first-signed \
1772 --format="%(signature)" >actual
&&
1773 test_cmp expect actual
1776 test_expect_success GPG 'show good signature with custom format
' '
1777 git verify-commit first-signed
&&
1778 cat >expect
<<-\EOF &&
1781 C O Mitter <committer@example.com>
1782 73D758744BE721698EC54E8713B6F51ECDDE430D
1783 73D758744BE721698EC54E8713B6F51ECDDE430D
1785 git for-each-ref refs/tags/first-signed \
1786 --format="$GRADE_FORMAT" >actual &&
1787 test_cmp expect actual
1789 test_expect_success GPGSSH 'show good signature with custom format
1791 test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
1792 FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
1793 cat >expect.tmpl <<-\
EOF &&
1796 principal with number 1
1800 sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
1801 git for-each-ref refs/tags/eighth-signed-ssh \
1802 --format="$GRADE_FORMAT" >actual &&
1803 test_cmp expect actual
1806 test_expect_success GPG 'signature atom with grade option and bad signature' '
1807 git cat-file commit third-signed >raw &&
1808 sed -e "s/^file: 3/file: 3 forged/" raw >forged1 &&
1809 FORGED1=$(git hash-object -w -t commit forged1) &&
1810 git update-ref refs/tags/third-signed "$FORGED1" &&
1811 test_must_fail git verify-commit "$FORGED1" &&
1813 cat >expect <<-\EOF &&
1816 C O Mitter <committer@example.com>
1820 git for-each-ref refs/tags/third-signed \
1821 --format="$GRADE_FORMAT" >actual &&
1822 test_cmp expect actual
1825 test_expect_success GPG 'show untrusted signature with custom format' '
1826 cat >expect <<-\EOF &&
1829 Eris Discordia <discord@example.net>
1830 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1831 D4BE22311AD3131E5EDA29A461092E85B7227189
1833 git for-each-ref refs/tags/fourth-signed \
1834 --format="$GRADE_FORMAT" >actual &&
1835 test_cmp expect actual
1838 test_expect_success GPG 'show untrusted signature with undefined trust level' '
1839 cat >expect <<-\EOF &&
1842 Eris Discordia <discord@example.net>
1843 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1844 D4BE22311AD3131E5EDA29A461092E85B7227189
1846 git for-each-ref refs/tags/fourth-signed \
1847 --format="$TRUSTLEVEL_FORMAT" >actual &&
1848 test_cmp expect actual
1851 test_expect_success GPG 'show untrusted signature with ultimate trust level' '
1852 cat >expect <<-\EOF &&
1855 C O Mitter <committer@example.com>
1856 73D758744BE721698EC54E8713B6F51ECDDE430D
1857 73D758744BE721698EC54E8713B6F51ECDDE430D
1859 git for-each-ref refs/tags/sixth-signed \
1860 --format="$TRUSTLEVEL_FORMAT" >actual &&
1861 test_cmp expect actual
1864 test_expect_success GPG 'show unknown signature with custom format' '
1865 cat >expect <<-\EOF &&
1872 GNUPGHOME="$GNUPGHOME_NOT_USED" git for-each-ref \
1873 refs/tags/sixth-signed --format="$GRADE_FORMAT" >actual &&
1874 test_cmp expect actual
1877 test_expect_success GPG 'show lack of signature with custom format' '
1878 cat >expect <<-\EOF &&
1885 git for-each-ref refs/tags/seventh-unsigned \
1886 --format="$GRADE_FORMAT" >actual &&
1887 test_cmp expect actual