]> git.ipfire.org Git - thirdparty/git.git/blob - t/t6300-for-each-ref.sh
submodule-config.c: strengthen URL fsck check
[thirdparty/git.git] / t / t6300-for-each-ref.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Andy Parkins
4 #
5
6 test_description='for-each-ref test'
7
8 . ./test-lib.sh
9 GNUPGHOME_NOT_USED=$GNUPGHOME
10 . "$TEST_DIRECTORY"/lib-gpg.sh
11 . "$TEST_DIRECTORY"/lib-terminal.sh
12
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
21 }
22
23 test_expect_success setup '
24 test_oid_cache <<-EOF &&
25 disklen sha1:138
26 disklen sha256:154
27 EOF
28
29 # setup .mailmap
30 cat >.mailmap <<-EOF &&
31 A Thor <athor@example.com> A U Thor <author@example.com>
32 C Mitter <cmitter@example.com> C O Mitter <committer@example.com>
33 EOF
34
35 setdate_and_increment &&
36 echo "Using $datestamp" > one &&
37 git add one &&
38 git commit -m "Initial" &&
39 git branch -M main &&
40 setdate_and_increment &&
41 git tag -a -m "Tagging at $datestamp" testtag &&
42 git update-ref refs/remotes/origin/main main &&
43 git remote add origin nowhere &&
44 git config branch.main.remote origin &&
45 git config branch.main.merge refs/heads/main &&
46 git remote add myfork elsewhere &&
47 git config remote.pushdefault myfork &&
48 git config push.default current
49 '
50
51 test_atom () {
52 case "$1" in
53 head) ref=refs/heads/main ;;
54 tag) ref=refs/tags/testtag ;;
55 sym) ref=refs/heads/sym ;;
56 *) ref=$1 ;;
57 esac
58 format=$2
59 test_do=test_expect_${4:-success}
60
61 printf '%s\n' "$3" >expected
62 $test_do $PREREQ "basic atom: $ref $format" '
63 git for-each-ref --format="%($format)" "$ref" >actual &&
64 sanitize_pgp <actual >actual.clean &&
65 test_cmp expected actual.clean
66 '
67
68 # Automatically test "contents:size" atom after testing "contents"
69 if test "$format" = "contents"
70 then
71 # for commit leg, $3 is changed there
72 expect=$(printf '%s' "$3" | wc -c)
73 $test_do $PREREQ "basic atom: $ref contents:size" '
74 type=$(git cat-file -t "$ref") &&
75 case $type in
76 tag)
77 # We cannot use $3 as it expects sanitize_pgp to run
78 git cat-file tag $ref >out &&
79 expect=$(tail -n +6 out | wc -c) &&
80 rm -f out ;;
81 tree | blob)
82 expect="" ;;
83 commit)
84 : "use the calculated expect" ;;
85 *)
86 BUG "unknown object type" ;;
87 esac &&
88 # Leave $expect unquoted to lose possible leading whitespaces
89 echo $expect >expected &&
90 git for-each-ref --format="%(contents:size)" "$ref" >actual &&
91 test_cmp expected actual
92 '
93 fi
94 }
95
96 hexlen=$(test_oid hexsz)
97 disklen=$(test_oid disklen)
98
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 $disklen
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)
150 "
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
197 '
198 test_atom head HEAD '*'
199
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 $disklen
207 test_atom tag '*objectsize:disk' $disklen
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
279 '
280 test_atom tag HEAD ' '
281
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
289 '
290
291 test_expect_success 'Check invalid atoms names are errors' '
292 test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
293 '
294
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
300 '
301
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
309 '
310
311 test_expect_success 'Check invalid format specifiers are errors' '
312 test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
313 '
314
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)"
319 '
320
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}
330
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
336 '
337 }
338
339 test_bad_atom head 'authoremail:foo' \
340 'fatal: unrecognized %(authoremail) argument: foo'
341
342 test_bad_atom head 'authoremail:mailmap,trim,bar' \
343 'fatal: unrecognized %(authoremail) argument: bar'
344
345 test_bad_atom head 'authoremail:trim,' \
346 'fatal: unrecognized %(authoremail) argument: '
347
348 test_bad_atom head 'authoremail:mailmaptrim' \
349 'fatal: unrecognized %(authoremail) argument: trim'
350
351 test_bad_atom head 'committeremail: ' \
352 'fatal: unrecognized %(committeremail) argument: '
353
354 test_bad_atom head 'committeremail: trim,foo' \
355 'fatal: unrecognized %(committeremail) argument: trim,foo'
356
357 test_bad_atom head 'committeremail:mailmap,localpart ' \
358 'fatal: unrecognized %(committeremail) argument: '
359
360 test_bad_atom head 'committeremail:trim_localpart' \
361 'fatal: unrecognized %(committeremail) argument: _localpart'
362
363 test_bad_atom head 'committeremail:localpart,,,trim' \
364 'fatal: unrecognized %(committeremail) argument: ,,trim'
365
366 test_bad_atom tag 'taggeremail:mailmap,trim, foo ' \
367 'fatal: unrecognized %(taggeremail) argument: foo '
368
369 test_bad_atom tag 'taggeremail:trim,localpart,' \
370 'fatal: unrecognized %(taggeremail) argument: '
371
372 test_bad_atom tag 'taggeremail:mailmap;localpart trim' \
373 'fatal: unrecognized %(taggeremail) argument: ;localpart trim'
374
375 test_bad_atom tag 'taggeremail:localpart trim' \
376 'fatal: unrecognized %(taggeremail) argument: trim'
377
378 test_bad_atom tag 'taggeremail:mailmap,mailmap,trim,qux,localpart,trim' \
379 'fatal: unrecognized %(taggeremail) argument: qux,localpart,trim'
380
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'
389 EOF
390 (
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
399 }
400
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"
406 '
407
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"
413 '
414
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"
417 '
418
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
427 '
428
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)"
435 '
436
437 test_expect_success 'Check format "short" date fields output' '
438 test_date short 2006-07-04 2006-07-04 2006-07-04
439 '
440
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
443 '
444
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"
450 '
451
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"
457 '
458
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"
461 '
462
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"
468 '
469
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"
472 '
473
474 test_expect_success 'Check format "raw" date fields output' '
475 test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
476 '
477
478 test_expect_success 'Check format "raw-local" date fields output' '
479 test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
480 '
481
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
488 '
489
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
496 '
497
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
506 '
507
508 cat >expected <<\EOF
509 refs/heads/main
510 refs/remotes/origin/main
511 refs/tags/testtag
512 EOF
513
514 test_expect_success 'Verify ascending sort' '
515 git for-each-ref --format="%(refname)" --sort=refname >actual &&
516 test_cmp expected actual
517 '
518
519
520 cat >expected <<\EOF
521 refs/tags/testtag
522 refs/remotes/origin/main
523 refs/heads/main
524 EOF
525
526 test_expect_success 'Verify descending sort' '
527 git for-each-ref --format="%(refname)" --sort=-refname >actual &&
528 test_cmp expected actual
529 '
530
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
534 '
535
536 cat >expected <<\EOF
537 refs/tags/testtag
538 refs/tags/testtag-2
539 EOF
540
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
547 '
548
549 cat >expected <<\EOF
550 refs/tags/testtag
551 refs/tags/testtag-2
552 EOF
553
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
560 '
561
562 cat >expected <<\EOF
563 refs/tags/bar
564 refs/tags/baz
565 refs/tags/testtag
566 EOF
567
568 test_expect_success 'exercise patterns with prefix exclusions' '
569 for tag in foo/one foo/two foo/three bar baz
570 do
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
577 '
578
579 cat >expected <<\EOF
580 refs/tags/bar
581 refs/tags/baz
582 refs/tags/foo/one
583 refs/tags/testtag
584 EOF
585
586 test_expect_success 'exercise patterns with pattern exclusions' '
587 for tag in foo/one foo/two foo/three bar baz
588 do
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
595 '
596
597 cat >expected <<\EOF
598 'refs/heads/main'
599 'refs/remotes/origin/main'
600 'refs/tags/testtag'
601 EOF
602
603 test_expect_success 'Quoting style: shell' '
604 git for-each-ref --shell --format="%(refname)" >actual &&
605 test_cmp expected actual
606 '
607
608 test_expect_success 'Quoting style: perl' '
609 git for-each-ref --perl --format="%(refname)" >actual &&
610 test_cmp expected actual
611 '
612
613 test_expect_success 'Quoting style: python' '
614 git for-each-ref --python --format="%(refname)" >actual &&
615 test_cmp expected actual
616 '
617
618 cat >expected <<\EOF
619 "refs/heads/main"
620 "refs/remotes/origin/main"
621 "refs/tags/testtag"
622 EOF
623
624 test_expect_success 'Quoting style: tcl' '
625 git for-each-ref --tcl --format="%(refname)" >actual &&
626 test_cmp expected actual
627 '
628
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
633 "
634 done
635
636 test_expect_success 'setup for upstream:track[short]' '
637 test_commit two
638 '
639
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'
644
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
649 '
650
651 test_atom head push:track '[behind 1]'
652 test_atom head push:trackshort '<'
653
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
657 '
658
659 test_expect_success 'Check that :track[short] works when upstream is invalid' '
660 cat >expected <<-\EOF &&
661 [gone]
662
663 EOF
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
670 '
671
672 test_expect_success 'Check for invalid refname format' '
673 test_must_fail git for-each-ref --format="%(refname:INVALID)"
674 '
675
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>
684 EOF
685 sed "s/<[^>]*>//g" <expected.color >expected.bare &&
686 color_format="%(objectname:short) %(color:green)%(refname:short)"
687 '
688
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
693 '
694
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
698 '
699
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
704 '
705
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
709 '
710
711 test_expect_success 'setup for describe atom tests' '
712 git init -b master describe-repo &&
713 (
714 cd describe-repo &&
715
716 test_commit --no-tag one &&
717 git tag tagone &&
718
719 test_commit --no-tag two &&
720 git tag -a -m "tag two" tagtwo
721 )
722 '
723
724 test_expect_success 'describe atom vs git describe' '
725 (
726 cd describe-repo &&
727
728 git for-each-ref --format="%(objectname)" \
729 refs/tags/ >obj &&
730 while read hash
731 do
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 &&
742
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
747 )
748 '
749
750 test_expect_success 'describe:tags vs describe --tags' '
751 (
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
757 )
758 '
759
760 test_expect_success 'describe:abbrev=... vs describe --abbrev=...' '
761 (
762 cd describe-repo &&
763
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 &&
771
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) &&
775
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)
784 )
785 '
786
787 test_expect_success 'describe:match=... vs describe --match ...' '
788 (
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
795 )
796 '
797
798 test_expect_success 'describe:exclude:... vs describe --exclude ...' '
799 (
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
806 )
807 '
808
809 test_expect_success 'deref with describe atom' '
810 (
811 cd describe-repo &&
812 cat >expect <<-\EOF &&
813
814 tagname
815 tagname
816 tagname
817
818 tagtwo
819 EOF
820 git for-each-ref --format="%(*describe)" >actual &&
821 test_cmp expect actual
822 )
823 '
824
825 test_expect_success 'err on bad describe atom arg' '
826 (
827 cd describe-repo &&
828
829 # The bad arg is the only arg passed to describe atom
830 cat >expect <<-\EOF &&
831 fatal: unrecognized %(describe) argument: baz
832 EOF
833 test_must_fail git for-each-ref --format="%(describe:baz)" \
834 refs/heads/master 2>actual &&
835 test_cmp expect actual &&
836
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
841 EOF
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
846 )
847 '
848
849 cat >expected <<\EOF
850 heads/main
851 tags/main
852 EOF
853
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
864 '
865
866 cat >expected <<\EOF
867 heads/main
868 main
869 EOF
870
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
875 '
876
877 cat >expected <<\EOF
878 heads/ambiguous
879 ambiguous
880 EOF
881
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
888 '
889
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)
895 '
896
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'
912
913 test_expect_success 'an unusual tag with an incomplete line' '
914
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
920
921 '
922
923 test_expect_success 'create tag with subject and body content' '
924 cat >>msg <<-\EOF &&
925 the subject line
926
927 first body line
928 second body line
929 EOF
930 git tag -F msg subject-body
931 '
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
936 '
937 test_atom refs/tags/subject-body contents 'the subject line
938
939 first body line
940 second body line
941 '
942
943 test_expect_success 'create tag with multiline subject' '
944 cat >msg <<-\EOF &&
945 first subject line
946 second subject line
947
948 first body line
949 second body line
950 EOF
951 git tag -F msg multiline
952 '
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
958 '
959 test_atom refs/tags/multiline contents:body 'first body line
960 second body line
961 '
962 test_atom refs/tags/multiline contents:signature ''
963 test_atom refs/tags/multiline contents 'first subject line
964 second subject line
965
966 first body line
967 second body line
968 '
969
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
975
976 body contents
977 EOF
978 git tag -s -F msg signed-long
979 '
980
981 sig='-----BEGIN PGP SIGNATURE-----
982 -----END PGP SIGNATURE-----
983 '
984
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"
993
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
1001 '
1002
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"
1011
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
1019 '
1020
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
1027 '
1028 test_atom refs/tags/signed-long contents:signature "$sig"
1029 test_atom refs/tags/signed-long contents "subject line
1030
1031 body contents
1032 $sig"
1033
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
1041 '
1042
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
1046 '
1047
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 ""
1054
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
1063 '
1064
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 ""
1071
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
1080 '
1081
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"
1107 '
1108
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
1122 EOF
1123 git for-each-ref --format="%(refname)" --sort=raw \
1124 refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
1125 test_cmp expected actual
1126 '
1127
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
1141 EOF
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
1145 '
1146
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
1161 EOF
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
1165 '
1166
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
1181 EOF
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
1185 '
1186
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
1198 EOF
1199 git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
1200 refs/myblobs/ >actual &&
1201 test_cmp expected actual
1202 '
1203
1204 test_expect_success '%(raw) with --python must fail' '
1205 test_must_fail git for-each-ref --format="%(raw)" --python
1206 '
1207
1208 test_expect_success '%(raw) with --tcl must fail' '
1209 test_must_fail git for-each-ref --format="%(raw)" --tcl
1210 '
1211
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
1229 '
1230
1231 test_expect_success '%(raw) with --shell must fail' '
1232 test_must_fail git for-each-ref --format="%(raw)" --shell
1233 '
1234
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
1237 '
1238
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
1243 '
1244
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
1250 '
1251
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
1257 EOF
1258 git for-each-ref --format="%(refname)" \
1259 --sort=contents:size refs/heads/ >actual &&
1260 test_cmp expect actual
1261 '
1262
1263 test_expect_success 'set up multiple-sort tags' '
1264 for when in 100000 200000
1265 do
1266 for email in user1 user2
1267 do
1268 for ref in ref1 ref2
1269 do
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
1277 '
1278
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
1289 EOF
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
1297 '
1298
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
1309 EOF
1310 git for-each-ref \
1311 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
1312 --sort=taggerdate \
1313 "refs/tags/multi-*" >actual &&
1314 test_cmp expected actual
1315 '
1316
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
1327 EOF
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
1336 '
1337
1338 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
1339 test_when_finished "git checkout main" &&
1340 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1341 sed -e "s/^\* / /" actual >expect &&
1342 git checkout --orphan orphaned-branch &&
1343 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
1344 test_cmp expect actual
1345 '
1346
1347 cat >trailers <<EOF
1348 Reviewed-by: A U Thor <author@example.com>
1349 Signed-off-by: A U Thor <author@example.com>
1350 [ v2 updated patch description ]
1351 Acked-by: A U Thor
1352 <author@example.com>
1353 EOF
1354
1355 unfold () {
1356 perl -0pe 's/\n\s+/ /g'
1357 }
1358
1359 test_expect_success 'set up trailers for next test' '
1360 echo "Some contents" > two &&
1361 git add two &&
1362 git commit -F - <<-EOF
1363 trailers: this commit message has trailers
1364
1365 Some message contents
1366
1367 $(cat trailers)
1368 EOF
1369 '
1370
1371 test_trailer_option () {
1372 title=$1 option=$2
1373 cat >expect
1374 test_expect_success "$title" '
1375 git for-each-ref --format="%($option)" refs/heads/main >actual &&
1376 test_cmp expect actual &&
1377 git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1378 test_cmp expect actual
1379 '
1380 }
1381
1382 test_trailer_option '%(trailers:unfold) unfolds trailers' \
1383 'trailers:unfold' <<-EOF
1384 $(unfold <trailers)
1385
1386 EOF
1387
1388 test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
1389 'trailers:only' <<-EOF
1390 $(grep -v patch.description <trailers)
1391
1392 EOF
1393
1394 test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \
1395 'trailers:only=no,only=true' <<-EOF
1396 $(grep -v patch.description <trailers)
1397
1398 EOF
1399
1400 test_trailer_option '%(trailers:only=yes) shows only "key: value" trailers' \
1401 'trailers:only=yes' <<-EOF
1402 $(grep -v patch.description <trailers)
1403
1404 EOF
1405
1406 test_trailer_option '%(trailers:only=no) shows all trailers' \
1407 'trailers:only=no' <<-EOF
1408 $(cat trailers)
1409
1410 EOF
1411
1412 test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
1413 'trailers:only,unfold' <<-EOF
1414 $(grep -v patch.description <trailers | unfold)
1415
1416 EOF
1417
1418 test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
1419 'trailers:unfold,only' <<-EOF
1420 $(grep -v patch.description <trailers | unfold)
1421
1422 EOF
1423
1424 test_trailer_option '%(trailers:key=foo) shows that trailer' \
1425 'trailers:key=Signed-off-by' <<-EOF
1426 Signed-off-by: A U Thor <author@example.com>
1427
1428 EOF
1429
1430 test_trailer_option '%(trailers:key=foo) is case insensitive' \
1431 'trailers:key=SiGned-oFf-bY' <<-EOF
1432 Signed-off-by: A U Thor <author@example.com>
1433
1434 EOF
1435
1436 test_trailer_option '%(trailers:key=foo:) trailing colon also works' \
1437 'trailers:key=Signed-off-by:' <<-EOF
1438 Signed-off-by: A U Thor <author@example.com>
1439
1440 EOF
1441
1442 test_trailer_option '%(trailers:key=foo) multiple keys' \
1443 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
1444 Reviewed-by: A U Thor <author@example.com>
1445 Signed-off-by: A U Thor <author@example.com>
1446
1447 EOF
1448
1449 test_trailer_option '%(trailers:key=nonexistent) becomes empty' \
1450 'trailers:key=Shined-off-by:' <<-EOF
1451
1452 EOF
1453
1454 test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \
1455 'trailers:key=Acked-by' <<-EOF
1456 $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
1457
1458 EOF
1459
1460 test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
1461 'trailers:key=Signed-Off-by,unfold' <<-EOF
1462 $(unfold <trailers | grep Signed-off-by)
1463
1464 EOF
1465
1466 test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
1467 'trailers:key=Signed-off-by,only=no' <<-EOF
1468 Signed-off-by: A U Thor <author@example.com>
1469 $(grep patch.description <trailers)
1470
1471 EOF
1472
1473 test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \
1474 'trailers:key=Signed-off-by,valueonly' <<-EOF
1475 A U Thor <author@example.com>
1476
1477 EOF
1478
1479 test_trailer_option '%(trailers:separator) changes separator' \
1480 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1481 Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
1482 EOF
1483
1484 test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \
1485 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1486 Reviewed-by,A U Thor <author@example.com>
1487 Signed-off-by,A U Thor <author@example.com>
1488
1489 EOF
1490
1491 test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \
1492 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
1493 Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
1494 EOF
1495
1496 test_failing_trailer_option () {
1497 title=$1 option=$2
1498 cat >expect
1499 test_expect_success "$title" '
1500 # error message cannot be checked under i18n
1501 test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
1502 test_cmp expect actual &&
1503 test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
1504 test_cmp expect actual
1505 '
1506 }
1507
1508 test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
1509 'trailers:unsupported' <<-\EOF
1510 fatal: unknown %(trailers) argument: unsupported
1511 EOF
1512
1513 test_failing_trailer_option '%(trailers:key) without value is error' \
1514 'trailers:key' <<-\EOF
1515 fatal: expected %(trailers:key=<value>)
1516 EOF
1517
1518 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
1519 cat >expect <<-EOF &&
1520 fatal: unrecognized %(contents) argument: trailersonly
1521 EOF
1522 test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
1523 test_cmp expect actual
1524 '
1525
1526 test_expect_success 'basic atom: head contents:trailers' '
1527 git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
1528 sanitize_pgp <actual >actual.clean &&
1529 # git for-each-ref ends with a blank line
1530 cat >expect <<-EOF &&
1531 $(cat trailers)
1532
1533 EOF
1534 test_cmp expect actual.clean
1535 '
1536
1537 test_expect_success 'basic atom: rest must fail' '
1538 test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1539 '
1540
1541 test_expect_success 'HEAD atom does not take arguments' '
1542 test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
1543 echo "fatal: %(HEAD) does not take arguments" >expect &&
1544 test_cmp expect err
1545 '
1546
1547 test_expect_success 'subject atom rejects unknown arguments' '
1548 test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
1549 echo "fatal: unrecognized %(subject) argument: foo" >expect &&
1550 test_cmp expect err
1551 '
1552
1553 test_expect_success 'refname atom rejects unknown arguments' '
1554 test_must_fail git for-each-ref --format="%(refname:foo)" 2>err &&
1555 echo "fatal: unrecognized %(refname) argument: foo" >expect &&
1556 test_cmp expect err
1557 '
1558
1559 test_expect_success 'trailer parsing not fooled by --- line' '
1560 git commit --allow-empty -F - <<-\EOF &&
1561 this is the subject
1562
1563 This is the body. The message has a "---" line which would confuse a
1564 message+patch parser. But here we know we have only a commit message,
1565 so we get it right.
1566
1567 trailer: wrong
1568 ---
1569 This is more body.
1570
1571 trailer: right
1572 EOF
1573
1574 {
1575 echo "trailer: right" &&
1576 echo
1577 } >expect &&
1578 git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
1579 test_cmp expect actual
1580 '
1581
1582 test_expect_success 'Add symbolic ref for the following tests' '
1583 git symbolic-ref refs/heads/sym refs/heads/main
1584 '
1585
1586 cat >expected <<EOF
1587 refs/heads/main
1588 EOF
1589
1590 test_expect_success 'Verify usage of %(symref) atom' '
1591 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
1592 test_cmp expected actual
1593 '
1594
1595 cat >expected <<EOF
1596 heads/main
1597 EOF
1598
1599 test_expect_success 'Verify usage of %(symref:short) atom' '
1600 git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
1601 test_cmp expected actual
1602 '
1603
1604 cat >expected <<EOF
1605 main
1606 heads/main
1607 EOF
1608
1609 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
1610 git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
1611 git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
1612 test_cmp expected actual &&
1613
1614 git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
1615 git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
1616 test_cmp expected actual
1617 '
1618
1619 cat >expected <<EOF
1620 refs
1621 refs/heads
1622 EOF
1623
1624 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
1625 git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
1626 git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
1627 test_cmp expected actual
1628 '
1629
1630 test_expect_success ':remotename and :remoteref' '
1631 git init remote-tests &&
1632 (
1633 cd remote-tests &&
1634 test_commit initial &&
1635 git branch -M main &&
1636 git remote add from fifth.coffee:blub &&
1637 git config branch.main.remote from &&
1638 git config branch.main.merge refs/heads/stable &&
1639 git remote add to southridge.audio:repo &&
1640 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
1641 git config branch.main.pushRemote to &&
1642 for pair in "%(upstream)=refs/remotes/from/stable" \
1643 "%(upstream:remotename)=from" \
1644 "%(upstream:remoteref)=refs/heads/stable" \
1645 "%(push)=refs/remotes/to/pushed/main" \
1646 "%(push:remotename)=to" \
1647 "%(push:remoteref)=refs/heads/pushed/main"
1648 do
1649 echo "${pair#*=}" >expect &&
1650 git for-each-ref --format="${pair%=*}" \
1651 refs/heads/main >actual &&
1652 test_cmp expect actual || exit 1
1653 done &&
1654 git branch push-simple &&
1655 git config branch.push-simple.pushRemote from &&
1656 actual="$(git for-each-ref \
1657 --format="%(push:remotename),%(push:remoteref)" \
1658 refs/heads/push-simple)" &&
1659 test from, = "$actual"
1660 )
1661 '
1662
1663 test_expect_success 'for-each-ref --ignore-case ignores case' '
1664 git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
1665 test_must_be_empty actual &&
1666
1667 echo refs/heads/main >expect &&
1668 git for-each-ref --format="%(refname)" --ignore-case \
1669 refs/heads/MAIN >actual &&
1670 test_cmp expect actual
1671 '
1672
1673 test_expect_success 'for-each-ref --omit-empty works' '
1674 git for-each-ref --format="%(refname)" >actual &&
1675 test_line_count -gt 1 actual &&
1676 git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
1677 echo refs/heads/main >expect &&
1678 test_cmp expect actual
1679 '
1680
1681 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1682 # name refs numerically to avoid case-insensitive filesystem conflicts
1683 nr=0 &&
1684 for email in a A b B
1685 do
1686 for subject in a A b B
1687 do
1688 GIT_COMMITTER_EMAIL="$email@example.com" \
1689 git tag -m "tag $subject" icase-$(printf %02d $nr) &&
1690 nr=$((nr+1))||
1691 return 1
1692 done
1693 done &&
1694 git for-each-ref --ignore-case \
1695 --format="%(taggeremail) %(subject) %(refname)" \
1696 --sort=refname \
1697 --sort=subject \
1698 --sort=taggeremail \
1699 refs/tags/icase-* >actual &&
1700 cat >expect <<-\EOF &&
1701 <a@example.com> tag a refs/tags/icase-00
1702 <a@example.com> tag A refs/tags/icase-01
1703 <A@example.com> tag a refs/tags/icase-04
1704 <A@example.com> tag A refs/tags/icase-05
1705 <a@example.com> tag b refs/tags/icase-02
1706 <a@example.com> tag B refs/tags/icase-03
1707 <A@example.com> tag b refs/tags/icase-06
1708 <A@example.com> tag B refs/tags/icase-07
1709 <b@example.com> tag a refs/tags/icase-08
1710 <b@example.com> tag A refs/tags/icase-09
1711 <B@example.com> tag a refs/tags/icase-12
1712 <B@example.com> tag A refs/tags/icase-13
1713 <b@example.com> tag b refs/tags/icase-10
1714 <b@example.com> tag B refs/tags/icase-11
1715 <B@example.com> tag b refs/tags/icase-14
1716 <B@example.com> tag B refs/tags/icase-15
1717 EOF
1718 test_cmp expect actual
1719 '
1720
1721 test_expect_success 'for-each-ref reports broken tags' '
1722 git tag -m "good tag" broken-tag-good HEAD &&
1723 git cat-file tag broken-tag-good >good &&
1724 sed s/commit/blob/ <good >bad &&
1725 bad=$(git hash-object -w -t tag bad) &&
1726 git update-ref refs/tags/broken-tag-bad $bad &&
1727 test_must_fail git for-each-ref --format="%(*objectname)" \
1728 refs/tags/broken-tag-*
1729 '
1730
1731 test_expect_success 'set up tag with signature and no blank lines' '
1732 git tag -F - fake-sig-no-blanks <<-\EOF
1733 this is the subject
1734 -----BEGIN PGP SIGNATURE-----
1735 not a real signature, but we just care about the
1736 subject/body parsing. It is important here that
1737 there are no blank lines in the signature.
1738 -----END PGP SIGNATURE-----
1739 EOF
1740 '
1741
1742 test_atom refs/tags/fake-sig-no-blanks contents:subject 'this is the subject'
1743 test_atom refs/tags/fake-sig-no-blanks contents:body ''
1744 test_atom refs/tags/fake-sig-no-blanks contents:signature "$sig"
1745
1746 test_expect_success 'set up tag with CRLF signature' '
1747 append_cr <<-\EOF |
1748 this is the subject
1749 -----BEGIN PGP SIGNATURE-----
1750
1751 not a real signature, but we just care about
1752 the subject/body parsing. It is important here
1753 that there is a blank line separating this
1754 from the signature header.
1755 -----END PGP SIGNATURE-----
1756 EOF
1757 git tag -F - --cleanup=verbatim fake-sig-crlf
1758 '
1759
1760 test_atom refs/tags/fake-sig-crlf contents:subject 'this is the subject'
1761 test_atom refs/tags/fake-sig-crlf contents:body ''
1762
1763 # CRLF is retained in the signature, so we have to pass our expected value
1764 # through append_cr. But test_atom requires a shell string, which means command
1765 # substitution, and the shell will strip trailing newlines from the output of
1766 # the substitution. Hack around it by adding and then removing a dummy line.
1767 sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
1768 sig_crlf=${sig_crlf%dummy}
1769 test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
1770
1771 test_expect_success 'git for-each-ref --stdin: empty' '
1772 >in &&
1773 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1774 git for-each-ref --format="%(refname)" >expect &&
1775 test_cmp expect actual
1776 '
1777
1778 test_expect_success 'git for-each-ref --stdin: fails if extra args' '
1779 >in &&
1780 test_must_fail git for-each-ref --format="%(refname)" \
1781 --stdin refs/heads/extra <in 2>err &&
1782 grep "unknown arguments supplied with --stdin" err
1783 '
1784
1785 test_expect_success 'git for-each-ref --stdin: matches' '
1786 cat >in <<-EOF &&
1787 refs/tags/multi*
1788 refs/heads/amb*
1789 EOF
1790
1791 cat >expect <<-EOF &&
1792 refs/heads/ambiguous
1793 refs/tags/multi-ref1-100000-user1
1794 refs/tags/multi-ref1-100000-user2
1795 refs/tags/multi-ref1-200000-user1
1796 refs/tags/multi-ref1-200000-user2
1797 refs/tags/multi-ref2-100000-user1
1798 refs/tags/multi-ref2-100000-user2
1799 refs/tags/multi-ref2-200000-user1
1800 refs/tags/multi-ref2-200000-user2
1801 refs/tags/multiline
1802 EOF
1803
1804 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1805 test_cmp expect actual
1806 '
1807
1808 test_expect_success 'git for-each-ref with non-existing refs' '
1809 cat >in <<-EOF &&
1810 refs/heads/this-ref-does-not-exist
1811 refs/tags/bogus
1812 EOF
1813
1814 git for-each-ref --format="%(refname)" --stdin <in >actual &&
1815 test_must_be_empty actual &&
1816
1817 xargs git for-each-ref --format="%(refname)" <in >actual &&
1818 test_must_be_empty actual
1819 '
1820
1821 GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1822 TRUSTLEVEL_FORMAT="%(signature:trustlevel)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
1823
1824 test_expect_success GPG 'setup for signature atom using gpg' '
1825 git checkout -b signed &&
1826
1827 test_when_finished "test_unconfig commit.gpgSign" &&
1828
1829 echo "1" >file &&
1830 git add file &&
1831 test_tick &&
1832 git commit -S -m "file: 1" &&
1833 git tag first-signed &&
1834
1835 echo "2" >file &&
1836 test_tick &&
1837 git commit -a -m "file: 2" &&
1838 git tag second-unsigned &&
1839
1840 git config commit.gpgSign 1 &&
1841 echo "3" >file &&
1842 test_tick &&
1843 git commit -a --no-gpg-sign -m "file: 3" &&
1844 git tag third-unsigned &&
1845
1846 test_tick &&
1847 git rebase -f HEAD^^ && git tag second-signed HEAD^ &&
1848 git tag third-signed &&
1849
1850 echo "4" >file &&
1851 test_tick &&
1852 git commit -a -SB7227189 -m "file: 4" &&
1853 git tag fourth-signed &&
1854
1855 echo "5" >file &&
1856 test_tick &&
1857 git commit -a --no-gpg-sign -m "file: 5" &&
1858 git tag fifth-unsigned &&
1859
1860 echo "6" >file &&
1861 test_tick &&
1862 git commit -a --no-gpg-sign -m "file: 6" &&
1863
1864 test_tick &&
1865 git rebase -f HEAD^^ &&
1866 git tag fifth-signed HEAD^ &&
1867 git tag sixth-signed &&
1868
1869 echo "7" >file &&
1870 test_tick &&
1871 git commit -a --no-gpg-sign -m "file: 7" &&
1872 git tag seventh-unsigned
1873 '
1874
1875 test_expect_success GPGSSH 'setup for signature atom using ssh' '
1876 test_when_finished "test_unconfig gpg.format user.signingkey" &&
1877
1878 test_config gpg.format ssh &&
1879 test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
1880 echo "8" >file &&
1881 test_tick &&
1882 git add file &&
1883 git commit -S -m "file: 8" &&
1884 git tag eighth-signed-ssh
1885 '
1886
1887 test_expect_success GPG2 'bare signature atom' '
1888 git verify-commit first-signed 2>expect &&
1889 echo >>expect &&
1890 git for-each-ref refs/tags/first-signed \
1891 --format="%(signature)" >actual &&
1892 test_cmp expect actual
1893 '
1894
1895 test_expect_success GPG 'show good signature with custom format' '
1896 git verify-commit first-signed &&
1897 cat >expect <<-\EOF &&
1898 G
1899 13B6F51ECDDE430D
1900 C O Mitter <committer@example.com>
1901 73D758744BE721698EC54E8713B6F51ECDDE430D
1902 73D758744BE721698EC54E8713B6F51ECDDE430D
1903 EOF
1904 git for-each-ref refs/tags/first-signed \
1905 --format="$GRADE_FORMAT" >actual &&
1906 test_cmp expect actual
1907 '
1908 test_expect_success GPGSSH 'show good signature with custom format
1909 with ssh' '
1910 test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
1911 FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
1912 cat >expect.tmpl <<-\EOF &&
1913 G
1914 FINGERPRINT
1915 principal with number 1
1916 FINGERPRINT
1917
1918 EOF
1919 sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
1920 git for-each-ref refs/tags/eighth-signed-ssh \
1921 --format="$GRADE_FORMAT" >actual &&
1922 test_cmp expect actual
1923 '
1924
1925 test_expect_success GPG 'signature atom with grade option and bad signature' '
1926 git cat-file commit third-signed >raw &&
1927 sed -e "s/^file: 3/file: 3 forged/" raw >forged1 &&
1928 FORGED1=$(git hash-object -w -t commit forged1) &&
1929 git update-ref refs/tags/third-signed "$FORGED1" &&
1930 test_must_fail git verify-commit "$FORGED1" &&
1931
1932 cat >expect <<-\EOF &&
1933 B
1934 13B6F51ECDDE430D
1935 C O Mitter <committer@example.com>
1936
1937
1938 EOF
1939 git for-each-ref refs/tags/third-signed \
1940 --format="$GRADE_FORMAT" >actual &&
1941 test_cmp expect actual
1942 '
1943
1944 test_expect_success GPG 'show untrusted signature with custom format' '
1945 cat >expect <<-\EOF &&
1946 U
1947 65A0EEA02E30CAD7
1948 Eris Discordia <discord@example.net>
1949 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1950 D4BE22311AD3131E5EDA29A461092E85B7227189
1951 EOF
1952 git for-each-ref refs/tags/fourth-signed \
1953 --format="$GRADE_FORMAT" >actual &&
1954 test_cmp expect actual
1955 '
1956
1957 test_expect_success GPG 'show untrusted signature with undefined trust level' '
1958 cat >expect <<-\EOF &&
1959 undefined
1960 65A0EEA02E30CAD7
1961 Eris Discordia <discord@example.net>
1962 F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
1963 D4BE22311AD3131E5EDA29A461092E85B7227189
1964 EOF
1965 git for-each-ref refs/tags/fourth-signed \
1966 --format="$TRUSTLEVEL_FORMAT" >actual &&
1967 test_cmp expect actual
1968 '
1969
1970 test_expect_success GPG 'show untrusted signature with ultimate trust level' '
1971 cat >expect <<-\EOF &&
1972 ultimate
1973 13B6F51ECDDE430D
1974 C O Mitter <committer@example.com>
1975 73D758744BE721698EC54E8713B6F51ECDDE430D
1976 73D758744BE721698EC54E8713B6F51ECDDE430D
1977 EOF
1978 git for-each-ref refs/tags/sixth-signed \
1979 --format="$TRUSTLEVEL_FORMAT" >actual &&
1980 test_cmp expect actual
1981 '
1982
1983 test_expect_success GPG 'show unknown signature with custom format' '
1984 cat >expect <<-\EOF &&
1985 E
1986 13B6F51ECDDE430D
1987
1988
1989
1990 EOF
1991 GNUPGHOME="$GNUPGHOME_NOT_USED" git for-each-ref \
1992 refs/tags/sixth-signed --format="$GRADE_FORMAT" >actual &&
1993 test_cmp expect actual
1994 '
1995
1996 test_expect_success GPG 'show lack of signature with custom format' '
1997 cat >expect <<-\EOF &&
1998 N
1999
2000
2001
2002
2003 EOF
2004 git for-each-ref refs/tags/seventh-unsigned \
2005 --format="$GRADE_FORMAT" >actual &&
2006 test_cmp expect actual
2007 '
2008
2009 test_done