Merge branch 'sg/subtree-signed-commits' into pu
[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 . "$TEST_DIRECTORY"/lib-gpg.sh
10 . "$TEST_DIRECTORY"/lib-terminal.sh
11
12 # Mon Jul 3 23:18:43 2006 +0000
13 datestamp=1151968723
14 setdate_and_increment () {
15     GIT_COMMITTER_DATE="$datestamp +0200"
16     datestamp=$(expr "$datestamp" + 1)
17     GIT_AUTHOR_DATE="$datestamp +0200"
18     datestamp=$(expr "$datestamp" + 1)
19     export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
20 }
21
22 test_expect_success setup '
23         setdate_and_increment &&
24         echo "Using $datestamp" > one &&
25         git add one &&
26         git commit -m "Initial" &&
27         setdate_and_increment &&
28         git tag -a -m "Tagging at $datestamp" testtag &&
29         git update-ref refs/remotes/origin/master master &&
30         git remote add origin nowhere &&
31         git config branch.master.remote origin &&
32         git config branch.master.merge refs/heads/master &&
33         git remote add myfork elsewhere &&
34         git config remote.pushdefault myfork &&
35         git config push.default current
36 '
37
38 test_atom() {
39         case "$1" in
40                 head) ref=refs/heads/master ;;
41                  tag) ref=refs/tags/testtag ;;
42                  sym) ref=refs/heads/sym ;;
43                    *) ref=$1 ;;
44         esac
45         printf '%s\n' "$3" >expected
46         test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
47                 git for-each-ref --format='%($2)' $ref >actual &&
48                 sanitize_pgp <actual >actual.clean &&
49                 test_cmp expected actual.clean
50         "
51 }
52
53 test_atom head refname refs/heads/master
54 test_atom head refname: refs/heads/master
55 test_atom head refname:short master
56 test_atom head refname:lstrip=1 heads/master
57 test_atom head refname:lstrip=2 master
58 test_atom head refname:lstrip=-1 master
59 test_atom head refname:lstrip=-2 heads/master
60 test_atom head refname:rstrip=1 refs/heads
61 test_atom head refname:rstrip=2 refs
62 test_atom head refname:rstrip=-1 refs
63 test_atom head refname:rstrip=-2 refs/heads
64 test_atom head refname:strip=1 heads/master
65 test_atom head refname:strip=2 master
66 test_atom head refname:strip=-1 master
67 test_atom head refname:strip=-2 heads/master
68 test_atom head upstream refs/remotes/origin/master
69 test_atom head upstream:short origin/master
70 test_atom head upstream:lstrip=2 origin/master
71 test_atom head upstream:lstrip=-2 origin/master
72 test_atom head upstream:rstrip=2 refs/remotes
73 test_atom head upstream:rstrip=-2 refs/remotes
74 test_atom head upstream:strip=2 origin/master
75 test_atom head upstream:strip=-2 origin/master
76 test_atom head push refs/remotes/myfork/master
77 test_atom head push:short myfork/master
78 test_atom head push:lstrip=1 remotes/myfork/master
79 test_atom head push:lstrip=-1 master
80 test_atom head push:rstrip=1 refs/remotes/myfork
81 test_atom head push:rstrip=-1 refs
82 test_atom head push:strip=1 remotes/myfork/master
83 test_atom head push:strip=-1 master
84 test_atom head objecttype commit
85 test_atom head objectsize 171
86 test_atom head objectname $(git rev-parse refs/heads/master)
87 test_atom head objectname:short $(git rev-parse --short refs/heads/master)
88 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
89 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
90 test_atom head tree $(git rev-parse refs/heads/master^{tree})
91 test_atom head parent ''
92 test_atom head numparent 0
93 test_atom head object ''
94 test_atom head type ''
95 test_atom head '*objectname' ''
96 test_atom head '*objecttype' ''
97 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
98 test_atom head authorname 'A U Thor'
99 test_atom head authoremail '<author@example.com>'
100 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
101 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
102 test_atom head committername 'C O Mitter'
103 test_atom head committeremail '<committer@example.com>'
104 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
105 test_atom head tag ''
106 test_atom head tagger ''
107 test_atom head taggername ''
108 test_atom head taggeremail ''
109 test_atom head taggerdate ''
110 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
111 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
112 test_atom head subject 'Initial'
113 test_atom head contents:subject 'Initial'
114 test_atom head body ''
115 test_atom head contents:body ''
116 test_atom head contents:signature ''
117 test_atom head contents 'Initial
118 '
119 test_atom head HEAD '*'
120
121 test_atom tag refname refs/tags/testtag
122 test_atom tag refname:short testtag
123 test_atom tag upstream ''
124 test_atom tag push ''
125 test_atom tag objecttype tag
126 test_atom tag objectsize 154
127 test_atom tag objectname $(git rev-parse refs/tags/testtag)
128 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
129 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
130 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
131 test_atom tag tree ''
132 test_atom tag parent ''
133 test_atom tag numparent ''
134 test_atom tag object $(git rev-parse refs/tags/testtag^0)
135 test_atom tag type 'commit'
136 test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
137 test_atom tag '*objecttype' 'commit'
138 test_atom tag author ''
139 test_atom tag authorname ''
140 test_atom tag authoremail ''
141 test_atom tag authordate ''
142 test_atom tag committer ''
143 test_atom tag committername ''
144 test_atom tag committeremail ''
145 test_atom tag committerdate ''
146 test_atom tag tag 'testtag'
147 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
148 test_atom tag taggername 'C O Mitter'
149 test_atom tag taggeremail '<committer@example.com>'
150 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
151 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
152 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
153 test_atom tag subject 'Tagging at 1151968727'
154 test_atom tag contents:subject 'Tagging at 1151968727'
155 test_atom tag body ''
156 test_atom tag contents:body ''
157 test_atom tag contents:signature ''
158 test_atom tag contents 'Tagging at 1151968727
159 '
160 test_atom tag HEAD ' '
161
162 test_expect_success 'Check invalid atoms names are errors' '
163         test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
164 '
165
166 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
167         git for-each-ref --format="%(authordate)" refs/heads &&
168         git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
169         git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
170         git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
171 '
172
173 test_expect_success 'Check valid format specifiers for date fields' '
174         git for-each-ref --format="%(authordate:default)" refs/heads &&
175         git for-each-ref --format="%(authordate:relative)" refs/heads &&
176         git for-each-ref --format="%(authordate:short)" refs/heads &&
177         git for-each-ref --format="%(authordate:local)" refs/heads &&
178         git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
179         git for-each-ref --format="%(authordate:rfc2822)" refs/heads
180 '
181
182 test_expect_success 'Check invalid format specifiers are errors' '
183         test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
184 '
185
186 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
187         test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
188         test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
189         test_must_fail git for-each-ref --format="%(objectname:short=foo)"
190 '
191
192 test_date () {
193         f=$1 &&
194         committer_date=$2 &&
195         author_date=$3 &&
196         tagger_date=$4 &&
197         cat >expected <<-EOF &&
198         'refs/heads/master' '$committer_date' '$author_date'
199         'refs/tags/testtag' '$tagger_date'
200         EOF
201         (
202                 git for-each-ref --shell \
203                         --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
204                         refs/heads &&
205                 git for-each-ref --shell \
206                         --format="%(refname) %(taggerdate${f:+:$f})" \
207                         refs/tags
208         ) >actual &&
209         test_cmp expected actual
210 }
211
212 test_expect_success 'Check unformatted date fields output' '
213         test_date "" \
214                 "Tue Jul 4 01:18:43 2006 +0200" \
215                 "Tue Jul 4 01:18:44 2006 +0200" \
216                 "Tue Jul 4 01:18:45 2006 +0200"
217 '
218
219 test_expect_success 'Check format "default" formatted date fields output' '
220         test_date default \
221                 "Tue Jul 4 01:18:43 2006 +0200" \
222                 "Tue Jul 4 01:18:44 2006 +0200" \
223                 "Tue Jul 4 01:18:45 2006 +0200"
224 '
225
226 test_expect_success 'Check format "default-local" date fields output' '
227         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"
228 '
229
230 # Don't know how to do relative check because I can't know when this script
231 # is going to be run and can't fake the current time to git, and hence can't
232 # provide expected output.  Instead, I'll just make sure that "relative"
233 # doesn't exit in error
234 test_expect_success 'Check format "relative" date fields output' '
235         f=relative &&
236         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
237         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
238 '
239
240 # We just check that this is the same as "relative" for now.
241 test_expect_success 'Check format "relative-local" date fields output' '
242         test_date relative-local \
243                 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
244                 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
245                 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
246 '
247
248 test_expect_success 'Check format "short" date fields output' '
249         test_date short 2006-07-04 2006-07-04 2006-07-04
250 '
251
252 test_expect_success 'Check format "short-local" date fields output' '
253         test_date short-local 2006-07-03 2006-07-03 2006-07-03
254 '
255
256 test_expect_success 'Check format "local" date fields output' '
257         test_date local \
258                 "Mon Jul 3 23:18:43 2006" \
259                 "Mon Jul 3 23:18:44 2006" \
260                 "Mon Jul 3 23:18:45 2006"
261 '
262
263 test_expect_success 'Check format "iso8601" date fields output' '
264         test_date iso8601 \
265                 "2006-07-04 01:18:43 +0200" \
266                 "2006-07-04 01:18:44 +0200" \
267                 "2006-07-04 01:18:45 +0200"
268 '
269
270 test_expect_success 'Check format "iso8601-local" date fields output' '
271         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"
272 '
273
274 test_expect_success 'Check format "rfc2822" date fields output' '
275         test_date rfc2822 \
276                 "Tue, 4 Jul 2006 01:18:43 +0200" \
277                 "Tue, 4 Jul 2006 01:18:44 +0200" \
278                 "Tue, 4 Jul 2006 01:18:45 +0200"
279 '
280
281 test_expect_success 'Check format "rfc2822-local" date fields output' '
282         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"
283 '
284
285 test_expect_success 'Check format "raw" date fields output' '
286         test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
287 '
288
289 test_expect_success 'Check format "raw-local" date fields output' '
290         test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
291 '
292
293 test_expect_success 'Check format of strftime date fields' '
294         echo "my date is 2006-07-04" >expected &&
295         git for-each-ref \
296           --format="%(authordate:format:my date is %Y-%m-%d)" \
297           refs/heads >actual &&
298         test_cmp expected actual
299 '
300
301 test_expect_success 'Check format of strftime-local date fields' '
302         echo "my date is 2006-07-03" >expected &&
303         git for-each-ref \
304           --format="%(authordate:format-local:my date is %Y-%m-%d)" \
305           refs/heads >actual &&
306         test_cmp expected actual
307 '
308
309 test_expect_success 'exercise strftime with odd fields' '
310         echo >expected &&
311         git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
312         test_cmp expected actual &&
313         long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
314         echo $long >expected &&
315         git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
316         test_cmp expected actual
317 '
318
319 test_expect_success 'Check format %(objectsize:disk) gives empty output ' '
320         echo >expected &&
321         git for-each-ref --format="%(objectsize:disk)" refs/heads >actual &&
322         test_cmp expected actual
323 '
324
325 test_expect_success 'Check format %(rest) gives empty output ' '
326         echo >expected &&
327         git for-each-ref --format="%(rest)" refs/heads >actual &&
328         test_cmp expected actual
329 '
330
331 test_expect_success 'Check format %(deltabase) gives empty output ' '
332         echo >expected &&
333         git for-each-ref --format="%(deltabase)" refs/heads >actual &&
334         test_cmp expected actual
335 '
336
337 cat >expected <<\EOF
338 refs/heads/master
339 refs/remotes/origin/master
340 refs/tags/testtag
341 EOF
342
343 test_expect_success 'Verify ascending sort' '
344         git for-each-ref --format="%(refname)" --sort=refname >actual &&
345         test_cmp expected actual
346 '
347
348
349 cat >expected <<\EOF
350 refs/tags/testtag
351 refs/remotes/origin/master
352 refs/heads/master
353 EOF
354
355 test_expect_success 'Verify descending sort' '
356         git for-each-ref --format="%(refname)" --sort=-refname >actual &&
357         test_cmp expected actual
358 '
359
360 cat >expected <<\EOF
361 'refs/heads/master'
362 'refs/remotes/origin/master'
363 'refs/tags/testtag'
364 EOF
365
366 test_expect_success 'Quoting style: shell' '
367         git for-each-ref --shell --format="%(refname)" >actual &&
368         test_cmp expected actual
369 '
370
371 test_expect_success 'Quoting style: perl' '
372         git for-each-ref --perl --format="%(refname)" >actual &&
373         test_cmp expected actual
374 '
375
376 test_expect_success 'Quoting style: python' '
377         git for-each-ref --python --format="%(refname)" >actual &&
378         test_cmp expected actual
379 '
380
381 cat >expected <<\EOF
382 "refs/heads/master"
383 "refs/remotes/origin/master"
384 "refs/tags/testtag"
385 EOF
386
387 test_expect_success 'Quoting style: tcl' '
388         git for-each-ref --tcl --format="%(refname)" >actual &&
389         test_cmp expected actual
390 '
391
392 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
393         test_expect_success "more than one quoting style: $i" "
394                 test_must_fail git for-each-ref $i 2>err &&
395                 grep '^error: more than one quoting style' err
396         "
397 done
398
399 test_expect_success 'setup for upstream:track[short]' '
400         test_commit two
401 '
402
403 test_atom head upstream:track '[ahead 1]'
404 test_atom head upstream:trackshort '>'
405 test_atom head upstream:track,nobracket 'ahead 1'
406 test_atom head upstream:nobracket,track 'ahead 1'
407 test_atom head push:track '[ahead 1]'
408 test_atom head push:trackshort '>'
409
410 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
411         test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
412         test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
413 '
414
415 test_expect_success 'Check that :track[short] works when upstream is invalid' '
416         cat >expected <<-\EOF &&
417         [gone]
418
419         EOF
420         test_when_finished "git config branch.master.merge refs/heads/master" &&
421         git config branch.master.merge refs/heads/does-not-exist &&
422         git for-each-ref \
423                 --format="%(upstream:track)$LF%(upstream:trackshort)" \
424                 refs/heads >actual &&
425         test_cmp expected actual
426 '
427
428 test_expect_success 'Check for invalid refname format' '
429         test_must_fail git for-each-ref --format="%(refname:INVALID)"
430 '
431
432 test_expect_success 'set up color tests' '
433         cat >expected.color <<-EOF &&
434         $(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
435         $(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
436         $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
437         $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
438         EOF
439         sed "s/<[^>]*>//g" <expected.color >expected.bare &&
440         color_format="%(objectname:short) %(color:green)%(refname:short)"
441 '
442
443 test_expect_success TTY '%(color) shows color with a tty' '
444         test_terminal git for-each-ref --format="$color_format" >actual.raw &&
445         test_decode_color <actual.raw >actual &&
446         test_cmp expected.color actual
447 '
448
449 test_expect_success '%(color) does not show color without tty' '
450         TERM=vt100 git for-each-ref --format="$color_format" >actual &&
451         test_cmp expected.bare actual
452 '
453
454 test_expect_success '--color can override tty check' '
455         git for-each-ref --color --format="$color_format" >actual.raw &&
456         test_decode_color <actual.raw >actual &&
457         test_cmp expected.color actual
458 '
459
460 test_expect_success 'color.ui=always does not override tty check' '
461         git -c color.ui=always for-each-ref --format="$color_format" >actual &&
462         test_cmp expected.bare actual
463 '
464
465 cat >expected <<\EOF
466 heads/master
467 tags/master
468 EOF
469
470 test_expect_success 'Check ambiguous head and tag refs (strict)' '
471         git config --bool core.warnambiguousrefs true &&
472         git checkout -b newtag &&
473         echo "Using $datestamp" > one &&
474         git add one &&
475         git commit -m "Branch" &&
476         setdate_and_increment &&
477         git tag -m "Tagging at $datestamp" master &&
478         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
479         test_cmp expected actual
480 '
481
482 cat >expected <<\EOF
483 heads/master
484 master
485 EOF
486
487 test_expect_success 'Check ambiguous head and tag refs (loose)' '
488         git config --bool core.warnambiguousrefs false &&
489         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
490         test_cmp expected actual
491 '
492
493 cat >expected <<\EOF
494 heads/ambiguous
495 ambiguous
496 EOF
497
498 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
499         git checkout master &&
500         git tag ambiguous testtag^0 &&
501         git branch ambiguous testtag^0 &&
502         git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
503         test_cmp expected actual
504 '
505
506 test_expect_success 'an unusual tag with an incomplete line' '
507
508         git tag -m "bogo" bogo &&
509         bogo=$(git cat-file tag bogo) &&
510         bogo=$(printf "%s" "$bogo" | git mktag) &&
511         git tag -f bogo "$bogo" &&
512         git for-each-ref --format "%(body)" refs/tags/bogo
513
514 '
515
516 test_expect_success 'create tag with subject and body content' '
517         cat >>msg <<-\EOF &&
518                 the subject line
519
520                 first body line
521                 second body line
522         EOF
523         git tag -F msg subject-body
524 '
525 test_atom refs/tags/subject-body subject 'the subject line'
526 test_atom refs/tags/subject-body body 'first body line
527 second body line
528 '
529 test_atom refs/tags/subject-body contents 'the subject line
530
531 first body line
532 second body line
533 '
534
535 test_expect_success 'create tag with multiline subject' '
536         cat >msg <<-\EOF &&
537                 first subject line
538                 second subject line
539
540                 first body line
541                 second body line
542         EOF
543         git tag -F msg multiline
544 '
545 test_atom refs/tags/multiline subject 'first subject line second subject line'
546 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
547 test_atom refs/tags/multiline body 'first body line
548 second body line
549 '
550 test_atom refs/tags/multiline contents:body 'first body line
551 second body line
552 '
553 test_atom refs/tags/multiline contents:signature ''
554 test_atom refs/tags/multiline contents 'first subject line
555 second subject line
556
557 first body line
558 second body line
559 '
560
561 test_expect_success GPG 'create signed tags' '
562         git tag -s -m "" signed-empty &&
563         git tag -s -m "subject line" signed-short &&
564         cat >msg <<-\EOF &&
565         subject line
566
567         body contents
568         EOF
569         git tag -s -F msg signed-long
570 '
571
572 sig='-----BEGIN PGP SIGNATURE-----
573 -----END PGP SIGNATURE-----
574 '
575
576 PREREQ=GPG
577 test_atom refs/tags/signed-empty subject ''
578 test_atom refs/tags/signed-empty contents:subject ''
579 test_atom refs/tags/signed-empty body "$sig"
580 test_atom refs/tags/signed-empty contents:body ''
581 test_atom refs/tags/signed-empty contents:signature "$sig"
582 test_atom refs/tags/signed-empty contents "$sig"
583
584 test_atom refs/tags/signed-short subject 'subject line'
585 test_atom refs/tags/signed-short contents:subject 'subject line'
586 test_atom refs/tags/signed-short body "$sig"
587 test_atom refs/tags/signed-short contents:body ''
588 test_atom refs/tags/signed-short contents:signature "$sig"
589 test_atom refs/tags/signed-short contents "subject line
590 $sig"
591
592 test_atom refs/tags/signed-long subject 'subject line'
593 test_atom refs/tags/signed-long contents:subject 'subject line'
594 test_atom refs/tags/signed-long body "body contents
595 $sig"
596 test_atom refs/tags/signed-long contents:body 'body contents
597 '
598 test_atom refs/tags/signed-long contents:signature "$sig"
599 test_atom refs/tags/signed-long contents "subject line
600
601 body contents
602 $sig"
603
604 cat >expected <<EOF
605 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
606 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
607 EOF
608
609 test_expect_success 'Verify sort with multiple keys' '
610         git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
611                 refs/tags/bogo refs/tags/master > actual &&
612         test_cmp expected actual
613 '
614
615
616 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
617         test_when_finished "git checkout master" &&
618         git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
619         sed -e "s/^\* /  /" actual >expect &&
620         git checkout --orphan orphaned-branch &&
621         git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
622         test_cmp expect actual
623 '
624
625 cat >trailers <<EOF
626 Reviewed-by: A U Thor <author@example.com>
627 Signed-off-by: A U Thor <author@example.com>
628 [ v2 updated patch description ]
629 Acked-by: A U Thor
630   <author@example.com>
631 EOF
632
633 unfold () {
634         perl -0pe 's/\n\s+/ /g'
635 }
636
637 test_expect_success 'set up trailers for next test' '
638         echo "Some contents" > two &&
639         git add two &&
640         git commit -F - <<-EOF
641         trailers: this commit message has trailers
642
643         Some message contents
644
645         $(cat trailers)
646         EOF
647 '
648
649 test_expect_success '%(trailers:unfold) unfolds trailers' '
650         git for-each-ref --format="%(trailers:unfold)" refs/heads/master >actual &&
651         {
652                 unfold <trailers
653                 echo
654         } >expect &&
655         test_cmp expect actual
656 '
657
658 test_expect_success '%(trailers:only) shows only "key: value" trailers' '
659         git for-each-ref --format="%(trailers:only)" refs/heads/master >actual &&
660         {
661                 grep -v patch.description <trailers &&
662                 echo
663         } >expect &&
664         test_cmp expect actual
665 '
666
667 test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
668         git for-each-ref --format="%(trailers:only,unfold)" refs/heads/master >actual &&
669         git for-each-ref --format="%(trailers:unfold,only)" refs/heads/master >reverse &&
670         test_cmp actual reverse &&
671         {
672                 grep -v patch.description <trailers | unfold &&
673                 echo
674         } >expect &&
675         test_cmp expect actual
676 '
677
678 test_expect_success '%(contents:trailers:unfold) unfolds trailers' '
679         git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/master >actual &&
680         {
681                 unfold <trailers
682                 echo
683         } >expect &&
684         test_cmp expect actual
685 '
686
687 test_expect_success '%(contents:trailers:only) shows only "key: value" trailers' '
688         git for-each-ref --format="%(contents:trailers:only)" refs/heads/master >actual &&
689         {
690                 grep -v patch.description <trailers &&
691                 echo
692         } >expect &&
693         test_cmp expect actual
694 '
695
696 test_expect_success '%(contents:trailers:only) and %(contents:trailers:unfold) work together' '
697         git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/master >actual &&
698         git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/master >reverse &&
699         test_cmp actual reverse &&
700         {
701                 grep -v patch.description <trailers | unfold &&
702                 echo
703         } >expect &&
704         test_cmp expect actual
705 '
706
707 test_expect_success '%(trailers) rejects unknown trailers arguments' '
708         # error message cannot be checked under i18n
709         cat >expect <<-EOF &&
710         fatal: unknown %(trailers) argument: unsupported
711         EOF
712         test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
713         test_i18ncmp expect actual
714 '
715
716 test_expect_success '%(contents:trailers) rejects unknown trailers arguments' '
717         # error message cannot be checked under i18n
718         cat >expect <<-EOF &&
719         fatal: unknown %(trailers) argument: unsupported
720         EOF
721         test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
722         test_i18ncmp expect actual
723 '
724
725 test_expect_success 'basic atom: head contents:trailers' '
726         git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
727         sanitize_pgp <actual >actual.clean &&
728         # git for-each-ref ends with a blank line
729         cat >expect <<-EOF &&
730         $(cat trailers)
731
732         EOF
733         test_cmp expect actual.clean
734 '
735
736 test_expect_success 'Add symbolic ref for the following tests' '
737         git symbolic-ref refs/heads/sym refs/heads/master
738 '
739
740 cat >expected <<EOF
741 refs/heads/master
742 EOF
743
744 test_expect_success 'Verify usage of %(symref) atom' '
745         git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
746         test_cmp expected actual
747 '
748
749 cat >expected <<EOF
750 heads/master
751 EOF
752
753 test_expect_success 'Verify usage of %(symref:short) atom' '
754         git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
755         test_cmp expected actual
756 '
757
758 cat >expected <<EOF
759 master
760 heads/master
761 EOF
762
763 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
764         git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
765         git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
766         test_cmp expected actual &&
767
768         git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
769         git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
770         test_cmp expected actual
771 '
772
773 cat >expected <<EOF
774 refs
775 refs/heads
776 EOF
777
778 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
779         git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
780         git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
781         test_cmp expected actual
782 '
783
784 test_expect_success ':remotename and :remoteref' '
785         git init remote-tests &&
786         (
787                 cd remote-tests &&
788                 test_commit initial &&
789                 git remote add from fifth.coffee:blub &&
790                 git config branch.master.remote from &&
791                 git config branch.master.merge refs/heads/stable &&
792                 git remote add to southridge.audio:repo &&
793                 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
794                 git config branch.master.pushRemote to &&
795                 for pair in "%(upstream)=refs/remotes/from/stable" \
796                         "%(upstream:remotename)=from" \
797                         "%(upstream:remoteref)=refs/heads/stable" \
798                         "%(push)=refs/remotes/to/pushed/master" \
799                         "%(push:remotename)=to" \
800                         "%(push:remoteref)=refs/heads/pushed/master"
801                 do
802                         echo "${pair#*=}" >expect &&
803                         git for-each-ref --format="${pair%=*}" \
804                                 refs/heads/master >actual &&
805                         test_cmp expect actual
806                 done &&
807                 git branch push-simple &&
808                 git config branch.push-simple.pushRemote from &&
809                 actual="$(git for-each-ref \
810                         --format="%(push:remotename),%(push:remoteref)" \
811                         refs/heads/push-simple)" &&
812                 test from, = "$actual"
813         )
814 '
815
816 test_done