Merge branch 'jk/rebase-i-squash-count-fix' into next
[git] / t / t7004-tag.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Carlos Rica
4 #
5
6 test_description='git tag
7
8 Tests for operations with tags.'
9
10 . ./test-lib.sh
11 . "$TEST_DIRECTORY"/lib-gpg.sh
12
13 # creating and listing lightweight tags:
14
15 tag_exists () {
16         git show-ref --quiet --verify refs/tags/"$1"
17 }
18
19 # todo: git tag -l now returns always zero, when fixed, change this test
20 test_expect_success 'listing all tags in an empty tree should succeed' '
21         git tag -l &&
22         git tag
23 '
24
25 test_expect_success 'listing all tags in an empty tree should output nothing' '
26         test $(git tag -l | wc -l) -eq 0 &&
27         test $(git tag | wc -l) -eq 0
28 '
29
30 test_expect_success 'sort tags, ignore case' '
31         (
32                 git init sort &&
33                 cd sort &&
34                 test_commit initial &&
35                 git tag tag-one &&
36                 git tag TAG-two &&
37                 git tag -l >actual &&
38                 cat >expected <<-\EOF &&
39                 TAG-two
40                 initial
41                 tag-one
42                 EOF
43                 test_cmp expected actual &&
44                 git tag -l -i >actual &&
45                 cat >expected <<-\EOF &&
46                 initial
47                 tag-one
48                 TAG-two
49                 EOF
50                 test_cmp expected actual
51         )
52 '
53
54 test_expect_success 'looking for a tag in an empty tree should fail' \
55         '! (tag_exists mytag)'
56
57 test_expect_success 'creating a tag in an empty tree should fail' '
58         test_must_fail git tag mynotag &&
59         ! tag_exists mynotag
60 '
61
62 test_expect_success 'creating a tag for HEAD in an empty tree should fail' '
63         test_must_fail git tag mytaghead HEAD &&
64         ! tag_exists mytaghead
65 '
66
67 test_expect_success 'creating a tag for an unknown revision should fail' '
68         test_must_fail git tag mytagnorev aaaaaaaaaaa &&
69         ! tag_exists mytagnorev
70 '
71
72 # commit used in the tests, test_tick is also called here to freeze the date:
73 test_expect_success 'creating a tag using default HEAD should succeed' '
74         test_tick &&
75         echo foo >foo &&
76         git add foo &&
77         git commit -m Foo &&
78         git tag mytag &&
79         test_must_fail git reflog exists refs/tags/mytag
80 '
81
82 test_expect_success 'creating a tag with --create-reflog should create reflog' '
83         test_when_finished "git tag -d tag_with_reflog" &&
84         git tag --create-reflog tag_with_reflog &&
85         git reflog exists refs/tags/tag_with_reflog
86 '
87
88 test_expect_success '--create-reflog does not create reflog on failure' '
89         test_must_fail git tag --create-reflog mytag &&
90         test_must_fail git reflog exists refs/tags/mytag
91 '
92
93 test_expect_success 'listing all tags if one exists should succeed' '
94         git tag -l &&
95         git tag
96 '
97
98 test_expect_success 'listing all tags if one exists should output that tag' '
99         test $(git tag -l) = mytag &&
100         test $(git tag) = mytag
101 '
102
103 # pattern matching:
104
105 test_expect_success 'listing a tag using a matching pattern should succeed' \
106         'git tag -l mytag'
107
108 test_expect_success 'listing a tag with --ignore-case' \
109         'test $(git tag -l --ignore-case MYTAG) = mytag'
110
111 test_expect_success \
112         'listing a tag using a matching pattern should output that tag' \
113         'test $(git tag -l mytag) = mytag'
114
115 # todo: git tag -l now returns always zero, when fixed, change this test
116 test_expect_success \
117         'listing tags using a non-matching pattern should suceed' \
118         'git tag -l xxx'
119
120 test_expect_success \
121         'listing tags using a non-matching pattern should output nothing' \
122         'test $(git tag -l xxx | wc -l) -eq 0'
123
124 # special cases for creating tags:
125
126 test_expect_success \
127         'trying to create a tag with the name of one existing should fail' \
128         'test_must_fail git tag mytag'
129
130 test_expect_success \
131         'trying to create a tag with a non-valid name should fail' '
132         test $(git tag -l | wc -l) -eq 1 &&
133         test_must_fail git tag "" &&
134         test_must_fail git tag .othertag &&
135         test_must_fail git tag "other tag" &&
136         test_must_fail git tag "othertag^" &&
137         test_must_fail git tag "other~tag" &&
138         test $(git tag -l | wc -l) -eq 1
139 '
140
141 test_expect_success 'creating a tag using HEAD directly should succeed' '
142         git tag myhead HEAD &&
143         tag_exists myhead
144 '
145
146 test_expect_success '--force can create a tag with the name of one existing' '
147         tag_exists mytag &&
148         git tag --force mytag &&
149         tag_exists mytag'
150
151 test_expect_success '--force is moot with a non-existing tag name' '
152         git tag newtag >expect &&
153         git tag --force forcetag >actual &&
154         test_cmp expect actual
155 '
156 git tag -d newtag forcetag
157
158 # deleting tags:
159
160 test_expect_success 'trying to delete an unknown tag should fail' '
161         ! tag_exists unknown-tag &&
162         test_must_fail git tag -d unknown-tag
163 '
164
165 cat >expect <<EOF
166 myhead
167 mytag
168 EOF
169 test_expect_success \
170         'trying to delete tags without params should succeed and do nothing' '
171         git tag -l > actual && test_cmp expect actual &&
172         git tag -d &&
173         git tag -l > actual && test_cmp expect actual
174 '
175
176 test_expect_success \
177         'deleting two existing tags in one command should succeed' '
178         tag_exists mytag &&
179         tag_exists myhead &&
180         git tag -d mytag myhead &&
181         ! tag_exists mytag &&
182         ! tag_exists myhead
183 '
184
185 test_expect_success \
186         'creating a tag with the name of another deleted one should succeed' '
187         ! tag_exists mytag &&
188         git tag mytag &&
189         tag_exists mytag
190 '
191
192 test_expect_success \
193         'trying to delete two tags, existing and not, should fail in the 2nd' '
194         tag_exists mytag &&
195         ! tag_exists myhead &&
196         test_must_fail git tag -d mytag anothertag &&
197         ! tag_exists mytag &&
198         ! tag_exists myhead
199 '
200
201 test_expect_success 'trying to delete an already deleted tag should fail' \
202         'test_must_fail git tag -d mytag'
203
204 # listing various tags with pattern matching:
205
206 cat >expect <<EOF
207 a1
208 aa1
209 cba
210 t210
211 t211
212 v0.2.1
213 v1.0
214 v1.0.1
215 v1.1.3
216 EOF
217 test_expect_success 'listing all tags should print them ordered' '
218         git tag v1.0.1 &&
219         git tag t211 &&
220         git tag aa1 &&
221         git tag v0.2.1 &&
222         git tag v1.1.3 &&
223         git tag cba &&
224         git tag a1 &&
225         git tag v1.0 &&
226         git tag t210 &&
227         git tag -l > actual &&
228         test_cmp expect actual &&
229         git tag > actual &&
230         test_cmp expect actual
231 '
232
233 cat >expect <<EOF
234 a1
235 aa1
236 cba
237 EOF
238 test_expect_success \
239         'listing tags with substring as pattern must print those matching' '
240         rm *a* &&
241         git tag -l "*a*" > current &&
242         test_cmp expect current
243 '
244
245 cat >expect <<EOF
246 v0.2.1
247 v1.0.1
248 EOF
249 test_expect_success \
250         'listing tags with a suffix as pattern must print those matching' '
251         git tag -l "*.1" > actual &&
252         test_cmp expect actual
253 '
254
255 cat >expect <<EOF
256 t210
257 t211
258 EOF
259 test_expect_success \
260         'listing tags with a prefix as pattern must print those matching' '
261         git tag -l "t21*" > actual &&
262         test_cmp expect actual
263 '
264
265 cat >expect <<EOF
266 a1
267 EOF
268 test_expect_success \
269         'listing tags using a name as pattern must print that one matching' '
270         git tag -l a1 > actual &&
271         test_cmp expect actual
272 '
273
274 cat >expect <<EOF
275 v1.0
276 EOF
277 test_expect_success \
278         'listing tags using a name as pattern must print that one matching' '
279         git tag -l v1.0 > actual &&
280         test_cmp expect actual
281 '
282
283 cat >expect <<EOF
284 v1.0.1
285 v1.1.3
286 EOF
287 test_expect_success \
288         'listing tags with ? in the pattern should print those matching' '
289         git tag -l "v1.?.?" > actual &&
290         test_cmp expect actual
291 '
292
293 >expect
294 test_expect_success \
295         'listing tags using v.* should print nothing because none have v.' '
296         git tag -l "v.*" > actual &&
297         test_cmp expect actual
298 '
299
300 cat >expect <<EOF
301 v0.2.1
302 v1.0
303 v1.0.1
304 v1.1.3
305 EOF
306 test_expect_success \
307         'listing tags using v* should print only those having v' '
308         git tag -l "v*" > actual &&
309         test_cmp expect actual
310 '
311
312 test_expect_success 'tag -l can accept multiple patterns' '
313         git tag -l "v1*" "v0*" >actual &&
314         test_cmp expect actual
315 '
316
317 test_expect_success 'listing tags in column' '
318         COLUMNS=40 git tag -l --column=row >actual &&
319         cat >expected <<\EOF &&
320 a1      aa1     cba     t210    t211
321 v0.2.1  v1.0    v1.0.1  v1.1.3
322 EOF
323         test_cmp expected actual
324 '
325
326 test_expect_success 'listing tags in column with column.*' '
327         git config column.tag row &&
328         git config column.ui dense &&
329         COLUMNS=40 git tag -l >actual &&
330         git config --unset column.ui &&
331         git config --unset column.tag &&
332         cat >expected <<\EOF &&
333 a1      aa1   cba     t210    t211
334 v0.2.1  v1.0  v1.0.1  v1.1.3
335 EOF
336         test_cmp expected actual
337 '
338
339 test_expect_success 'listing tag with -n --column should fail' '
340         test_must_fail git tag --column -n
341 '
342
343 test_expect_success 'listing tags -n in column with column.ui ignored' '
344         git config column.ui "row dense" &&
345         COLUMNS=40 git tag -l -n >actual &&
346         git config --unset column.ui &&
347         cat >expected <<\EOF &&
348 a1              Foo
349 aa1             Foo
350 cba             Foo
351 t210            Foo
352 t211            Foo
353 v0.2.1          Foo
354 v1.0            Foo
355 v1.0.1          Foo
356 v1.1.3          Foo
357 EOF
358         test_cmp expected actual
359 '
360
361 # creating and verifying lightweight tags:
362
363 test_expect_success \
364         'a non-annotated tag created without parameters should point to HEAD' '
365         git tag non-annotated-tag &&
366         test $(git cat-file -t non-annotated-tag) = commit &&
367         test $(git rev-parse non-annotated-tag) = $(git rev-parse HEAD)
368 '
369
370 test_expect_success 'trying to verify an unknown tag should fail' \
371         'test_must_fail git tag -v unknown-tag'
372
373 test_expect_success \
374         'trying to verify a non-annotated and non-signed tag should fail' \
375         'test_must_fail git tag -v non-annotated-tag'
376
377 test_expect_success \
378         'trying to verify many non-annotated or unknown tags, should fail' \
379         'test_must_fail git tag -v unknown-tag1 non-annotated-tag unknown-tag2'
380
381 # creating annotated tags:
382
383 get_tag_msg () {
384         git cat-file tag "$1" | sed -e "/BEGIN PGP/q"
385 }
386
387 # run test_tick before committing always gives the time in that timezone
388 get_tag_header () {
389 cat <<EOF
390 object $2
391 type $3
392 tag $1
393 tagger C O Mitter <committer@example.com> $4 -0700
394
395 EOF
396 }
397
398 commit=$(git rev-parse HEAD)
399 time=$test_tick
400
401 get_tag_header annotated-tag $commit commit $time >expect
402 echo "A message" >>expect
403 test_expect_success \
404         'creating an annotated tag with -m message should succeed' '
405         git tag -m "A message" annotated-tag &&
406         get_tag_msg annotated-tag >actual &&
407         test_cmp expect actual
408 '
409
410 cat >msgfile <<EOF
411 Another message
412 in a file.
413 EOF
414 get_tag_header file-annotated-tag $commit commit $time >expect
415 cat msgfile >>expect
416 test_expect_success \
417         'creating an annotated tag with -F messagefile should succeed' '
418         git tag -F msgfile file-annotated-tag &&
419         get_tag_msg file-annotated-tag >actual &&
420         test_cmp expect actual
421 '
422
423 cat >inputmsg <<EOF
424 A message from the
425 standard input
426 EOF
427 get_tag_header stdin-annotated-tag $commit commit $time >expect
428 cat inputmsg >>expect
429 test_expect_success 'creating an annotated tag with -F - should succeed' '
430         git tag -F - stdin-annotated-tag <inputmsg &&
431         get_tag_msg stdin-annotated-tag >actual &&
432         test_cmp expect actual
433 '
434
435 test_expect_success \
436         'trying to create a tag with a non-existing -F file should fail' '
437         ! test -f nonexistingfile &&
438         ! tag_exists notag &&
439         test_must_fail git tag -F nonexistingfile notag &&
440         ! tag_exists notag
441 '
442
443 test_expect_success \
444         'trying to create tags giving both -m or -F options should fail' '
445         echo "message file 1" >msgfile1 &&
446         echo "message file 2" >msgfile2 &&
447         ! tag_exists msgtag &&
448         test_must_fail git tag -m "message 1" -F msgfile1 msgtag &&
449         ! tag_exists msgtag &&
450         test_must_fail git tag -F msgfile1 -m "message 1" msgtag &&
451         ! tag_exists msgtag &&
452         test_must_fail git tag -m "message 1" -F msgfile1 \
453                 -m "message 2" msgtag &&
454         ! tag_exists msgtag
455 '
456
457 # blank and empty messages:
458
459 get_tag_header empty-annotated-tag $commit commit $time >expect
460 test_expect_success \
461         'creating a tag with an empty -m message should succeed' '
462         git tag -m "" empty-annotated-tag &&
463         get_tag_msg empty-annotated-tag >actual &&
464         test_cmp expect actual
465 '
466
467 >emptyfile
468 get_tag_header emptyfile-annotated-tag $commit commit $time >expect
469 test_expect_success \
470         'creating a tag with an empty -F messagefile should succeed' '
471         git tag -F emptyfile emptyfile-annotated-tag &&
472         get_tag_msg emptyfile-annotated-tag >actual &&
473         test_cmp expect actual
474 '
475
476 printf '\n\n  \n\t\nLeading blank lines\n' >blanksfile
477 printf '\n\t \t  \nRepeated blank lines\n' >>blanksfile
478 printf '\n\n\nTrailing spaces      \t  \n' >>blanksfile
479 printf '\nTrailing blank lines\n\n\t \n\n' >>blanksfile
480 get_tag_header blanks-annotated-tag $commit commit $time >expect
481 cat >>expect <<EOF
482 Leading blank lines
483
484 Repeated blank lines
485
486 Trailing spaces
487
488 Trailing blank lines
489 EOF
490 test_expect_success \
491         'extra blanks in the message for an annotated tag should be removed' '
492         git tag -F blanksfile blanks-annotated-tag &&
493         get_tag_msg blanks-annotated-tag >actual &&
494         test_cmp expect actual
495 '
496
497 get_tag_header blank-annotated-tag $commit commit $time >expect
498 test_expect_success \
499         'creating a tag with blank -m message with spaces should succeed' '
500         git tag -m "     " blank-annotated-tag &&
501         get_tag_msg blank-annotated-tag >actual &&
502         test_cmp expect actual
503 '
504
505 echo '     ' >blankfile
506 echo ''      >>blankfile
507 echo '  '    >>blankfile
508 get_tag_header blankfile-annotated-tag $commit commit $time >expect
509 test_expect_success \
510         'creating a tag with blank -F messagefile with spaces should succeed' '
511         git tag -F blankfile blankfile-annotated-tag &&
512         get_tag_msg blankfile-annotated-tag >actual &&
513         test_cmp expect actual
514 '
515
516 printf '      ' >blanknonlfile
517 get_tag_header blanknonlfile-annotated-tag $commit commit $time >expect
518 test_expect_success \
519         'creating a tag with -F file of spaces and no newline should succeed' '
520         git tag -F blanknonlfile blanknonlfile-annotated-tag &&
521         get_tag_msg blanknonlfile-annotated-tag >actual &&
522         test_cmp expect actual
523 '
524
525 # messages with commented lines:
526
527 cat >commentsfile <<EOF
528 # A comment
529
530 ############
531 The message.
532 ############
533 One line.
534
535
536 # commented lines
537 # commented lines
538
539 Another line.
540 # comments
541
542 Last line.
543 EOF
544 get_tag_header comments-annotated-tag $commit commit $time >expect
545 cat >>expect <<EOF
546 The message.
547 One line.
548
549 Another line.
550
551 Last line.
552 EOF
553 test_expect_success \
554         'creating a tag using a -F messagefile with #comments should succeed' '
555         git tag -F commentsfile comments-annotated-tag &&
556         get_tag_msg comments-annotated-tag >actual &&
557         test_cmp expect actual
558 '
559
560 get_tag_header comment-annotated-tag $commit commit $time >expect
561 test_expect_success \
562         'creating a tag with a #comment in the -m message should succeed' '
563         git tag -m "#comment" comment-annotated-tag &&
564         get_tag_msg comment-annotated-tag >actual &&
565         test_cmp expect actual
566 '
567
568 echo '#comment' >commentfile
569 echo ''         >>commentfile
570 echo '####'     >>commentfile
571 get_tag_header commentfile-annotated-tag $commit commit $time >expect
572 test_expect_success \
573         'creating a tag with #comments in the -F messagefile should succeed' '
574         git tag -F commentfile commentfile-annotated-tag &&
575         get_tag_msg commentfile-annotated-tag >actual &&
576         test_cmp expect actual
577 '
578
579 printf '#comment' >commentnonlfile
580 get_tag_header commentnonlfile-annotated-tag $commit commit $time >expect
581 test_expect_success \
582         'creating a tag with a file of #comment and no newline should succeed' '
583         git tag -F commentnonlfile commentnonlfile-annotated-tag &&
584         get_tag_msg commentnonlfile-annotated-tag >actual &&
585         test_cmp expect actual
586 '
587
588 # listing messages for annotated non-signed tags:
589
590 test_expect_success \
591         'listing the one-line message of a non-signed tag should succeed' '
592         git tag -m "A msg" tag-one-line &&
593
594         echo "tag-one-line" >expect &&
595         git tag -l | grep "^tag-one-line" >actual &&
596         test_cmp expect actual &&
597         git tag -n0 -l | grep "^tag-one-line" >actual &&
598         test_cmp expect actual &&
599         git tag -n0 -l tag-one-line >actual &&
600         test_cmp expect actual &&
601
602         echo "tag-one-line    A msg" >expect &&
603         git tag -n1 -l | grep "^tag-one-line" >actual &&
604         test_cmp expect actual &&
605         git tag -n -l | grep "^tag-one-line" >actual &&
606         test_cmp expect actual &&
607         git tag -n1 -l tag-one-line >actual &&
608         test_cmp expect actual &&
609         git tag -n2 -l tag-one-line >actual &&
610         test_cmp expect actual &&
611         git tag -n999 -l tag-one-line >actual &&
612         test_cmp expect actual
613 '
614
615 test_expect_success \
616         'listing the zero-lines message of a non-signed tag should succeed' '
617         git tag -m "" tag-zero-lines &&
618
619         echo "tag-zero-lines" >expect &&
620         git tag -l | grep "^tag-zero-lines" >actual &&
621         test_cmp expect actual &&
622         git tag -n0 -l | grep "^tag-zero-lines" >actual &&
623         test_cmp expect actual &&
624         git tag -n0 -l tag-zero-lines >actual &&
625         test_cmp expect actual &&
626
627         echo "tag-zero-lines  " >expect &&
628         git tag -n1 -l | grep "^tag-zero-lines" >actual &&
629         test_cmp expect actual &&
630         git tag -n -l | grep "^tag-zero-lines" >actual &&
631         test_cmp expect actual &&
632         git tag -n1 -l tag-zero-lines >actual &&
633         test_cmp expect actual &&
634         git tag -n2 -l tag-zero-lines >actual &&
635         test_cmp expect actual &&
636         git tag -n999 -l tag-zero-lines >actual &&
637         test_cmp expect actual
638 '
639
640 echo 'tag line one' >annotagmsg
641 echo 'tag line two' >>annotagmsg
642 echo 'tag line three' >>annotagmsg
643 test_expect_success \
644         'listing many message lines of a non-signed tag should succeed' '
645         git tag -F annotagmsg tag-lines &&
646
647         echo "tag-lines" >expect &&
648         git tag -l | grep "^tag-lines" >actual &&
649         test_cmp expect actual &&
650         git tag -n0 -l | grep "^tag-lines" >actual &&
651         test_cmp expect actual &&
652         git tag -n0 -l tag-lines >actual &&
653         test_cmp expect actual &&
654
655         echo "tag-lines       tag line one" >expect &&
656         git tag -n1 -l | grep "^tag-lines" >actual &&
657         test_cmp expect actual &&
658         git tag -n -l | grep "^tag-lines" >actual &&
659         test_cmp expect actual &&
660         git tag -n1 -l tag-lines >actual &&
661         test_cmp expect actual &&
662
663         echo "    tag line two" >>expect &&
664         git tag -n2 -l | grep "^ *tag.line" >actual &&
665         test_cmp expect actual &&
666         git tag -n2 -l tag-lines >actual &&
667         test_cmp expect actual &&
668
669         echo "    tag line three" >>expect &&
670         git tag -n3 -l | grep "^ *tag.line" >actual &&
671         test_cmp expect actual &&
672         git tag -n3 -l tag-lines >actual &&
673         test_cmp expect actual &&
674         git tag -n4 -l | grep "^ *tag.line" >actual &&
675         test_cmp expect actual &&
676         git tag -n4 -l tag-lines >actual &&
677         test_cmp expect actual &&
678         git tag -n99 -l | grep "^ *tag.line" >actual &&
679         test_cmp expect actual &&
680         git tag -n99 -l tag-lines >actual &&
681         test_cmp expect actual
682 '
683
684 test_expect_success 'annotations for blobs are empty' '
685         blob=$(git hash-object -w --stdin <<-\EOF
686         Blob paragraph 1.
687
688         Blob paragraph 2.
689         EOF
690         ) &&
691         git tag tag-blob $blob &&
692         echo "tag-blob        " >expect &&
693         git tag -n1 -l tag-blob >actual &&
694         test_cmp expect actual
695 '
696
697 # trying to verify annotated non-signed tags:
698
699 test_expect_success GPG \
700         'trying to verify an annotated non-signed tag should fail' '
701         tag_exists annotated-tag &&
702         test_must_fail git tag -v annotated-tag
703 '
704
705 test_expect_success GPG \
706         'trying to verify a file-annotated non-signed tag should fail' '
707         tag_exists file-annotated-tag &&
708         test_must_fail git tag -v file-annotated-tag
709 '
710
711 test_expect_success GPG \
712         'trying to verify two annotated non-signed tags should fail' '
713         tag_exists annotated-tag file-annotated-tag &&
714         test_must_fail git tag -v annotated-tag file-annotated-tag
715 '
716
717 # creating and verifying signed tags:
718
719 get_tag_header signed-tag $commit commit $time >expect
720 echo 'A signed tag message' >>expect
721 echo '-----BEGIN PGP SIGNATURE-----' >>expect
722 test_expect_success GPG 'creating a signed tag with -m message should succeed' '
723         git tag -s -m "A signed tag message" signed-tag &&
724         get_tag_msg signed-tag >actual &&
725         test_cmp expect actual
726 '
727
728 get_tag_header u-signed-tag $commit commit $time >expect
729 echo 'Another message' >>expect
730 echo '-----BEGIN PGP SIGNATURE-----' >>expect
731 test_expect_success GPG 'sign with a given key id' '
732
733         git tag -u committer@example.com -m "Another message" u-signed-tag &&
734         get_tag_msg u-signed-tag >actual &&
735         test_cmp expect actual
736
737 '
738
739 test_expect_success GPG 'sign with an unknown id (1)' '
740
741         test_must_fail git tag -u author@example.com \
742                 -m "Another message" o-signed-tag
743
744 '
745
746 test_expect_success GPG 'sign with an unknown id (2)' '
747
748         test_must_fail git tag -u DEADBEEF -m "Another message" o-signed-tag
749
750 '
751
752 cat >fakeeditor <<'EOF'
753 #!/bin/sh
754 test -n "$1" && exec >"$1"
755 echo A signed tag message
756 echo from a fake editor.
757 EOF
758 chmod +x fakeeditor
759
760 get_tag_header implied-sign $commit commit $time >expect
761 ./fakeeditor >>expect
762 echo '-----BEGIN PGP SIGNATURE-----' >>expect
763 test_expect_success GPG '-u implies signed tag' '
764         GIT_EDITOR=./fakeeditor git tag -u CDDE430D implied-sign &&
765         get_tag_msg implied-sign >actual &&
766         test_cmp expect actual
767 '
768
769 cat >sigmsgfile <<EOF
770 Another signed tag
771 message in a file.
772 EOF
773 get_tag_header file-signed-tag $commit commit $time >expect
774 cat sigmsgfile >>expect
775 echo '-----BEGIN PGP SIGNATURE-----' >>expect
776 test_expect_success GPG \
777         'creating a signed tag with -F messagefile should succeed' '
778         git tag -s -F sigmsgfile file-signed-tag &&
779         get_tag_msg file-signed-tag >actual &&
780         test_cmp expect actual
781 '
782
783 cat >siginputmsg <<EOF
784 A signed tag message from
785 the standard input
786 EOF
787 get_tag_header stdin-signed-tag $commit commit $time >expect
788 cat siginputmsg >>expect
789 echo '-----BEGIN PGP SIGNATURE-----' >>expect
790 test_expect_success GPG 'creating a signed tag with -F - should succeed' '
791         git tag -s -F - stdin-signed-tag <siginputmsg &&
792         get_tag_msg stdin-signed-tag >actual &&
793         test_cmp expect actual
794 '
795
796 get_tag_header implied-annotate $commit commit $time >expect
797 ./fakeeditor >>expect
798 echo '-----BEGIN PGP SIGNATURE-----' >>expect
799 test_expect_success GPG '-s implies annotated tag' '
800         GIT_EDITOR=./fakeeditor git tag -s implied-annotate &&
801         get_tag_msg implied-annotate >actual &&
802         test_cmp expect actual
803 '
804
805 get_tag_header forcesignannotated-implied-sign $commit commit $time >expect
806 echo "A message" >>expect
807 echo '-----BEGIN PGP SIGNATURE-----' >>expect
808 test_expect_success GPG \
809         'git tag -s implied if configured with tag.forcesignannotated' \
810         'test_config tag.forcesignannotated true &&
811         git tag -m "A message" forcesignannotated-implied-sign &&
812         get_tag_msg forcesignannotated-implied-sign >actual &&
813         test_cmp expect actual
814 '
815
816 test_expect_success GPG \
817         'lightweight with no message when configured with tag.forcesignannotated' \
818         'test_config tag.forcesignannotated true &&
819         git tag forcesignannotated-lightweight &&
820         tag_exists forcesignannotated-lightweight &&
821         test_must_fail git tag -v forcesignannotated-no-message
822 '
823
824 get_tag_header forcesignannotated-annotate $commit commit $time >expect
825 echo "A message" >>expect
826 test_expect_success GPG \
827         'git tag -a disable configured tag.forcesignannotated' \
828         'test_config tag.forcesignannotated true &&
829         git tag -a -m "A message" forcesignannotated-annotate &&
830         get_tag_msg forcesignannotated-annotate >actual &&
831         test_cmp expect actual &&
832         test_must_fail git tag -v forcesignannotated-annotate
833 '
834
835 get_tag_header forcesignannotated-disabled $commit commit $time >expect
836 echo "A message" >>expect
837 echo '-----BEGIN PGP SIGNATURE-----' >>expect
838 test_expect_success GPG \
839         'git tag --sign enable GPG sign' \
840         'test_config tag.forcesignannotated false &&
841         git tag --sign -m "A message" forcesignannotated-disabled &&
842         get_tag_msg forcesignannotated-disabled >actual &&
843         test_cmp expect actual
844 '
845
846 test_expect_success GPG \
847         'trying to create a signed tag with non-existing -F file should fail' '
848         ! test -f nonexistingfile &&
849         ! tag_exists nosigtag &&
850         test_must_fail git tag -s -F nonexistingfile nosigtag &&
851         ! tag_exists nosigtag
852 '
853
854 test_expect_success GPG 'verifying a signed tag should succeed' \
855         'git tag -v signed-tag'
856
857 test_expect_success GPG 'verifying two signed tags in one command should succeed' \
858         'git tag -v signed-tag file-signed-tag'
859
860 test_expect_success GPG \
861         'verifying many signed and non-signed tags should fail' '
862         test_must_fail git tag -v signed-tag annotated-tag &&
863         test_must_fail git tag -v file-annotated-tag file-signed-tag &&
864         test_must_fail git tag -v annotated-tag \
865                 file-signed-tag file-annotated-tag &&
866         test_must_fail git tag -v signed-tag annotated-tag file-signed-tag
867 '
868
869 test_expect_success GPG 'verifying a forged tag should fail' '
870         forged=$(git cat-file tag signed-tag |
871                 sed -e "s/signed-tag/forged-tag/" |
872                 git mktag) &&
873         git tag forged-tag $forged &&
874         test_must_fail git tag -v forged-tag
875 '
876
877 # blank and empty messages for signed tags:
878
879 get_tag_header empty-signed-tag $commit commit $time >expect
880 echo '-----BEGIN PGP SIGNATURE-----' >>expect
881 test_expect_success GPG \
882         'creating a signed tag with an empty -m message should succeed' '
883         git tag -s -m "" empty-signed-tag &&
884         get_tag_msg empty-signed-tag >actual &&
885         test_cmp expect actual &&
886         git tag -v empty-signed-tag
887 '
888
889 >sigemptyfile
890 get_tag_header emptyfile-signed-tag $commit commit $time >expect
891 echo '-----BEGIN PGP SIGNATURE-----' >>expect
892 test_expect_success GPG \
893         'creating a signed tag with an empty -F messagefile should succeed' '
894         git tag -s -F sigemptyfile emptyfile-signed-tag &&
895         get_tag_msg emptyfile-signed-tag >actual &&
896         test_cmp expect actual &&
897         git tag -v emptyfile-signed-tag
898 '
899
900 printf '\n\n  \n\t\nLeading blank lines\n' > sigblanksfile
901 printf '\n\t \t  \nRepeated blank lines\n' >>sigblanksfile
902 printf '\n\n\nTrailing spaces      \t  \n' >>sigblanksfile
903 printf '\nTrailing blank lines\n\n\t \n\n' >>sigblanksfile
904 get_tag_header blanks-signed-tag $commit commit $time >expect
905 cat >>expect <<EOF
906 Leading blank lines
907
908 Repeated blank lines
909
910 Trailing spaces
911
912 Trailing blank lines
913 EOF
914 echo '-----BEGIN PGP SIGNATURE-----' >>expect
915 test_expect_success GPG \
916         'extra blanks in the message for a signed tag should be removed' '
917         git tag -s -F sigblanksfile blanks-signed-tag &&
918         get_tag_msg blanks-signed-tag >actual &&
919         test_cmp expect actual &&
920         git tag -v blanks-signed-tag
921 '
922
923 get_tag_header blank-signed-tag $commit commit $time >expect
924 echo '-----BEGIN PGP SIGNATURE-----' >>expect
925 test_expect_success GPG \
926         'creating a signed tag with a blank -m message should succeed' '
927         git tag -s -m "     " blank-signed-tag &&
928         get_tag_msg blank-signed-tag >actual &&
929         test_cmp expect actual &&
930         git tag -v blank-signed-tag
931 '
932
933 echo '     ' >sigblankfile
934 echo ''      >>sigblankfile
935 echo '  '    >>sigblankfile
936 get_tag_header blankfile-signed-tag $commit commit $time >expect
937 echo '-----BEGIN PGP SIGNATURE-----' >>expect
938 test_expect_success GPG \
939         'creating a signed tag with blank -F file with spaces should succeed' '
940         git tag -s -F sigblankfile blankfile-signed-tag &&
941         get_tag_msg blankfile-signed-tag >actual &&
942         test_cmp expect actual &&
943         git tag -v blankfile-signed-tag
944 '
945
946 printf '      ' >sigblanknonlfile
947 get_tag_header blanknonlfile-signed-tag $commit commit $time >expect
948 echo '-----BEGIN PGP SIGNATURE-----' >>expect
949 test_expect_success GPG \
950         'creating a signed tag with spaces and no newline should succeed' '
951         git tag -s -F sigblanknonlfile blanknonlfile-signed-tag &&
952         get_tag_msg blanknonlfile-signed-tag >actual &&
953         test_cmp expect actual &&
954         git tag -v signed-tag
955 '
956
957 # messages with commented lines for signed tags:
958
959 cat >sigcommentsfile <<EOF
960 # A comment
961
962 ############
963 The message.
964 ############
965 One line.
966
967
968 # commented lines
969 # commented lines
970
971 Another line.
972 # comments
973
974 Last line.
975 EOF
976 get_tag_header comments-signed-tag $commit commit $time >expect
977 cat >>expect <<EOF
978 The message.
979 One line.
980
981 Another line.
982
983 Last line.
984 EOF
985 echo '-----BEGIN PGP SIGNATURE-----' >>expect
986 test_expect_success GPG \
987         'creating a signed tag with a -F file with #comments should succeed' '
988         git tag -s -F sigcommentsfile comments-signed-tag &&
989         get_tag_msg comments-signed-tag >actual &&
990         test_cmp expect actual &&
991         git tag -v comments-signed-tag
992 '
993
994 get_tag_header comment-signed-tag $commit commit $time >expect
995 echo '-----BEGIN PGP SIGNATURE-----' >>expect
996 test_expect_success GPG \
997         'creating a signed tag with #commented -m message should succeed' '
998         git tag -s -m "#comment" comment-signed-tag &&
999         get_tag_msg comment-signed-tag >actual &&
1000         test_cmp expect actual &&
1001         git tag -v comment-signed-tag
1002 '
1003
1004 echo '#comment' >sigcommentfile
1005 echo ''         >>sigcommentfile
1006 echo '####'     >>sigcommentfile
1007 get_tag_header commentfile-signed-tag $commit commit $time >expect
1008 echo '-----BEGIN PGP SIGNATURE-----' >>expect
1009 test_expect_success GPG \
1010         'creating a signed tag with #commented -F messagefile should succeed' '
1011         git tag -s -F sigcommentfile commentfile-signed-tag &&
1012         get_tag_msg commentfile-signed-tag >actual &&
1013         test_cmp expect actual &&
1014         git tag -v commentfile-signed-tag
1015 '
1016
1017 printf '#comment' >sigcommentnonlfile
1018 get_tag_header commentnonlfile-signed-tag $commit commit $time >expect
1019 echo '-----BEGIN PGP SIGNATURE-----' >>expect
1020 test_expect_success GPG \
1021         'creating a signed tag with a #comment and no newline should succeed' '
1022         git tag -s -F sigcommentnonlfile commentnonlfile-signed-tag &&
1023         get_tag_msg commentnonlfile-signed-tag >actual &&
1024         test_cmp expect actual &&
1025         git tag -v commentnonlfile-signed-tag
1026 '
1027
1028 # listing messages for signed tags:
1029
1030 test_expect_success GPG \
1031         'listing the one-line message of a signed tag should succeed' '
1032         git tag -s -m "A message line signed" stag-one-line &&
1033
1034         echo "stag-one-line" >expect &&
1035         git tag -l | grep "^stag-one-line" >actual &&
1036         test_cmp expect actual &&
1037         git tag -n0 -l | grep "^stag-one-line" >actual &&
1038         test_cmp expect actual &&
1039         git tag -n0 -l stag-one-line >actual &&
1040         test_cmp expect actual &&
1041
1042         echo "stag-one-line   A message line signed" >expect &&
1043         git tag -n1 -l | grep "^stag-one-line" >actual &&
1044         test_cmp expect actual &&
1045         git tag -n -l | grep "^stag-one-line" >actual &&
1046         test_cmp expect actual &&
1047         git tag -n1 -l stag-one-line >actual &&
1048         test_cmp expect actual &&
1049         git tag -n2 -l stag-one-line >actual &&
1050         test_cmp expect actual &&
1051         git tag -n999 -l stag-one-line >actual &&
1052         test_cmp expect actual
1053 '
1054
1055 test_expect_success GPG \
1056         'listing the zero-lines message of a signed tag should succeed' '
1057         git tag -s -m "" stag-zero-lines &&
1058
1059         echo "stag-zero-lines" >expect &&
1060         git tag -l | grep "^stag-zero-lines" >actual &&
1061         test_cmp expect actual &&
1062         git tag -n0 -l | grep "^stag-zero-lines" >actual &&
1063         test_cmp expect actual &&
1064         git tag -n0 -l stag-zero-lines >actual &&
1065         test_cmp expect actual &&
1066
1067         echo "stag-zero-lines " >expect &&
1068         git tag -n1 -l | grep "^stag-zero-lines" >actual &&
1069         test_cmp expect actual &&
1070         git tag -n -l | grep "^stag-zero-lines" >actual &&
1071         test_cmp expect actual &&
1072         git tag -n1 -l stag-zero-lines >actual &&
1073         test_cmp expect actual &&
1074         git tag -n2 -l stag-zero-lines >actual &&
1075         test_cmp expect actual &&
1076         git tag -n999 -l stag-zero-lines >actual &&
1077         test_cmp expect actual
1078 '
1079
1080 echo 'stag line one' >sigtagmsg
1081 echo 'stag line two' >>sigtagmsg
1082 echo 'stag line three' >>sigtagmsg
1083 test_expect_success GPG \
1084         'listing many message lines of a signed tag should succeed' '
1085         git tag -s -F sigtagmsg stag-lines &&
1086
1087         echo "stag-lines" >expect &&
1088         git tag -l | grep "^stag-lines" >actual &&
1089         test_cmp expect actual &&
1090         git tag -n0 -l | grep "^stag-lines" >actual &&
1091         test_cmp expect actual &&
1092         git tag -n0 -l stag-lines >actual &&
1093         test_cmp expect actual &&
1094
1095         echo "stag-lines      stag line one" >expect &&
1096         git tag -n1 -l | grep "^stag-lines" >actual &&
1097         test_cmp expect actual &&
1098         git tag -n -l | grep "^stag-lines" >actual &&
1099         test_cmp expect actual &&
1100         git tag -n1 -l stag-lines >actual &&
1101         test_cmp expect actual &&
1102
1103         echo "    stag line two" >>expect &&
1104         git tag -n2 -l | grep "^ *stag.line" >actual &&
1105         test_cmp expect actual &&
1106         git tag -n2 -l stag-lines >actual &&
1107         test_cmp expect actual &&
1108
1109         echo "    stag line three" >>expect &&
1110         git tag -n3 -l | grep "^ *stag.line" >actual &&
1111         test_cmp expect actual &&
1112         git tag -n3 -l stag-lines >actual &&
1113         test_cmp expect actual &&
1114         git tag -n4 -l | grep "^ *stag.line" >actual &&
1115         test_cmp expect actual &&
1116         git tag -n4 -l stag-lines >actual &&
1117         test_cmp expect actual &&
1118         git tag -n99 -l | grep "^ *stag.line" >actual &&
1119         test_cmp expect actual &&
1120         git tag -n99 -l stag-lines >actual &&
1121         test_cmp expect actual
1122 '
1123
1124 # tags pointing to objects different from commits:
1125
1126 tree=$(git rev-parse HEAD^{tree})
1127 blob=$(git rev-parse HEAD:foo)
1128 tag=$(git rev-parse signed-tag 2>/dev/null)
1129
1130 get_tag_header tree-signed-tag $tree tree $time >expect
1131 echo "A message for a tree" >>expect
1132 echo '-----BEGIN PGP SIGNATURE-----' >>expect
1133 test_expect_success GPG \
1134         'creating a signed tag pointing to a tree should succeed' '
1135         git tag -s -m "A message for a tree" tree-signed-tag HEAD^{tree} &&
1136         get_tag_msg tree-signed-tag >actual &&
1137         test_cmp expect actual
1138 '
1139
1140 get_tag_header blob-signed-tag $blob blob $time >expect
1141 echo "A message for a blob" >>expect
1142 echo '-----BEGIN PGP SIGNATURE-----' >>expect
1143 test_expect_success GPG \
1144         'creating a signed tag pointing to a blob should succeed' '
1145         git tag -s -m "A message for a blob" blob-signed-tag HEAD:foo &&
1146         get_tag_msg blob-signed-tag >actual &&
1147         test_cmp expect actual
1148 '
1149
1150 get_tag_header tag-signed-tag $tag tag $time >expect
1151 echo "A message for another tag" >>expect
1152 echo '-----BEGIN PGP SIGNATURE-----' >>expect
1153 test_expect_success GPG \
1154         'creating a signed tag pointing to another tag should succeed' '
1155         git tag -s -m "A message for another tag" tag-signed-tag signed-tag &&
1156         get_tag_msg tag-signed-tag >actual &&
1157         test_cmp expect actual
1158 '
1159
1160 # usage with rfc1991 signatures
1161 get_tag_header rfc1991-signed-tag $commit commit $time >expect
1162 echo "RFC1991 signed tag" >>expect
1163 echo '-----BEGIN PGP MESSAGE-----' >>expect
1164 test_expect_success GPG,RFC1991 \
1165         'creating a signed tag with rfc1991' '
1166         echo "rfc1991" >gpghome/gpg.conf &&
1167         git tag -s -m "RFC1991 signed tag" rfc1991-signed-tag $commit &&
1168         get_tag_msg rfc1991-signed-tag >actual &&
1169         test_cmp expect actual
1170 '
1171
1172 cat >fakeeditor <<'EOF'
1173 #!/bin/sh
1174 cp "$1" actual
1175 EOF
1176 chmod +x fakeeditor
1177
1178 test_expect_success GPG,RFC1991 \
1179         'reediting a signed tag body omits signature' '
1180         echo "rfc1991" >gpghome/gpg.conf &&
1181         echo "RFC1991 signed tag" >expect &&
1182         GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1183         test_cmp expect actual
1184 '
1185
1186 test_expect_success GPG,RFC1991 \
1187         'verifying rfc1991 signature' '
1188         echo "rfc1991" >gpghome/gpg.conf &&
1189         git tag -v rfc1991-signed-tag
1190 '
1191
1192 test_expect_success GPG,RFC1991 \
1193         'list tag with rfc1991 signature' '
1194         echo "rfc1991" >gpghome/gpg.conf &&
1195         echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1196         git tag -l -n1 rfc1991-signed-tag >actual &&
1197         test_cmp expect actual &&
1198         git tag -l -n2 rfc1991-signed-tag >actual &&
1199         test_cmp expect actual &&
1200         git tag -l -n999 rfc1991-signed-tag >actual &&
1201         test_cmp expect actual
1202 '
1203
1204 rm -f gpghome/gpg.conf
1205
1206 test_expect_success GPG,RFC1991 \
1207         'verifying rfc1991 signature without --rfc1991' '
1208         git tag -v rfc1991-signed-tag
1209 '
1210
1211 test_expect_success GPG,RFC1991 \
1212         'list tag with rfc1991 signature without --rfc1991' '
1213         echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1214         git tag -l -n1 rfc1991-signed-tag >actual &&
1215         test_cmp expect actual &&
1216         git tag -l -n2 rfc1991-signed-tag >actual &&
1217         test_cmp expect actual &&
1218         git tag -l -n999 rfc1991-signed-tag >actual &&
1219         test_cmp expect actual
1220 '
1221
1222 test_expect_success GPG,RFC1991 \
1223         'reediting a signed tag body omits signature' '
1224         echo "RFC1991 signed tag" >expect &&
1225         GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1226         test_cmp expect actual
1227 '
1228
1229 # try to sign with bad user.signingkey
1230 git config user.signingkey BobTheMouse
1231 test_expect_success GPG \
1232         'git tag -s fails if gpg is misconfigured (bad key)' \
1233         'test_must_fail git tag -s -m tail tag-gpg-failure'
1234 git config --unset user.signingkey
1235
1236 # try to produce invalid signature
1237 test_expect_success GPG \
1238         'git tag -s fails if gpg is misconfigured (bad signature format)' \
1239         'test_config gpg.program echo &&
1240          test_must_fail git tag -s -m tail tag-gpg-failure'
1241
1242
1243 # try to verify without gpg:
1244
1245 rm -rf gpghome
1246 test_expect_success GPG \
1247         'verify signed tag fails when public key is not present' \
1248         'test_must_fail git tag -v signed-tag'
1249
1250 test_expect_success \
1251         'git tag -a fails if tag annotation is empty' '
1252         ! (GIT_EDITOR=cat git tag -a initial-comment)
1253 '
1254
1255 test_expect_success \
1256         'message in editor has initial comment' '
1257         ! (GIT_EDITOR=cat git tag -a initial-comment > actual)
1258 '
1259
1260 test_expect_success 'message in editor has initial comment: first line' '
1261         # check the first line --- should be empty
1262         echo >first.expect &&
1263         sed -e 1q <actual >first.actual &&
1264         test_i18ncmp first.expect first.actual
1265 '
1266
1267 test_expect_success \
1268         'message in editor has initial comment: remainder' '
1269         # remove commented lines from the remainder -- should be empty
1270         >rest.expect &&
1271         sed -e 1d -e "/^#/d" <actual >rest.actual &&
1272         test_cmp rest.expect rest.actual
1273 '
1274
1275 get_tag_header reuse $commit commit $time >expect
1276 echo "An annotation to be reused" >> expect
1277 test_expect_success \
1278         'overwriting an annoted tag should use its previous body' '
1279         git tag -a -m "An annotation to be reused" reuse &&
1280         GIT_EDITOR=true git tag -f -a reuse &&
1281         get_tag_msg reuse >actual &&
1282         test_cmp expect actual
1283 '
1284
1285 test_expect_success 'filename for the message is relative to cwd' '
1286         mkdir subdir &&
1287         echo "Tag message in top directory" >msgfile-5 &&
1288         echo "Tag message in sub directory" >subdir/msgfile-5 &&
1289         (
1290                 cd subdir &&
1291                 git tag -a -F msgfile-5 tag-from-subdir
1292         ) &&
1293         git cat-file tag tag-from-subdir | grep "in sub directory"
1294 '
1295
1296 test_expect_success 'filename for the message is relative to cwd' '
1297         echo "Tag message in sub directory" >subdir/msgfile-6 &&
1298         (
1299                 cd subdir &&
1300                 git tag -a -F msgfile-6 tag-from-subdir-2
1301         ) &&
1302         git cat-file tag tag-from-subdir-2 | grep "in sub directory"
1303 '
1304
1305 # create a few more commits to test --contains
1306
1307 hash1=$(git rev-parse HEAD)
1308
1309 test_expect_success 'creating second commit and tag' '
1310         echo foo-2.0 >foo &&
1311         git add foo &&
1312         git commit -m second &&
1313         git tag v2.0
1314 '
1315
1316 hash2=$(git rev-parse HEAD)
1317
1318 test_expect_success 'creating third commit without tag' '
1319         echo foo-dev >foo &&
1320         git add foo &&
1321         git commit -m third
1322 '
1323
1324 hash3=$(git rev-parse HEAD)
1325
1326 # simple linear checks of --continue
1327
1328 cat > expected <<EOF
1329 v0.2.1
1330 v1.0
1331 v1.0.1
1332 v1.1.3
1333 v2.0
1334 EOF
1335
1336 test_expect_success 'checking that first commit is in all tags (hash)' "
1337         git tag -l --contains $hash1 v* >actual &&
1338         test_cmp expected actual
1339 "
1340
1341 # other ways of specifying the commit
1342 test_expect_success 'checking that first commit is in all tags (tag)' "
1343         git tag -l --contains v1.0 v* >actual &&
1344         test_cmp expected actual
1345 "
1346
1347 test_expect_success 'checking that first commit is in all tags (relative)' "
1348         git tag -l --contains HEAD~2 v* >actual &&
1349         test_cmp expected actual
1350 "
1351
1352 cat > expected <<EOF
1353 v2.0
1354 EOF
1355
1356 test_expect_success 'checking that second commit only has one tag' "
1357         git tag -l --contains $hash2 v* >actual &&
1358         test_cmp expected actual
1359 "
1360
1361
1362 cat > expected <<EOF
1363 EOF
1364
1365 test_expect_success 'checking that third commit has no tags' "
1366         git tag -l --contains $hash3 v* >actual &&
1367         test_cmp expected actual
1368 "
1369
1370 # how about a simple merge?
1371
1372 test_expect_success 'creating simple branch' '
1373         git branch stable v2.0 &&
1374         git checkout stable &&
1375         echo foo-3.0 > foo &&
1376         git commit foo -m fourth &&
1377         git tag v3.0
1378 '
1379
1380 hash4=$(git rev-parse HEAD)
1381
1382 cat > expected <<EOF
1383 v3.0
1384 EOF
1385
1386 test_expect_success 'checking that branch head only has one tag' "
1387         git tag -l --contains $hash4 v* >actual &&
1388         test_cmp expected actual
1389 "
1390
1391 test_expect_success 'merging original branch into this branch' '
1392         git merge --strategy=ours master &&
1393         git tag v4.0
1394 '
1395
1396 cat > expected <<EOF
1397 v4.0
1398 EOF
1399
1400 test_expect_success 'checking that original branch head has one tag now' "
1401         git tag -l --contains $hash3 v* >actual &&
1402         test_cmp expected actual
1403 "
1404
1405 cat > expected <<EOF
1406 v0.2.1
1407 v1.0
1408 v1.0.1
1409 v1.1.3
1410 v2.0
1411 v3.0
1412 v4.0
1413 EOF
1414
1415 test_expect_success 'checking that initial commit is in all tags' "
1416         git tag -l --contains $hash1 v* >actual &&
1417         test_cmp expected actual
1418 "
1419
1420 # mixing modes and options:
1421
1422 test_expect_success 'mixing incompatibles modes and options is forbidden' '
1423         test_must_fail git tag -a &&
1424         test_must_fail git tag -l -v &&
1425         test_must_fail git tag -n 100 &&
1426         test_must_fail git tag -l -m msg &&
1427         test_must_fail git tag -l -F some file &&
1428         test_must_fail git tag -v -s
1429 '
1430
1431 # check points-at
1432
1433 test_expect_success '--points-at cannot be used in non-list mode' '
1434         test_must_fail git tag --points-at=v4.0 foo
1435 '
1436
1437 test_expect_success '--points-at finds lightweight tags' '
1438         echo v4.0 >expect &&
1439         git tag --points-at v4.0 >actual &&
1440         test_cmp expect actual
1441 '
1442
1443 test_expect_success '--points-at finds annotated tags of commits' '
1444         git tag -m "v4.0, annotated" annotated-v4.0 v4.0 &&
1445         echo annotated-v4.0 >expect &&
1446         git tag -l --points-at v4.0 "annotated*" >actual &&
1447         test_cmp expect actual
1448 '
1449
1450 test_expect_success '--points-at finds annotated tags of tags' '
1451         git tag -m "describing the v4.0 tag object" \
1452                 annotated-again-v4.0 annotated-v4.0 &&
1453         cat >expect <<-\EOF &&
1454         annotated-again-v4.0
1455         annotated-v4.0
1456         EOF
1457         git tag --points-at=annotated-v4.0 >actual &&
1458         test_cmp expect actual
1459 '
1460
1461 test_expect_success 'multiple --points-at are OR-ed together' '
1462         cat >expect <<-\EOF &&
1463         v2.0
1464         v3.0
1465         EOF
1466         git tag --points-at=v2.0 --points-at=v3.0 >actual &&
1467         test_cmp expect actual
1468 '
1469
1470 test_expect_success 'lexical sort' '
1471         git tag foo1.3 &&
1472         git tag foo1.6 &&
1473         git tag foo1.10 &&
1474         git tag -l --sort=refname "foo*" >actual &&
1475         cat >expect <<-\EOF &&
1476         foo1.10
1477         foo1.3
1478         foo1.6
1479         EOF
1480         test_cmp expect actual
1481 '
1482
1483 test_expect_success 'version sort' '
1484         git tag -l --sort=version:refname "foo*" >actual &&
1485         cat >expect <<-\EOF &&
1486         foo1.3
1487         foo1.6
1488         foo1.10
1489         EOF
1490         test_cmp expect actual
1491 '
1492
1493 test_expect_success 'reverse version sort' '
1494         git tag -l --sort=-version:refname "foo*" >actual &&
1495         cat >expect <<-\EOF &&
1496         foo1.10
1497         foo1.6
1498         foo1.3
1499         EOF
1500         test_cmp expect actual
1501 '
1502
1503 test_expect_success 'reverse lexical sort' '
1504         git tag -l --sort=-refname "foo*" >actual &&
1505         cat >expect <<-\EOF &&
1506         foo1.6
1507         foo1.3
1508         foo1.10
1509         EOF
1510         test_cmp expect actual
1511 '
1512
1513 test_expect_success 'configured lexical sort' '
1514         git config tag.sort "v:refname" &&
1515         git tag -l "foo*" >actual &&
1516         cat >expect <<-\EOF &&
1517         foo1.3
1518         foo1.6
1519         foo1.10
1520         EOF
1521         test_cmp expect actual
1522 '
1523
1524 test_expect_success 'option override configured sort' '
1525         git tag -l --sort=-refname "foo*" >actual &&
1526         cat >expect <<-\EOF &&
1527         foo1.6
1528         foo1.3
1529         foo1.10
1530         EOF
1531         test_cmp expect actual
1532 '
1533
1534 test_expect_success 'invalid sort parameter on command line' '
1535         test_must_fail git tag -l --sort=notvalid "foo*" >actual
1536 '
1537
1538 test_expect_success 'invalid sort parameter in configuratoin' '
1539         git config tag.sort "v:notvalid" &&
1540         test_must_fail git tag -l "foo*"
1541 '
1542
1543 test_expect_success 'version sort with prerelease reordering' '
1544         git config --unset tag.sort &&
1545         git config versionsort.prereleaseSuffix -rc &&
1546         git tag foo1.6-rc1 &&
1547         git tag foo1.6-rc2 &&
1548         git tag -l --sort=version:refname "foo*" >actual &&
1549         cat >expect <<-\EOF &&
1550         foo1.3
1551         foo1.6-rc1
1552         foo1.6-rc2
1553         foo1.6
1554         foo1.10
1555         EOF
1556         test_cmp expect actual
1557 '
1558
1559 test_expect_success 'reverse version sort with prerelease reordering' '
1560         git tag -l --sort=-version:refname "foo*" >actual &&
1561         cat >expect <<-\EOF &&
1562         foo1.10
1563         foo1.6
1564         foo1.6-rc2
1565         foo1.6-rc1
1566         foo1.3
1567         EOF
1568         test_cmp expect actual
1569 '
1570
1571 run_with_limited_stack () {
1572         (ulimit -s 128 && "$@")
1573 }
1574
1575 test_lazy_prereq ULIMIT_STACK_SIZE 'run_with_limited_stack true'
1576
1577 # we require ulimit, this excludes Windows
1578 test_expect_success ULIMIT_STACK_SIZE '--contains works in a deep repo' '
1579         >expect &&
1580         i=1 &&
1581         while test $i -lt 8000
1582         do
1583                 echo "commit refs/heads/master
1584 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
1585 data <<EOF
1586 commit #$i
1587 EOF"
1588                 test $i = 1 && echo "from refs/heads/master^0"
1589                 i=$(($i + 1))
1590         done | git fast-import &&
1591         git checkout master &&
1592         git tag far-far-away HEAD^ &&
1593         run_with_limited_stack git tag --contains HEAD >actual &&
1594         test_cmp expect actual
1595 '
1596
1597 test_expect_success '--format should list tags as per format given' '
1598         cat >expect <<-\EOF &&
1599         refname : refs/tags/foo1.10
1600         refname : refs/tags/foo1.3
1601         refname : refs/tags/foo1.6
1602         refname : refs/tags/foo1.6-rc1
1603         refname : refs/tags/foo1.6-rc2
1604         EOF
1605         git tag -l --format="refname : %(refname)" "foo*" >actual &&
1606         test_cmp expect actual
1607 '
1608
1609 test_expect_success 'setup --merged test tags' '
1610         git tag mergetest-1 HEAD~2 &&
1611         git tag mergetest-2 HEAD~1 &&
1612         git tag mergetest-3 HEAD
1613 '
1614
1615 test_expect_success '--merged cannot be used in non-list mode' '
1616         test_must_fail git tag --merged=mergetest-2 foo
1617 '
1618
1619 test_expect_success '--merged shows merged tags' '
1620         cat >expect <<-\EOF &&
1621         mergetest-1
1622         mergetest-2
1623         EOF
1624         git tag -l --merged=mergetest-2 mergetest-* >actual &&
1625         test_cmp expect actual
1626 '
1627
1628 test_expect_success '--no-merged show unmerged tags' '
1629         cat >expect <<-\EOF &&
1630         mergetest-3
1631         EOF
1632         git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
1633         test_cmp expect actual
1634 '
1635
1636 test_expect_success 'ambiguous branch/tags not marked' '
1637         git tag ambiguous &&
1638         git branch ambiguous &&
1639         echo ambiguous >expect &&
1640         git tag -l ambiguous >actual &&
1641         test_cmp expect actual
1642 '
1643
1644 test_done