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