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 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 # other ways of specifying the commit
1615 test_expect_success 'checking that first commit is in all tags (tag)' '
1616 cat >expected <<-\EOF &&
1617 v0.2.1
1618 v1.0
1619 v1.0.1
1620 v1.1.3
1621 v2.0
1622 EOF
1623 git tag -l --contains v1.0 v* >actual &&
1624 test_cmp expected actual
1625 '
1626
1627 test_expect_success 'checking that first commit is in all tags (relative)' '
1628 cat >expected <<-\EOF &&
1629 v0.2.1
1630 v1.0
1631 v1.0.1
1632 v1.1.3
1633 v2.0
1634 EOF
1635 git tag -l --contains HEAD~2 v* >actual &&
1636 test_cmp expected actual
1637 '
1638
1639 # All the --contains tests above, but with --no-contains
1640 test_expect_success 'checking that first commit is not listed in any tag with --no-contains (hash)' '
1641 git tag -l --no-contains $hash1 v* >actual &&
1642 test_must_be_empty actual
1643 '
1644
1645 test_expect_success 'checking that first commit is in all tags (tag)' '
1646 git tag -l --no-contains v1.0 v* >actual &&
1647 test_must_be_empty actual
1648 '
1649
1650 test_expect_success 'checking that first commit is in all tags (relative)' '
1651 git tag -l --no-contains HEAD~2 v* >actual &&
1652 test_must_be_empty actual
1653 '
1654
1655 test_expect_success 'checking that second commit only has one tag' '
1656 cat >expected <<-\EOF &&
1657 v2.0
1658 EOF
1659 git tag -l --contains $hash2 v* >actual &&
1660 test_cmp expected actual
1661 '
1662
1663 test_expect_success 'inverse of the last test, with --no-contains' '
1664 cat >expected <<-\EOF &&
1665 v0.2.1
1666 v1.0
1667 v1.0.1
1668 v1.1.3
1669 EOF
1670 git tag -l --no-contains $hash2 v* >actual &&
1671 test_cmp expected actual
1672 '
1673
1674 test_expect_success 'checking that third commit has no tags' '
1675 git tag -l --contains $hash3 v* >actual &&
1676 test_must_be_empty actual
1677 '
1678
1679 test_expect_success 'conversely --no-contains on the third commit lists all tags' '
1680 cat >expected <<-\EOF &&
1681 v0.2.1
1682 v1.0
1683 v1.0.1
1684 v1.1.3
1685 v2.0
1686 EOF
1687 git tag -l --no-contains $hash3 v* >actual &&
1688 test_cmp expected actual
1689 '
1690
1691 # how about a simple merge?
1692
1693 test_expect_success 'creating simple branch' '
1694 git branch stable v2.0 &&
1695 git checkout stable &&
1696 echo foo-3.0 >foo &&
1697 git commit foo -m fourth &&
1698 git tag v3.0
1699 '
1700
1701 test_expect_success 'checking that branch head only has one tag' '
1702 hash4=$(git rev-parse HEAD) &&
1703 cat >expected <<-\EOF &&
1704 v3.0
1705 EOF
1706 git tag -l --contains $hash4 v* >actual &&
1707 test_cmp expected actual
1708 '
1709
1710 test_expect_success 'checking that branch head with --no-contains lists all but one tag' '
1711 cat >expected <<-\EOF &&
1712 v0.2.1
1713 v1.0
1714 v1.0.1
1715 v1.1.3
1716 v2.0
1717 EOF
1718 git tag -l --no-contains $hash4 v* >actual &&
1719 test_cmp expected actual
1720 '
1721
1722 test_expect_success 'merging original branch into this branch' '
1723 git merge --strategy=ours main &&
1724 git tag v4.0
1725 '
1726
1727 test_expect_success 'checking that original branch head has one tag now' '
1728 cat >expected <<-\EOF &&
1729 v4.0
1730 EOF
1731 git tag -l --contains $hash3 v* >actual &&
1732 test_cmp expected actual
1733 '
1734
1735 test_expect_success 'checking that original branch head with --no-contains lists all but one tag now' '
1736 cat >expected <<-\EOF &&
1737 v0.2.1
1738 v1.0
1739 v1.0.1
1740 v1.1.3
1741 v2.0
1742 v3.0
1743 EOF
1744 git tag -l --no-contains $hash3 v* >actual &&
1745 test_cmp expected actual
1746 '
1747
1748 test_expect_success 'checking that initial commit is in all tags' '
1749 cat >expected <<-\EOF &&
1750 v0.2.1
1751 v1.0
1752 v1.0.1
1753 v1.1.3
1754 v2.0
1755 v3.0
1756 v4.0
1757 EOF
1758 git tag -l --contains $hash1 v* >actual &&
1759 test_cmp expected actual
1760 '
1761
1762 test_expect_success 'checking that --contains can be used in non-list mode' '
1763 cat >expected <<-\EOF &&
1764 v0.2.1
1765 v1.0
1766 v1.0.1
1767 v1.1.3
1768 v2.0
1769 v3.0
1770 v4.0
1771 EOF
1772 git tag --contains $hash1 v* >actual &&
1773 test_cmp expected actual
1774 '
1775
1776 test_expect_success 'checking that initial commit is in all tags with --no-contains' '
1777 git tag -l --no-contains $hash1 v* >actual &&
1778 test_must_be_empty actual
1779 '
1780
1781 # mixing modes and options:
1782
1783 test_expect_success 'mixing incompatibles modes and options is forbidden' '
1784 test_must_fail git tag -a &&
1785 test_must_fail git tag -a -l &&
1786 test_must_fail git tag -s &&
1787 test_must_fail git tag -s -l &&
1788 test_must_fail git tag -m &&
1789 test_must_fail git tag -m -l &&
1790 test_must_fail git tag -m "hlagh" &&
1791 test_must_fail git tag -m "hlagh" -l &&
1792 test_must_fail git tag -F &&
1793 test_must_fail git tag -F -l &&
1794 test_must_fail git tag -f &&
1795 test_must_fail git tag -f -l &&
1796 test_must_fail git tag -a -s -m -F &&
1797 test_must_fail git tag -a -s -m -F -l &&
1798 test_must_fail git tag -l -v &&
1799 test_must_fail git tag -l -d &&
1800 test_must_fail git tag -l -v -d &&
1801 test_must_fail git tag -n 100 -v &&
1802 test_must_fail git tag -l -m msg &&
1803 test_must_fail git tag -l -F some file &&
1804 test_must_fail git tag -v -s &&
1805 test_must_fail git tag --contains tag-tree &&
1806 test_must_fail git tag --contains tag-blob &&
1807 test_must_fail git tag --no-contains tag-tree &&
1808 test_must_fail git tag --no-contains tag-blob &&
1809 test_must_fail git tag --contains --no-contains &&
1810 test_must_fail git tag --no-with HEAD &&
1811 test_must_fail git tag --no-without HEAD
1812 '
1813
1814 for option in --contains --with --no-contains --without --merged --no-merged --points-at
1815 do
1816 test_expect_success "mixing incompatible modes with $option is forbidden" '
1817 test_must_fail git tag -d $option HEAD &&
1818 test_must_fail git tag -d $option HEAD some-tag &&
1819 test_must_fail git tag -v $option HEAD
1820 '
1821 test_expect_success "Doing 'git tag --list-like $option <commit> <pattern> is permitted" '
1822 git tag -n $option HEAD HEAD &&
1823 git tag $option HEAD HEAD &&
1824 git tag $option
1825 '
1826 done
1827
1828 # check points-at
1829
1830 test_expect_success '--points-at can be used in non-list mode' '
1831 echo v4.0 >expect &&
1832 git tag --points-at=v4.0 "v*" >actual &&
1833 test_cmp expect actual
1834 '
1835
1836 test_expect_success '--points-at is a synonym for --points-at HEAD' '
1837 echo v4.0 >expect &&
1838 git tag --points-at >actual &&
1839 test_cmp expect actual
1840 '
1841
1842 test_expect_success '--points-at finds lightweight tags' '
1843 echo v4.0 >expect &&
1844 git tag --points-at v4.0 >actual &&
1845 test_cmp expect actual
1846 '
1847
1848 test_expect_success '--points-at finds annotated tags of commits' '
1849 git tag -m "v4.0, annotated" annotated-v4.0 v4.0 &&
1850 echo annotated-v4.0 >expect &&
1851 git tag -l --points-at v4.0 "annotated*" >actual &&
1852 test_cmp expect actual
1853 '
1854
1855 test_expect_success '--points-at finds annotated tags of tags' '
1856 git tag -m "describing the v4.0 tag object" \
1857 annotated-again-v4.0 annotated-v4.0 &&
1858 cat >expect <<-\EOF &&
1859 annotated-again-v4.0
1860 annotated-v4.0
1861 EOF
1862 git tag --points-at=annotated-v4.0 >actual &&
1863 test_cmp expect actual
1864 '
1865
1866 test_expect_success 'recursive tagging should give advice' '
1867 cat >expect <<-EOF &&
1868 hint: You have created a nested tag. The object referred to by your new tag is
1869 hint: already a tag. If you meant to tag the object that it points to, use:
1870 hint:
1871 hint: git tag -f nested annotated-v4.0^{}
1872 hint: Disable this message with "git config set advice.nestedTag false"
1873 EOF
1874 git tag -m nested nested annotated-v4.0 2>actual &&
1875 test_cmp expect actual
1876 '
1877
1878 test_expect_success 'multiple --points-at are OR-ed together' '
1879 cat >expect <<-\EOF &&
1880 v2.0
1881 v3.0
1882 EOF
1883 git tag --points-at=v2.0 --points-at=v3.0 >actual &&
1884 test_cmp expect actual
1885 '
1886
1887 test_expect_success 'lexical sort' '
1888 git tag foo1.3 &&
1889 git tag foo1.6 &&
1890 git tag foo1.10 &&
1891 git tag -l --sort=refname "foo*" >actual &&
1892 cat >expect <<-\EOF &&
1893 foo1.10
1894 foo1.3
1895 foo1.6
1896 EOF
1897 test_cmp expect actual
1898 '
1899
1900 test_expect_success 'version sort' '
1901 git tag -l --sort=version:refname "foo*" >actual &&
1902 cat >expect <<-\EOF &&
1903 foo1.3
1904 foo1.6
1905 foo1.10
1906 EOF
1907 test_cmp expect actual
1908 '
1909
1910 test_expect_success 'reverse version sort' '
1911 git tag -l --sort=-version:refname "foo*" >actual &&
1912 cat >expect <<-\EOF &&
1913 foo1.10
1914 foo1.6
1915 foo1.3
1916 EOF
1917 test_cmp expect actual
1918 '
1919
1920 test_expect_success 'reverse lexical sort' '
1921 git tag -l --sort=-refname "foo*" >actual &&
1922 cat >expect <<-\EOF &&
1923 foo1.6
1924 foo1.3
1925 foo1.10
1926 EOF
1927 test_cmp expect actual
1928 '
1929
1930 test_expect_success 'configured lexical sort' '
1931 test_config tag.sort "v:refname" &&
1932 git tag -l "foo*" >actual &&
1933 cat >expect <<-\EOF &&
1934 foo1.3
1935 foo1.6
1936 foo1.10
1937 EOF
1938 test_cmp expect actual
1939 '
1940
1941 test_expect_success 'option override configured sort' '
1942 test_config tag.sort "v:refname" &&
1943 git tag -l --sort=-refname "foo*" >actual &&
1944 cat >expect <<-\EOF &&
1945 foo1.6
1946 foo1.3
1947 foo1.10
1948 EOF
1949 test_cmp expect actual
1950 '
1951
1952 test_expect_success '--no-sort cancels config sort keys' '
1953 test_config tag.sort "-refname" &&
1954
1955 # objecttype is identical for all of them, so sort falls back on
1956 # default (ascending refname)
1957 git tag -l \
1958 --no-sort \
1959 --sort="objecttype" \
1960 "foo*" >actual &&
1961 cat >expect <<-\EOF &&
1962 foo1.10
1963 foo1.3
1964 foo1.6
1965 EOF
1966 test_cmp expect actual
1967 '
1968
1969 test_expect_success '--no-sort cancels command line sort keys' '
1970 # objecttype is identical for all of them, so sort falls back on
1971 # default (ascending refname)
1972 git tag -l \
1973 --sort="-refname" \
1974 --no-sort \
1975 --sort="objecttype" \
1976 "foo*" >actual &&
1977 cat >expect <<-\EOF &&
1978 foo1.10
1979 foo1.3
1980 foo1.6
1981 EOF
1982 test_cmp expect actual
1983 '
1984
1985 test_expect_success '--no-sort without subsequent --sort prints expected tags' '
1986 # Sort the results with `sort` for a consistent comparison against
1987 # expected
1988 git tag -l --no-sort "foo*" | sort >actual &&
1989 cat >expect <<-\EOF &&
1990 foo1.10
1991 foo1.3
1992 foo1.6
1993 EOF
1994 test_cmp expect actual
1995 '
1996
1997 test_expect_success 'invalid sort parameter on command line' '
1998 test_must_fail git tag -l --sort=notvalid "foo*" >actual
1999 '
2000
2001 test_expect_success 'invalid sort parameter in configuratoin' '
2002 test_config tag.sort "v:notvalid" &&
2003 test_must_fail git tag -l "foo*"
2004 '
2005
2006 test_expect_success 'version sort handles empty value for versionsort.{prereleaseSuffix,suffix}' '
2007 cp .git/config .git/config.orig &&
2008 test_when_finished mv .git/config.orig .git/config &&
2009
2010 cat >>.git/config <<-\EOF &&
2011 [versionsort]
2012 prereleaseSuffix
2013 suffix
2014 EOF
2015 cat >expect <<-\EOF &&
2016 error: missing value for '\''versionsort.suffix'\''
2017 error: missing value for '\''versionsort.prereleasesuffix'\''
2018 EOF
2019 git tag -l --sort=version:refname 2>actual &&
2020 test_cmp expect actual
2021 '
2022
2023 test_expect_success 'version sort with prerelease reordering' '
2024 test_config versionsort.prereleaseSuffix -rc &&
2025 git tag foo1.6-rc1 &&
2026 git tag foo1.6-rc2 &&
2027 git tag -l --sort=version:refname "foo*" >actual &&
2028 cat >expect <<-\EOF &&
2029 foo1.3
2030 foo1.6-rc1
2031 foo1.6-rc2
2032 foo1.6
2033 foo1.10
2034 EOF
2035 test_cmp expect actual
2036 '
2037
2038 test_expect_success 'reverse version sort with prerelease reordering' '
2039 test_config versionsort.prereleaseSuffix -rc &&
2040 git tag -l --sort=-version:refname "foo*" >actual &&
2041 cat >expect <<-\EOF &&
2042 foo1.10
2043 foo1.6
2044 foo1.6-rc2
2045 foo1.6-rc1
2046 foo1.3
2047 EOF
2048 test_cmp expect actual
2049 '
2050
2051 test_expect_success 'version sort with prerelease reordering and common leading character' '
2052 test_config versionsort.prereleaseSuffix -before &&
2053 git tag foo1.7-before1 &&
2054 git tag foo1.7 &&
2055 git tag foo1.7-after1 &&
2056 git tag -l --sort=version:refname "foo1.7*" >actual &&
2057 cat >expect <<-\EOF &&
2058 foo1.7-before1
2059 foo1.7
2060 foo1.7-after1
2061 EOF
2062 test_cmp expect actual
2063 '
2064
2065 test_expect_success 'version sort with prerelease reordering, multiple suffixes and common leading character' '
2066 test_config versionsort.prereleaseSuffix -before &&
2067 git config --add versionsort.prereleaseSuffix -after &&
2068 git tag -l --sort=version:refname "foo1.7*" >actual &&
2069 cat >expect <<-\EOF &&
2070 foo1.7-before1
2071 foo1.7-after1
2072 foo1.7
2073 EOF
2074 test_cmp expect actual
2075 '
2076
2077 test_expect_success 'version sort with prerelease reordering, multiple suffixes match the same tag' '
2078 test_config versionsort.prereleaseSuffix -bar &&
2079 git config --add versionsort.prereleaseSuffix -foo-baz &&
2080 git config --add versionsort.prereleaseSuffix -foo-bar &&
2081 git tag foo1.8-foo-bar &&
2082 git tag foo1.8-foo-baz &&
2083 git tag foo1.8 &&
2084 git tag -l --sort=version:refname "foo1.8*" >actual &&
2085 cat >expect <<-\EOF &&
2086 foo1.8-foo-baz
2087 foo1.8-foo-bar
2088 foo1.8
2089 EOF
2090 test_cmp expect actual
2091 '
2092
2093 test_expect_success 'version sort with prerelease reordering, multiple suffixes match starting at the same position' '
2094 test_config versionsort.prereleaseSuffix -pre &&
2095 git config --add versionsort.prereleaseSuffix -prerelease &&
2096 git tag foo1.9-pre1 &&
2097 git tag foo1.9-pre2 &&
2098 git tag foo1.9-prerelease1 &&
2099 git tag -l --sort=version:refname "foo1.9*" >actual &&
2100 cat >expect <<-\EOF &&
2101 foo1.9-pre1
2102 foo1.9-pre2
2103 foo1.9-prerelease1
2104 EOF
2105 test_cmp expect actual
2106 '
2107
2108 test_expect_success 'version sort with general suffix reordering' '
2109 test_config versionsort.suffix -alpha &&
2110 git config --add versionsort.suffix -beta &&
2111 git config --add versionsort.suffix "" &&
2112 git config --add versionsort.suffix -gamma &&
2113 git config --add versionsort.suffix -delta &&
2114 git tag foo1.10-alpha &&
2115 git tag foo1.10-beta &&
2116 git tag foo1.10-gamma &&
2117 git tag foo1.10-delta &&
2118 git tag foo1.10-unlisted-suffix &&
2119 git tag -l --sort=version:refname "foo1.10*" >actual &&
2120 cat >expect <<-\EOF &&
2121 foo1.10-alpha
2122 foo1.10-beta
2123 foo1.10
2124 foo1.10-unlisted-suffix
2125 foo1.10-gamma
2126 foo1.10-delta
2127 EOF
2128 test_cmp expect actual
2129 '
2130
2131 test_expect_success 'versionsort.suffix overrides versionsort.prereleaseSuffix' '
2132 test_config versionsort.suffix -before &&
2133 test_config versionsort.prereleaseSuffix -after &&
2134 git tag -l --sort=version:refname "foo1.7*" >actual &&
2135 cat >expect <<-\EOF &&
2136 foo1.7-before1
2137 foo1.7
2138 foo1.7-after1
2139 EOF
2140 test_cmp expect actual
2141 '
2142
2143 test_expect_success 'version sort with very long prerelease suffix' '
2144 test_config versionsort.prereleaseSuffix -very-looooooooooooooooooooooooong-prerelease-suffix &&
2145 git tag -l --sort=version:refname
2146 '
2147
2148 test_expect_success ULIMIT_STACK_SIZE '--contains and --no-contains work in a deep repo' '
2149 i=1 &&
2150 while test $i -lt 8000
2151 do
2152 echo "commit refs/heads/main
2153 committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
2154 data <<EOF
2155 commit #$i
2156 EOF" &&
2157 if test $i = 1
2158 then
2159 echo "from refs/heads/main^0"
2160 fi &&
2161 i=$(($i + 1)) || return 1
2162 done | git fast-import &&
2163 git checkout main &&
2164 git tag far-far-away HEAD^ &&
2165 run_with_limited_stack git tag --contains HEAD >actual &&
2166 test_must_be_empty actual &&
2167 run_with_limited_stack git tag --no-contains HEAD >actual &&
2168 test_line_count "-gt" 10 actual
2169 '
2170
2171 test_expect_success '--format should list tags as per format given' '
2172 cat >expect <<-\EOF &&
2173 refname : refs/tags/v1.0
2174 refname : refs/tags/v1.0.1
2175 refname : refs/tags/v1.1.3
2176 EOF
2177 git tag -l --format="refname : %(refname)" "v1*" >actual &&
2178 test_cmp expect actual
2179 '
2180
2181 test_expect_success '--format --omit-empty works' '
2182 cat >expect <<-\EOF &&
2183 refname : refs/tags/v1.0
2184
2185 refname : refs/tags/v1.1.3
2186 EOF
2187 git tag -l --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
2188 test_cmp expect actual &&
2189 cat >expect <<-\EOF &&
2190 refname : refs/tags/v1.0
2191 refname : refs/tags/v1.1.3
2192 EOF
2193 git tag -l --omit-empty --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
2194 test_cmp expect actual
2195 '
2196
2197 test_expect_success 'git tag -l with --format="%(rest)" must fail' '
2198 test_must_fail git tag -l --format="%(rest)" "v1*"
2199 '
2200
2201 test_expect_success 'set up color tests' '
2202 echo "<RED>v1.0<RESET>" >expect.color &&
2203 echo "v1.0" >expect.bare &&
2204 color_args="--format=%(color:red)%(refname:short) --list v1.0"
2205 '
2206
2207 test_expect_success '%(color) omitted without tty' '
2208 TERM=vt100 git tag $color_args >actual.raw &&
2209 test_decode_color <actual.raw >actual &&
2210 test_cmp expect.bare actual
2211 '
2212
2213 test_expect_success TTY '%(color) present with tty' '
2214 test_terminal git tag $color_args >actual.raw &&
2215 test_decode_color <actual.raw >actual &&
2216 test_cmp expect.color actual
2217 '
2218
2219 test_expect_success '--color overrides auto-color' '
2220 git tag --color $color_args >actual.raw &&
2221 test_decode_color <actual.raw >actual &&
2222 test_cmp expect.color actual
2223 '
2224
2225 test_expect_success 'color.ui=always overrides auto-color' '
2226 git -c color.ui=always tag $color_args >actual.raw &&
2227 test_decode_color <actual.raw >actual &&
2228 test_cmp expect.color actual
2229 '
2230
2231 test_expect_success 'setup --merged test tags' '
2232 git tag mergetest-1 HEAD~2 &&
2233 git tag mergetest-2 HEAD~1 &&
2234 git tag mergetest-3 HEAD
2235 '
2236
2237 test_expect_success '--merged can be used in non-list mode' '
2238 cat >expect <<-\EOF &&
2239 mergetest-1
2240 mergetest-2
2241 EOF
2242 git tag --merged=mergetest-2 "mergetest*" >actual &&
2243 test_cmp expect actual
2244 '
2245
2246 test_expect_success '--merged is compatible with --no-merged' '
2247 git tag --merged HEAD --no-merged HEAD
2248 '
2249
2250 test_expect_success '--merged shows merged tags' '
2251 cat >expect <<-\EOF &&
2252 mergetest-1
2253 mergetest-2
2254 EOF
2255 git tag -l --merged=mergetest-2 mergetest-* >actual &&
2256 test_cmp expect actual
2257 '
2258
2259 test_expect_success '--no-merged show unmerged tags' '
2260 cat >expect <<-\EOF &&
2261 mergetest-3
2262 EOF
2263 git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
2264 test_cmp expect actual
2265 '
2266
2267 test_expect_success '--no-merged can be used in non-list mode' '
2268 git tag --no-merged=mergetest-2 mergetest-* >actual &&
2269 test_cmp expect actual
2270 '
2271
2272 test_expect_success 'ambiguous branch/tags not marked' '
2273 git tag ambiguous &&
2274 git branch ambiguous &&
2275 echo ambiguous >expect &&
2276 git tag -l ambiguous >actual &&
2277 test_cmp expect actual
2278 '
2279
2280 test_expect_success '--contains combined with --no-contains' '
2281 (
2282 git init no-contains &&
2283 cd no-contains &&
2284 test_commit v0.1 &&
2285 test_commit v0.2 &&
2286 test_commit v0.3 &&
2287 test_commit v0.4 &&
2288 test_commit v0.5 &&
2289 cat >expected <<-\EOF &&
2290 v0.2
2291 v0.3
2292 v0.4
2293 EOF
2294 git tag --contains v0.2 --no-contains v0.5 >actual &&
2295 test_cmp expected actual
2296 )
2297 '
2298
2299 # As the docs say, list tags which contain a specified *commit*. We
2300 # don't recurse down to tags for trees or blobs pointed to by *those*
2301 # commits.
2302 test_expect_success 'Does --[no-]contains stop at commits? Yes!' '
2303 (
2304 cd no-contains &&
2305 blob=$(git rev-parse v0.3:v0.3.t) &&
2306 tree=$(git rev-parse v0.3^{tree}) &&
2307 git tag tag-blob $blob &&
2308 git tag tag-tree $tree &&
2309 git tag --contains v0.3 >actual &&
2310 cat >expected <<-\EOF &&
2311 v0.3
2312 v0.4
2313 v0.5
2314 EOF
2315 test_cmp expected actual &&
2316 git tag --no-contains v0.3 >actual &&
2317 cat >expected <<-\EOF &&
2318 v0.1
2319 v0.2
2320 EOF
2321 test_cmp expected actual
2322 )
2323 '
2324
2325 test_expect_success 'If tag is created then tag message file is unlinked' '
2326 test_when_finished "git tag -d foo" &&
2327 write_script fakeeditor <<-\EOF &&
2328 echo Message >.git/TAG_EDITMSG
2329 EOF
2330 GIT_EDITOR=./fakeeditor git tag -a foo &&
2331 test_path_is_missing .git/TAG_EDITMSG
2332 '
2333
2334 test_expect_success 'If tag cannot be created then tag message file is not unlinked' '
2335 test_when_finished "git tag -d foo/bar && rm .git/TAG_EDITMSG" &&
2336 write_script fakeeditor <<-\EOF &&
2337 echo Message >.git/TAG_EDITMSG
2338 EOF
2339 git tag foo/bar &&
2340 test_must_fail env GIT_EDITOR=./fakeeditor git tag -a foo &&
2341 test_path_exists .git/TAG_EDITMSG
2342 '
2343
2344 test_expect_success 'annotated tag version sort' '
2345 git tag -a -m "sample 1.0" vsample-1.0 &&
2346 git tag -a -m "sample 2.0" vsample-2.0 &&
2347 git tag -a -m "sample 10.0" vsample-10.0 &&
2348 cat >expect <<-EOF &&
2349 vsample-1.0
2350 vsample-2.0
2351 vsample-10.0
2352 EOF
2353
2354 git tag --list --sort=version:tag vsample-\* >actual &&
2355 test_cmp expect actual &&
2356
2357 # Ensure that we also handle this case alright in the case we have the
2358 # peeled values cached e.g. via the packed-refs file.
2359 git pack-refs --all &&
2360 git tag --list --sort=version:tag vsample-\* &&
2361 test_cmp expect actual
2362 '
2363
2364 test_done