1#!/bin/sh
2#
3# Copyright (c) 2007 Carlos Rica
4#
5
6test_description='git tag
7
8Tests for operations with tags.'
9
10. ./test-lib.sh
11. "$TEST_DIRECTORY"/lib-gpg.sh
12. "$TEST_DIRECTORY"/lib-terminal.sh
13
14# creating and listing lightweight tags:
15
16tag_exists () {
17 git show-ref --quiet --verify refs/tags/"$1"
18}
19
20test_expect_success 'listing all tags in an empty tree should succeed' '
21 git tag -l &&
22 git tag
23'
24
25test_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
30test_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
54test_expect_success 'looking for a tag in an empty tree should fail' \
55 '! (tag_exists mytag)'
56
57test_expect_success 'creating a tag in an empty tree should fail' '
58 test_must_fail git tag mynotag &&
59 ! tag_exists mynotag
60'
61
62test_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
67test_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:
73test_expect_success 'creating a tag using default HEAD should succeed' '
74 test_config core.logAllRefUpdates true &&
75 test_tick &&
76 echo foo >foo &&
77 git add foo &&
78 git commit -m Foo &&
79 git tag mytag &&
80 test_must_fail git reflog exists refs/tags/mytag
81'
82
83test_expect_success 'creating a tag with --create-reflog should create reflog' '
84 git log -1 \
85 --format="format:tag: tagging %h (%s, %cd)%n" \
86 --date=format:%Y-%m-%d >expected &&
87 test_when_finished "git tag -d tag_with_reflog" &&
88 git tag --create-reflog tag_with_reflog &&
89 git reflog exists refs/tags/tag_with_reflog &&
90 sed -e "s/^.* //" .git/logs/refs/tags/tag_with_reflog >actual &&
91 test_i18ncmp expected actual
92'
93
94test_expect_success 'annotated tag with --create-reflog has correct message' '
95 git log -1 \
96 --format="format:tag: tagging %h (%s, %cd)%n" \
97 --date=format:%Y-%m-%d >expected &&
98 test_when_finished "git tag -d tag_with_reflog" &&
99 git tag -m "annotated tag" --create-reflog tag_with_reflog &&
100 git reflog exists refs/tags/tag_with_reflog &&
101 sed -e "s/^.* //" .git/logs/refs/tags/tag_with_reflog >actual &&
102 test_i18ncmp expected actual
103'
104
105test_expect_success '--create-reflog does not create reflog on failure' '
106 test_must_fail git tag --create-reflog mytag &&
107 test_must_fail git reflog exists refs/tags/mytag
108'
109
110test_expect_success 'option core.logAllRefUpdates=always creates reflog' '
111 test_when_finished "git tag -d tag_with_reflog" &&
112 test_config core.logAllRefUpdates always &&
113 git tag tag_with_reflog &&
114 git reflog exists refs/tags/tag_with_reflog
115'
116
117test_expect_success 'listing all tags if one exists should succeed' '
118 git tag -l &&
119 git tag
120'
121
122cat >expect <<EOF
123mytag
124EOF
125test_expect_success 'Multiple -l or --list options are equivalent to one -l option' '
126 git tag -l -l >actual &&
127 test_cmp expect actual &&
128 git tag --list --list >actual &&
129 test_cmp expect actual &&
130 git tag --list -l --list >actual &&
131 test_cmp expect actual
132'
133
134test_expect_success 'listing all tags if one exists should output that tag' '
135 test $(git tag -l) = mytag &&
136 test $(git tag) = mytag
137'
138
139# pattern matching:
140
141test_expect_success 'listing a tag using a matching pattern should succeed' \
142 'git tag -l mytag'
143
144test_expect_success 'listing a tag with --ignore-case' \
145 'test $(git tag -l --ignore-case MYTAG) = mytag'
146
147test_expect_success \
148 'listing a tag using a matching pattern should output that tag' \
149 'test $(git tag -l mytag) = mytag'
150
151test_expect_success \
152 'listing tags using a non-matching pattern should succeed' \
153 'git tag -l xxx'
154
155test_expect_success \
156 'listing tags using a non-matching pattern should output nothing' \
157 'test $(git tag -l xxx | wc -l) -eq 0'
158
159# special cases for creating tags:
160
161test_expect_success \
162 'trying to create a tag with the name of one existing should fail' \
163 'test_must_fail git tag mytag'
164
165test_expect_success \
166 'trying to create a tag with a non-valid name should fail' '
167 test $(git tag -l | wc -l) -eq 1 &&
168 test_must_fail git tag "" &&
169 test_must_fail git tag .othertag &&
170 test_must_fail git tag "other tag" &&
171 test_must_fail git tag "othertag^" &&
172 test_must_fail git tag "other~tag" &&
173 test $(git tag -l | wc -l) -eq 1
174'
175
176test_expect_success 'creating a tag using HEAD directly should succeed' '
177 git tag myhead HEAD &&
178 tag_exists myhead
179'
180
181test_expect_success '--force can create a tag with the name of one existing' '
182 tag_exists mytag &&
183 git tag --force mytag &&
184 tag_exists mytag'
185
186test_expect_success '--force is moot with a non-existing tag name' '
187 test_when_finished git tag -d newtag forcetag &&
188 git tag newtag >expect &&
189 git tag --force forcetag >actual &&
190 test_cmp expect actual
191'
192
193# deleting tags:
194
195test_expect_success 'trying to delete an unknown tag should fail' '
196 ! tag_exists unknown-tag &&
197 test_must_fail git tag -d unknown-tag
198'
199
200cat >expect <<EOF
201myhead
202mytag
203EOF
204test_expect_success \
205 'trying to delete tags without params should succeed and do nothing' '
206 git tag -l > actual && test_cmp expect actual &&
207 git tag -d &&
208 git tag -l > actual && test_cmp expect actual
209'
210
211test_expect_success \
212 'deleting two existing tags in one command should succeed' '
213 tag_exists mytag &&
214 tag_exists myhead &&
215 git tag -d mytag myhead &&
216 ! tag_exists mytag &&
217 ! tag_exists myhead
218'
219
220test_expect_success \
221 'creating a tag with the name of another deleted one should succeed' '
222 ! tag_exists mytag &&
223 git tag mytag &&
224 tag_exists mytag
225'
226
227test_expect_success \
228 'trying to delete two tags, existing and not, should fail in the 2nd' '
229 tag_exists mytag &&
230 ! tag_exists myhead &&
231 test_must_fail git tag -d mytag anothertag &&
232 ! tag_exists mytag &&
233 ! tag_exists myhead
234'
235
236test_expect_success 'trying to delete an already deleted tag should fail' \
237 'test_must_fail git tag -d mytag'
238
239# listing various tags with pattern matching:
240
241cat >expect <<EOF
242a1
243aa1
244cba
245t210
246t211
247v0.2.1
248v1.0
249v1.0.1
250v1.1.3
251EOF
252test_expect_success 'listing all tags should print them ordered' '
253 git tag v1.0.1 &&
254 git tag t211 &&
255 git tag aa1 &&
256 git tag v0.2.1 &&
257 git tag v1.1.3 &&
258 git tag cba &&
259 git tag a1 &&
260 git tag v1.0 &&
261 git tag t210 &&
262 git tag -l > actual &&
263 test_cmp expect actual &&
264 git tag > actual &&
265 test_cmp expect actual
266'
267
268cat >expect <<EOF
269a1
270aa1
271cba
272EOF
273test_expect_success \
274 'listing tags with substring as pattern must print those matching' '
275 rm *a* &&
276 git tag -l "*a*" > current &&
277 test_cmp expect current
278'
279
280cat >expect <<EOF
281v0.2.1
282v1.0.1
283EOF
284test_expect_success \
285 'listing tags with a suffix as pattern must print those matching' '
286 git tag -l "*.1" > actual &&
287 test_cmp expect actual
288'
289
290cat >expect <<EOF
291t210
292t211
293EOF
294test_expect_success \
295 'listing tags with a prefix as pattern must print those matching' '
296 git tag -l "t21*" > actual &&
297 test_cmp expect actual
298'
299
300cat >expect <<EOF
301a1
302EOF
303test_expect_success \
304 'listing tags using a name as pattern must print that one matching' '
305 git tag -l a1 > actual &&
306 test_cmp expect actual
307'
308
309cat >expect <<EOF
310v1.0
311EOF
312test_expect_success \
313 'listing tags using a name as pattern must print that one matching' '
314 git tag -l v1.0 > actual &&
315 test_cmp expect actual
316'
317
318cat >expect <<EOF
319v1.0.1
320v1.1.3
321EOF
322test_expect_success \
323 'listing tags with ? in the pattern should print those matching' '
324 git tag -l "v1.?.?" > actual &&
325 test_cmp expect actual
326'
327
328>expect
329test_expect_success \
330 'listing tags using v.* should print nothing because none have v.' '
331 git tag -l "v.*" > actual &&
332 test_cmp expect actual
333'
334
335cat >expect <<EOF
336v0.2.1
337v1.0
338v1.0.1
339v1.1.3
340EOF
341test_expect_success \
342 'listing tags using v* should print only those having v' '
343 git tag -l "v*" > actual &&
344 test_cmp expect actual
345'
346
347test_expect_success 'tag -l can accept multiple patterns' '
348 git tag -l "v1*" "v0*" >actual &&
349 test_cmp expect actual
350'
351
352# Between v1.7.7 & v2.13.0 a fair reading of the git-tag documentation
353# could leave you with the impression that "-l <pattern> -l <pattern>"
354# was how we wanted to accept multiple patterns.
355#
356# This test should not imply that this is a sane thing to support. but
357# since the documentation was worded like it was let's at least find
358# out if we're going to break this long-documented form of taking
359# multiple patterns.
360test_expect_success 'tag -l <pattern> -l <pattern> works, as our buggy documentation previously suggested' '
361 git tag -l "v1*" -l "v0*" >actual &&
362 test_cmp expect actual
363'
364
365test_expect_success 'listing tags in column' '
366 COLUMNS=40 git tag -l --column=row >actual &&
367 cat >expected <<\EOF &&
368a1 aa1 cba t210 t211
369v0.2.1 v1.0 v1.0.1 v1.1.3
370EOF
371 test_cmp expected actual
372'
373
374test_expect_success 'listing tags in column with column.*' '
375 test_config column.tag row &&
376 test_config column.ui dense &&
377 COLUMNS=40 git tag -l >actual &&
378 cat >expected <<\EOF &&
379a1 aa1 cba t210 t211
380v0.2.1 v1.0 v1.0.1 v1.1.3
381EOF
382 test_cmp expected actual
383'
384
385test_expect_success 'listing tag with -n --column should fail' '
386 test_must_fail git tag --column -n
387'
388
389test_expect_success 'listing tags -n in column with column.ui ignored' '
390 test_config column.ui "row dense" &&
391 COLUMNS=40 git tag -l -n >actual &&
392 cat >expected <<\EOF &&
393a1 Foo
394aa1 Foo
395cba Foo
396t210 Foo
397t211 Foo
398v0.2.1 Foo
399v1.0 Foo
400v1.0.1 Foo
401v1.1.3 Foo
402EOF
403 test_cmp expected actual
404'
405
406# creating and verifying lightweight tags:
407
408test_expect_success \
409 'a non-annotated tag created without parameters should point to HEAD' '
410 git tag non-annotated-tag &&
411 test $(git cat-file -t non-annotated-tag) = commit &&
412 test $(git rev-parse non-annotated-tag) = $(git rev-parse HEAD)
413'
414
415test_expect_success 'trying to verify an unknown tag should fail' \
416 'test_must_fail git tag -v unknown-tag'
417
418test_expect_success \
419 'trying to verify a non-annotated and non-signed tag should fail' \
420 'test_must_fail git tag -v non-annotated-tag'
421
422test_expect_success \
423 'trying to verify many non-annotated or unknown tags, should fail' \
424 'test_must_fail git tag -v unknown-tag1 non-annotated-tag unknown-tag2'
425
426# creating annotated tags:
427
428get_tag_msg () {
429 git cat-file tag "$1" | sed -e "/BEGIN PGP/q"
430}
431
432# run test_tick before committing always gives the time in that timezone
433get_tag_header () {
434cat <<EOF
435object $2
436type $3
437tag $1
438tagger C O Mitter <committer@example.com> $4 -0700
439
440EOF
441}
442
443commit=$(git rev-parse HEAD)
444time=$test_tick
445
446get_tag_header annotated-tag $commit commit $time >expect
447echo "A message" >>expect
448test_expect_success \
449 'creating an annotated tag with -m message should succeed' '
450 git tag -m "A message" annotated-tag &&
451 get_tag_msg annotated-tag >actual &&
452 test_cmp expect actual
453'
454
455cat >msgfile <<EOF
456Another message
457in a file.
458EOF
459get_tag_header file-annotated-tag $commit commit $time >expect
460cat msgfile >>expect
461test_expect_success \
462 'creating an annotated tag with -F messagefile should succeed' '
463 git tag -F msgfile file-annotated-tag &&
464 get_tag_msg file-annotated-tag >actual &&
465 test_cmp expect actual
466'
467
468cat >inputmsg <<EOF
469A message from the
470standard input
471EOF
472get_tag_header stdin-annotated-tag $commit commit $time >expect
473cat inputmsg >>expect
474test_expect_success 'creating an annotated tag with -F - should succeed' '
475 git tag -F - stdin-annotated-tag <inputmsg &&
476 get_tag_msg stdin-annotated-tag >actual &&
477 test_cmp expect actual
478'
479
480test_expect_success \
481 'trying to create a tag with a non-existing -F file should fail' '
482 ! test -f nonexistingfile &&
483 ! tag_exists notag &&
484 test_must_fail git tag -F nonexistingfile notag &&
485 ! tag_exists notag
486'
487
488test_expect_success \
489 'trying to create tags giving both -m or -F options should fail' '
490 echo "message file 1" >msgfile1 &&
491 echo "message file 2" >msgfile2 &&
492 ! tag_exists msgtag &&
493 test_must_fail git tag -m "message 1" -F msgfile1 msgtag &&
494 ! tag_exists msgtag &&
495 test_must_fail git tag -F msgfile1 -m "message 1" msgtag &&
496 ! tag_exists msgtag &&
497 test_must_fail git tag -m "message 1" -F msgfile1 \
498 -m "message 2" msgtag &&
499 ! tag_exists msgtag
500'
501
502# blank and empty messages:
503
504get_tag_header empty-annotated-tag $commit commit $time >expect
505test_expect_success \
506 'creating a tag with an empty -m message should succeed' '
507 git tag -m "" empty-annotated-tag &&
508 get_tag_msg empty-annotated-tag >actual &&
509 test_cmp expect actual
510'
511
512>emptyfile
513get_tag_header emptyfile-annotated-tag $commit commit $time >expect
514test_expect_success \
515 'creating a tag with an empty -F messagefile should succeed' '
516 git tag -F emptyfile emptyfile-annotated-tag &&
517 get_tag_msg emptyfile-annotated-tag >actual &&
518 test_cmp expect actual
519'
520
521printf '\n\n \n\t\nLeading blank lines\n' >blanksfile
522printf '\n\t \t \nRepeated blank lines\n' >>blanksfile
523printf '\n\n\nTrailing spaces \t \n' >>blanksfile
524printf '\nTrailing blank lines\n\n\t \n\n' >>blanksfile
525get_tag_header blanks-annotated-tag $commit commit $time >expect
526cat >>expect <<EOF
527Leading blank lines
528
529Repeated blank lines
530
531Trailing spaces
532
533Trailing blank lines
534EOF
535test_expect_success \
536 'extra blanks in the message for an annotated tag should be removed' '
537 git tag -F blanksfile blanks-annotated-tag &&
538 get_tag_msg blanks-annotated-tag >actual &&
539 test_cmp expect actual
540'
541
542get_tag_header blank-annotated-tag $commit commit $time >expect
543test_expect_success \
544 'creating a tag with blank -m message with spaces should succeed' '
545 git tag -m " " blank-annotated-tag &&
546 get_tag_msg blank-annotated-tag >actual &&
547 test_cmp expect actual
548'
549
550echo ' ' >blankfile
551echo '' >>blankfile
552echo ' ' >>blankfile
553get_tag_header blankfile-annotated-tag $commit commit $time >expect
554test_expect_success \
555 'creating a tag with blank -F messagefile with spaces should succeed' '
556 git tag -F blankfile blankfile-annotated-tag &&
557 get_tag_msg blankfile-annotated-tag >actual &&
558 test_cmp expect actual
559'
560
561printf ' ' >blanknonlfile
562get_tag_header blanknonlfile-annotated-tag $commit commit $time >expect
563test_expect_success \
564 'creating a tag with -F file of spaces and no newline should succeed' '
565 git tag -F blanknonlfile blanknonlfile-annotated-tag &&
566 get_tag_msg blanknonlfile-annotated-tag >actual &&
567 test_cmp expect actual
568'
569
570# messages with commented lines:
571
572cat >commentsfile <<EOF
573# A comment
574
575############
576The message.
577############
578One line.
579
580
581# commented lines
582# commented lines
583
584Another line.
585# comments
586
587Last line.
588EOF
589get_tag_header comments-annotated-tag $commit commit $time >expect
590cat >>expect <<EOF
591The message.
592One line.
593
594Another line.
595
596Last line.
597EOF
598test_expect_success \
599 'creating a tag using a -F messagefile with #comments should succeed' '
600 git tag -F commentsfile comments-annotated-tag &&
601 get_tag_msg comments-annotated-tag >actual &&
602 test_cmp expect actual
603'
604
605get_tag_header comment-annotated-tag $commit commit $time >expect
606test_expect_success \
607 'creating a tag with a #comment in the -m message should succeed' '
608 git tag -m "#comment" comment-annotated-tag &&
609 get_tag_msg comment-annotated-tag >actual &&
610 test_cmp expect actual
611'
612
613echo '#comment' >commentfile
614echo '' >>commentfile
615echo '####' >>commentfile
616get_tag_header commentfile-annotated-tag $commit commit $time >expect
617test_expect_success \
618 'creating a tag with #comments in the -F messagefile should succeed' '
619 git tag -F commentfile commentfile-annotated-tag &&
620 get_tag_msg commentfile-annotated-tag >actual &&
621 test_cmp expect actual
622'
623
624printf '#comment' >commentnonlfile
625get_tag_header commentnonlfile-annotated-tag $commit commit $time >expect
626test_expect_success \
627 'creating a tag with a file of #comment and no newline should succeed' '
628 git tag -F commentnonlfile commentnonlfile-annotated-tag &&
629 get_tag_msg commentnonlfile-annotated-tag >actual &&
630 test_cmp expect actual
631'
632
633# listing messages for annotated non-signed tags:
634
635test_expect_success \
636 'listing the one-line message of a non-signed tag should succeed' '
637 git tag -m "A msg" tag-one-line &&
638
639 echo "tag-one-line" >expect &&
640 git tag -l | grep "^tag-one-line" >actual &&
641 test_cmp expect actual &&
642 git tag -n0 -l | grep "^tag-one-line" >actual &&
643 test_cmp expect actual &&
644 git tag -n0 -l tag-one-line >actual &&
645 test_cmp expect actual &&
646
647 git tag -n0 | grep "^tag-one-line" >actual &&
648 test_cmp expect actual &&
649 git tag -n0 tag-one-line >actual &&
650 test_cmp expect actual &&
651
652 echo "tag-one-line A msg" >expect &&
653 git tag -n1 -l | grep "^tag-one-line" >actual &&
654 test_cmp expect actual &&
655 git tag -n -l | grep "^tag-one-line" >actual &&
656 test_cmp expect actual &&
657 git tag -n1 -l tag-one-line >actual &&
658 test_cmp expect actual &&
659 git tag -n2 -l tag-one-line >actual &&
660 test_cmp expect actual &&
661 git tag -n999 -l tag-one-line >actual &&
662 test_cmp expect actual
663'
664
665test_expect_success 'The -n 100 invocation means -n --list 100, not -n100' '
666 >expect &&
667 git tag -n 100 >actual &&
668 test_cmp expect actual &&
669
670 git tag -m "A msg" 100 &&
671 echo "100 A msg" >expect &&
672 git tag -n 100 >actual &&
673 test_cmp expect actual
674'
675
676test_expect_success \
677 'listing the zero-lines message of a non-signed tag should succeed' '
678 git tag -m "" tag-zero-lines &&
679
680 echo "tag-zero-lines" >expect &&
681 git tag -l | grep "^tag-zero-lines" >actual &&
682 test_cmp expect actual &&
683 git tag -n0 -l | grep "^tag-zero-lines" >actual &&
684 test_cmp expect actual &&
685 git tag -n0 -l tag-zero-lines >actual &&
686 test_cmp expect actual &&
687
688 echo "tag-zero-lines " >expect &&
689 git tag -n1 -l | grep "^tag-zero-lines" >actual &&
690 test_cmp expect actual &&
691 git tag -n -l | grep "^tag-zero-lines" >actual &&
692 test_cmp expect actual &&
693 git tag -n1 -l tag-zero-lines >actual &&
694 test_cmp expect actual &&
695 git tag -n2 -l tag-zero-lines >actual &&
696 test_cmp expect actual &&
697 git tag -n999 -l tag-zero-lines >actual &&
698 test_cmp expect actual
699'
700
701echo 'tag line one' >annotagmsg
702echo 'tag line two' >>annotagmsg
703echo 'tag line three' >>annotagmsg
704test_expect_success \
705 'listing many message lines of a non-signed tag should succeed' '
706 git tag -F annotagmsg tag-lines &&
707
708 echo "tag-lines" >expect &&
709 git tag -l | grep "^tag-lines" >actual &&
710 test_cmp expect actual &&
711 git tag -n0 -l | grep "^tag-lines" >actual &&
712 test_cmp expect actual &&
713 git tag -n0 -l tag-lines >actual &&
714 test_cmp expect actual &&
715
716 echo "tag-lines tag line one" >expect &&
717 git tag -n1 -l | grep "^tag-lines" >actual &&
718 test_cmp expect actual &&
719 git tag -n -l | grep "^tag-lines" >actual &&
720 test_cmp expect actual &&
721 git tag -n1 -l tag-lines >actual &&
722 test_cmp expect actual &&
723
724 echo " tag line two" >>expect &&
725 git tag -n2 -l | grep "^ *tag.line" >actual &&
726 test_cmp expect actual &&
727 git tag -n2 -l tag-lines >actual &&
728 test_cmp expect actual &&
729
730 echo " tag line three" >>expect &&
731 git tag -n3 -l | grep "^ *tag.line" >actual &&
732 test_cmp expect actual &&
733 git tag -n3 -l tag-lines >actual &&
734 test_cmp expect actual &&
735 git tag -n4 -l | grep "^ *tag.line" >actual &&
736 test_cmp expect actual &&
737 git tag -n4 -l tag-lines >actual &&
738 test_cmp expect actual &&
739 git tag -n99 -l | grep "^ *tag.line" >actual &&
740 test_cmp expect actual &&
741 git tag -n99 -l tag-lines >actual &&
742 test_cmp expect actual
743'
744
745test_expect_success 'annotations for blobs are empty' '
746 blob=$(git hash-object -w --stdin <<-\EOF
747 Blob paragraph 1.
748
749 Blob paragraph 2.
750 EOF
751 ) &&
752 git tag tag-blob $blob &&
753 echo "tag-blob " >expect &&
754 git tag -n1 -l tag-blob >actual &&
755 test_cmp expect actual
756'
757
758# trying to verify annotated non-signed tags:
759
760test_expect_success GPG \
761 'trying to verify an annotated non-signed tag should fail' '
762 tag_exists annotated-tag &&
763 test_must_fail git tag -v annotated-tag
764'
765
766test_expect_success GPG \
767 'trying to verify a file-annotated non-signed tag should fail' '
768 tag_exists file-annotated-tag &&
769 test_must_fail git tag -v file-annotated-tag
770'
771
772test_expect_success GPG \
773 'trying to verify two annotated non-signed tags should fail' '
774 tag_exists annotated-tag file-annotated-tag &&
775 test_must_fail git tag -v annotated-tag file-annotated-tag
776'
777
778# creating and verifying signed tags:
779
780get_tag_header signed-tag $commit commit $time >expect
781echo 'A signed tag message' >>expect
782echo '-----BEGIN PGP SIGNATURE-----' >>expect
783test_expect_success GPG 'creating a signed tag with -m message should succeed' '
784 git tag -s -m "A signed tag message" signed-tag &&
785 get_tag_msg signed-tag >actual &&
786 test_cmp expect actual
787'
788
789get_tag_header u-signed-tag $commit commit $time >expect
790echo 'Another message' >>expect
791echo '-----BEGIN PGP SIGNATURE-----' >>expect
792test_expect_success GPG 'sign with a given key id' '
793
794 git tag -u committer@example.com -m "Another message" u-signed-tag &&
795 get_tag_msg u-signed-tag >actual &&
796 test_cmp expect actual
797
798'
799
800test_expect_success GPG 'sign with an unknown id (1)' '
801
802 test_must_fail git tag -u author@example.com \
803 -m "Another message" o-signed-tag
804
805'
806
807test_expect_success GPG 'sign with an unknown id (2)' '
808
809 test_must_fail git tag -u DEADBEEF -m "Another message" o-signed-tag
810
811'
812
813cat >fakeeditor <<'EOF'
814#!/bin/sh
815test -n "$1" && exec >"$1"
816echo A signed tag message
817echo from a fake editor.
818EOF
819chmod +x fakeeditor
820
821get_tag_header implied-sign $commit commit $time >expect
822./fakeeditor >>expect
823echo '-----BEGIN PGP SIGNATURE-----' >>expect
824test_expect_success GPG '-u implies signed tag' '
825 GIT_EDITOR=./fakeeditor git tag -u CDDE430D implied-sign &&
826 get_tag_msg implied-sign >actual &&
827 test_cmp expect actual
828'
829
830cat >sigmsgfile <<EOF
831Another signed tag
832message in a file.
833EOF
834get_tag_header file-signed-tag $commit commit $time >expect
835cat sigmsgfile >>expect
836echo '-----BEGIN PGP SIGNATURE-----' >>expect
837test_expect_success GPG \
838 'creating a signed tag with -F messagefile should succeed' '
839 git tag -s -F sigmsgfile file-signed-tag &&
840 get_tag_msg file-signed-tag >actual &&
841 test_cmp expect actual
842'
843
844cat >siginputmsg <<EOF
845A signed tag message from
846the standard input
847EOF
848get_tag_header stdin-signed-tag $commit commit $time >expect
849cat siginputmsg >>expect
850echo '-----BEGIN PGP SIGNATURE-----' >>expect
851test_expect_success GPG 'creating a signed tag with -F - should succeed' '
852 git tag -s -F - stdin-signed-tag <siginputmsg &&
853 get_tag_msg stdin-signed-tag >actual &&
854 test_cmp expect actual
855'
856
857get_tag_header implied-annotate $commit commit $time >expect
858./fakeeditor >>expect
859echo '-----BEGIN PGP SIGNATURE-----' >>expect
860test_expect_success GPG '-s implies annotated tag' '
861 GIT_EDITOR=./fakeeditor git tag -s implied-annotate &&
862 get_tag_msg implied-annotate >actual &&
863 test_cmp expect actual
864'
865
866get_tag_header forcesignannotated-implied-sign $commit commit $time >expect
867echo "A message" >>expect
868echo '-----BEGIN PGP SIGNATURE-----' >>expect
869test_expect_success GPG \
870 'git tag -s implied if configured with tag.forcesignannotated' \
871 'test_config tag.forcesignannotated true &&
872 git tag -m "A message" forcesignannotated-implied-sign &&
873 get_tag_msg forcesignannotated-implied-sign >actual &&
874 test_cmp expect actual
875'
876
877test_expect_success GPG \
878 'lightweight with no message when configured with tag.forcesignannotated' \
879 'test_config tag.forcesignannotated true &&
880 git tag forcesignannotated-lightweight &&
881 tag_exists forcesignannotated-lightweight &&
882 test_must_fail git tag -v forcesignannotated-no-message
883'
884
885get_tag_header forcesignannotated-annotate $commit commit $time >expect
886echo "A message" >>expect
887test_expect_success GPG \
888 'git tag -a disable configured tag.forcesignannotated' \
889 'test_config tag.forcesignannotated true &&
890 git tag -a -m "A message" forcesignannotated-annotate &&
891 get_tag_msg forcesignannotated-annotate >actual &&
892 test_cmp expect actual &&
893 test_must_fail git tag -v forcesignannotated-annotate
894'
895
896get_tag_header forcesignannotated-disabled $commit commit $time >expect
897echo "A message" >>expect
898echo '-----BEGIN PGP SIGNATURE-----' >>expect
899test_expect_success GPG \
900 'git tag --sign enable GPG sign' \
901 'test_config tag.forcesignannotated false &&
902 git tag --sign -m "A message" forcesignannotated-disabled &&
903 get_tag_msg forcesignannotated-disabled >actual &&
904 test_cmp expect actual
905'
906
907test_expect_success GPG \
908 'trying to create a signed tag with non-existing -F file should fail' '
909 ! test -f nonexistingfile &&
910 ! tag_exists nosigtag &&
911 test_must_fail git tag -s -F nonexistingfile nosigtag &&
912 ! tag_exists nosigtag
913'
914
915test_expect_success GPG 'verifying a signed tag should succeed' \
916 'git tag -v signed-tag'
917
918test_expect_success GPG 'verifying two signed tags in one command should succeed' \
919 'git tag -v signed-tag file-signed-tag'
920
921test_expect_success GPG \
922 'verifying many signed and non-signed tags should fail' '
923 test_must_fail git tag -v signed-tag annotated-tag &&
924 test_must_fail git tag -v file-annotated-tag file-signed-tag &&
925 test_must_fail git tag -v annotated-tag \
926 file-signed-tag file-annotated-tag &&
927 test_must_fail git tag -v signed-tag annotated-tag file-signed-tag
928'
929
930test_expect_success GPG 'verifying a forged tag should fail' '
931 forged=$(git cat-file tag signed-tag |
932 sed -e "s/signed-tag/forged-tag/" |
933 git mktag) &&
934 git tag forged-tag $forged &&
935 test_must_fail git tag -v forged-tag
936'
937
938test_expect_success GPG 'verifying a proper tag with --format pass and format accordingly' '
939 cat >expect <<-\EOF &&
940 tagname : signed-tag
941 EOF
942 git tag -v --format="tagname : %(tag)" "signed-tag" >actual &&
943 test_cmp expect actual
944'
945
946test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
947 >expect &&
948 test_must_fail git tag -v --format="tagname : %(tag)" "forged-tag" >actual &&
949 test_cmp expect actual
950'
951
952# blank and empty messages for signed tags:
953
954get_tag_header empty-signed-tag $commit commit $time >expect
955echo '-----BEGIN PGP SIGNATURE-----' >>expect
956test_expect_success GPG \
957 'creating a signed tag with an empty -m message should succeed' '
958 git tag -s -m "" empty-signed-tag &&
959 get_tag_msg empty-signed-tag >actual &&
960 test_cmp expect actual &&
961 git tag -v empty-signed-tag
962'
963
964>sigemptyfile
965get_tag_header emptyfile-signed-tag $commit commit $time >expect
966echo '-----BEGIN PGP SIGNATURE-----' >>expect
967test_expect_success GPG \
968 'creating a signed tag with an empty -F messagefile should succeed' '
969 git tag -s -F sigemptyfile emptyfile-signed-tag &&
970 get_tag_msg emptyfile-signed-tag >actual &&
971 test_cmp expect actual &&
972 git tag -v emptyfile-signed-tag
973'
974
975printf '\n\n \n\t\nLeading blank lines\n' > sigblanksfile
976printf '\n\t \t \nRepeated blank lines\n' >>sigblanksfile
977printf '\n\n\nTrailing spaces \t \n' >>sigblanksfile
978printf '\nTrailing blank lines\n\n\t \n\n' >>sigblanksfile
979get_tag_header blanks-signed-tag $commit commit $time >expect
980cat >>expect <<EOF
981Leading blank lines
982
983Repeated blank lines
984
985Trailing spaces
986
987Trailing blank lines
988EOF
989echo '-----BEGIN PGP SIGNATURE-----' >>expect
990test_expect_success GPG \
991 'extra blanks in the message for a signed tag should be removed' '
992 git tag -s -F sigblanksfile blanks-signed-tag &&
993 get_tag_msg blanks-signed-tag >actual &&
994 test_cmp expect actual &&
995 git tag -v blanks-signed-tag
996'
997
998get_tag_header blank-signed-tag $commit commit $time >expect
999echo '-----BEGIN PGP SIGNATURE-----' >>expect
1000test_expect_success GPG \
1001 'creating a signed tag with a blank -m message should succeed' '
1002 git tag -s -m " " blank-signed-tag &&
1003 get_tag_msg blank-signed-tag >actual &&
1004 test_cmp expect actual &&
1005 git tag -v blank-signed-tag
1006'
1007
1008echo ' ' >sigblankfile
1009echo '' >>sigblankfile
1010echo ' ' >>sigblankfile
1011get_tag_header blankfile-signed-tag $commit commit $time >expect
1012echo '-----BEGIN PGP SIGNATURE-----' >>expect
1013test_expect_success GPG \
1014 'creating a signed tag with blank -F file with spaces should succeed' '
1015 git tag -s -F sigblankfile blankfile-signed-tag &&
1016 get_tag_msg blankfile-signed-tag >actual &&
1017 test_cmp expect actual &&
1018 git tag -v blankfile-signed-tag
1019'
1020
1021printf ' ' >sigblanknonlfile
1022get_tag_header blanknonlfile-signed-tag $commit commit $time >expect
1023echo '-----BEGIN PGP SIGNATURE-----' >>expect
1024test_expect_success GPG \
1025 'creating a signed tag with spaces and no newline should succeed' '
1026 git tag -s -F sigblanknonlfile blanknonlfile-signed-tag &&
1027 get_tag_msg blanknonlfile-signed-tag >actual &&
1028 test_cmp expect actual &&
1029 git tag -v signed-tag
1030'
1031
1032# messages with commented lines for signed tags:
1033
1034cat >sigcommentsfile <<EOF
1035# A comment
1036
1037############
1038The message.
1039############
1040One line.
1041
1042
1043# commented lines
1044# commented lines
1045
1046Another line.
1047# comments
1048
1049Last line.
1050EOF
1051get_tag_header comments-signed-tag $commit commit $time >expect
1052cat >>expect <<EOF
1053The message.
1054One line.
1055
1056Another line.
1057
1058Last line.
1059EOF
1060echo '-----BEGIN PGP SIGNATURE-----' >>expect
1061test_expect_success GPG \
1062 'creating a signed tag with a -F file with #comments should succeed' '
1063 git tag -s -F sigcommentsfile comments-signed-tag &&
1064 get_tag_msg comments-signed-tag >actual &&
1065 test_cmp expect actual &&
1066 git tag -v comments-signed-tag
1067'
1068
1069get_tag_header comment-signed-tag $commit commit $time >expect
1070echo '-----BEGIN PGP SIGNATURE-----' >>expect
1071test_expect_success GPG \
1072 'creating a signed tag with #commented -m message should succeed' '
1073 git tag -s -m "#comment" comment-signed-tag &&
1074 get_tag_msg comment-signed-tag >actual &&
1075 test_cmp expect actual &&
1076 git tag -v comment-signed-tag
1077'
1078
1079echo '#comment' >sigcommentfile
1080echo '' >>sigcommentfile
1081echo '####' >>sigcommentfile
1082get_tag_header commentfile-signed-tag $commit commit $time >expect
1083echo '-----BEGIN PGP SIGNATURE-----' >>expect
1084test_expect_success GPG \
1085 'creating a signed tag with #commented -F messagefile should succeed' '
1086 git tag -s -F sigcommentfile commentfile-signed-tag &&
1087 get_tag_msg commentfile-signed-tag >actual &&
1088 test_cmp expect actual &&
1089 git tag -v commentfile-signed-tag
1090'
1091
1092printf '#comment' >sigcommentnonlfile
1093get_tag_header commentnonlfile-signed-tag $commit commit $time >expect
1094echo '-----BEGIN PGP SIGNATURE-----' >>expect
1095test_expect_success GPG \
1096 'creating a signed tag with a #comment and no newline should succeed' '
1097 git tag -s -F sigcommentnonlfile commentnonlfile-signed-tag &&
1098 get_tag_msg commentnonlfile-signed-tag >actual &&
1099 test_cmp expect actual &&
1100 git tag -v commentnonlfile-signed-tag
1101'
1102
1103# listing messages for signed tags:
1104
1105test_expect_success GPG \
1106 'listing the one-line message of a signed tag should succeed' '
1107 git tag -s -m "A message line signed" stag-one-line &&
1108
1109 echo "stag-one-line" >expect &&
1110 git tag -l | grep "^stag-one-line" >actual &&
1111 test_cmp expect actual &&
1112 git tag -n0 -l | grep "^stag-one-line" >actual &&
1113 test_cmp expect actual &&
1114 git tag -n0 -l stag-one-line >actual &&
1115 test_cmp expect actual &&
1116
1117 echo "stag-one-line A message line signed" >expect &&
1118 git tag -n1 -l | grep "^stag-one-line" >actual &&
1119 test_cmp expect actual &&
1120 git tag -n -l | grep "^stag-one-line" >actual &&
1121 test_cmp expect actual &&
1122 git tag -n1 -l stag-one-line >actual &&
1123 test_cmp expect actual &&
1124 git tag -n2 -l stag-one-line >actual &&
1125 test_cmp expect actual &&
1126 git tag -n999 -l stag-one-line >actual &&
1127 test_cmp expect actual
1128'
1129
1130test_expect_success GPG \
1131 'listing the zero-lines message of a signed tag should succeed' '
1132 git tag -s -m "" stag-zero-lines &&
1133
1134 echo "stag-zero-lines" >expect &&
1135 git tag -l | grep "^stag-zero-lines" >actual &&
1136 test_cmp expect actual &&
1137 git tag -n0 -l | grep "^stag-zero-lines" >actual &&
1138 test_cmp expect actual &&
1139 git tag -n0 -l stag-zero-lines >actual &&
1140 test_cmp expect actual &&
1141
1142 echo "stag-zero-lines " >expect &&
1143 git tag -n1 -l | grep "^stag-zero-lines" >actual &&
1144 test_cmp expect actual &&
1145 git tag -n -l | grep "^stag-zero-lines" >actual &&
1146 test_cmp expect actual &&
1147 git tag -n1 -l stag-zero-lines >actual &&
1148 test_cmp expect actual &&
1149 git tag -n2 -l stag-zero-lines >actual &&
1150 test_cmp expect actual &&
1151 git tag -n999 -l stag-zero-lines >actual &&
1152 test_cmp expect actual
1153'
1154
1155echo 'stag line one' >sigtagmsg
1156echo 'stag line two' >>sigtagmsg
1157echo 'stag line three' >>sigtagmsg
1158test_expect_success GPG \
1159 'listing many message lines of a signed tag should succeed' '
1160 git tag -s -F sigtagmsg stag-lines &&
1161
1162 echo "stag-lines" >expect &&
1163 git tag -l | grep "^stag-lines" >actual &&
1164 test_cmp expect actual &&
1165 git tag -n0 -l | grep "^stag-lines" >actual &&
1166 test_cmp expect actual &&
1167 git tag -n0 -l stag-lines >actual &&
1168 test_cmp expect actual &&
1169
1170 echo "stag-lines stag line one" >expect &&
1171 git tag -n1 -l | grep "^stag-lines" >actual &&
1172 test_cmp expect actual &&
1173 git tag -n -l | grep "^stag-lines" >actual &&
1174 test_cmp expect actual &&
1175 git tag -n1 -l stag-lines >actual &&
1176 test_cmp expect actual &&
1177
1178 echo " stag line two" >>expect &&
1179 git tag -n2 -l | grep "^ *stag.line" >actual &&
1180 test_cmp expect actual &&
1181 git tag -n2 -l stag-lines >actual &&
1182 test_cmp expect actual &&
1183
1184 echo " stag line three" >>expect &&
1185 git tag -n3 -l | grep "^ *stag.line" >actual &&
1186 test_cmp expect actual &&
1187 git tag -n3 -l stag-lines >actual &&
1188 test_cmp expect actual &&
1189 git tag -n4 -l | grep "^ *stag.line" >actual &&
1190 test_cmp expect actual &&
1191 git tag -n4 -l stag-lines >actual &&
1192 test_cmp expect actual &&
1193 git tag -n99 -l | grep "^ *stag.line" >actual &&
1194 test_cmp expect actual &&
1195 git tag -n99 -l stag-lines >actual &&
1196 test_cmp expect actual
1197'
1198
1199# tags pointing to objects different from commits:
1200
1201tree=$(git rev-parse HEAD^{tree})
1202blob=$(git rev-parse HEAD:foo)
1203tag=$(git rev-parse signed-tag 2>/dev/null)
1204
1205get_tag_header tree-signed-tag $tree tree $time >expect
1206echo "A message for a tree" >>expect
1207echo '-----BEGIN PGP SIGNATURE-----' >>expect
1208test_expect_success GPG \
1209 'creating a signed tag pointing to a tree should succeed' '
1210 git tag -s -m "A message for a tree" tree-signed-tag HEAD^{tree} &&
1211 get_tag_msg tree-signed-tag >actual &&
1212 test_cmp expect actual
1213'
1214
1215get_tag_header blob-signed-tag $blob blob $time >expect
1216echo "A message for a blob" >>expect
1217echo '-----BEGIN PGP SIGNATURE-----' >>expect
1218test_expect_success GPG \
1219 'creating a signed tag pointing to a blob should succeed' '
1220 git tag -s -m "A message for a blob" blob-signed-tag HEAD:foo &&
1221 get_tag_msg blob-signed-tag >actual &&
1222 test_cmp expect actual
1223'
1224
1225get_tag_header tag-signed-tag $tag tag $time >expect
1226echo "A message for another tag" >>expect
1227echo '-----BEGIN PGP SIGNATURE-----' >>expect
1228test_expect_success GPG \
1229 'creating a signed tag pointing to another tag should succeed' '
1230 git tag -s -m "A message for another tag" tag-signed-tag signed-tag &&
1231 get_tag_msg tag-signed-tag >actual &&
1232 test_cmp expect actual
1233'
1234
1235# usage with rfc1991 signatures
1236get_tag_header rfc1991-signed-tag $commit commit $time >expect
1237echo "RFC1991 signed tag" >>expect
1238echo '-----BEGIN PGP MESSAGE-----' >>expect
1239test_expect_success GPG,RFC1991 \
1240 'creating a signed tag with rfc1991' '
1241 echo "rfc1991" >gpghome/gpg.conf &&
1242 git tag -s -m "RFC1991 signed tag" rfc1991-signed-tag $commit &&
1243 get_tag_msg rfc1991-signed-tag >actual &&
1244 test_cmp expect actual
1245'
1246
1247cat >fakeeditor <<'EOF'
1248#!/bin/sh
1249cp "$1" actual
1250EOF
1251chmod +x fakeeditor
1252
1253test_expect_success GPG,RFC1991 \
1254 'reediting a signed tag body omits signature' '
1255 echo "rfc1991" >gpghome/gpg.conf &&
1256 echo "RFC1991 signed tag" >expect &&
1257 GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1258 test_cmp expect actual
1259'
1260
1261test_expect_success GPG,RFC1991 \
1262 'verifying rfc1991 signature' '
1263 echo "rfc1991" >gpghome/gpg.conf &&
1264 git tag -v rfc1991-signed-tag
1265'
1266
1267test_expect_success GPG,RFC1991 \
1268 'list tag with rfc1991 signature' '
1269 echo "rfc1991" >gpghome/gpg.conf &&
1270 echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1271 git tag -l -n1 rfc1991-signed-tag >actual &&
1272 test_cmp expect actual &&
1273 git tag -l -n2 rfc1991-signed-tag >actual &&
1274 test_cmp expect actual &&
1275 git tag -l -n999 rfc1991-signed-tag >actual &&
1276 test_cmp expect actual
1277'
1278
1279rm -f gpghome/gpg.conf
1280
1281test_expect_success GPG,RFC1991 \
1282 'verifying rfc1991 signature without --rfc1991' '
1283 git tag -v rfc1991-signed-tag
1284'
1285
1286test_expect_success GPG,RFC1991 \
1287 'list tag with rfc1991 signature without --rfc1991' '
1288 echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1289 git tag -l -n1 rfc1991-signed-tag >actual &&
1290 test_cmp expect actual &&
1291 git tag -l -n2 rfc1991-signed-tag >actual &&
1292 test_cmp expect actual &&
1293 git tag -l -n999 rfc1991-signed-tag >actual &&
1294 test_cmp expect actual
1295'
1296
1297test_expect_success GPG,RFC1991 \
1298 'reediting a signed tag body omits signature' '
1299 echo "RFC1991 signed tag" >expect &&
1300 GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1301 test_cmp expect actual
1302'
1303
1304# try to sign with bad user.signingkey
1305test_expect_success GPG \
1306 'git tag -s fails if gpg is misconfigured (bad key)' \
1307 'test_config user.signingkey BobTheMouse &&
1308 test_must_fail git tag -s -m tail tag-gpg-failure'
1309
1310# try to produce invalid signature
1311test_expect_success GPG \
1312 'git tag -s fails if gpg is misconfigured (bad signature format)' \
1313 'test_config gpg.program echo &&
1314 test_must_fail git tag -s -m tail tag-gpg-failure'
1315
1316
1317# try to verify without gpg:
1318
1319rm -rf gpghome
1320test_expect_success GPG \
1321 'verify signed tag fails when public key is not present' \
1322 'test_must_fail git tag -v signed-tag'
1323
1324test_expect_success \
1325 'git tag -a fails if tag annotation is empty' '
1326 ! (GIT_EDITOR=cat git tag -a initial-comment)
1327'
1328
1329test_expect_success \
1330 'message in editor has initial comment' '
1331 ! (GIT_EDITOR=cat git tag -a initial-comment > actual)
1332'
1333
1334test_expect_success 'message in editor has initial comment: first line' '
1335 # check the first line --- should be empty
1336 echo >first.expect &&
1337 sed -e 1q <actual >first.actual &&
1338 test_i18ncmp first.expect first.actual
1339'
1340
1341test_expect_success \
1342 'message in editor has initial comment: remainder' '
1343 # remove commented lines from the remainder -- should be empty
1344 >rest.expect &&
1345 sed -e 1d -e "/^#/d" <actual >rest.actual &&
1346 test_cmp rest.expect rest.actual
1347'
1348
1349get_tag_header reuse $commit commit $time >expect
1350echo "An annotation to be reused" >> expect
1351test_expect_success \
1352 'overwriting an annoted tag should use its previous body' '
1353 git tag -a -m "An annotation to be reused" reuse &&
1354 GIT_EDITOR=true git tag -f -a reuse &&
1355 get_tag_msg reuse >actual &&
1356 test_cmp expect actual
1357'
1358
1359test_expect_success 'filename for the message is relative to cwd' '
1360 mkdir subdir &&
1361 echo "Tag message in top directory" >msgfile-5 &&
1362 echo "Tag message in sub directory" >subdir/msgfile-5 &&
1363 (
1364 cd subdir &&
1365 git tag -a -F msgfile-5 tag-from-subdir
1366 ) &&
1367 git cat-file tag tag-from-subdir | grep "in sub directory"
1368'
1369
1370test_expect_success 'filename for the message is relative to cwd' '
1371 echo "Tag message in sub directory" >subdir/msgfile-6 &&
1372 (
1373 cd subdir &&
1374 git tag -a -F msgfile-6 tag-from-subdir-2
1375 ) &&
1376 git cat-file tag tag-from-subdir-2 | grep "in sub directory"
1377'
1378
1379# create a few more commits to test --contains
1380
1381hash1=$(git rev-parse HEAD)
1382
1383test_expect_success 'creating second commit and tag' '
1384 echo foo-2.0 >foo &&
1385 git add foo &&
1386 git commit -m second &&
1387 git tag v2.0
1388'
1389
1390hash2=$(git rev-parse HEAD)
1391
1392test_expect_success 'creating third commit without tag' '
1393 echo foo-dev >foo &&
1394 git add foo &&
1395 git commit -m third
1396'
1397
1398hash3=$(git rev-parse HEAD)
1399
1400# simple linear checks of --continue
1401
1402cat > expected <<EOF
1403v0.2.1
1404v1.0
1405v1.0.1
1406v1.1.3
1407v2.0
1408EOF
1409
1410test_expect_success 'checking that first commit is in all tags (hash)' "
1411 git tag -l --contains $hash1 v* >actual &&
1412 test_cmp expected actual
1413"
1414
1415# other ways of specifying the commit
1416test_expect_success 'checking that first commit is in all tags (tag)' "
1417 git tag -l --contains v1.0 v* >actual &&
1418 test_cmp expected actual
1419"
1420
1421test_expect_success 'checking that first commit is in all tags (relative)' "
1422 git tag -l --contains HEAD~2 v* >actual &&
1423 test_cmp expected actual
1424"
1425
1426# All the --contains tests above, but with --no-contains
1427test_expect_success 'checking that first commit is not listed in any tag with --no-contains (hash)' "
1428 >expected &&
1429 git tag -l --no-contains $hash1 v* >actual &&
1430 test_cmp expected actual
1431"
1432
1433test_expect_success 'checking that first commit is in all tags (tag)' "
1434 git tag -l --no-contains v1.0 v* >actual &&
1435 test_cmp expected actual
1436"
1437
1438test_expect_success 'checking that first commit is in all tags (relative)' "
1439 git tag -l --no-contains HEAD~2 v* >actual &&
1440 test_cmp expected actual
1441"
1442
1443cat > expected <<EOF
1444v2.0
1445EOF
1446
1447test_expect_success 'checking that second commit only has one tag' "
1448 git tag -l --contains $hash2 v* >actual &&
1449 test_cmp expected actual
1450"
1451
1452cat > expected <<EOF
1453v0.2.1
1454v1.0
1455v1.0.1
1456v1.1.3
1457EOF
1458
1459test_expect_success 'inverse of the last test, with --no-contains' "
1460 git tag -l --no-contains $hash2 v* >actual &&
1461 test_cmp expected actual
1462"
1463
1464cat > expected <<EOF
1465EOF
1466
1467test_expect_success 'checking that third commit has no tags' "
1468 git tag -l --contains $hash3 v* >actual &&
1469 test_cmp expected actual
1470"
1471
1472cat > expected <<EOF
1473v0.2.1
1474v1.0
1475v1.0.1
1476v1.1.3
1477v2.0
1478EOF
1479
1480test_expect_success 'conversely --no-contains on the third commit lists all tags' "
1481 git tag -l --no-contains $hash3 v* >actual &&
1482 test_cmp expected actual
1483"
1484
1485# how about a simple merge?
1486
1487test_expect_success 'creating simple branch' '
1488 git branch stable v2.0 &&
1489 git checkout stable &&
1490 echo foo-3.0 > foo &&
1491 git commit foo -m fourth &&
1492 git tag v3.0
1493'
1494
1495hash4=$(git rev-parse HEAD)
1496
1497cat > expected <<EOF
1498v3.0
1499EOF
1500
1501test_expect_success 'checking that branch head only has one tag' "
1502 git tag -l --contains $hash4 v* >actual &&
1503 test_cmp expected actual
1504"
1505
1506cat > expected <<EOF
1507v0.2.1
1508v1.0
1509v1.0.1
1510v1.1.3
1511v2.0
1512EOF
1513
1514test_expect_success 'checking that branch head with --no-contains lists all but one tag' "
1515 git tag -l --no-contains $hash4 v* >actual &&
1516 test_cmp expected actual
1517"
1518
1519test_expect_success 'merging original branch into this branch' '
1520 git merge --strategy=ours master &&
1521 git tag v4.0
1522'
1523
1524cat > expected <<EOF
1525v4.0
1526EOF
1527
1528test_expect_success 'checking that original branch head has one tag now' "
1529 git tag -l --contains $hash3 v* >actual &&
1530 test_cmp expected actual
1531"
1532
1533cat > expected <<EOF
1534v0.2.1
1535v1.0
1536v1.0.1
1537v1.1.3
1538v2.0
1539v3.0
1540EOF
1541
1542test_expect_success 'checking that original branch head with --no-contains lists all but one tag now' "
1543 git tag -l --no-contains $hash3 v* >actual &&
1544 test_cmp expected actual
1545"
1546
1547cat > expected <<EOF
1548v0.2.1
1549v1.0
1550v1.0.1
1551v1.1.3
1552v2.0
1553v3.0
1554v4.0
1555EOF
1556
1557test_expect_success 'checking that initial commit is in all tags' "
1558 git tag -l --contains $hash1 v* >actual &&
1559 test_cmp expected actual
1560"
1561
1562test_expect_success 'checking that --contains can be used in non-list mode' '
1563 git tag --contains $hash1 v* >actual &&
1564 test_cmp expected actual
1565'
1566
1567test_expect_success 'checking that initial commit is in all tags with --no-contains' "
1568 >expected &&
1569 git tag -l --no-contains $hash1 v* >actual &&
1570 test_cmp expected actual
1571"
1572
1573# mixing modes and options:
1574
1575test_expect_success 'mixing incompatibles modes and options is forbidden' '
1576 test_must_fail git tag -a &&
1577 test_must_fail git tag -a -l &&
1578 test_must_fail git tag -s &&
1579 test_must_fail git tag -s -l &&
1580 test_must_fail git tag -m &&
1581 test_must_fail git tag -m -l &&
1582 test_must_fail git tag -m "hlagh" &&
1583 test_must_fail git tag -m "hlagh" -l &&
1584 test_must_fail git tag -F &&
1585 test_must_fail git tag -F -l &&
1586 test_must_fail git tag -f &&
1587 test_must_fail git tag -f -l &&
1588 test_must_fail git tag -a -s -m -F &&
1589 test_must_fail git tag -a -s -m -F -l &&
1590 test_must_fail git tag -l -v &&
1591 test_must_fail git tag -l -d &&
1592 test_must_fail git tag -l -v -d &&
1593 test_must_fail git tag -n 100 -v &&
1594 test_must_fail git tag -l -m msg &&
1595 test_must_fail git tag -l -F some file &&
1596 test_must_fail git tag -v -s &&
1597 test_must_fail git tag --contains tag-tree &&
1598 test_must_fail git tag --contains tag-blob &&
1599 test_must_fail git tag --no-contains tag-tree &&
1600 test_must_fail git tag --no-contains tag-blob &&
1601 test_must_fail git tag --contains --no-contains &&
1602 test_must_fail git tag --no-with HEAD &&
1603 test_must_fail git tag --no-without HEAD
1604'
1605
1606for option in --contains --with --no-contains --without --merged --no-merged --points-at
1607do
1608 test_expect_success "mixing incompatible modes with $option is forbidden" "
1609 test_must_fail git tag -d $option HEAD &&
1610 test_must_fail git tag -d $option HEAD some-tag &&
1611 test_must_fail git tag -v $option HEAD
1612 "
1613 test_expect_success "Doing 'git tag --list-like $option <commit> <pattern> is permitted" "
1614 git tag -n $option HEAD HEAD &&
1615 git tag $option HEAD HEAD &&
1616 git tag $option
1617 "
1618done
1619
1620# check points-at
1621
1622test_expect_success '--points-at can be used in non-list mode' '
1623 echo v4.0 >expect &&
1624 git tag --points-at=v4.0 "v*" >actual &&
1625 test_cmp expect actual
1626'
1627
1628test_expect_success '--points-at is a synonym for --points-at HEAD' '
1629 echo v4.0 >expect &&
1630 git tag --points-at >actual &&
1631 test_cmp expect actual
1632'
1633
1634test_expect_success '--points-at finds lightweight tags' '
1635 echo v4.0 >expect &&
1636 git tag --points-at v4.0 >actual &&
1637 test_cmp expect actual
1638'
1639
1640test_expect_success '--points-at finds annotated tags of commits' '
1641 git tag -m "v4.0, annotated" annotated-v4.0 v4.0 &&
1642 echo annotated-v4.0 >expect &&
1643 git tag -l --points-at v4.0 "annotated*" >actual &&
1644 test_cmp expect actual
1645'
1646
1647test_expect_success '--points-at finds annotated tags of tags' '
1648 git tag -m "describing the v4.0 tag object" \
1649 annotated-again-v4.0 annotated-v4.0 &&
1650 cat >expect <<-\EOF &&
1651 annotated-again-v4.0
1652 annotated-v4.0
1653 EOF
1654 git tag --points-at=annotated-v4.0 >actual &&
1655 test_cmp expect actual
1656'
1657
1658test_expect_success 'multiple --points-at are OR-ed together' '
1659 cat >expect <<-\EOF &&
1660 v2.0
1661 v3.0
1662 EOF
1663 git tag --points-at=v2.0 --points-at=v3.0 >actual &&
1664 test_cmp expect actual
1665'
1666
1667test_expect_success 'lexical sort' '
1668 git tag foo1.3 &&
1669 git tag foo1.6 &&
1670 git tag foo1.10 &&
1671 git tag -l --sort=refname "foo*" >actual &&
1672 cat >expect <<-\EOF &&
1673 foo1.10
1674 foo1.3
1675 foo1.6
1676 EOF
1677 test_cmp expect actual
1678'
1679
1680test_expect_success 'version sort' '
1681 git tag -l --sort=version:refname "foo*" >actual &&
1682 cat >expect <<-\EOF &&
1683 foo1.3
1684 foo1.6
1685 foo1.10
1686 EOF
1687 test_cmp expect actual
1688'
1689
1690test_expect_success 'reverse version sort' '
1691 git tag -l --sort=-version:refname "foo*" >actual &&
1692 cat >expect <<-\EOF &&
1693 foo1.10
1694 foo1.6
1695 foo1.3
1696 EOF
1697 test_cmp expect actual
1698'
1699
1700test_expect_success 'reverse lexical sort' '
1701 git tag -l --sort=-refname "foo*" >actual &&
1702 cat >expect <<-\EOF &&
1703 foo1.6
1704 foo1.3
1705 foo1.10
1706 EOF
1707 test_cmp expect actual
1708'
1709
1710test_expect_success 'configured lexical sort' '
1711 test_config tag.sort "v:refname" &&
1712 git tag -l "foo*" >actual &&
1713 cat >expect <<-\EOF &&
1714 foo1.3
1715 foo1.6
1716 foo1.10
1717 EOF
1718 test_cmp expect actual
1719'
1720
1721test_expect_success 'option override configured sort' '
1722 test_config tag.sort "v:refname" &&
1723 git tag -l --sort=-refname "foo*" >actual &&
1724 cat >expect <<-\EOF &&
1725 foo1.6
1726 foo1.3
1727 foo1.10
1728 EOF
1729 test_cmp expect actual
1730'
1731
1732test_expect_success 'invalid sort parameter on command line' '
1733 test_must_fail git tag -l --sort=notvalid "foo*" >actual
1734'
1735
1736test_expect_success 'invalid sort parameter in configuratoin' '
1737 test_config tag.sort "v:notvalid" &&
1738 test_must_fail git tag -l "foo*"
1739'
1740
1741test_expect_success 'version sort with prerelease reordering' '
1742 test_config versionsort.prereleaseSuffix -rc &&
1743 git tag foo1.6-rc1 &&
1744 git tag foo1.6-rc2 &&
1745 git tag -l --sort=version:refname "foo*" >actual &&
1746 cat >expect <<-\EOF &&
1747 foo1.3
1748 foo1.6-rc1
1749 foo1.6-rc2
1750 foo1.6
1751 foo1.10
1752 EOF
1753 test_cmp expect actual
1754'
1755
1756test_expect_success 'reverse version sort with prerelease reordering' '
1757 test_config versionsort.prereleaseSuffix -rc &&
1758 git tag -l --sort=-version:refname "foo*" >actual &&
1759 cat >expect <<-\EOF &&
1760 foo1.10
1761 foo1.6
1762 foo1.6-rc2
1763 foo1.6-rc1
1764 foo1.3
1765 EOF
1766 test_cmp expect actual
1767'
1768
1769test_expect_success 'version sort with prerelease reordering and common leading character' '
1770 test_config versionsort.prereleaseSuffix -before &&
1771 git tag foo1.7-before1 &&
1772 git tag foo1.7 &&
1773 git tag foo1.7-after1 &&
1774 git tag -l --sort=version:refname "foo1.7*" >actual &&
1775 cat >expect <<-\EOF &&
1776 foo1.7-before1
1777 foo1.7
1778 foo1.7-after1
1779 EOF
1780 test_cmp expect actual
1781'
1782
1783test_expect_success 'version sort with prerelease reordering, multiple suffixes and common leading character' '
1784 test_config versionsort.prereleaseSuffix -before &&
1785 git config --add versionsort.prereleaseSuffix -after &&
1786 git tag -l --sort=version:refname "foo1.7*" >actual &&
1787 cat >expect <<-\EOF &&
1788 foo1.7-before1
1789 foo1.7-after1
1790 foo1.7
1791 EOF
1792 test_cmp expect actual
1793'
1794
1795test_expect_success 'version sort with prerelease reordering, multiple suffixes match the same tag' '
1796 test_config versionsort.prereleaseSuffix -bar &&
1797 git config --add versionsort.prereleaseSuffix -foo-baz &&
1798 git config --add versionsort.prereleaseSuffix -foo-bar &&
1799 git tag foo1.8-foo-bar &&
1800 git tag foo1.8-foo-baz &&
1801 git tag foo1.8 &&
1802 git tag -l --sort=version:refname "foo1.8*" >actual &&
1803 cat >expect <<-\EOF &&
1804 foo1.8-foo-baz
1805 foo1.8-foo-bar
1806 foo1.8
1807 EOF
1808 test_cmp expect actual
1809'
1810
1811test_expect_success 'version sort with prerelease reordering, multiple suffixes match starting at the same position' '
1812 test_config versionsort.prereleaseSuffix -pre &&
1813 git config --add versionsort.prereleaseSuffix -prerelease &&
1814 git tag foo1.9-pre1 &&
1815 git tag foo1.9-pre2 &&
1816 git tag foo1.9-prerelease1 &&
1817 git tag -l --sort=version:refname "foo1.9*" >actual &&
1818 cat >expect <<-\EOF &&
1819 foo1.9-pre1
1820 foo1.9-pre2
1821 foo1.9-prerelease1
1822 EOF
1823 test_cmp expect actual
1824'
1825
1826test_expect_success 'version sort with general suffix reordering' '
1827 test_config versionsort.suffix -alpha &&
1828 git config --add versionsort.suffix -beta &&
1829 git config --add versionsort.suffix "" &&
1830 git config --add versionsort.suffix -gamma &&
1831 git config --add versionsort.suffix -delta &&
1832 git tag foo1.10-alpha &&
1833 git tag foo1.10-beta &&
1834 git tag foo1.10-gamma &&
1835 git tag foo1.10-delta &&
1836 git tag foo1.10-unlisted-suffix &&
1837 git tag -l --sort=version:refname "foo1.10*" >actual &&
1838 cat >expect <<-\EOF &&
1839 foo1.10-alpha
1840 foo1.10-beta
1841 foo1.10
1842 foo1.10-unlisted-suffix
1843 foo1.10-gamma
1844 foo1.10-delta
1845 EOF
1846 test_cmp expect actual
1847'
1848
1849test_expect_success 'versionsort.suffix overrides versionsort.prereleaseSuffix' '
1850 test_config versionsort.suffix -before &&
1851 test_config versionsort.prereleaseSuffix -after &&
1852 git tag -l --sort=version:refname "foo1.7*" >actual &&
1853 cat >expect <<-\EOF &&
1854 foo1.7-before1
1855 foo1.7
1856 foo1.7-after1
1857 EOF
1858 test_cmp expect actual
1859'
1860
1861test_expect_success 'version sort with very long prerelease suffix' '
1862 test_config versionsort.prereleaseSuffix -very-looooooooooooooooooooooooong-prerelease-suffix &&
1863 git tag -l --sort=version:refname
1864'
1865
1866run_with_limited_stack () {
1867 (ulimit -s 128 && "$@")
1868}
1869
1870test_lazy_prereq ULIMIT_STACK_SIZE 'run_with_limited_stack true'
1871
1872# we require ulimit, this excludes Windows
1873test_expect_success ULIMIT_STACK_SIZE '--contains and --no-contains work in a deep repo' '
1874 >expect &&
1875 i=1 &&
1876 while test $i -lt 8000
1877 do
1878 echo "commit refs/heads/master
1879committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
1880data <<EOF
1881commit #$i
1882EOF"
1883 test $i = 1 && echo "from refs/heads/master^0"
1884 i=$(($i + 1))
1885 done | git fast-import &&
1886 git checkout master &&
1887 git tag far-far-away HEAD^ &&
1888 run_with_limited_stack git tag --contains HEAD >actual &&
1889 test_cmp expect actual &&
1890 run_with_limited_stack git tag --no-contains HEAD >actual &&
1891 test_line_count ">" 10 actual
1892'
1893
1894test_expect_success '--format should list tags as per format given' '
1895 cat >expect <<-\EOF &&
1896 refname : refs/tags/v1.0
1897 refname : refs/tags/v1.0.1
1898 refname : refs/tags/v1.1.3
1899 EOF
1900 git tag -l --format="refname : %(refname)" "v1*" >actual &&
1901 test_cmp expect actual
1902'
1903
1904test_expect_success "set up color tests" '
1905 echo "<RED>v1.0<RESET>" >expect.color &&
1906 echo "v1.0" >expect.bare &&
1907 color_args="--format=%(color:red)%(refname:short) --list v1.0"
1908'
1909
1910test_expect_success '%(color) omitted without tty' '
1911 TERM=vt100 git tag $color_args >actual.raw &&
1912 test_decode_color <actual.raw >actual &&
1913 test_cmp expect.bare actual
1914'
1915
1916test_expect_success TTY '%(color) present with tty' '
1917 test_terminal env TERM=vt100 git tag $color_args >actual.raw &&
1918 test_decode_color <actual.raw >actual &&
1919 test_cmp expect.color actual
1920'
1921
1922test_expect_success 'color.ui=always overrides auto-color' '
1923 git -c color.ui=always tag $color_args >actual.raw &&
1924 test_decode_color <actual.raw >actual &&
1925 test_cmp expect.color actual
1926'
1927
1928test_expect_success 'setup --merged test tags' '
1929 git tag mergetest-1 HEAD~2 &&
1930 git tag mergetest-2 HEAD~1 &&
1931 git tag mergetest-3 HEAD
1932'
1933
1934test_expect_success '--merged can be used in non-list mode' '
1935 cat >expect <<-\EOF &&
1936 mergetest-1
1937 mergetest-2
1938 EOF
1939 git tag --merged=mergetest-2 "mergetest*" >actual &&
1940 test_cmp expect actual
1941'
1942
1943test_expect_success '--merged is incompatible with --no-merged' '
1944 test_must_fail git tag --merged HEAD --no-merged HEAD
1945'
1946
1947test_expect_success '--merged shows merged tags' '
1948 cat >expect <<-\EOF &&
1949 mergetest-1
1950 mergetest-2
1951 EOF
1952 git tag -l --merged=mergetest-2 mergetest-* >actual &&
1953 test_cmp expect actual
1954'
1955
1956test_expect_success '--no-merged show unmerged tags' '
1957 cat >expect <<-\EOF &&
1958 mergetest-3
1959 EOF
1960 git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
1961 test_cmp expect actual
1962'
1963
1964test_expect_success '--no-merged can be used in non-list mode' '
1965 git tag --no-merged=mergetest-2 mergetest-* >actual &&
1966 test_cmp expect actual
1967'
1968
1969test_expect_success 'ambiguous branch/tags not marked' '
1970 git tag ambiguous &&
1971 git branch ambiguous &&
1972 echo ambiguous >expect &&
1973 git tag -l ambiguous >actual &&
1974 test_cmp expect actual
1975'
1976
1977test_expect_success '--contains combined with --no-contains' '
1978 (
1979 git init no-contains &&
1980 cd no-contains &&
1981 test_commit v0.1 &&
1982 test_commit v0.2 &&
1983 test_commit v0.3 &&
1984 test_commit v0.4 &&
1985 test_commit v0.5 &&
1986 cat >expected <<-\EOF &&
1987 v0.2
1988 v0.3
1989 v0.4
1990 EOF
1991 git tag --contains v0.2 --no-contains v0.5 >actual &&
1992 test_cmp expected actual
1993 )
1994'
1995
1996# As the docs say, list tags which contain a specified *commit*. We
1997# don't recurse down to tags for trees or blobs pointed to by *those*
1998# commits.
1999test_expect_success 'Does --[no-]contains stop at commits? Yes!' '
2000 cd no-contains &&
2001 blob=$(git rev-parse v0.3:v0.3.t) &&
2002 tree=$(git rev-parse v0.3^{tree}) &&
2003 git tag tag-blob $blob &&
2004 git tag tag-tree $tree &&
2005 git tag --contains v0.3 >actual &&
2006 cat >expected <<-\EOF &&
2007 v0.3
2008 v0.4
2009 v0.5
2010 EOF
2011 test_cmp expected actual &&
2012 git tag --no-contains v0.3 >actual &&
2013 cat >expected <<-\EOF &&
2014 v0.1
2015 v0.2
2016 EOF
2017 test_cmp expected actual
2018'
2019
2020test_done