dir-iterator: support iteration in sorted order
[git.git] / t / t6300-for-each-ref.sh
blobeb6c8204e8bd0edd653d74102eb92db3da720202
1 #!/bin/sh
3 # Copyright (c) 2007 Andy Parkins
6 test_description='for-each-ref test'
8 . ./test-lib.sh
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
14 datestamp=1151968723
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_object_file_size () {
24 oid=$(git rev-parse "$1")
25 path=".git/objects/$(test_oid_to_path $oid)"
26 test_file_size "$path"
29 test_expect_success setup '
30 # setup .mailmap
31 cat >.mailmap <<-EOF &&
32 A Thor <athor@example.com> A U Thor <author@example.com>
33 C Mitter <cmitter@example.com> C O Mitter <committer@example.com>
34 EOF
36 setdate_and_increment &&
37 echo "Using $datestamp" > one &&
38 git add one &&
39 git commit -m "Initial" &&
40 git branch -M main &&
41 setdate_and_increment &&
42 git tag -a -m "Tagging at $datestamp" testtag &&
43 git update-ref refs/remotes/origin/main main &&
44 git remote add origin nowhere &&
45 git config branch.main.remote origin &&
46 git config branch.main.merge refs/heads/main &&
47 git remote add myfork elsewhere &&
48 git config remote.pushdefault myfork &&
49 git config push.default current
52 test_atom () {
53 case "$1" in
54 head) ref=refs/heads/main ;;
55 tag) ref=refs/tags/testtag ;;
56 sym) ref=refs/heads/sym ;;
57 *) ref=$1 ;;
58 esac
59 format=$2
60 test_do=test_expect_${4:-success}
62 printf '%s\n' "$3" >expected
63 $test_do $PREREQ "basic atom: $ref $format" '
64 git for-each-ref --format="%($format)" "$ref" >actual &&
65 sanitize_pgp <actual >actual.clean &&
66 test_cmp expected actual.clean
69 # Automatically test "contents:size" atom after testing "contents"
70 if test "$format" = "contents"
71 then
72 # for commit leg, $3 is changed there
73 expect=$(printf '%s' "$3" | wc -c)
74 $test_do $PREREQ "basic atom: $ref contents:size" '
75 type=$(git cat-file -t "$ref") &&
76 case $type in
77 tag)
78 # We cannot use $3 as it expects sanitize_pgp to run
79 git cat-file tag $ref >out &&
80 expect=$(tail -n +6 out | wc -c) &&
81 rm -f out ;;
82 tree | blob)
83 expect="" ;;
84 commit)
85 : "use the calculated expect" ;;
87 BUG "unknown object type" ;;
88 esac &&
89 # Leave $expect unquoted to lose possible leading whitespaces
90 echo $expect >expected &&
91 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
92 test_cmp expected actual
97 hexlen=$(test_oid hexsz)
99 test_atom head refname refs/heads/main
100 test_atom head refname: refs/heads/main
101 test_atom head refname:short main
102 test_atom head refname:lstrip=1 heads/main
103 test_atom head refname:lstrip=2 main
104 test_atom head refname:lstrip=-1 main
105 test_atom head refname:lstrip=-2 heads/main
106 test_atom head refname:rstrip=1 refs/heads
107 test_atom head refname:rstrip=2 refs
108 test_atom head refname:rstrip=-1 refs
109 test_atom head refname:rstrip=-2 refs/heads
110 test_atom head refname:strip=1 heads/main
111 test_atom head refname:strip=2 main
112 test_atom head refname:strip=-1 main
113 test_atom head refname:strip=-2 heads/main
114 test_atom head upstream refs/remotes/origin/main
115 test_atom head upstream:short origin/main
116 test_atom head upstream:lstrip=2 origin/main
117 test_atom head upstream:lstrip=-2 origin/main
118 test_atom head upstream:rstrip=2 refs/remotes
119 test_atom head upstream:rstrip=-2 refs/remotes
120 test_atom head upstream:strip=2 origin/main
121 test_atom head upstream:strip=-2 origin/main
122 test_atom head push refs/remotes/myfork/main
123 test_atom head push:short myfork/main
124 test_atom head push:lstrip=1 remotes/myfork/main
125 test_atom head push:lstrip=-1 main
126 test_atom head push:rstrip=1 refs/remotes/myfork
127 test_atom head push:rstrip=-1 refs
128 test_atom head push:strip=1 remotes/myfork/main
129 test_atom head push:strip=-1 main
130 test_atom head objecttype commit
131 test_atom head objectsize $((131 + hexlen))
132 test_atom head objectsize:disk $(test_object_file_size refs/heads/main)
133 test_atom head deltabase $ZERO_OID
134 test_atom head objectname $(git rev-parse refs/heads/main)
135 test_atom head objectname:short $(git rev-parse --short refs/heads/main)
136 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
137 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
138 test_atom head tree $(git rev-parse refs/heads/main^{tree})
139 test_atom head tree:short $(git rev-parse --short refs/heads/main^{tree})
140 test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/main^{tree})
141 test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/main^{tree})
142 test_atom head parent ''
143 test_atom head parent:short ''
144 test_atom head parent:short=1 ''
145 test_atom head parent:short=10 ''
146 test_atom head numparent 0
147 test_atom head object ''
148 test_atom head type ''
149 test_atom head raw "$(git cat-file commit refs/heads/main)
151 test_atom head '*objectname' ''
152 test_atom head '*objecttype' ''
153 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
154 test_atom head authorname 'A U Thor'
155 test_atom head authorname:mailmap 'A Thor'
156 test_atom head authoremail '<author@example.com>'
157 test_atom head authoremail:trim 'author@example.com'
158 test_atom head authoremail:localpart 'author'
159 test_atom head authoremail:trim,localpart 'author'
160 test_atom head authoremail:mailmap '<athor@example.com>'
161 test_atom head authoremail:mailmap,trim 'athor@example.com'
162 test_atom head authoremail:trim,mailmap 'athor@example.com'
163 test_atom head authoremail:mailmap,localpart 'athor'
164 test_atom head authoremail:localpart,mailmap 'athor'
165 test_atom head authoremail:mailmap,trim,localpart,mailmap,trim 'athor'
166 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
167 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
168 test_atom head committername 'C O Mitter'
169 test_atom head committername:mailmap 'C Mitter'
170 test_atom head committeremail '<committer@example.com>'
171 test_atom head committeremail:trim 'committer@example.com'
172 test_atom head committeremail:localpart 'committer'
173 test_atom head committeremail:localpart,trim 'committer'
174 test_atom head committeremail:mailmap '<cmitter@example.com>'
175 test_atom head committeremail:mailmap,trim 'cmitter@example.com'
176 test_atom head committeremail:trim,mailmap 'cmitter@example.com'
177 test_atom head committeremail:mailmap,localpart 'cmitter'
178 test_atom head committeremail:localpart,mailmap 'cmitter'
179 test_atom head committeremail:trim,mailmap,trim,trim,localpart 'cmitter'
180 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
181 test_atom head tag ''
182 test_atom head tagger ''
183 test_atom head taggername ''
184 test_atom head taggeremail ''
185 test_atom head taggeremail:trim ''
186 test_atom head taggeremail:localpart ''
187 test_atom head taggerdate ''
188 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
189 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
190 test_atom head subject 'Initial'
191 test_atom head subject:sanitize 'Initial'
192 test_atom head contents:subject 'Initial'
193 test_atom head body ''
194 test_atom head contents:body ''
195 test_atom head contents:signature ''
196 test_atom head contents 'Initial
198 test_atom head HEAD '*'
200 test_atom tag refname refs/tags/testtag
201 test_atom tag refname:short testtag
202 test_atom tag upstream ''
203 test_atom tag push ''
204 test_atom tag objecttype tag
205 test_atom tag objectsize $((114 + hexlen))
206 test_atom tag objectsize:disk $(test_object_file_size refs/tags/testtag)
207 test_atom tag '*objectsize:disk' $(test_object_file_size refs/heads/main)
208 test_atom tag deltabase $ZERO_OID
209 test_atom tag '*deltabase' $ZERO_OID
210 test_atom tag objectname $(git rev-parse refs/tags/testtag)
211 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
212 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
213 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
214 test_atom tag tree ''
215 test_atom tag tree:short ''
216 test_atom tag tree:short=1 ''
217 test_atom tag tree:short=10 ''
218 test_atom tag parent ''
219 test_atom tag parent:short ''
220 test_atom tag parent:short=1 ''
221 test_atom tag parent:short=10 ''
222 test_atom tag numparent ''
223 test_atom tag object $(git rev-parse refs/tags/testtag^0)
224 test_atom tag type 'commit'
225 test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
226 test_atom tag '*objecttype' 'commit'
227 test_atom tag author ''
228 test_atom tag authorname ''
229 test_atom tag authorname:mailmap ''
230 test_atom tag authoremail ''
231 test_atom tag authoremail:trim ''
232 test_atom tag authoremail:localpart ''
233 test_atom tag authoremail:trim,localpart ''
234 test_atom tag authoremail:mailmap ''
235 test_atom tag authoremail:mailmap,trim ''
236 test_atom tag authoremail:trim,mailmap ''
237 test_atom tag authoremail:mailmap,localpart ''
238 test_atom tag authoremail:localpart,mailmap ''
239 test_atom tag authoremail:mailmap,trim,localpart,mailmap,trim ''
240 test_atom tag authordate ''
241 test_atom tag committer ''
242 test_atom tag committername ''
243 test_atom tag committername:mailmap ''
244 test_atom tag committeremail ''
245 test_atom tag committeremail:trim ''
246 test_atom tag committeremail:localpart ''
247 test_atom tag committeremail:localpart,trim ''
248 test_atom tag committeremail:mailmap ''
249 test_atom tag committeremail:mailmap,trim ''
250 test_atom tag committeremail:trim,mailmap ''
251 test_atom tag committeremail:mailmap,localpart ''
252 test_atom tag committeremail:localpart,mailmap ''
253 test_atom tag committeremail:trim,mailmap,trim,trim,localpart ''
254 test_atom tag committerdate ''
255 test_atom tag tag 'testtag'
256 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
257 test_atom tag taggername 'C O Mitter'
258 test_atom tag taggername:mailmap 'C Mitter'
259 test_atom tag taggeremail '<committer@example.com>'
260 test_atom tag taggeremail:trim 'committer@example.com'
261 test_atom tag taggeremail:localpart 'committer'
262 test_atom tag taggeremail:trim,localpart 'committer'
263 test_atom tag taggeremail:mailmap '<cmitter@example.com>'
264 test_atom tag taggeremail:mailmap,trim 'cmitter@example.com'
265 test_atom tag taggeremail:trim,mailmap 'cmitter@example.com'
266 test_atom tag taggeremail:mailmap,localpart 'cmitter'
267 test_atom tag taggeremail:localpart,mailmap 'cmitter'
268 test_atom tag taggeremail:trim,mailmap,trim,localpart,localpart 'cmitter'
269 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
270 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
271 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
272 test_atom tag subject 'Tagging at 1151968727'
273 test_atom tag subject:sanitize 'Tagging-at-1151968727'
274 test_atom tag contents:subject 'Tagging at 1151968727'
275 test_atom tag body ''
276 test_atom tag contents:body ''
277 test_atom tag contents:signature ''
278 test_atom tag contents 'Tagging at 1151968727
280 test_atom tag HEAD ' '
282 test_expect_success 'basic atom: refs/tags/testtag *raw' '
283 git cat-file commit refs/tags/testtag^{} >expected &&
284 git for-each-ref --format="%(*raw)" refs/tags/testtag >actual &&
285 sanitize_pgp <expected >expected.clean &&
286 echo >>expected.clean &&
287 sanitize_pgp <actual >actual.clean &&
288 test_cmp expected.clean actual.clean
291 test_expect_success 'Check invalid atoms names are errors' '
292 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
295 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
296 git for-each-ref --format="%(authordate)" refs/heads &&
297 git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
298 git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
299 git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
302 test_expect_success 'Check valid format specifiers for date fields' '
303 git for-each-ref --format="%(authordate:default)" refs/heads &&
304 git for-each-ref --format="%(authordate:relative)" refs/heads &&
305 git for-each-ref --format="%(authordate:short)" refs/heads &&
306 git for-each-ref --format="%(authordate:local)" refs/heads &&
307 git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
308 git for-each-ref --format="%(authordate:rfc2822)" refs/heads
311 test_expect_success 'Check invalid format specifiers are errors' '
312 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
315 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
316 test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
317 test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
318 test_must_fail git for-each-ref --format="%(objectname:short=foo)"
321 test_bad_atom () {
322 case "$1" in
323 head) ref=refs/heads/main ;;
324 tag) ref=refs/tags/testtag ;;
325 sym) ref=refs/heads/sym ;;
326 *) ref=$1 ;;
327 esac
328 format=$2
329 test_do=test_expect_${4:-success}
331 printf '%s\n' "$3" >expect
332 $test_do $PREREQ "err basic atom: $ref $format" '
333 test_must_fail git for-each-ref \
334 --format="%($format)" "$ref" 2>error &&
335 test_cmp expect error
339 test_bad_atom head 'authoremail:foo' \
340 'fatal: unrecognized %(authoremail) argument: foo'
342 test_bad_atom head 'authoremail:mailmap,trim,bar' \
343 'fatal: unrecognized %(authoremail) argument: bar'
345 test_bad_atom head 'authoremail:trim,' \
346 'fatal: unrecognized %(authoremail) argument: '
348 test_bad_atom head 'authoremail:mailmaptrim' \
349 'fatal: unrecognized %(authoremail) argument: trim'
351 test_bad_atom head 'committeremail: ' \
352 'fatal: unrecognized %(committeremail) argument: '
354 test_bad_atom head 'committeremail: trim,foo' \
355 'fatal: unrecognized %(committeremail) argument: trim,foo'
357 test_bad_atom head 'committeremail:mailmap,localpart ' \
358 'fatal: unrecognized %(committeremail) argument: '
360 test_bad_atom head 'committeremail:trim_localpart' \
361 'fatal: unrecognized %(committeremail) argument: _localpart'
363 test_bad_atom head 'committeremail:localpart,,,trim' \
364 'fatal: unrecognized %(committeremail) argument: ,,trim'
366 test_bad_atom tag 'taggeremail:mailmap,trim, foo ' \
367 'fatal: unrecognized %(taggeremail) argument: foo '
369 test_bad_atom tag 'taggeremail:trim,localpart,' \
370 'fatal: unrecognized %(taggeremail) argument: '
372 test_bad_atom tag 'taggeremail:mailmap;localpart trim' \
373 'fatal: unrecognized %(taggeremail) argument: ;localpart trim'
375 test_bad_atom tag 'taggeremail:localpart trim' \
376 'fatal: unrecognized %(taggeremail) argument: trim'
378 test_bad_atom tag 'taggeremail:mailmap,mailmap,trim,qux,localpart,trim' \
379 'fatal: unrecognized %(taggeremail) argument: qux,localpart,trim'
381 test_date () {
382 f=$1 &&
383 committer_date=$2 &&
384 author_date=$3 &&
385 tagger_date=$4 &&
386 cat >expected <<-EOF &&
387 'refs/heads/main' '$committer_date' '$author_date'
388 'refs/tags/testtag' '$tagger_date'
391 git for-each-ref --shell \
392 --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
393 refs/heads &&
394 git for-each-ref --shell \
395 --format="%(refname) %(taggerdate${f:+:$f})" \
396 refs/tags
397 ) >actual &&
398 test_cmp expected actual
401 test_expect_success 'Check unformatted date fields output' '
402 test_date "" \
403 "Tue Jul 4 01:18:43 2006 +0200" \
404 "Tue Jul 4 01:18:44 2006 +0200" \
405 "Tue Jul 4 01:18:45 2006 +0200"
408 test_expect_success 'Check format "default" formatted date fields output' '
409 test_date default \
410 "Tue Jul 4 01:18:43 2006 +0200" \
411 "Tue Jul 4 01:18:44 2006 +0200" \
412 "Tue Jul 4 01:18:45 2006 +0200"
415 test_expect_success 'Check format "default-local" date fields output' '
416 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"
419 # Don't know how to do relative check because I can't know when this script
420 # is going to be run and can't fake the current time to git, and hence can't
421 # provide expected output. Instead, I'll just make sure that "relative"
422 # doesn't exit in error
423 test_expect_success 'Check format "relative" date fields output' '
424 f=relative &&
425 (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
426 git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
429 # We just check that this is the same as "relative" for now.
430 test_expect_success 'Check format "relative-local" date fields output' '
431 test_date relative-local \
432 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
433 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
434 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
437 test_expect_success 'Check format "short" date fields output' '
438 test_date short 2006-07-04 2006-07-04 2006-07-04
441 test_expect_success 'Check format "short-local" date fields output' '
442 test_date short-local 2006-07-03 2006-07-03 2006-07-03
445 test_expect_success 'Check format "local" date fields output' '
446 test_date local \
447 "Mon Jul 3 23:18:43 2006" \
448 "Mon Jul 3 23:18:44 2006" \
449 "Mon Jul 3 23:18:45 2006"
452 test_expect_success 'Check format "iso8601" date fields output' '
453 test_date iso8601 \
454 "2006-07-04 01:18:43 +0200" \
455 "2006-07-04 01:18:44 +0200" \
456 "2006-07-04 01:18:45 +0200"
459 test_expect_success 'Check format "iso8601-local" date fields output' '
460 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"
463 test_expect_success 'Check format "rfc2822" date fields output' '
464 test_date rfc2822 \
465 "Tue, 4 Jul 2006 01:18:43 +0200" \
466 "Tue, 4 Jul 2006 01:18:44 +0200" \
467 "Tue, 4 Jul 2006 01:18:45 +0200"
470 test_expect_success 'Check format "rfc2822-local" date fields output' '
471 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"
474 test_expect_success 'Check format "raw" date fields output' '
475 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
478 test_expect_success 'Check format "raw-local" date fields output' '
479 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
482 test_expect_success 'Check format of strftime date fields' '
483 echo "my date is 2006-07-04" >expected &&
484 git for-each-ref \
485 --format="%(authordate:format:my date is %Y-%m-%d)" \
486 refs/heads >actual &&
487 test_cmp expected actual
490 test_expect_success 'Check format of strftime-local date fields' '
491 echo "my date is 2006-07-03" >expected &&
492 git for-each-ref \
493 --format="%(authordate:format-local:my date is %Y-%m-%d)" \
494 refs/heads >actual &&
495 test_cmp expected actual
498 test_expect_success 'exercise strftime with odd fields' '
499 echo >expected &&
500 git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
501 test_cmp expected actual &&
502 long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
503 echo $long >expected &&
504 git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
505 test_cmp expected actual
508 cat >expected <<\EOF
509 refs/heads/main
510 refs/remotes/origin/main
511 refs/tags/testtag
514 test_expect_success 'Verify ascending sort' '
515 git for-each-ref --format="%(refname)" --sort=refname >actual &&
516 test_cmp expected actual
520 cat >expected <<\EOF
521 refs/tags/testtag
522 refs/remotes/origin/main
523 refs/heads/main
526 test_expect_success 'Verify descending sort' '
527 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
528 test_cmp expected actual
531 test_expect_success 'Give help even with invalid sort atoms' '
532 test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
533 grep "^usage: git for-each-ref" actual
536 cat >expected <<\EOF
537 refs/tags/testtag
538 refs/tags/testtag-2
541 test_expect_success 'exercise patterns with prefixes' '
542 git tag testtag-2 &&
543 test_when_finished "git tag -d testtag-2" &&
544 git for-each-ref --format="%(refname)" \
545 refs/tags/testtag refs/tags/testtag-2 >actual &&
546 test_cmp expected actual
549 cat >expected <<\EOF
550 refs/tags/testtag
551 refs/tags/testtag-2
554 test_expect_success 'exercise glob patterns with prefixes' '
555 git tag testtag-2 &&
556 test_when_finished "git tag -d testtag-2" &&
557 git for-each-ref --format="%(refname)" \
558 refs/tags/testtag "refs/tags/testtag-*" >actual &&
559 test_cmp expected actual
562 cat >expected <<\EOF
563 refs/tags/bar
564 refs/tags/baz
565 refs/tags/testtag
568 test_expect_success 'exercise patterns with prefix exclusions' '
569 for tag in foo/one foo/two foo/three bar baz
571 git tag "$tag" || return 1
572 done &&
573 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
574 git for-each-ref --format="%(refname)" \
575 refs/tags/ --exclude=refs/tags/foo >actual &&
576 test_cmp expected actual
579 cat >expected <<\EOF
580 refs/tags/bar
581 refs/tags/baz
582 refs/tags/foo/one
583 refs/tags/testtag
586 test_expect_success 'exercise patterns with pattern exclusions' '
587 for tag in foo/one foo/two foo/three bar baz
589 git tag "$tag" || return 1
590 done &&
591 test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
592 git for-each-ref --format="%(refname)" \
593 refs/tags/ --exclude="refs/tags/foo/t*" >actual &&
594 test_cmp expected actual
597 cat >expected <<\EOF
598 'refs/heads/main'
599 'refs/remotes/origin/main'
600 'refs/tags/testtag'
603 test_expect_success 'Quoting style: shell' '
604 git for-each-ref --shell --format="%(refname)" >actual &&
605 test_cmp expected actual
608 test_expect_success 'Quoting style: perl' '
609 git for-each-ref --perl --format="%(refname)" >actual &&
610 test_cmp expected actual
613 test_expect_success 'Quoting style: python' '
614 git for-each-ref --python --format="%(refname)" >actual &&
615 test_cmp expected actual
618 cat >expected <<\EOF
619 "refs/heads/main"
620 "refs/remotes/origin/main"
621 "refs/tags/testtag"
624 test_expect_success 'Quoting style: tcl' '
625 git for-each-ref --tcl --format="%(refname)" >actual &&
626 test_cmp expected actual
629 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
630 test_expect_success "more than one quoting style: $i" "
631 test_must_fail git for-each-ref $i 2>err &&
632 grep '^error: more than one quoting style' err
634 done
636 test_expect_success 'setup for upstream:track[short]' '
637 test_commit two
640 test_atom head upstream:track '[ahead 1]'
641 test_atom head upstream:trackshort '>'
642 test_atom head upstream:track,nobracket 'ahead 1'
643 test_atom head upstream:nobracket,track 'ahead 1'
645 test_expect_success 'setup for push:track[short]' '
646 test_commit third &&
647 git update-ref refs/remotes/myfork/main main &&
648 git reset main~1
651 test_atom head push:track '[behind 1]'
652 test_atom head push:trackshort '<'
654 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
655 test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
656 test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
659 test_expect_success 'Check that :track[short] works when upstream is invalid' '
660 cat >expected <<-\EOF &&
661 [gone]
664 test_when_finished "git config branch.main.merge refs/heads/main" &&
665 git config branch.main.merge refs/heads/does-not-exist &&
666 git for-each-ref \
667 --format="%(upstream:track)$LF%(upstream:trackshort)" \
668 refs/heads >actual &&
669 test_cmp expected actual
672 test_expect_success 'Check for invalid refname format' '
673 test_must_fail git for-each-ref --format="%(refname:INVALID)"
676 test_expect_success 'set up color tests' '
677 cat >expected.color <<-EOF &&
678 $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
679 $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
680 $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
681 $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
682 $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
683 $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
685 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
686 color_format="%(objectname:short) %(color:green)%(refname:short)"
689 test_expect_success TTY '%(color) shows color with a tty' '
690 test_terminal git for-each-ref --format="$color_format" >actual.raw &&
691 test_decode_color <actual.raw >actual &&
692 test_cmp expected.color actual
695 test_expect_success '%(color) does not show color without tty' '
696 TERM=vt100 git for-each-ref --format="$color_format" >actual &&
697 test_cmp expected.bare actual
700 test_expect_success '--color can override tty check' '
701 git for-each-ref --color --format="$color_format" >actual.raw &&
702 test_decode_color <actual.raw >actual &&
703 test_cmp expected.color actual
706 test_expect_success 'color.ui=always does not override tty check' '
707 git -c color.ui=always for-each-ref --format="$color_format" >actual &&
708 test_cmp expected.bare actual
711 test_expect_success 'setup for describe atom tests' '
712 git init -b master describe-repo &&
714 cd describe-repo &&
716 test_commit --no-tag one &&
717 git tag tagone &&
719 test_commit --no-tag two &&
720 git tag -a -m "tag two" tagtwo
724 test_expect_success 'describe atom vs git describe' '
726 cd describe-repo &&
728 git for-each-ref --format="%(objectname)" \
729 refs/tags/ >obj &&
730 while read hash
732 if desc=$(git describe $hash)
733 then
734 : >expect-contains-good
735 else
736 : >expect-contains-bad
737 fi &&
738 echo "$hash $desc" || return 1
739 done <obj >expect &&
740 test_path_exists expect-contains-good &&
741 test_path_exists expect-contains-bad &&
743 git for-each-ref --format="%(objectname) %(describe)" \
744 refs/tags/ >actual 2>err &&
745 test_cmp expect actual &&
746 test_must_be_empty err
750 test_expect_success 'describe:tags vs describe --tags' '
752 cd describe-repo &&
753 git describe --tags >expect &&
754 git for-each-ref --format="%(describe:tags)" \
755 refs/heads/master >actual &&
756 test_cmp expect actual
760 test_expect_success 'describe:abbrev=... vs describe --abbrev=...' '
762 cd describe-repo &&
764 # Case 1: We have commits between HEAD and the most
765 # recent tag reachable from it
766 test_commit --no-tag file &&
767 git describe --abbrev=14 >expect &&
768 git for-each-ref --format="%(describe:abbrev=14)" \
769 refs/heads/master >actual &&
770 test_cmp expect actual &&
772 # Make sure the hash used is atleast 14 digits long
773 sed -e "s/^.*-g\([0-9a-f]*\)$/\1/" <actual >hexpart &&
774 test 15 -le $(wc -c <hexpart) &&
776 # Case 2: We have a tag at HEAD, describe directly gives
777 # the name of the tag
778 git tag -a -m tagged tagname &&
779 git describe --abbrev=14 >expect &&
780 git for-each-ref --format="%(describe:abbrev=14)" \
781 refs/heads/master >actual &&
782 test_cmp expect actual &&
783 test tagname = $(cat actual)
787 test_expect_success 'describe:match=... vs describe --match ...' '
789 cd describe-repo &&
790 git tag -a -m "tag foo" tag-foo &&
791 git describe --match "*-foo" >expect &&
792 git for-each-ref --format="%(describe:match="*-foo")" \
793 refs/heads/master >actual &&
794 test_cmp expect actual
798 test_expect_success 'describe:exclude:... vs describe --exclude ...' '
800 cd describe-repo &&
801 git tag -a -m "tag bar" tag-bar &&
802 git describe --exclude "*-bar" >expect &&
803 git for-each-ref --format="%(describe:exclude="*-bar")" \
804 refs/heads/master >actual &&
805 test_cmp expect actual
809 test_expect_success 'deref with describe atom' '
811 cd describe-repo &&
812 cat >expect <<-\EOF &&
814 tagname
815 tagname
816 tagname
818 tagtwo
820 git for-each-ref --format="%(*describe)" >actual &&
821 test_cmp expect actual
825 test_expect_success 'err on bad describe atom arg' '
827 cd describe-repo &&
829 # The bad arg is the only arg passed to describe atom
830 cat >expect <<-\EOF &&
831 fatal: unrecognized %(describe) argument: baz
833 test_must_fail git for-each-ref --format="%(describe:baz)" \
834 refs/heads/master 2>actual &&
835 test_cmp expect actual &&
837 # The bad arg is in the middle of the option string
838 # passed to the describe atom
839 cat >expect <<-\EOF &&
840 fatal: unrecognized %(describe) argument: qux=1,abbrev=14
842 test_must_fail git for-each-ref \
843 --format="%(describe:tags,qux=1,abbrev=14)" \
844 ref/heads/master 2>actual &&
845 test_cmp expect actual
849 cat >expected <<\EOF
850 heads/main
851 tags/main
854 test_expect_success 'Check ambiguous head and tag refs (strict)' '
855 git config --bool core.warnambiguousrefs true &&
856 git checkout -b newtag &&
857 echo "Using $datestamp" > one &&
858 git add one &&
859 git commit -m "Branch" &&
860 setdate_and_increment &&
861 git tag -m "Tagging at $datestamp" main &&
862 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
863 test_cmp expected actual
866 cat >expected <<\EOF
867 heads/main
868 main
871 test_expect_success 'Check ambiguous head and tag refs (loose)' '
872 git config --bool core.warnambiguousrefs false &&
873 git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
874 test_cmp expected actual
877 cat >expected <<\EOF
878 heads/ambiguous
879 ambiguous
882 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
883 git checkout main &&
884 git tag ambiguous testtag^0 &&
885 git branch ambiguous testtag^0 &&
886 git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
887 test_cmp expected actual
890 test_expect_success 'create tag without tagger' '
891 git tag -a -m "Broken tag" taggerless &&
892 git tag -f taggerless $(git cat-file tag taggerless |
893 sed -e "/^tagger /d" |
894 git hash-object --literally --stdin -w -t tag)
897 test_atom refs/tags/taggerless type 'commit'
898 test_atom refs/tags/taggerless tag 'taggerless'
899 test_atom refs/tags/taggerless tagger ''
900 test_atom refs/tags/taggerless taggername ''
901 test_atom refs/tags/taggerless taggeremail ''
902 test_atom refs/tags/taggerless taggeremail:trim ''
903 test_atom refs/tags/taggerless taggeremail:localpart ''
904 test_atom refs/tags/taggerless taggerdate ''
905 test_atom refs/tags/taggerless committer ''
906 test_atom refs/tags/taggerless committername ''
907 test_atom refs/tags/taggerless committeremail ''
908 test_atom refs/tags/taggerless committeremail:trim ''
909 test_atom refs/tags/taggerless committeremail:localpart ''
910 test_atom refs/tags/taggerless committerdate ''
911 test_atom refs/tags/taggerless subject 'Broken tag'
913 test_expect_success 'an unusual tag with an incomplete line' '
915 git tag -m "bogo" bogo &&
916 bogo=$(git cat-file tag bogo) &&
917 bogo=$(printf "%s" "$bogo" | git mktag) &&
918 git tag -f bogo "$bogo" &&
919 git for-each-ref --format "%(body)" refs/tags/bogo
923 test_expect_success 'create tag with subject and body content' '
924 cat >>msg <<-\EOF &&
925 the subject line
927 first body line
928 second body line
930 git tag -F msg subject-body
932 test_atom refs/tags/subject-body subject 'the subject line'
933 test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
934 test_atom refs/tags/subject-body body 'first body line
935 second body line
937 test_atom refs/tags/subject-body contents 'the subject line
939 first body line
940 second body line
943 test_expect_success 'create tag with multiline subject' '
944 cat >msg <<-\EOF &&
945 first subject line
946 second subject line
948 first body line
949 second body line
951 git tag -F msg multiline
953 test_atom refs/tags/multiline subject 'first subject line second subject line'
954 test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
955 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
956 test_atom refs/tags/multiline body 'first body line
957 second body line
959 test_atom refs/tags/multiline contents:body 'first body line
960 second body line
962 test_atom refs/tags/multiline contents:signature ''
963 test_atom refs/tags/multiline contents 'first subject line
964 second subject line
966 first body line
967 second body line
970 test_expect_success GPG 'create signed tags' '
971 git tag -s -m "" signed-empty &&
972 git tag -s -m "subject line" signed-short &&
973 cat >msg <<-\EOF &&
974 subject line
976 body contents
978 git tag -s -F msg signed-long
981 sig='-----BEGIN PGP SIGNATURE-----
982 -----END PGP SIGNATURE-----
985 PREREQ=GPG
986 test_atom refs/tags/signed-empty subject ''
987 test_atom refs/tags/signed-empty subject:sanitize ''
988 test_atom refs/tags/signed-empty contents:subject ''
989 test_atom refs/tags/signed-empty body "$sig"
990 test_atom refs/tags/signed-empty contents:body ''
991 test_atom refs/tags/signed-empty contents:signature "$sig"
992 test_atom refs/tags/signed-empty contents "$sig"
994 test_expect_success GPG 'basic atom: refs/tags/signed-empty raw' '
995 git cat-file tag refs/tags/signed-empty >expected &&
996 git for-each-ref --format="%(raw)" refs/tags/signed-empty >actual &&
997 sanitize_pgp <expected >expected.clean &&
998 echo >>expected.clean &&
999 sanitize_pgp <actual >actual.clean &&
1000 test_cmp expected.clean actual.clean
1003 test_atom refs/tags/signed-short subject 'subject line'
1004 test_atom refs/tags/signed-short subject:sanitize 'subject-line'
1005 test_atom refs/tags/signed-short contents:subject 'subject line'
1006 test_atom refs/tags/signed-short body "$sig"
1007 test_atom refs/tags/signed-short contents:body ''
1008 test_atom refs/tags/signed-short contents:signature "$sig"
1009 test_atom refs/tags/signed-short contents "subject line
1010 $sig"
1012 test_expect_success GPG 'basic atom: refs/tags/signed-short raw' '
1013 git cat-file tag refs/tags/signed-short >expected &&
1014 git for-each-ref --format="%(raw)" refs/tags/signed-short >actual &&
1015 sanitize_pgp <expected >expected.clean &&
1016 echo >>expected.clean &&
1017 sanitize_pgp <actual >actual.clean &&
1018 test_cmp expected.clean actual.clean
1021 test_atom refs/tags/signed-long subject 'subject line'
1022 test_atom refs/tags/signed-long subject:sanitize 'subject-line'
1023 test_atom refs/tags/signed-long contents:subject 'subject line'
1024 test_atom refs/tags/signed-long body "body contents
1025 $sig"
1026 test_atom refs/tags/signed-long contents:body 'body contents
1028 test_atom refs/tags/signed-long contents:signature "$sig"
1029 test_atom refs/tags/signed-long contents "subject line
1031 body contents
1032 $sig"
1034 test_expect_success GPG 'basic atom: refs/tags/signed-long raw' '
1035 git cat-file tag refs/tags/signed-long >expected &&
1036 git for-each-ref --format="%(raw)" refs/tags/signed-long >actual &&
1037 sanitize_pgp <expected >expected.clean &&
1038 echo >>expected.clean &&
1039 sanitize_pgp <actual >actual.clean &&
1040 test_cmp expected.clean actual.clean
1043 test_expect_success 'set up refs pointing to tree and blob' '
1044 git update-ref refs/mytrees/first refs/heads/main^{tree} &&
1045 git update-ref refs/myblobs/first refs/heads/main:one
1048 test_atom refs/mytrees/first subject ""
1049 test_atom refs/mytrees/first contents:subject ""
1050 test_atom refs/mytrees/first body ""
1051 test_atom refs/mytrees/first contents:body ""
1052 test_atom refs/mytrees/first contents:signature ""
1053 test_atom refs/mytrees/first contents ""
1055 test_expect_success 'basic atom: refs/mytrees/first raw' '
1056 git cat-file tree refs/mytrees/first >expected &&
1057 echo >>expected &&
1058 git for-each-ref --format="%(raw)" refs/mytrees/first >actual &&
1059 test_cmp expected actual &&
1060 git cat-file -s refs/mytrees/first >expected &&
1061 git for-each-ref --format="%(raw:size)" refs/mytrees/first >actual &&
1062 test_cmp expected actual
1065 test_atom refs/myblobs/first subject ""
1066 test_atom refs/myblobs/first contents:subject ""
1067 test_atom refs/myblobs/first body ""
1068 test_atom refs/myblobs/first contents:body ""
1069 test_atom refs/myblobs/first contents:signature ""
1070 test_atom refs/myblobs/first contents ""
1072 test_expect_success 'basic atom: refs/myblobs/first raw' '
1073 git cat-file blob refs/myblobs/first >expected &&
1074 echo >>expected &&
1075 git for-each-ref --format="%(raw)" refs/myblobs/first >actual &&
1076 test_cmp expected actual &&
1077 git cat-file -s refs/myblobs/first >expected &&
1078 git for-each-ref --format="%(raw:size)" refs/myblobs/first >actual &&
1079 test_cmp expected actual
1082 test_expect_success 'set up refs pointing to binary blob' '
1083 printf "a\0b\0c" >blob1 &&
1084 printf "a\0c\0b" >blob2 &&
1085 printf "\0a\0b\0c" >blob3 &&
1086 printf "abc" >blob4 &&
1087 printf "\0 \0 \0 " >blob5 &&
1088 printf "\0 \0a\0 " >blob6 &&
1089 printf " " >blob7 &&
1090 >blob8 &&
1091 obj=$(git hash-object -w blob1) &&
1092 git update-ref refs/myblobs/blob1 "$obj" &&
1093 obj=$(git hash-object -w blob2) &&
1094 git update-ref refs/myblobs/blob2 "$obj" &&
1095 obj=$(git hash-object -w blob3) &&
1096 git update-ref refs/myblobs/blob3 "$obj" &&
1097 obj=$(git hash-object -w blob4) &&
1098 git update-ref refs/myblobs/blob4 "$obj" &&
1099 obj=$(git hash-object -w blob5) &&
1100 git update-ref refs/myblobs/blob5 "$obj" &&
1101 obj=$(git hash-object -w blob6) &&
1102 git update-ref refs/myblobs/blob6 "$obj" &&
1103 obj=$(git hash-object -w blob7) &&
1104 git update-ref refs/myblobs/blob7 "$obj" &&
1105 obj=$(git hash-object -w blob8) &&
1106 git update-ref refs/myblobs/blob8 "$obj"
1109 test_expect_success 'Verify sorts with raw' '
1110 cat >expected <<-EOF &&
1111 refs/myblobs/blob8
1112 refs/myblobs/blob5
1113 refs/myblobs/blob6
1114 refs/myblobs/blob3
1115 refs/myblobs/blob7
1116 refs/mytrees/first
1117 refs/myblobs/first
1118 refs/myblobs/blob1
1119 refs/myblobs/blob2
1120 refs/myblobs/blob4
1121 refs/heads/main
1123 git for-each-ref --format="%(refname)" --sort=raw \
1124 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1125 test_cmp expected actual
1128 test_expect_success 'Verify sorts with raw:size' '
1129 cat >expected <<-EOF &&
1130 refs/myblobs/blob8
1131 refs/myblobs/blob7
1132 refs/myblobs/blob4
1133 refs/myblobs/blob1
1134 refs/myblobs/blob2
1135 refs/myblobs/blob3
1136 refs/myblobs/blob5
1137 refs/myblobs/blob6
1138 refs/myblobs/first
1139 refs/mytrees/first
1140 refs/heads/main
1142 git for-each-ref --format="%(refname)" --sort=raw:size \
1143 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1144 test_cmp expected actual
1147 test_expect_success 'validate raw atom with %(if:equals)' '
1148 cat >expected <<-EOF &&
1149 not equals
1150 not equals
1151 not equals
1152 not equals
1153 not equals
1154 not equals
1155 refs/myblobs/blob4
1156 not equals
1157 not equals
1158 not equals
1159 not equals
1160 not equals
1162 git for-each-ref --format="%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)" \
1163 refs/myblobs/ refs/heads/ >actual &&
1164 test_cmp expected actual
1167 test_expect_success 'validate raw atom with %(if:notequals)' '
1168 cat >expected <<-EOF &&
1169 refs/heads/ambiguous
1170 refs/heads/main
1171 refs/heads/newtag
1172 refs/myblobs/blob1
1173 refs/myblobs/blob2
1174 refs/myblobs/blob3
1175 equals
1176 refs/myblobs/blob5
1177 refs/myblobs/blob6
1178 refs/myblobs/blob7
1179 refs/myblobs/blob8
1180 refs/myblobs/first
1182 git for-each-ref --format="%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)" \
1183 refs/myblobs/ refs/heads/ >actual &&
1184 test_cmp expected actual
1187 test_expect_success 'empty raw refs with %(if)' '
1188 cat >expected <<-EOF &&
1189 refs/myblobs/blob1 not empty
1190 refs/myblobs/blob2 not empty
1191 refs/myblobs/blob3 not empty
1192 refs/myblobs/blob4 not empty
1193 refs/myblobs/blob5 not empty
1194 refs/myblobs/blob6 not empty
1195 refs/myblobs/blob7 empty
1196 refs/myblobs/blob8 empty
1197 refs/myblobs/first not empty
1199 git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
1200 refs/myblobs/ >actual &&
1201 test_cmp expected actual
1204 test_expect_success '%(raw) with --python must fail' '
1205 test_must_fail git for-each-ref --format="%(raw)" --python
1208 test_expect_success '%(raw) with --tcl must fail' '
1209 test_must_fail git for-each-ref --format="%(raw)" --tcl
1212 test_expect_success '%(raw) with --perl' '
1213 git for-each-ref --format="\$name= %(raw);
1214 print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
1215 cmp blob1 actual &&
1216 git for-each-ref --format="\$name= %(raw);
1217 print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
1218 cmp blob3 actual &&
1219 git for-each-ref --format="\$name= %(raw);
1220 print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
1221 cmp blob8 actual &&
1222 git for-each-ref --format="\$name= %(raw);
1223 print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
1224 cmp one actual &&
1225 git cat-file tree refs/mytrees/first > expected &&
1226 git for-each-ref --format="\$name= %(raw);
1227 print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
1228 cmp expected actual
1231 test_expect_success '%(raw) with --shell must fail' '
1232 test_must_fail git for-each-ref --format="%(raw)" --shell
1235 test_expect_success '%(raw) with --shell and --sort=raw must fail' '
1236 test_must_fail git for-each-ref --format="%(raw)" --sort=raw --shell
1239 test_expect_success '%(raw:size) with --shell' '
1240 git for-each-ref --format="%(raw:size)" | sed "s/^/$SQ/;s/$/$SQ/" >expect &&
1241 git for-each-ref --format="%(raw:size)" --shell >actual &&
1242 test_cmp expect actual
1245 test_expect_success 'for-each-ref --format compare with cat-file --batch' '
1246 git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
1247 git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)
1248 %(raw)" refs/mytrees/first >actual &&
1249 test_cmp expected actual
1252 test_expect_success 'verify sorts with contents:size' '
1253 cat >expect <<-\EOF &&
1254 refs/heads/main
1255 refs/heads/newtag
1256 refs/heads/ambiguous
1258 git for-each-ref --format="%(refname)" \
1259 --sort=contents:size refs/heads/ >actual &&
1260 test_cmp expect actual
1263 test_expect_success 'set up multiple-sort tags' '
1264 for when in 100000 200000
1266 for email in user1 user2
1268 for ref in ref1 ref2
1270 GIT_COMMITTER_DATE="@$when +0000" \
1271 GIT_COMMITTER_EMAIL="$email@example.com" \
1272 git tag -m "tag $ref-$when-$email" \
1273 multi-$ref-$when-$email || return 1
1274 done
1275 done
1276 done
1279 test_expect_success 'Verify sort with multiple keys' '
1280 cat >expected <<-\EOF &&
1281 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1282 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1283 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1284 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1285 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1286 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1287 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1288 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1290 git for-each-ref \
1291 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1292 --sort=-refname \
1293 --sort=taggeremail \
1294 --sort=taggerdate \
1295 "refs/tags/multi-*" >actual &&
1296 test_cmp expected actual
1299 test_expect_success 'equivalent sorts fall back on refname' '
1300 cat >expected <<-\EOF &&
1301 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1302 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1303 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1304 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1305 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1306 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1307 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1308 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1310 git for-each-ref \
1311 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1312 --sort=taggerdate \
1313 "refs/tags/multi-*" >actual &&
1314 test_cmp expected actual
1317 test_expect_success '--no-sort cancels the previous sort keys' '
1318 cat >expected <<-\EOF &&
1319 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
1320 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
1321 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
1322 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
1323 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
1324 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
1325 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
1326 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
1328 git for-each-ref \
1329 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1330 --sort=-refname \
1331 --sort=taggeremail \
1332 --no-sort \
1333 --sort=taggerdate \
1334 "refs/tags/multi-*" >actual &&
1335 test_cmp expected actual
1338 test_expect_success '--no-sort without subsequent --sort prints expected refs' '
1339 cat >expected <<-\EOF &&
1340 refs/tags/multi-ref1-100000-user1
1341 refs/tags/multi-ref1-100000-user2
1342 refs/tags/multi-ref1-200000-user1
1343 refs/tags/multi-ref1-200000-user2
1344 refs/tags/multi-ref2-100000-user1
1345 refs/tags/multi-ref2-100000-user2
1346 refs/tags/multi-ref2-200000-user1
1347 refs/tags/multi-ref2-200000-user2
1350 # Sort the results with `sort` for a consistent comparison against
1351 # expected
1352 git for-each-ref \
1353 --format="%(refname)" \
1354 --no-sort \
1355 "refs/tags/multi-*" | sort >actual &&
1356 test_cmp expected actual
1359 test_expect_success 'set up custom date sorting' '
1360 # Dates:
1361 # - Wed Feb 07 2024 21:34:20 +0000
1362 # - Tue Dec 14 1999 00:05:22 +0000
1363 # - Fri Jun 04 2021 11:26:51 +0000
1364 # - Mon Jan 22 2007 16:44:01 GMT+0000
1365 i=1 &&
1366 for when in 1707341660 945129922 1622806011 1169484241
1368 GIT_COMMITTER_DATE="@$when +0000" \
1369 GIT_COMMITTER_EMAIL="user@example.com" \
1370 git tag -m "tag $when" custom-dates-$i &&
1371 i=$(($i+1)) || return 1
1372 done
1375 test_expect_success 'sort by date defaults to full timestamp' '
1376 cat >expected <<-\EOF &&
1377 945129922 refs/tags/custom-dates-2
1378 1169484241 refs/tags/custom-dates-4
1379 1622806011 refs/tags/custom-dates-3
1380 1707341660 refs/tags/custom-dates-1
1383 git for-each-ref \
1384 --format="%(creatordate:unix) %(refname)" \
1385 --sort=creatordate \
1386 "refs/tags/custom-dates-*" >actual &&
1387 test_cmp expected actual
1390 test_expect_success 'sort by custom date format' '
1391 cat >expected <<-\EOF &&
1392 00:05:22 refs/tags/custom-dates-2
1393 11:26:51 refs/tags/custom-dates-3
1394 16:44:01 refs/tags/custom-dates-4
1395 21:34:20 refs/tags/custom-dates-1
1398 git for-each-ref \
1399 --format="%(creatordate:format:%H:%M:%S) %(refname)" \
1400 --sort="creatordate:format:%H:%M:%S" \
1401 "refs/tags/custom-dates-*" >actual &&
1402 test_cmp expected actual
1405 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
1406 test_when_finished "git checkout main" &&
1407 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1408 sed -e "s/^\* / /" actual >expect &&
1409 git checkout --orphan orphaned-branch &&
1410 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1411 test_cmp expect actual
1414 cat >trailers <<EOF
1415 Reviewed-by: A U Thor <author@example.com>
1416 Signed-off-by: A U Thor <author@example.com>
1417 [ v2 updated patch description ]
1418 Acked-by: A U Thor
1419 <author@example.com>
1422 unfold () {
1423 perl -0pe 's/\n\s+/ /g'
1426 test_expect_success 'set up trailers for next test' '
1427 echo "Some contents" > two &&
1428 git add two &&
1429 git commit -F - <<-EOF
1430 trailers: this commit message has trailers
1432 Some message contents
1434 $(cat trailers)
1438 test_trailer_option () {
1439 title=$1 option=$2
1440 cat >expect
1441 test_expect_success "$title" '
1442 git for-each-ref --format="%($option)" refs/heads/main >actual &&
1443 test_cmp expect actual &&
1444 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1445 test_cmp expect actual
1449 test_trailer_option '%(trailers:unfold) unfolds trailers' \
1450 'trailers:unfold' <<-EOF
1451 $(unfold <trailers)
1455 test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
1456 'trailers:only' <<-EOF
1457 $(grep -v patch.description <trailers)
1461 test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \
1462 'trailers:only=no,only=true' <<-EOF
1463 $(grep -v patch.description <trailers)
1467 test_trailer_option '%(trailers:only=yes) shows only "key: value" trailers' \
1468 'trailers:only=yes' <<-EOF
1469 $(grep -v patch.description <trailers)
1473 test_trailer_option '%(trailers:only=no) shows all trailers' \
1474 'trailers:only=no' <<-EOF
1475 $(cat trailers)
1479 test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
1480 'trailers:only,unfold' <<-EOF
1481 $(grep -v patch.description <trailers | unfold)
1485 test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
1486 'trailers:unfold,only' <<-EOF
1487 $(grep -v patch.description <trailers | unfold)
1491 test_trailer_option '%(trailers:key=foo) shows that trailer' \
1492 'trailers:key=Signed-off-by' <<-EOF
1493 Signed-off-by: A U Thor <author@example.com>
1497 test_trailer_option '%(trailers:key=foo) is case insensitive' \
1498 'trailers:key=SiGned-oFf-bY' <<-EOF
1499 Signed-off-by: A U Thor <author@example.com>
1503 test_trailer_option '%(trailers:key=foo:) trailing colon also works' \
1504 'trailers:key=Signed-off-by:' <<-EOF
1505 Signed-off-by: A U Thor <author@example.com>
1509 test_trailer_option '%(trailers:key=foo) multiple keys' \
1510 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
1511 Reviewed-by: A U Thor <author@example.com>
1512 Signed-off-by: A U Thor <author@example.com>
1516 test_trailer_option '%(trailers:key=nonexistent) becomes empty' \
1517 'trailers:key=Shined-off-by:' <<-EOF
1521 test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \
1522 'trailers:key=Acked-by' <<-EOF
1523 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
1527 test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
1528 'trailers:key=Signed-Off-by,unfold' <<-EOF
1529 $(unfold <trailers | grep Signed-off-by)
1533 test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
1534 'trailers:key=Signed-off-by,only=no' <<-EOF
1535 Signed-off-by: A U Thor <author@example.com>
1536 $(grep patch.description <trailers)
1540 test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \
1541 'trailers:key=Signed-off-by,valueonly' <<-EOF
1542 A U Thor <author@example.com>
1546 test_trailer_option '%(trailers:separator) changes separator' \
1547 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1548 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
1551 test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \
1552 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1553 Reviewed-by,A U Thor <author@example.com>
1554 Signed-off-by,A U Thor <author@example.com>
1558 test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \
1559 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1560 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
1563 test_failing_trailer_option () {
1564 title=$1 option=$2
1565 cat >expect
1566 test_expect_success "$title" '
1567 # error message cannot be checked under i18n
1568 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
1569 test_cmp expect actual &&
1570 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
1571 test_cmp expect actual
1575 test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
1576 'trailers:unsupported' <<-\EOF
1577 fatal: unknown %(trailers) argument: unsupported
1580 test_failing_trailer_option '%(trailers:key) without value is error' \
1581 'trailers:key' <<-\EOF
1582 fatal: expected %(trailers:key=<value>)
1585 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
1586 cat >expect <<-EOF &&
1587 fatal: unrecognized %(contents) argument: trailersonly
1589 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
1590 test_cmp expect actual
1593 test_expect_success 'basic atom: head contents:trailers' '
1594 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
1595 sanitize_pgp <actual >actual.clean &&
1596 # git for-each-ref ends with a blank line
1597 cat >expect <<-EOF &&
1598 $(cat trailers)
1601 test_cmp expect actual.clean
1604 test_expect_success 'basic atom: rest must fail' '
1605 test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1608 test_expect_success 'HEAD atom does not take arguments' '
1609 test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
1610 echo "fatal: %(HEAD) does not take arguments" >expect &&
1611 test_cmp expect err
1614 test_expect_success 'subject atom rejects unknown arguments' '
1615 test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
1616 echo "fatal: unrecognized %(subject) argument: foo" >expect &&
1617 test_cmp expect err
1620 test_expect_success 'refname atom rejects unknown arguments' '
1621 test_must_fail git for-each-ref --format="%(refname:foo)" 2>err &&
1622 echo "fatal: unrecognized %(refname) argument: foo" >expect &&
1623 test_cmp expect err
1626 test_expect_success 'trailer parsing not fooled by --- line' '
1627 git commit --allow-empty -F - <<-\EOF &&
1628 this is the subject
1630 This is the body. The message has a "---" line which would confuse a
1631 message+patch parser. But here we know we have only a commit message,
1632 so we get it right.
1634 trailer: wrong
1636 This is more body.
1638 trailer: right
1642 echo "trailer: right" &&
1643 echo
1644 } >expect &&
1645 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
1646 test_cmp expect actual
1649 test_expect_success 'Add symbolic ref for the following tests' '
1650 git symbolic-ref refs/heads/sym refs/heads/main
1653 cat >expected <<EOF
1654 refs/heads/main
1657 test_expect_success 'Verify usage of %(symref) atom' '
1658 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1659 test_cmp expected actual
1662 cat >expected <<EOF
1663 heads/main
1666 test_expect_success 'Verify usage of %(symref:short) atom' '
1667 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1668 test_cmp expected actual
1671 cat >expected <<EOF
1672 main
1673 heads/main
1676 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1677 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1678 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
1679 test_cmp expected actual &&
1681 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1682 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
1683 test_cmp expected actual
1686 cat >expected <<EOF
1687 refs
1688 refs/heads
1691 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1692 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1693 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1694 test_cmp expected actual
1697 test_expect_success ':remotename and :remoteref' '
1698 git init remote-tests &&
1700 cd remote-tests &&
1701 test_commit initial &&
1702 git branch -M main &&
1703 git remote add from fifth.coffee:blub &&
1704 git config branch.main.remote from &&
1705 git config branch.main.merge refs/heads/stable &&
1706 git remote add to southridge.audio:repo &&
1707 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
1708 git config branch.main.pushRemote to &&
1709 for pair in "%(upstream)=refs/remotes/from/stable" \
1710 "%(upstream:remotename)=from" \
1711 "%(upstream:remoteref)=refs/heads/stable" \
1712 "%(push)=refs/remotes/to/pushed/main" \
1713 "%(push:remotename)=to" \
1714 "%(push:remoteref)=refs/heads/pushed/main"
1716 echo "${pair#*=}" >expect &&
1717 git for-each-ref --format="${pair%=*}" \
1718 refs/heads/main >actual &&
1719 test_cmp expect actual || exit 1
1720 done &&
1721 git branch push-simple &&
1722 git config branch.push-simple.pushRemote from &&
1723 actual="$(git for-each-ref \
1724 --format="%(push:remotename),%(push:remoteref)" \
1725 refs/heads/push-simple)" &&
1726 test from, = "$actual"
1730 test_expect_success 'for-each-ref --ignore-case ignores case' '
1731 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
1732 test_must_be_empty actual &&
1734 echo refs/heads/main >expect &&
1735 git for-each-ref --format="%(refname)" --ignore-case \
1736 refs/heads/MAIN >actual &&
1737 test_cmp expect actual
1740 test_expect_success 'for-each-ref --omit-empty works' '
1741 git for-each-ref --format="%(refname)" >actual &&
1742 test_line_count -gt 1 actual &&
1743 git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
1744 echo refs/heads/main >expect &&
1745 test_cmp expect actual
1748 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1749 # name refs numerically to avoid case-insensitive filesystem conflicts
1750 nr=0 &&
1751 for email in a A b B
1753 for subject in a A b B
1755 GIT_COMMITTER_EMAIL="$email@example.com" \
1756 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1757 nr=$((nr+1))||
1758 return 1
1759 done
1760 done &&
1761 git for-each-ref --ignore-case \
1762 --format="%(taggeremail) %(subject) %(refname)" \
1763 --sort=refname \
1764 --sort=subject \
1765 --sort=taggeremail \
1766 refs/tags/icase-* >actual &&
1767 cat >expect <<-\EOF &&
1768 <a@example.com> tag a refs/tags/icase-00
1769 <a@example.com> tag A refs/tags/icase-01
1770 <A@example.com> tag a refs/tags/icase-04
1771 <A@example.com> tag A refs/tags/icase-05
1772 <a@example.com> tag b refs/tags/icase-02
1773 <a@example.com> tag B refs/tags/icase-03
1774 <A@example.com> tag b refs/tags/icase-06
1775 <A@example.com> tag B refs/tags/icase-07
1776 <b@example.com> tag a refs/tags/icase-08
1777 <b@example.com> tag A refs/tags/icase-09
1778 <B@example.com> tag a refs/tags/icase-12
1779 <B@example.com> tag A refs/tags/icase-13
1780 <b@example.com> tag b refs/tags/icase-10
1781 <b@example.com> tag B refs/tags/icase-11
1782 <B@example.com> tag b refs/tags/icase-14
1783 <B@example.com> tag B refs/tags/icase-15
1785 test_cmp expect actual
1788 test_expect_success 'for-each-ref reports broken tags' '
1789 git tag -m "good tag" broken-tag-good HEAD &&
1790 git cat-file tag broken-tag-good >good &&
1791 sed s/commit/blob/ <good >bad &&
1792 bad=$(git hash-object -w -t tag bad) &&
1793 git update-ref refs/tags/broken-tag-bad $bad &&
1794 test_must_fail git for-each-ref --format="%(*objectname)" \
1795 refs/tags/broken-tag-*
1798 test_expect_success 'set up tag with signature and no blank lines' '
1799 git tag -F - fake-sig-no-blanks <<-\EOF
1800 this is the subject
1801 -----BEGIN PGP SIGNATURE-----
1802 not a real signature, but we just care about the
1803 subject/body parsing. It is important here that
1804 there are no blank lines in the signature.
1805 -----END PGP SIGNATURE-----
1809 test_atom refs/tags/fake-sig-no-blanks contents:subject 'this is the subject'
1810 test_atom refs/tags/fake-sig-no-blanks contents:body ''
1811 test_atom refs/tags/fake-sig-no-blanks contents:signature "$sig"
1813 test_expect_success 'set up tag with CRLF signature' '
1814 append_cr <<-\EOF |
1815 this is the subject
1816 -----BEGIN PGP SIGNATURE-----
1818 not a real signature, but we just care about
1819 the subject/body parsing. It is important here
1820 that there is a blank line separating this
1821 from the signature header.
1822 -----END PGP SIGNATURE-----
1824 git tag -F - --cleanup=verbatim fake-sig-crlf
1827 test_atom refs/tags/fake-sig-crlf contents:subject 'this is the subject'
1828 test_atom refs/tags/fake-sig-crlf contents:body ''
1830 # CRLF is retained in the signature, so we have to pass our expected value
1831 # through append_cr. But test_atom requires a shell string, which means command
1832 # substitution, and the shell will strip trailing newlines from the output of
1833 # the substitution. Hack around it by adding and then removing a dummy line.
1834 sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
1835 sig_crlf=${sig_crlf%dummy}
1836 test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
1838 test_expect_success 'git for-each-ref --stdin: empty' '
1839 >in &&
1840 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1841 git for-each-ref --format="%(refname)" >expect &&
1842 test_cmp expect actual
1845 test_expect_success 'git for-each-ref --stdin: fails if extra args' '
1846 >in &&
1847 test_must_fail git for-each-ref --format="%(refname)" \
1848 --stdin refs/heads/extra <in 2>err &&
1849 grep "unknown arguments supplied with --stdin" err
1852 test_expect_success 'git for-each-ref --stdin: matches' '
1853 cat >in <<-EOF &&
1854 refs/tags/multi*
1855 refs/heads/amb*
1858 cat >expect <<-EOF &&
1859 refs/heads/ambiguous
1860 refs/tags/multi-ref1-100000-user1
1861 refs/tags/multi-ref1-100000-user2
1862 refs/tags/multi-ref1-200000-user1
1863 refs/tags/multi-ref1-200000-user2
1864 refs/tags/multi-ref2-100000-user1
1865 refs/tags/multi-ref2-100000-user2
1866 refs/tags/multi-ref2-200000-user1
1867 refs/tags/multi-ref2-200000-user2
1868 refs/tags/multiline
1871 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1872 test_cmp expect actual
1875 test_expect_success 'git for-each-ref with non-existing refs' '
1876 cat >in <<-EOF &&
1877 refs/heads/this-ref-does-not-exist
1878 refs/tags/bogus
1881 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1882 test_must_be_empty actual &&
1884 xargs git for-each-ref --format="%(refname)" <in >actual &&
1885 test_must_be_empty actual
1888 test_expect_success 'git for-each-ref with nested tags' '
1889 git tag -am "Normal tag" nested/base HEAD &&
1890 git tag -am "Nested tag" nested/nest1 refs/tags/nested/base &&
1891 git tag -am "Double nested tag" nested/nest2 refs/tags/nested/nest1 &&
1893 head_oid="$(git rev-parse HEAD)" &&
1894 base_tag_oid="$(git rev-parse refs/tags/nested/base)" &&
1895 nest1_tag_oid="$(git rev-parse refs/tags/nested/nest1)" &&
1896 nest2_tag_oid="$(git rev-parse refs/tags/nested/nest2)" &&
1898 cat >expect <<-EOF &&
1899 refs/tags/nested/base $base_tag_oid tag $head_oid commit
1900 refs/tags/nested/nest1 $nest1_tag_oid tag $head_oid commit
1901 refs/tags/nested/nest2 $nest2_tag_oid tag $head_oid commit
1904 git for-each-ref \
1905 --format="%(refname) %(objectname) %(objecttype) %(*objectname) %(*objecttype)" \
1906 refs/tags/nested/ >actual &&
1907 test_cmp expect actual
1910 GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1911 TRUSTLEVEL_FORMAT="%(signature:trustlevel)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1913 test_expect_success GPG 'setup for signature atom using gpg' '
1914 git checkout -b signed &&
1916 test_when_finished "test_unconfig commit.gpgSign" &&
1918 echo "1" >file &&
1919 git add file &&
1920 test_tick &&
1921 git commit -S -m "file: 1" &&
1922 git tag first-signed &&
1924 echo "2" >file &&
1925 test_tick &&
1926 git commit -a -m "file: 2" &&
1927 git tag second-unsigned &&
1929 git config commit.gpgSign 1 &&
1930 echo "3" >file &&
1931 test_tick &&
1932 git commit -a --no-gpg-sign -m "file: 3" &&
1933 git tag third-unsigned &&
1935 test_tick &&
1936 git rebase -f HEAD^^ && git tag second-signed HEAD^ &&
1937 git tag third-signed &&
1939 echo "4" >file &&
1940 test_tick &&
1941 git commit -a -SB7227189 -m "file: 4" &&
1942 git tag fourth-signed &&
1944 echo "5" >file &&
1945 test_tick &&
1946 git commit -a --no-gpg-sign -m "file: 5" &&
1947 git tag fifth-unsigned &&
1949 echo "6" >file &&
1950 test_tick &&
1951 git commit -a --no-gpg-sign -m "file: 6" &&
1953 test_tick &&
1954 git rebase -f HEAD^^ &&
1955 git tag fifth-signed HEAD^ &&
1956 git tag sixth-signed &&
1958 echo "7" >file &&
1959 test_tick &&
1960 git commit -a --no-gpg-sign -m "file: 7" &&
1961 git tag seventh-unsigned
1964 test_expect_success GPGSSH 'setup for signature atom using ssh' '
1965 test_when_finished "test_unconfig gpg.format user.signingkey" &&
1967 test_config gpg.format ssh &&
1968 test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
1969 echo "8" >file &&
1970 test_tick &&
1971 git add file &&
1972 git commit -S -m "file: 8" &&
1973 git tag eighth-signed-ssh
1976 test_expect_success GPG2 'bare signature atom' '
1977 git verify-commit first-signed 2>expect &&
1978 echo >>expect &&
1979 git for-each-ref refs/tags/first-signed \
1980 --format="%(signature)" >actual &&
1981 test_cmp expect actual
1984 test_expect_success GPG 'show good signature with custom format' '
1985 git verify-commit first-signed &&
1986 cat >expect <<-\EOF &&
1988 13B6F51ECDDE430D
1989 C O Mitter <committer@example.com>
1990 73D758744BE721698EC54E8713B6F51ECDDE430D
1991 73D758744BE721698EC54E8713B6F51ECDDE430D
1993 git for-each-ref refs/tags/first-signed \
1994 --format="$GRADE_FORMAT" >actual &&
1995 test_cmp expect actual
1997 test_expect_success GPGSSH 'show good signature with custom format
1998 with ssh' '
1999 test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
2000 FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
2001 cat >expect.tmpl <<-\EOF &&
2003 FINGERPRINT
2004 principal with number 1
2005 FINGERPRINT
2008 sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
2009 git for-each-ref refs/tags/eighth-signed-ssh \
2010 --format="$GRADE_FORMAT" >actual &&
2011 test_cmp expect actual
2014 test_expect_success GPG 'signature atom with grade option and bad signature' '
2015 git cat-file commit third-signed >raw &&
2016 sed -e "s/^file: 3/file: 3 forged/" raw >forged1 &&
2017 FORGED1=$(git hash-object -w -t commit forged1) &&
2018 git update-ref refs/tags/third-signed "$FORGED1" &&
2019 test_must_fail git verify-commit "$FORGED1" &&
2021 cat >expect <<-\EOF &&
2023 13B6F51ECDDE430D
2024 C O Mitter <committer@example.com>
2028 git for-each-ref refs/tags/third-signed \
2029 --format="$GRADE_FORMAT" >actual &&
2030 test_cmp expect actual
2033 test_expect_success GPG 'show untrusted signature with custom format' '
2034 cat >expect <<-\EOF &&
2036 65A0EEA02E30CAD7
2037 Eris Discordia <discord@example.net>
2038 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
2039 D4BE22311AD3131E5EDA29A461092E85B7227189
2041 git for-each-ref refs/tags/fourth-signed \
2042 --format="$GRADE_FORMAT" >actual &&
2043 test_cmp expect actual
2046 test_expect_success GPG 'show untrusted signature with undefined trust level' '
2047 cat >expect <<-\EOF &&
2048 undefined
2049 65A0EEA02E30CAD7
2050 Eris Discordia <discord@example.net>
2051 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
2052 D4BE22311AD3131E5EDA29A461092E85B7227189
2054 git for-each-ref refs/tags/fourth-signed \
2055 --format="$TRUSTLEVEL_FORMAT" >actual &&
2056 test_cmp expect actual
2059 test_expect_success GPG 'show untrusted signature with ultimate trust level' '
2060 cat >expect <<-\EOF &&
2061 ultimate
2062 13B6F51ECDDE430D
2063 C O Mitter <committer@example.com>
2064 73D758744BE721698EC54E8713B6F51ECDDE430D
2065 73D758744BE721698EC54E8713B6F51ECDDE430D
2067 git for-each-ref refs/tags/sixth-signed \
2068 --format="$TRUSTLEVEL_FORMAT" >actual &&
2069 test_cmp expect actual
2072 test_expect_success GPG 'show unknown signature with custom format' '
2073 cat >expect <<-\EOF &&
2075 13B6F51ECDDE430D
2080 GNUPGHOME="$GNUPGHOME_NOT_USED" git for-each-ref \
2081 refs/tags/sixth-signed --format="$GRADE_FORMAT" >actual &&
2082 test_cmp expect actual
2085 test_expect_success GPG 'show lack of signature with custom format' '
2086 cat >expect <<-\EOF &&
2093 git for-each-ref refs/tags/seventh-unsigned \
2094 --format="$GRADE_FORMAT" >actual &&
2095 test_cmp expect actual
2098 test_done