Merge branch 'es/worktree-repair'
[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         test_oid_cache <<-EOF &&
24         disklen sha1:138
25         disklen sha256:154
26         EOF
27         setdate_and_increment &&
28         echo "Using $datestamp" > one &&
29         git add one &&
30         git commit -m "Initial" &&
31         setdate_and_increment &&
32         git tag -a -m "Tagging at $datestamp" testtag &&
33         git update-ref refs/remotes/origin/master master &&
34         git remote add origin nowhere &&
35         git config branch.master.remote origin &&
36         git config branch.master.merge refs/heads/master &&
37         git remote add myfork elsewhere &&
38         git config remote.pushdefault myfork &&
39         git config push.default current
40 '
41
42 test_atom() {
43         case "$1" in
44                 head) ref=refs/heads/master ;;
45                  tag) ref=refs/tags/testtag ;;
46                  sym) ref=refs/heads/sym ;;
47                    *) ref=$1 ;;
48         esac
49         printf '%s\n' "$3" >expected
50         test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
51                 git for-each-ref --format='%($2)' $ref >actual &&
52                 sanitize_pgp <actual >actual.clean &&
53                 test_cmp expected actual.clean
54         "
55         # Automatically test "contents:size" atom after testing "contents"
56         if test "$2" = "contents"
57         then
58                 case $(git cat-file -t "$ref") in
59                 tag)
60                         # We cannot use $3 as it expects sanitize_pgp to run
61                         expect=$(git cat-file tag $ref | tail -n +6 | wc -c) ;;
62                 tree | blob)
63                         expect='' ;;
64                 commit)
65                         expect=$(printf '%s' "$3" | wc -c) ;;
66                 esac
67                 # Leave $expect unquoted to lose possible leading whitespaces
68                 echo $expect >expected
69                 test_expect_${4:-success} $PREREQ "basic atom: $1 contents:size" '
70                         git for-each-ref --format="%(contents:size)" "$ref" >actual &&
71                         test_cmp expected actual
72                 '
73         fi
74 }
75
76 hexlen=$(test_oid hexsz)
77 disklen=$(test_oid disklen)
78
79 test_atom head refname refs/heads/master
80 test_atom head refname: refs/heads/master
81 test_atom head refname:short master
82 test_atom head refname:lstrip=1 heads/master
83 test_atom head refname:lstrip=2 master
84 test_atom head refname:lstrip=-1 master
85 test_atom head refname:lstrip=-2 heads/master
86 test_atom head refname:rstrip=1 refs/heads
87 test_atom head refname:rstrip=2 refs
88 test_atom head refname:rstrip=-1 refs
89 test_atom head refname:rstrip=-2 refs/heads
90 test_atom head refname:strip=1 heads/master
91 test_atom head refname:strip=2 master
92 test_atom head refname:strip=-1 master
93 test_atom head refname:strip=-2 heads/master
94 test_atom head upstream refs/remotes/origin/master
95 test_atom head upstream:short origin/master
96 test_atom head upstream:lstrip=2 origin/master
97 test_atom head upstream:lstrip=-2 origin/master
98 test_atom head upstream:rstrip=2 refs/remotes
99 test_atom head upstream:rstrip=-2 refs/remotes
100 test_atom head upstream:strip=2 origin/master
101 test_atom head upstream:strip=-2 origin/master
102 test_atom head push refs/remotes/myfork/master
103 test_atom head push:short myfork/master
104 test_atom head push:lstrip=1 remotes/myfork/master
105 test_atom head push:lstrip=-1 master
106 test_atom head push:rstrip=1 refs/remotes/myfork
107 test_atom head push:rstrip=-1 refs
108 test_atom head push:strip=1 remotes/myfork/master
109 test_atom head push:strip=-1 master
110 test_atom head objecttype commit
111 test_atom head objectsize $((131 + hexlen))
112 test_atom head objectsize:disk $disklen
113 test_atom head deltabase $ZERO_OID
114 test_atom head objectname $(git rev-parse refs/heads/master)
115 test_atom head objectname:short $(git rev-parse --short refs/heads/master)
116 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
117 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
118 test_atom head tree $(git rev-parse refs/heads/master^{tree})
119 test_atom head parent ''
120 test_atom head numparent 0
121 test_atom head object ''
122 test_atom head type ''
123 test_atom head '*objectname' ''
124 test_atom head '*objecttype' ''
125 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
126 test_atom head authorname 'A U Thor'
127 test_atom head authoremail '<author@example.com>'
128 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
129 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
130 test_atom head committername 'C O Mitter'
131 test_atom head committeremail '<committer@example.com>'
132 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
133 test_atom head tag ''
134 test_atom head tagger ''
135 test_atom head taggername ''
136 test_atom head taggeremail ''
137 test_atom head taggerdate ''
138 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
139 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
140 test_atom head subject 'Initial'
141 test_atom head contents:subject 'Initial'
142 test_atom head body ''
143 test_atom head contents:body ''
144 test_atom head contents:signature ''
145 test_atom head contents 'Initial
146 '
147 test_atom head HEAD '*'
148
149 test_atom tag refname refs/tags/testtag
150 test_atom tag refname:short testtag
151 test_atom tag upstream ''
152 test_atom tag push ''
153 test_atom tag objecttype tag
154 test_atom tag objectsize $((114 + hexlen))
155 test_atom tag objectsize:disk $disklen
156 test_atom tag '*objectsize:disk' $disklen
157 test_atom tag deltabase $ZERO_OID
158 test_atom tag '*deltabase' $ZERO_OID
159 test_atom tag objectname $(git rev-parse refs/tags/testtag)
160 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
161 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
162 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
163 test_atom tag tree ''
164 test_atom tag parent ''
165 test_atom tag numparent ''
166 test_atom tag object $(git rev-parse refs/tags/testtag^0)
167 test_atom tag type 'commit'
168 test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
169 test_atom tag '*objecttype' 'commit'
170 test_atom tag author ''
171 test_atom tag authorname ''
172 test_atom tag authoremail ''
173 test_atom tag authordate ''
174 test_atom tag committer ''
175 test_atom tag committername ''
176 test_atom tag committeremail ''
177 test_atom tag committerdate ''
178 test_atom tag tag 'testtag'
179 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
180 test_atom tag taggername 'C O Mitter'
181 test_atom tag taggeremail '<committer@example.com>'
182 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
183 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
184 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
185 test_atom tag subject 'Tagging at 1151968727'
186 test_atom tag contents:subject 'Tagging at 1151968727'
187 test_atom tag body ''
188 test_atom tag contents:body ''
189 test_atom tag contents:signature ''
190 test_atom tag contents 'Tagging at 1151968727
191 '
192 test_atom tag HEAD ' '
193
194 test_expect_success 'Check invalid atoms names are errors' '
195         test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
196 '
197
198 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
199         git for-each-ref --format="%(authordate)" refs/heads &&
200         git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
201         git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
202         git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
203 '
204
205 test_expect_success 'Check valid format specifiers for date fields' '
206         git for-each-ref --format="%(authordate:default)" refs/heads &&
207         git for-each-ref --format="%(authordate:relative)" refs/heads &&
208         git for-each-ref --format="%(authordate:short)" refs/heads &&
209         git for-each-ref --format="%(authordate:local)" refs/heads &&
210         git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
211         git for-each-ref --format="%(authordate:rfc2822)" refs/heads
212 '
213
214 test_expect_success 'Check invalid format specifiers are errors' '
215         test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
216 '
217
218 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
219         test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
220         test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
221         test_must_fail git for-each-ref --format="%(objectname:short=foo)"
222 '
223
224 test_date () {
225         f=$1 &&
226         committer_date=$2 &&
227         author_date=$3 &&
228         tagger_date=$4 &&
229         cat >expected <<-EOF &&
230         'refs/heads/master' '$committer_date' '$author_date'
231         'refs/tags/testtag' '$tagger_date'
232         EOF
233         (
234                 git for-each-ref --shell \
235                         --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
236                         refs/heads &&
237                 git for-each-ref --shell \
238                         --format="%(refname) %(taggerdate${f:+:$f})" \
239                         refs/tags
240         ) >actual &&
241         test_cmp expected actual
242 }
243
244 test_expect_success 'Check unformatted date fields output' '
245         test_date "" \
246                 "Tue Jul 4 01:18:43 2006 +0200" \
247                 "Tue Jul 4 01:18:44 2006 +0200" \
248                 "Tue Jul 4 01:18:45 2006 +0200"
249 '
250
251 test_expect_success 'Check format "default" formatted date fields output' '
252         test_date default \
253                 "Tue Jul 4 01:18:43 2006 +0200" \
254                 "Tue Jul 4 01:18:44 2006 +0200" \
255                 "Tue Jul 4 01:18:45 2006 +0200"
256 '
257
258 test_expect_success 'Check format "default-local" date fields output' '
259         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"
260 '
261
262 # Don't know how to do relative check because I can't know when this script
263 # is going to be run and can't fake the current time to git, and hence can't
264 # provide expected output.  Instead, I'll just make sure that "relative"
265 # doesn't exit in error
266 test_expect_success 'Check format "relative" date fields output' '
267         f=relative &&
268         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
269         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
270 '
271
272 # We just check that this is the same as "relative" for now.
273 test_expect_success 'Check format "relative-local" date fields output' '
274         test_date relative-local \
275                 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
276                 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
277                 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
278 '
279
280 test_expect_success 'Check format "short" date fields output' '
281         test_date short 2006-07-04 2006-07-04 2006-07-04
282 '
283
284 test_expect_success 'Check format "short-local" date fields output' '
285         test_date short-local 2006-07-03 2006-07-03 2006-07-03
286 '
287
288 test_expect_success 'Check format "local" date fields output' '
289         test_date local \
290                 "Mon Jul 3 23:18:43 2006" \
291                 "Mon Jul 3 23:18:44 2006" \
292                 "Mon Jul 3 23:18:45 2006"
293 '
294
295 test_expect_success 'Check format "iso8601" date fields output' '
296         test_date iso8601 \
297                 "2006-07-04 01:18:43 +0200" \
298                 "2006-07-04 01:18:44 +0200" \
299                 "2006-07-04 01:18:45 +0200"
300 '
301
302 test_expect_success 'Check format "iso8601-local" date fields output' '
303         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"
304 '
305
306 test_expect_success 'Check format "rfc2822" date fields output' '
307         test_date rfc2822 \
308                 "Tue, 4 Jul 2006 01:18:43 +0200" \
309                 "Tue, 4 Jul 2006 01:18:44 +0200" \
310                 "Tue, 4 Jul 2006 01:18:45 +0200"
311 '
312
313 test_expect_success 'Check format "rfc2822-local" date fields output' '
314         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"
315 '
316
317 test_expect_success 'Check format "raw" date fields output' '
318         test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
319 '
320
321 test_expect_success 'Check format "raw-local" date fields output' '
322         test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
323 '
324
325 test_expect_success 'Check format of strftime date fields' '
326         echo "my date is 2006-07-04" >expected &&
327         git for-each-ref \
328           --format="%(authordate:format:my date is %Y-%m-%d)" \
329           refs/heads >actual &&
330         test_cmp expected actual
331 '
332
333 test_expect_success 'Check format of strftime-local date fields' '
334         echo "my date is 2006-07-03" >expected &&
335         git for-each-ref \
336           --format="%(authordate:format-local:my date is %Y-%m-%d)" \
337           refs/heads >actual &&
338         test_cmp expected actual
339 '
340
341 test_expect_success 'exercise strftime with odd fields' '
342         echo >expected &&
343         git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
344         test_cmp expected actual &&
345         long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
346         echo $long >expected &&
347         git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
348         test_cmp expected actual
349 '
350
351 cat >expected <<\EOF
352 refs/heads/master
353 refs/remotes/origin/master
354 refs/tags/testtag
355 EOF
356
357 test_expect_success 'Verify ascending sort' '
358         git for-each-ref --format="%(refname)" --sort=refname >actual &&
359         test_cmp expected actual
360 '
361
362
363 cat >expected <<\EOF
364 refs/tags/testtag
365 refs/remotes/origin/master
366 refs/heads/master
367 EOF
368
369 test_expect_success 'Verify descending sort' '
370         git for-each-ref --format="%(refname)" --sort=-refname >actual &&
371         test_cmp expected actual
372 '
373
374 cat >expected <<\EOF
375 refs/tags/testtag
376 refs/tags/testtag-2
377 EOF
378
379 test_expect_success 'exercise patterns with prefixes' '
380         git tag testtag-2 &&
381         test_when_finished "git tag -d testtag-2" &&
382         git for-each-ref --format="%(refname)" \
383                 refs/tags/testtag refs/tags/testtag-2 >actual &&
384         test_cmp expected actual
385 '
386
387 cat >expected <<\EOF
388 refs/tags/testtag
389 refs/tags/testtag-2
390 EOF
391
392 test_expect_success 'exercise glob patterns with prefixes' '
393         git tag testtag-2 &&
394         test_when_finished "git tag -d testtag-2" &&
395         git for-each-ref --format="%(refname)" \
396                 refs/tags/testtag "refs/tags/testtag-*" >actual &&
397         test_cmp expected actual
398 '
399
400 cat >expected <<\EOF
401 'refs/heads/master'
402 'refs/remotes/origin/master'
403 'refs/tags/testtag'
404 EOF
405
406 test_expect_success 'Quoting style: shell' '
407         git for-each-ref --shell --format="%(refname)" >actual &&
408         test_cmp expected actual
409 '
410
411 test_expect_success 'Quoting style: perl' '
412         git for-each-ref --perl --format="%(refname)" >actual &&
413         test_cmp expected actual
414 '
415
416 test_expect_success 'Quoting style: python' '
417         git for-each-ref --python --format="%(refname)" >actual &&
418         test_cmp expected actual
419 '
420
421 cat >expected <<\EOF
422 "refs/heads/master"
423 "refs/remotes/origin/master"
424 "refs/tags/testtag"
425 EOF
426
427 test_expect_success 'Quoting style: tcl' '
428         git for-each-ref --tcl --format="%(refname)" >actual &&
429         test_cmp expected actual
430 '
431
432 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
433         test_expect_success "more than one quoting style: $i" "
434                 test_must_fail git for-each-ref $i 2>err &&
435                 grep '^error: more than one quoting style' err
436         "
437 done
438
439 test_expect_success 'setup for upstream:track[short]' '
440         test_commit two
441 '
442
443 test_atom head upstream:track '[ahead 1]'
444 test_atom head upstream:trackshort '>'
445 test_atom head upstream:track,nobracket 'ahead 1'
446 test_atom head upstream:nobracket,track 'ahead 1'
447
448 test_expect_success 'setup for push:track[short]' '
449         test_commit third &&
450         git update-ref refs/remotes/myfork/master master &&
451         git reset master~1
452 '
453
454 test_atom head push:track '[behind 1]'
455 test_atom head push:trackshort '<'
456
457 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
458         test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
459         test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
460 '
461
462 test_expect_success 'Check that :track[short] works when upstream is invalid' '
463         cat >expected <<-\EOF &&
464         [gone]
465
466         EOF
467         test_when_finished "git config branch.master.merge refs/heads/master" &&
468         git config branch.master.merge refs/heads/does-not-exist &&
469         git for-each-ref \
470                 --format="%(upstream:track)$LF%(upstream:trackshort)" \
471                 refs/heads >actual &&
472         test_cmp expected actual
473 '
474
475 test_expect_success 'Check for invalid refname format' '
476         test_must_fail git for-each-ref --format="%(refname:INVALID)"
477 '
478
479 test_expect_success 'set up color tests' '
480         cat >expected.color <<-EOF &&
481         $(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
482         $(git rev-parse --short refs/remotes/myfork/master) <GREEN>myfork/master<RESET>
483         $(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
484         $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
485         $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
486         $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
487         EOF
488         sed "s/<[^>]*>//g" <expected.color >expected.bare &&
489         color_format="%(objectname:short) %(color:green)%(refname:short)"
490 '
491
492 test_expect_success TTY '%(color) shows color with a tty' '
493         test_terminal git for-each-ref --format="$color_format" >actual.raw &&
494         test_decode_color <actual.raw >actual &&
495         test_cmp expected.color actual
496 '
497
498 test_expect_success '%(color) does not show color without tty' '
499         TERM=vt100 git for-each-ref --format="$color_format" >actual &&
500         test_cmp expected.bare actual
501 '
502
503 test_expect_success '--color can override tty check' '
504         git for-each-ref --color --format="$color_format" >actual.raw &&
505         test_decode_color <actual.raw >actual &&
506         test_cmp expected.color actual
507 '
508
509 test_expect_success 'color.ui=always does not override tty check' '
510         git -c color.ui=always for-each-ref --format="$color_format" >actual &&
511         test_cmp expected.bare actual
512 '
513
514 cat >expected <<\EOF
515 heads/master
516 tags/master
517 EOF
518
519 test_expect_success 'Check ambiguous head and tag refs (strict)' '
520         git config --bool core.warnambiguousrefs true &&
521         git checkout -b newtag &&
522         echo "Using $datestamp" > one &&
523         git add one &&
524         git commit -m "Branch" &&
525         setdate_and_increment &&
526         git tag -m "Tagging at $datestamp" master &&
527         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
528         test_cmp expected actual
529 '
530
531 cat >expected <<\EOF
532 heads/master
533 master
534 EOF
535
536 test_expect_success 'Check ambiguous head and tag refs (loose)' '
537         git config --bool core.warnambiguousrefs false &&
538         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
539         test_cmp expected actual
540 '
541
542 cat >expected <<\EOF
543 heads/ambiguous
544 ambiguous
545 EOF
546
547 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
548         git checkout master &&
549         git tag ambiguous testtag^0 &&
550         git branch ambiguous testtag^0 &&
551         git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
552         test_cmp expected actual
553 '
554
555 test_expect_success 'create tag without tagger' '
556         git tag -a -m "Broken tag" taggerless &&
557         git tag -f taggerless $(git cat-file tag taggerless |
558                 sed -e "/^tagger /d" |
559                 git hash-object --stdin -w -t tag)
560 '
561
562 test_atom refs/tags/taggerless type 'commit'
563 test_atom refs/tags/taggerless tag 'taggerless'
564 test_atom refs/tags/taggerless tagger ''
565 test_atom refs/tags/taggerless taggername ''
566 test_atom refs/tags/taggerless taggeremail ''
567 test_atom refs/tags/taggerless taggerdate ''
568 test_atom refs/tags/taggerless committer ''
569 test_atom refs/tags/taggerless committername ''
570 test_atom refs/tags/taggerless committeremail ''
571 test_atom refs/tags/taggerless committerdate ''
572 test_atom refs/tags/taggerless subject 'Broken tag'
573
574 test_expect_success 'an unusual tag with an incomplete line' '
575
576         git tag -m "bogo" bogo &&
577         bogo=$(git cat-file tag bogo) &&
578         bogo=$(printf "%s" "$bogo" | git mktag) &&
579         git tag -f bogo "$bogo" &&
580         git for-each-ref --format "%(body)" refs/tags/bogo
581
582 '
583
584 test_expect_success 'create tag with subject and body content' '
585         cat >>msg <<-\EOF &&
586                 the subject line
587
588                 first body line
589                 second body line
590         EOF
591         git tag -F msg subject-body
592 '
593 test_atom refs/tags/subject-body subject 'the subject line'
594 test_atom refs/tags/subject-body body 'first body line
595 second body line
596 '
597 test_atom refs/tags/subject-body contents 'the subject line
598
599 first body line
600 second body line
601 '
602
603 test_expect_success 'create tag with multiline subject' '
604         cat >msg <<-\EOF &&
605                 first subject line
606                 second subject line
607
608                 first body line
609                 second body line
610         EOF
611         git tag -F msg multiline
612 '
613 test_atom refs/tags/multiline subject 'first subject line second subject line'
614 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
615 test_atom refs/tags/multiline body 'first body line
616 second body line
617 '
618 test_atom refs/tags/multiline contents:body 'first body line
619 second body line
620 '
621 test_atom refs/tags/multiline contents:signature ''
622 test_atom refs/tags/multiline contents 'first subject line
623 second subject line
624
625 first body line
626 second body line
627 '
628
629 test_expect_success GPG 'create signed tags' '
630         git tag -s -m "" signed-empty &&
631         git tag -s -m "subject line" signed-short &&
632         cat >msg <<-\EOF &&
633         subject line
634
635         body contents
636         EOF
637         git tag -s -F msg signed-long
638 '
639
640 sig='-----BEGIN PGP SIGNATURE-----
641 -----END PGP SIGNATURE-----
642 '
643
644 PREREQ=GPG
645 test_atom refs/tags/signed-empty subject ''
646 test_atom refs/tags/signed-empty contents:subject ''
647 test_atom refs/tags/signed-empty body "$sig"
648 test_atom refs/tags/signed-empty contents:body ''
649 test_atom refs/tags/signed-empty contents:signature "$sig"
650 test_atom refs/tags/signed-empty contents "$sig"
651
652 test_atom refs/tags/signed-short subject 'subject line'
653 test_atom refs/tags/signed-short contents:subject 'subject line'
654 test_atom refs/tags/signed-short body "$sig"
655 test_atom refs/tags/signed-short contents:body ''
656 test_atom refs/tags/signed-short contents:signature "$sig"
657 test_atom refs/tags/signed-short contents "subject line
658 $sig"
659
660 test_atom refs/tags/signed-long subject 'subject line'
661 test_atom refs/tags/signed-long contents:subject 'subject line'
662 test_atom refs/tags/signed-long body "body contents
663 $sig"
664 test_atom refs/tags/signed-long contents:body 'body contents
665 '
666 test_atom refs/tags/signed-long contents:signature "$sig"
667 test_atom refs/tags/signed-long contents "subject line
668
669 body contents
670 $sig"
671
672 test_expect_success 'set up refs pointing to tree and blob' '
673         git update-ref refs/mytrees/first refs/heads/master^{tree} &&
674         git update-ref refs/myblobs/first refs/heads/master:one
675 '
676
677 test_atom refs/mytrees/first subject ""
678 test_atom refs/mytrees/first contents:subject ""
679 test_atom refs/mytrees/first body ""
680 test_atom refs/mytrees/first contents:body ""
681 test_atom refs/mytrees/first contents:signature ""
682 test_atom refs/mytrees/first contents ""
683
684 test_atom refs/myblobs/first subject ""
685 test_atom refs/myblobs/first contents:subject ""
686 test_atom refs/myblobs/first body ""
687 test_atom refs/myblobs/first contents:body ""
688 test_atom refs/myblobs/first contents:signature ""
689 test_atom refs/myblobs/first contents ""
690
691 test_expect_success 'set up multiple-sort tags' '
692         for when in 100000 200000
693         do
694                 for email in user1 user2
695                 do
696                         for ref in ref1 ref2
697                         do
698                                 GIT_COMMITTER_DATE="@$when +0000" \
699                                 GIT_COMMITTER_EMAIL="$email@example.com" \
700                                 git tag -m "tag $ref-$when-$email" \
701                                 multi-$ref-$when-$email || return 1
702                         done
703                 done
704         done
705 '
706
707 test_expect_success 'Verify sort with multiple keys' '
708         cat >expected <<-\EOF &&
709         100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
710         100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
711         100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
712         100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
713         200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
714         200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
715         200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
716         200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
717         EOF
718         git for-each-ref \
719                 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
720                 --sort=-refname \
721                 --sort=taggeremail \
722                 --sort=taggerdate \
723                 "refs/tags/multi-*" >actual &&
724         test_cmp expected actual
725 '
726
727 test_expect_success 'equivalent sorts fall back on refname' '
728         cat >expected <<-\EOF &&
729         100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
730         100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
731         100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
732         100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
733         200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
734         200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
735         200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
736         200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
737         EOF
738         git for-each-ref \
739                 --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
740                 --sort=taggerdate \
741                 "refs/tags/multi-*" >actual &&
742         test_cmp expected actual
743 '
744
745 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
746         test_when_finished "git checkout master" &&
747         git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
748         sed -e "s/^\* /  /" actual >expect &&
749         git checkout --orphan orphaned-branch &&
750         git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
751         test_cmp expect actual
752 '
753
754 cat >trailers <<EOF
755 Reviewed-by: A U Thor <author@example.com>
756 Signed-off-by: A U Thor <author@example.com>
757 [ v2 updated patch description ]
758 Acked-by: A U Thor
759   <author@example.com>
760 EOF
761
762 unfold () {
763         perl -0pe 's/\n\s+/ /g'
764 }
765
766 test_expect_success 'set up trailers for next test' '
767         echo "Some contents" > two &&
768         git add two &&
769         git commit -F - <<-EOF
770         trailers: this commit message has trailers
771
772         Some message contents
773
774         $(cat trailers)
775         EOF
776 '
777
778 test_expect_success '%(trailers:unfold) unfolds trailers' '
779         {
780                 unfold <trailers
781                 echo
782         } >expect &&
783         git for-each-ref --format="%(trailers:unfold)" refs/heads/master >actual &&
784         test_cmp expect actual &&
785         git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/master >actual &&
786         test_cmp expect actual
787 '
788
789 test_expect_success '%(trailers:only) shows only "key: value" trailers' '
790         {
791                 grep -v patch.description <trailers &&
792                 echo
793         } >expect &&
794         git for-each-ref --format="%(trailers:only)" refs/heads/master >actual &&
795         test_cmp expect actual &&
796         git for-each-ref --format="%(contents:trailers:only)" refs/heads/master >actual &&
797         test_cmp expect actual
798 '
799
800 test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
801         {
802                 grep -v patch.description <trailers | unfold &&
803                 echo
804         } >expect &&
805         git for-each-ref --format="%(trailers:only,unfold)" refs/heads/master >actual &&
806         test_cmp expect actual &&
807         git for-each-ref --format="%(trailers:unfold,only)" refs/heads/master >actual &&
808         test_cmp actual actual &&
809         git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/master >actual &&
810         test_cmp expect actual &&
811         git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/master >actual &&
812         test_cmp actual actual
813 '
814
815 test_expect_success '%(trailers) rejects unknown trailers arguments' '
816         # error message cannot be checked under i18n
817         cat >expect <<-EOF &&
818         fatal: unknown %(trailers) argument: unsupported
819         EOF
820         test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
821         test_i18ncmp expect actual &&
822         test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
823         test_i18ncmp expect actual
824 '
825
826 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
827         cat >expect <<-EOF &&
828         fatal: unrecognized %(contents) argument: trailersonly
829         EOF
830         test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
831         test_i18ncmp expect actual
832 '
833
834 test_expect_success 'basic atom: head contents:trailers' '
835         git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
836         sanitize_pgp <actual >actual.clean &&
837         # git for-each-ref ends with a blank line
838         cat >expect <<-EOF &&
839         $(cat trailers)
840
841         EOF
842         test_cmp expect actual.clean
843 '
844
845 test_expect_success 'trailer parsing not fooled by --- line' '
846         git commit --allow-empty -F - <<-\EOF &&
847         this is the subject
848
849         This is the body. The message has a "---" line which would confuse a
850         message+patch parser. But here we know we have only a commit message,
851         so we get it right.
852
853         trailer: wrong
854         ---
855         This is more body.
856
857         trailer: right
858         EOF
859
860         {
861                 echo "trailer: right" &&
862                 echo
863         } >expect &&
864         git for-each-ref --format="%(trailers)" refs/heads/master >actual &&
865         test_cmp expect actual
866 '
867
868 test_expect_success 'Add symbolic ref for the following tests' '
869         git symbolic-ref refs/heads/sym refs/heads/master
870 '
871
872 cat >expected <<EOF
873 refs/heads/master
874 EOF
875
876 test_expect_success 'Verify usage of %(symref) atom' '
877         git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
878         test_cmp expected actual
879 '
880
881 cat >expected <<EOF
882 heads/master
883 EOF
884
885 test_expect_success 'Verify usage of %(symref:short) atom' '
886         git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
887         test_cmp expected actual
888 '
889
890 cat >expected <<EOF
891 master
892 heads/master
893 EOF
894
895 test_expect_success 'Verify usage of %(symref:lstrip) atom' '
896         git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
897         git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
898         test_cmp expected actual &&
899
900         git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
901         git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
902         test_cmp expected actual
903 '
904
905 cat >expected <<EOF
906 refs
907 refs/heads
908 EOF
909
910 test_expect_success 'Verify usage of %(symref:rstrip) atom' '
911         git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
912         git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
913         test_cmp expected actual
914 '
915
916 test_expect_success ':remotename and :remoteref' '
917         git init remote-tests &&
918         (
919                 cd remote-tests &&
920                 test_commit initial &&
921                 git remote add from fifth.coffee:blub &&
922                 git config branch.master.remote from &&
923                 git config branch.master.merge refs/heads/stable &&
924                 git remote add to southridge.audio:repo &&
925                 git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
926                 git config branch.master.pushRemote to &&
927                 for pair in "%(upstream)=refs/remotes/from/stable" \
928                         "%(upstream:remotename)=from" \
929                         "%(upstream:remoteref)=refs/heads/stable" \
930                         "%(push)=refs/remotes/to/pushed/master" \
931                         "%(push:remotename)=to" \
932                         "%(push:remoteref)=refs/heads/pushed/master"
933                 do
934                         echo "${pair#*=}" >expect &&
935                         git for-each-ref --format="${pair%=*}" \
936                                 refs/heads/master >actual &&
937                         test_cmp expect actual
938                 done &&
939                 git branch push-simple &&
940                 git config branch.push-simple.pushRemote from &&
941                 actual="$(git for-each-ref \
942                         --format="%(push:remotename),%(push:remoteref)" \
943                         refs/heads/push-simple)" &&
944                 test from, = "$actual"
945         )
946 '
947
948 test_expect_success 'for-each-ref --ignore-case ignores case' '
949         git for-each-ref --format="%(refname)" refs/heads/MASTER >actual &&
950         test_must_be_empty actual &&
951
952         echo refs/heads/master >expect &&
953         git for-each-ref --format="%(refname)" --ignore-case \
954                 refs/heads/MASTER >actual &&
955         test_cmp expect actual
956 '
957
958 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
959         # name refs numerically to avoid case-insensitive filesystem conflicts
960         nr=0 &&
961         for email in a A b B
962         do
963                 for subject in a A b B
964                 do
965                         GIT_COMMITTER_EMAIL="$email@example.com" \
966                         git tag -m "tag $subject" icase-$(printf %02d $nr) &&
967                         nr=$((nr+1))||
968                         return 1
969                 done
970         done &&
971         git for-each-ref --ignore-case \
972                 --format="%(taggeremail) %(subject) %(refname)" \
973                 --sort=refname \
974                 --sort=subject \
975                 --sort=taggeremail \
976                 refs/tags/icase-* >actual &&
977         cat >expect <<-\EOF &&
978         <a@example.com> tag a refs/tags/icase-00
979         <a@example.com> tag A refs/tags/icase-01
980         <A@example.com> tag a refs/tags/icase-04
981         <A@example.com> tag A refs/tags/icase-05
982         <a@example.com> tag b refs/tags/icase-02
983         <a@example.com> tag B refs/tags/icase-03
984         <A@example.com> tag b refs/tags/icase-06
985         <A@example.com> tag B refs/tags/icase-07
986         <b@example.com> tag a refs/tags/icase-08
987         <b@example.com> tag A refs/tags/icase-09
988         <B@example.com> tag a refs/tags/icase-12
989         <B@example.com> tag A refs/tags/icase-13
990         <b@example.com> tag b refs/tags/icase-10
991         <b@example.com> tag B refs/tags/icase-11
992         <B@example.com> tag b refs/tags/icase-14
993         <B@example.com> tag B refs/tags/icase-15
994         EOF
995         test_cmp expect actual
996 '
997
998 test_done