ref-filter: make "%(symref)" atom work with the ':short' modifier
[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
11 # Mon Jul 3 23:18:43 2006 +0000
12 datestamp=1151968723
13 setdate_and_increment () {
14     GIT_COMMITTER_DATE="$datestamp +0200"
15     datestamp=$(expr "$datestamp" + 1)
16     GIT_AUTHOR_DATE="$datestamp +0200"
17     datestamp=$(expr "$datestamp" + 1)
18     export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
19 }
20
21 test_expect_success setup '
22         setdate_and_increment &&
23         echo "Using $datestamp" > one &&
24         git add one &&
25         git commit -m "Initial" &&
26         setdate_and_increment &&
27         git tag -a -m "Tagging at $datestamp" testtag &&
28         git update-ref refs/remotes/origin/master master &&
29         git remote add origin nowhere &&
30         git config branch.master.remote origin &&
31         git config branch.master.merge refs/heads/master &&
32         git remote add myfork elsewhere &&
33         git config remote.pushdefault myfork &&
34         git config push.default current
35 '
36
37 test_atom() {
38         case "$1" in
39                 head) ref=refs/heads/master ;;
40                  tag) ref=refs/tags/testtag ;;
41                  sym) ref=refs/heads/sym ;;
42                    *) ref=$1 ;;
43         esac
44         printf '%s\n' "$3" >expected
45         test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
46                 git for-each-ref --format='%($2)' $ref >actual &&
47                 sanitize_pgp <actual >actual.clean &&
48                 test_cmp expected actual.clean
49         "
50 }
51
52 test_atom head refname refs/heads/master
53 test_atom head refname:short master
54 test_atom head refname:strip=1 heads/master
55 test_atom head refname:strip=2 master
56 test_atom head upstream refs/remotes/origin/master
57 test_atom head upstream:short origin/master
58 test_atom head push refs/remotes/myfork/master
59 test_atom head push:short myfork/master
60 test_atom head objecttype commit
61 test_atom head objectsize 171
62 test_atom head objectname $(git rev-parse refs/heads/master)
63 test_atom head objectname:short $(git rev-parse --short refs/heads/master)
64 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
65 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
66 test_atom head tree $(git rev-parse refs/heads/master^{tree})
67 test_atom head parent ''
68 test_atom head numparent 0
69 test_atom head object ''
70 test_atom head type ''
71 test_atom head '*objectname' ''
72 test_atom head '*objecttype' ''
73 test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
74 test_atom head authorname 'A U Thor'
75 test_atom head authoremail '<author@example.com>'
76 test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
77 test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
78 test_atom head committername 'C O Mitter'
79 test_atom head committeremail '<committer@example.com>'
80 test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
81 test_atom head tag ''
82 test_atom head tagger ''
83 test_atom head taggername ''
84 test_atom head taggeremail ''
85 test_atom head taggerdate ''
86 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
87 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
88 test_atom head subject 'Initial'
89 test_atom head contents:subject 'Initial'
90 test_atom head body ''
91 test_atom head contents:body ''
92 test_atom head contents:signature ''
93 test_atom head contents 'Initial
94 '
95 test_atom head HEAD '*'
96
97 test_atom tag refname refs/tags/testtag
98 test_atom tag refname:short testtag
99 test_atom tag upstream ''
100 test_atom tag push ''
101 test_atom tag objecttype tag
102 test_atom tag objectsize 154
103 test_atom tag objectname $(git rev-parse refs/tags/testtag)
104 test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
105 test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
106 test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
107 test_atom tag tree ''
108 test_atom tag parent ''
109 test_atom tag numparent ''
110 test_atom tag object $(git rev-parse refs/tags/testtag^0)
111 test_atom tag type 'commit'
112 test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
113 test_atom tag '*objecttype' 'commit'
114 test_atom tag author ''
115 test_atom tag authorname ''
116 test_atom tag authoremail ''
117 test_atom tag authordate ''
118 test_atom tag committer ''
119 test_atom tag committername ''
120 test_atom tag committeremail ''
121 test_atom tag committerdate ''
122 test_atom tag tag 'testtag'
123 test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
124 test_atom tag taggername 'C O Mitter'
125 test_atom tag taggeremail '<committer@example.com>'
126 test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
127 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
128 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
129 test_atom tag subject 'Tagging at 1151968727'
130 test_atom tag contents:subject 'Tagging at 1151968727'
131 test_atom tag body ''
132 test_atom tag contents:body ''
133 test_atom tag contents:signature ''
134 test_atom tag contents 'Tagging at 1151968727
135 '
136 test_atom tag HEAD ' '
137
138 test_expect_success 'Check invalid atoms names are errors' '
139         test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
140 '
141
142 test_expect_success 'arguments to :strip must be positive integers' '
143         test_must_fail git for-each-ref --format="%(refname:strip=0)" &&
144         test_must_fail git for-each-ref --format="%(refname:strip=-1)" &&
145         test_must_fail git for-each-ref --format="%(refname:strip=foo)"
146 '
147
148 test_expect_success 'stripping refnames too far gives an error' '
149         test_must_fail git for-each-ref --format="%(refname:strip=3)"
150 '
151
152 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
153         git for-each-ref --format="%(authordate)" refs/heads &&
154         git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
155         git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
156         git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
157 '
158
159 test_expect_success 'Check valid format specifiers for date fields' '
160         git for-each-ref --format="%(authordate:default)" refs/heads &&
161         git for-each-ref --format="%(authordate:relative)" refs/heads &&
162         git for-each-ref --format="%(authordate:short)" refs/heads &&
163         git for-each-ref --format="%(authordate:local)" refs/heads &&
164         git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
165         git for-each-ref --format="%(authordate:rfc2822)" refs/heads
166 '
167
168 test_expect_success 'Check invalid format specifiers are errors' '
169         test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
170 '
171
172 test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
173         test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
174         test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
175         test_must_fail git for-each-ref --format="%(objectname:short=foo)"
176 '
177
178 test_date () {
179         f=$1 &&
180         committer_date=$2 &&
181         author_date=$3 &&
182         tagger_date=$4 &&
183         cat >expected <<-EOF &&
184         'refs/heads/master' '$committer_date' '$author_date'
185         'refs/tags/testtag' '$tagger_date'
186         EOF
187         (
188                 git for-each-ref --shell \
189                         --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
190                         refs/heads &&
191                 git for-each-ref --shell \
192                         --format="%(refname) %(taggerdate${f:+:$f})" \
193                         refs/tags
194         ) >actual &&
195         test_cmp expected actual
196 }
197
198 test_expect_success 'Check unformatted date fields output' '
199         test_date "" \
200                 "Tue Jul 4 01:18:43 2006 +0200" \
201                 "Tue Jul 4 01:18:44 2006 +0200" \
202                 "Tue Jul 4 01:18:45 2006 +0200"
203 '
204
205 test_expect_success 'Check format "default" formatted date fields output' '
206         test_date default \
207                 "Tue Jul 4 01:18:43 2006 +0200" \
208                 "Tue Jul 4 01:18:44 2006 +0200" \
209                 "Tue Jul 4 01:18:45 2006 +0200"
210 '
211
212 test_expect_success 'Check format "default-local" date fields output' '
213         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"
214 '
215
216 # Don't know how to do relative check because I can't know when this script
217 # is going to be run and can't fake the current time to git, and hence can't
218 # provide expected output.  Instead, I'll just make sure that "relative"
219 # doesn't exit in error
220 test_expect_success 'Check format "relative" date fields output' '
221         f=relative &&
222         (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
223         git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
224 '
225
226 # We just check that this is the same as "relative" for now.
227 test_expect_success 'Check format "relative-local" date fields output' '
228         test_date relative-local \
229                 "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
230                 "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
231                 "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
232 '
233
234 test_expect_success 'Check format "short" date fields output' '
235         test_date short 2006-07-04 2006-07-04 2006-07-04
236 '
237
238 test_expect_success 'Check format "short-local" date fields output' '
239         test_date short-local 2006-07-03 2006-07-03 2006-07-03
240 '
241
242 test_expect_success 'Check format "local" date fields output' '
243         test_date local \
244                 "Mon Jul 3 23:18:43 2006" \
245                 "Mon Jul 3 23:18:44 2006" \
246                 "Mon Jul 3 23:18:45 2006"
247 '
248
249 test_expect_success 'Check format "iso8601" date fields output' '
250         test_date iso8601 \
251                 "2006-07-04 01:18:43 +0200" \
252                 "2006-07-04 01:18:44 +0200" \
253                 "2006-07-04 01:18:45 +0200"
254 '
255
256 test_expect_success 'Check format "iso8601-local" date fields output' '
257         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"
258 '
259
260 test_expect_success 'Check format "rfc2822" date fields output' '
261         test_date rfc2822 \
262                 "Tue, 4 Jul 2006 01:18:43 +0200" \
263                 "Tue, 4 Jul 2006 01:18:44 +0200" \
264                 "Tue, 4 Jul 2006 01:18:45 +0200"
265 '
266
267 test_expect_success 'Check format "rfc2822-local" date fields output' '
268         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"
269 '
270
271 test_expect_success 'Check format "raw" date fields output' '
272         test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
273 '
274
275 test_expect_success 'Check format "raw-local" date fields output' '
276         test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
277 '
278
279 test_expect_success 'Check format of strftime date fields' '
280         echo "my date is 2006-07-04" >expected &&
281         git for-each-ref \
282           --format="%(authordate:format:my date is %Y-%m-%d)" \
283           refs/heads >actual &&
284         test_cmp expected actual
285 '
286
287 test_expect_success 'Check format of strftime-local date fields' '
288         echo "my date is 2006-07-03" >expected &&
289         git for-each-ref \
290           --format="%(authordate:format-local:my date is %Y-%m-%d)" \
291           refs/heads >actual &&
292         test_cmp expected actual
293 '
294
295 test_expect_success 'exercise strftime with odd fields' '
296         echo >expected &&
297         git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
298         test_cmp expected actual &&
299         long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
300         echo $long >expected &&
301         git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
302         test_cmp expected actual
303 '
304
305 cat >expected <<\EOF
306 refs/heads/master
307 refs/remotes/origin/master
308 refs/tags/testtag
309 EOF
310
311 test_expect_success 'Verify ascending sort' '
312         git for-each-ref --format="%(refname)" --sort=refname >actual &&
313         test_cmp expected actual
314 '
315
316
317 cat >expected <<\EOF
318 refs/tags/testtag
319 refs/remotes/origin/master
320 refs/heads/master
321 EOF
322
323 test_expect_success 'Verify descending sort' '
324         git for-each-ref --format="%(refname)" --sort=-refname >actual &&
325         test_cmp expected actual
326 '
327
328 cat >expected <<\EOF
329 'refs/heads/master'
330 'refs/remotes/origin/master'
331 'refs/tags/testtag'
332 EOF
333
334 test_expect_success 'Quoting style: shell' '
335         git for-each-ref --shell --format="%(refname)" >actual &&
336         test_cmp expected actual
337 '
338
339 test_expect_success 'Quoting style: perl' '
340         git for-each-ref --perl --format="%(refname)" >actual &&
341         test_cmp expected actual
342 '
343
344 test_expect_success 'Quoting style: python' '
345         git for-each-ref --python --format="%(refname)" >actual &&
346         test_cmp expected actual
347 '
348
349 cat >expected <<\EOF
350 "refs/heads/master"
351 "refs/remotes/origin/master"
352 "refs/tags/testtag"
353 EOF
354
355 test_expect_success 'Quoting style: tcl' '
356         git for-each-ref --tcl --format="%(refname)" >actual &&
357         test_cmp expected actual
358 '
359
360 for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
361         test_expect_success "more than one quoting style: $i" "
362                 git for-each-ref $i 2>&1 | (read line &&
363                 case \$line in
364                 \"error: more than one quoting style\"*) : happy;;
365                 *) false
366                 esac)
367         "
368 done
369
370 test_expect_success 'setup for upstream:track[short]' '
371         test_commit two
372 '
373
374 test_atom head upstream:track '[ahead 1]'
375 test_atom head upstream:trackshort '>'
376 test_atom head upstream:track,nobracket 'ahead 1'
377 test_atom head upstream:nobracket,track 'ahead 1'
378 test_atom head push:track '[ahead 1]'
379 test_atom head push:trackshort '>'
380
381 test_expect_success 'Check that :track[short] cannot be used with other atoms' '
382         test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
383         test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
384 '
385
386 test_expect_success 'Check that :track[short] works when upstream is invalid' '
387         cat >expected <<-\EOF &&
388         [gone]
389
390         EOF
391         test_when_finished "git config branch.master.merge refs/heads/master" &&
392         git config branch.master.merge refs/heads/does-not-exist &&
393         git for-each-ref \
394                 --format="%(upstream:track)$LF%(upstream:trackshort)" \
395                 refs/heads >actual &&
396         test_cmp expected actual
397 '
398
399 test_expect_success 'Check for invalid refname format' '
400         test_must_fail git for-each-ref --format="%(refname:INVALID)"
401 '
402
403 get_color ()
404 {
405         git config --get-color no.such.slot "$1"
406 }
407
408 cat >expected <<EOF
409 $(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
410 $(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
411 $(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
412 $(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
413 EOF
414
415 test_expect_success 'Check %(color:...) ' '
416         git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
417         test_cmp expected actual
418 '
419
420 cat >expected <<\EOF
421 heads/master
422 tags/master
423 EOF
424
425 test_expect_success 'Check ambiguous head and tag refs (strict)' '
426         git config --bool core.warnambiguousrefs true &&
427         git checkout -b newtag &&
428         echo "Using $datestamp" > one &&
429         git add one &&
430         git commit -m "Branch" &&
431         setdate_and_increment &&
432         git tag -m "Tagging at $datestamp" master &&
433         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
434         test_cmp expected actual
435 '
436
437 cat >expected <<\EOF
438 heads/master
439 master
440 EOF
441
442 test_expect_success 'Check ambiguous head and tag refs (loose)' '
443         git config --bool core.warnambiguousrefs false &&
444         git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
445         test_cmp expected actual
446 '
447
448 cat >expected <<\EOF
449 heads/ambiguous
450 ambiguous
451 EOF
452
453 test_expect_success 'Check ambiguous head and tag refs II (loose)' '
454         git checkout master &&
455         git tag ambiguous testtag^0 &&
456         git branch ambiguous testtag^0 &&
457         git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
458         test_cmp expected actual
459 '
460
461 test_expect_success 'an unusual tag with an incomplete line' '
462
463         git tag -m "bogo" bogo &&
464         bogo=$(git cat-file tag bogo) &&
465         bogo=$(printf "%s" "$bogo" | git mktag) &&
466         git tag -f bogo "$bogo" &&
467         git for-each-ref --format "%(body)" refs/tags/bogo
468
469 '
470
471 test_expect_success 'create tag with subject and body content' '
472         cat >>msg <<-\EOF &&
473                 the subject line
474
475                 first body line
476                 second body line
477         EOF
478         git tag -F msg subject-body
479 '
480 test_atom refs/tags/subject-body subject 'the subject line'
481 test_atom refs/tags/subject-body body 'first body line
482 second body line
483 '
484 test_atom refs/tags/subject-body contents 'the subject line
485
486 first body line
487 second body line
488 '
489
490 test_expect_success 'create tag with multiline subject' '
491         cat >msg <<-\EOF &&
492                 first subject line
493                 second subject line
494
495                 first body line
496                 second body line
497         EOF
498         git tag -F msg multiline
499 '
500 test_atom refs/tags/multiline subject 'first subject line second subject line'
501 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
502 test_atom refs/tags/multiline body 'first body line
503 second body line
504 '
505 test_atom refs/tags/multiline contents:body 'first body line
506 second body line
507 '
508 test_atom refs/tags/multiline contents:signature ''
509 test_atom refs/tags/multiline contents 'first subject line
510 second subject line
511
512 first body line
513 second body line
514 '
515
516 test_expect_success GPG 'create signed tags' '
517         git tag -s -m "" signed-empty &&
518         git tag -s -m "subject line" signed-short &&
519         cat >msg <<-\EOF &&
520         subject line
521
522         body contents
523         EOF
524         git tag -s -F msg signed-long
525 '
526
527 sig='-----BEGIN PGP SIGNATURE-----
528 -----END PGP SIGNATURE-----
529 '
530
531 PREREQ=GPG
532 test_atom refs/tags/signed-empty subject ''
533 test_atom refs/tags/signed-empty contents:subject ''
534 test_atom refs/tags/signed-empty body "$sig"
535 test_atom refs/tags/signed-empty contents:body ''
536 test_atom refs/tags/signed-empty contents:signature "$sig"
537 test_atom refs/tags/signed-empty contents "$sig"
538
539 test_atom refs/tags/signed-short subject 'subject line'
540 test_atom refs/tags/signed-short contents:subject 'subject line'
541 test_atom refs/tags/signed-short body "$sig"
542 test_atom refs/tags/signed-short contents:body ''
543 test_atom refs/tags/signed-short contents:signature "$sig"
544 test_atom refs/tags/signed-short contents "subject line
545 $sig"
546
547 test_atom refs/tags/signed-long subject 'subject line'
548 test_atom refs/tags/signed-long contents:subject 'subject line'
549 test_atom refs/tags/signed-long body "body contents
550 $sig"
551 test_atom refs/tags/signed-long contents:body 'body contents
552 '
553 test_atom refs/tags/signed-long contents:signature "$sig"
554 test_atom refs/tags/signed-long contents "subject line
555
556 body contents
557 $sig"
558
559 cat >expected <<EOF
560 $(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
561 $(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
562 EOF
563
564 test_expect_success 'Verify sort with multiple keys' '
565         git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
566                 refs/tags/bogo refs/tags/master > actual &&
567         test_cmp expected actual
568 '
569
570
571 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
572         test_when_finished "git checkout master" &&
573         git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
574         sed -e "s/^\* /  /" actual >expect &&
575         git checkout --orphan HEAD &&
576         git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
577         test_cmp expect actual
578 '
579
580 cat >trailers <<EOF
581 Reviewed-by: A U Thor <author@example.com>
582 Signed-off-by: A U Thor <author@example.com>
583 EOF
584
585 test_expect_success 'basic atom: head contents:trailers' '
586         echo "Some contents" > two &&
587         git add two &&
588         git commit -F - <<-EOF &&
589         trailers: this commit message has trailers
590
591         Some message contents
592
593         $(cat trailers)
594         EOF
595         git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
596         sanitize_pgp <actual >actual.clean &&
597         # git for-each-ref ends with a blank line
598         cat >expect <<-EOF &&
599         $(cat trailers)
600
601         EOF
602         test_cmp expect actual.clean
603 '
604
605 test_expect_success 'Add symbolic ref for the following tests' '
606         git symbolic-ref refs/heads/sym refs/heads/master
607 '
608
609 cat >expected <<EOF
610 refs/heads/master
611 EOF
612
613 test_expect_success 'Verify usage of %(symref) atom' '
614         git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
615         test_cmp expected actual
616 '
617
618 cat >expected <<EOF
619 heads/master
620 EOF
621
622 test_expect_success 'Verify usage of %(symref:short) atom' '
623         git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
624         test_cmp expected actual
625 '
626
627 test_done