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