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