t / t7004-tag.shon commit Merge branch 'js/unzip-in-usr-bin-workaround' (d008809)
   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        test_when_finished git tag -d newtag forcetag &&
 153        git tag newtag >expect &&
 154        git tag --force forcetag >actual &&
 155        test_cmp expect actual
 156'
 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        test_config column.tag row &&
 328        test_config column.ui dense &&
 329        COLUMNS=40 git tag -l >actual &&
 330        cat >expected <<\EOF &&
 331a1      aa1   cba     t210    t211
 332v0.2.1  v1.0  v1.0.1  v1.1.3
 333EOF
 334        test_cmp expected actual
 335'
 336
 337test_expect_success 'listing tag with -n --column should fail' '
 338        test_must_fail git tag --column -n
 339'
 340
 341test_expect_success 'listing tags -n in column with column.ui ignored' '
 342        test_config column.ui "row dense" &&
 343        COLUMNS=40 git tag -l -n >actual &&
 344        cat >expected <<\EOF &&
 345a1              Foo
 346aa1             Foo
 347cba             Foo
 348t210            Foo
 349t211            Foo
 350v0.2.1          Foo
 351v1.0            Foo
 352v1.0.1          Foo
 353v1.1.3          Foo
 354EOF
 355        test_cmp expected actual
 356'
 357
 358# creating and verifying lightweight tags:
 359
 360test_expect_success \
 361        'a non-annotated tag created without parameters should point to HEAD' '
 362        git tag non-annotated-tag &&
 363        test $(git cat-file -t non-annotated-tag) = commit &&
 364        test $(git rev-parse non-annotated-tag) = $(git rev-parse HEAD)
 365'
 366
 367test_expect_success 'trying to verify an unknown tag should fail' \
 368        'test_must_fail git tag -v unknown-tag'
 369
 370test_expect_success \
 371        'trying to verify a non-annotated and non-signed tag should fail' \
 372        'test_must_fail git tag -v non-annotated-tag'
 373
 374test_expect_success \
 375        'trying to verify many non-annotated or unknown tags, should fail' \
 376        'test_must_fail git tag -v unknown-tag1 non-annotated-tag unknown-tag2'
 377
 378# creating annotated tags:
 379
 380get_tag_msg () {
 381        git cat-file tag "$1" | sed -e "/BEGIN PGP/q"
 382}
 383
 384# run test_tick before committing always gives the time in that timezone
 385get_tag_header () {
 386cat <<EOF
 387object $2
 388type $3
 389tag $1
 390tagger C O Mitter <committer@example.com> $4 -0700
 391
 392EOF
 393}
 394
 395commit=$(git rev-parse HEAD)
 396time=$test_tick
 397
 398get_tag_header annotated-tag $commit commit $time >expect
 399echo "A message" >>expect
 400test_expect_success \
 401        'creating an annotated tag with -m message should succeed' '
 402        git tag -m "A message" annotated-tag &&
 403        get_tag_msg annotated-tag >actual &&
 404        test_cmp expect actual
 405'
 406
 407cat >msgfile <<EOF
 408Another message
 409in a file.
 410EOF
 411get_tag_header file-annotated-tag $commit commit $time >expect
 412cat msgfile >>expect
 413test_expect_success \
 414        'creating an annotated tag with -F messagefile should succeed' '
 415        git tag -F msgfile file-annotated-tag &&
 416        get_tag_msg file-annotated-tag >actual &&
 417        test_cmp expect actual
 418'
 419
 420cat >inputmsg <<EOF
 421A message from the
 422standard input
 423EOF
 424get_tag_header stdin-annotated-tag $commit commit $time >expect
 425cat inputmsg >>expect
 426test_expect_success 'creating an annotated tag with -F - should succeed' '
 427        git tag -F - stdin-annotated-tag <inputmsg &&
 428        get_tag_msg stdin-annotated-tag >actual &&
 429        test_cmp expect actual
 430'
 431
 432test_expect_success \
 433        'trying to create a tag with a non-existing -F file should fail' '
 434        ! test -f nonexistingfile &&
 435        ! tag_exists notag &&
 436        test_must_fail git tag -F nonexistingfile notag &&
 437        ! tag_exists notag
 438'
 439
 440test_expect_success \
 441        'trying to create tags giving both -m or -F options should fail' '
 442        echo "message file 1" >msgfile1 &&
 443        echo "message file 2" >msgfile2 &&
 444        ! tag_exists msgtag &&
 445        test_must_fail git tag -m "message 1" -F msgfile1 msgtag &&
 446        ! tag_exists msgtag &&
 447        test_must_fail git tag -F msgfile1 -m "message 1" msgtag &&
 448        ! tag_exists msgtag &&
 449        test_must_fail git tag -m "message 1" -F msgfile1 \
 450                -m "message 2" msgtag &&
 451        ! tag_exists msgtag
 452'
 453
 454# blank and empty messages:
 455
 456get_tag_header empty-annotated-tag $commit commit $time >expect
 457test_expect_success \
 458        'creating a tag with an empty -m message should succeed' '
 459        git tag -m "" empty-annotated-tag &&
 460        get_tag_msg empty-annotated-tag >actual &&
 461        test_cmp expect actual
 462'
 463
 464>emptyfile
 465get_tag_header emptyfile-annotated-tag $commit commit $time >expect
 466test_expect_success \
 467        'creating a tag with an empty -F messagefile should succeed' '
 468        git tag -F emptyfile emptyfile-annotated-tag &&
 469        get_tag_msg emptyfile-annotated-tag >actual &&
 470        test_cmp expect actual
 471'
 472
 473printf '\n\n  \n\t\nLeading blank lines\n' >blanksfile
 474printf '\n\t \t  \nRepeated blank lines\n' >>blanksfile
 475printf '\n\n\nTrailing spaces      \t  \n' >>blanksfile
 476printf '\nTrailing blank lines\n\n\t \n\n' >>blanksfile
 477get_tag_header blanks-annotated-tag $commit commit $time >expect
 478cat >>expect <<EOF
 479Leading blank lines
 480
 481Repeated blank lines
 482
 483Trailing spaces
 484
 485Trailing blank lines
 486EOF
 487test_expect_success \
 488        'extra blanks in the message for an annotated tag should be removed' '
 489        git tag -F blanksfile blanks-annotated-tag &&
 490        get_tag_msg blanks-annotated-tag >actual &&
 491        test_cmp expect actual
 492'
 493
 494get_tag_header blank-annotated-tag $commit commit $time >expect
 495test_expect_success \
 496        'creating a tag with blank -m message with spaces should succeed' '
 497        git tag -m "     " blank-annotated-tag &&
 498        get_tag_msg blank-annotated-tag >actual &&
 499        test_cmp expect actual
 500'
 501
 502echo '     ' >blankfile
 503echo ''      >>blankfile
 504echo '  '    >>blankfile
 505get_tag_header blankfile-annotated-tag $commit commit $time >expect
 506test_expect_success \
 507        'creating a tag with blank -F messagefile with spaces should succeed' '
 508        git tag -F blankfile blankfile-annotated-tag &&
 509        get_tag_msg blankfile-annotated-tag >actual &&
 510        test_cmp expect actual
 511'
 512
 513printf '      ' >blanknonlfile
 514get_tag_header blanknonlfile-annotated-tag $commit commit $time >expect
 515test_expect_success \
 516        'creating a tag with -F file of spaces and no newline should succeed' '
 517        git tag -F blanknonlfile blanknonlfile-annotated-tag &&
 518        get_tag_msg blanknonlfile-annotated-tag >actual &&
 519        test_cmp expect actual
 520'
 521
 522# messages with commented lines:
 523
 524cat >commentsfile <<EOF
 525# A comment
 526
 527############
 528The message.
 529############
 530One line.
 531
 532
 533# commented lines
 534# commented lines
 535
 536Another line.
 537# comments
 538
 539Last line.
 540EOF
 541get_tag_header comments-annotated-tag $commit commit $time >expect
 542cat >>expect <<EOF
 543The message.
 544One line.
 545
 546Another line.
 547
 548Last line.
 549EOF
 550test_expect_success \
 551        'creating a tag using a -F messagefile with #comments should succeed' '
 552        git tag -F commentsfile comments-annotated-tag &&
 553        get_tag_msg comments-annotated-tag >actual &&
 554        test_cmp expect actual
 555'
 556
 557get_tag_header comment-annotated-tag $commit commit $time >expect
 558test_expect_success \
 559        'creating a tag with a #comment in the -m message should succeed' '
 560        git tag -m "#comment" comment-annotated-tag &&
 561        get_tag_msg comment-annotated-tag >actual &&
 562        test_cmp expect actual
 563'
 564
 565echo '#comment' >commentfile
 566echo ''         >>commentfile
 567echo '####'     >>commentfile
 568get_tag_header commentfile-annotated-tag $commit commit $time >expect
 569test_expect_success \
 570        'creating a tag with #comments in the -F messagefile should succeed' '
 571        git tag -F commentfile commentfile-annotated-tag &&
 572        get_tag_msg commentfile-annotated-tag >actual &&
 573        test_cmp expect actual
 574'
 575
 576printf '#comment' >commentnonlfile
 577get_tag_header commentnonlfile-annotated-tag $commit commit $time >expect
 578test_expect_success \
 579        'creating a tag with a file of #comment and no newline should succeed' '
 580        git tag -F commentnonlfile commentnonlfile-annotated-tag &&
 581        get_tag_msg commentnonlfile-annotated-tag >actual &&
 582        test_cmp expect actual
 583'
 584
 585# listing messages for annotated non-signed tags:
 586
 587test_expect_success \
 588        'listing the one-line message of a non-signed tag should succeed' '
 589        git tag -m "A msg" tag-one-line &&
 590
 591        echo "tag-one-line" >expect &&
 592        git tag -l | grep "^tag-one-line" >actual &&
 593        test_cmp expect actual &&
 594        git tag -n0 -l | grep "^tag-one-line" >actual &&
 595        test_cmp expect actual &&
 596        git tag -n0 -l tag-one-line >actual &&
 597        test_cmp expect actual &&
 598
 599        echo "tag-one-line    A msg" >expect &&
 600        git tag -n1 -l | grep "^tag-one-line" >actual &&
 601        test_cmp expect actual &&
 602        git tag -n -l | grep "^tag-one-line" >actual &&
 603        test_cmp expect actual &&
 604        git tag -n1 -l tag-one-line >actual &&
 605        test_cmp expect actual &&
 606        git tag -n2 -l tag-one-line >actual &&
 607        test_cmp expect actual &&
 608        git tag -n999 -l tag-one-line >actual &&
 609        test_cmp expect actual
 610'
 611
 612test_expect_success \
 613        'listing the zero-lines message of a non-signed tag should succeed' '
 614        git tag -m "" tag-zero-lines &&
 615
 616        echo "tag-zero-lines" >expect &&
 617        git tag -l | grep "^tag-zero-lines" >actual &&
 618        test_cmp expect actual &&
 619        git tag -n0 -l | grep "^tag-zero-lines" >actual &&
 620        test_cmp expect actual &&
 621        git tag -n0 -l tag-zero-lines >actual &&
 622        test_cmp expect actual &&
 623
 624        echo "tag-zero-lines  " >expect &&
 625        git tag -n1 -l | grep "^tag-zero-lines" >actual &&
 626        test_cmp expect actual &&
 627        git tag -n -l | grep "^tag-zero-lines" >actual &&
 628        test_cmp expect actual &&
 629        git tag -n1 -l tag-zero-lines >actual &&
 630        test_cmp expect actual &&
 631        git tag -n2 -l tag-zero-lines >actual &&
 632        test_cmp expect actual &&
 633        git tag -n999 -l tag-zero-lines >actual &&
 634        test_cmp expect actual
 635'
 636
 637echo 'tag line one' >annotagmsg
 638echo 'tag line two' >>annotagmsg
 639echo 'tag line three' >>annotagmsg
 640test_expect_success \
 641        'listing many message lines of a non-signed tag should succeed' '
 642        git tag -F annotagmsg tag-lines &&
 643
 644        echo "tag-lines" >expect &&
 645        git tag -l | grep "^tag-lines" >actual &&
 646        test_cmp expect actual &&
 647        git tag -n0 -l | grep "^tag-lines" >actual &&
 648        test_cmp expect actual &&
 649        git tag -n0 -l tag-lines >actual &&
 650        test_cmp expect actual &&
 651
 652        echo "tag-lines       tag line one" >expect &&
 653        git tag -n1 -l | grep "^tag-lines" >actual &&
 654        test_cmp expect actual &&
 655        git tag -n -l | grep "^tag-lines" >actual &&
 656        test_cmp expect actual &&
 657        git tag -n1 -l tag-lines >actual &&
 658        test_cmp expect actual &&
 659
 660        echo "    tag line two" >>expect &&
 661        git tag -n2 -l | grep "^ *tag.line" >actual &&
 662        test_cmp expect actual &&
 663        git tag -n2 -l tag-lines >actual &&
 664        test_cmp expect actual &&
 665
 666        echo "    tag line three" >>expect &&
 667        git tag -n3 -l | grep "^ *tag.line" >actual &&
 668        test_cmp expect actual &&
 669        git tag -n3 -l tag-lines >actual &&
 670        test_cmp expect actual &&
 671        git tag -n4 -l | grep "^ *tag.line" >actual &&
 672        test_cmp expect actual &&
 673        git tag -n4 -l tag-lines >actual &&
 674        test_cmp expect actual &&
 675        git tag -n99 -l | grep "^ *tag.line" >actual &&
 676        test_cmp expect actual &&
 677        git tag -n99 -l tag-lines >actual &&
 678        test_cmp expect actual
 679'
 680
 681test_expect_success 'annotations for blobs are empty' '
 682        blob=$(git hash-object -w --stdin <<-\EOF
 683        Blob paragraph 1.
 684
 685        Blob paragraph 2.
 686        EOF
 687        ) &&
 688        git tag tag-blob $blob &&
 689        echo "tag-blob        " >expect &&
 690        git tag -n1 -l tag-blob >actual &&
 691        test_cmp expect actual
 692'
 693
 694# trying to verify annotated non-signed tags:
 695
 696test_expect_success GPG \
 697        'trying to verify an annotated non-signed tag should fail' '
 698        tag_exists annotated-tag &&
 699        test_must_fail git tag -v annotated-tag
 700'
 701
 702test_expect_success GPG \
 703        'trying to verify a file-annotated non-signed tag should fail' '
 704        tag_exists file-annotated-tag &&
 705        test_must_fail git tag -v file-annotated-tag
 706'
 707
 708test_expect_success GPG \
 709        'trying to verify two annotated non-signed tags should fail' '
 710        tag_exists annotated-tag file-annotated-tag &&
 711        test_must_fail git tag -v annotated-tag file-annotated-tag
 712'
 713
 714# creating and verifying signed tags:
 715
 716get_tag_header signed-tag $commit commit $time >expect
 717echo 'A signed tag message' >>expect
 718echo '-----BEGIN PGP SIGNATURE-----' >>expect
 719test_expect_success GPG 'creating a signed tag with -m message should succeed' '
 720        git tag -s -m "A signed tag message" signed-tag &&
 721        get_tag_msg signed-tag >actual &&
 722        test_cmp expect actual
 723'
 724
 725get_tag_header u-signed-tag $commit commit $time >expect
 726echo 'Another message' >>expect
 727echo '-----BEGIN PGP SIGNATURE-----' >>expect
 728test_expect_success GPG 'sign with a given key id' '
 729
 730        git tag -u committer@example.com -m "Another message" u-signed-tag &&
 731        get_tag_msg u-signed-tag >actual &&
 732        test_cmp expect actual
 733
 734'
 735
 736test_expect_success GPG 'sign with an unknown id (1)' '
 737
 738        test_must_fail git tag -u author@example.com \
 739                -m "Another message" o-signed-tag
 740
 741'
 742
 743test_expect_success GPG 'sign with an unknown id (2)' '
 744
 745        test_must_fail git tag -u DEADBEEF -m "Another message" o-signed-tag
 746
 747'
 748
 749cat >fakeeditor <<'EOF'
 750#!/bin/sh
 751test -n "$1" && exec >"$1"
 752echo A signed tag message
 753echo from a fake editor.
 754EOF
 755chmod +x fakeeditor
 756
 757get_tag_header implied-sign $commit commit $time >expect
 758./fakeeditor >>expect
 759echo '-----BEGIN PGP SIGNATURE-----' >>expect
 760test_expect_success GPG '-u implies signed tag' '
 761        GIT_EDITOR=./fakeeditor git tag -u CDDE430D implied-sign &&
 762        get_tag_msg implied-sign >actual &&
 763        test_cmp expect actual
 764'
 765
 766cat >sigmsgfile <<EOF
 767Another signed tag
 768message in a file.
 769EOF
 770get_tag_header file-signed-tag $commit commit $time >expect
 771cat sigmsgfile >>expect
 772echo '-----BEGIN PGP SIGNATURE-----' >>expect
 773test_expect_success GPG \
 774        'creating a signed tag with -F messagefile should succeed' '
 775        git tag -s -F sigmsgfile file-signed-tag &&
 776        get_tag_msg file-signed-tag >actual &&
 777        test_cmp expect actual
 778'
 779
 780cat >siginputmsg <<EOF
 781A signed tag message from
 782the standard input
 783EOF
 784get_tag_header stdin-signed-tag $commit commit $time >expect
 785cat siginputmsg >>expect
 786echo '-----BEGIN PGP SIGNATURE-----' >>expect
 787test_expect_success GPG 'creating a signed tag with -F - should succeed' '
 788        git tag -s -F - stdin-signed-tag <siginputmsg &&
 789        get_tag_msg stdin-signed-tag >actual &&
 790        test_cmp expect actual
 791'
 792
 793get_tag_header implied-annotate $commit commit $time >expect
 794./fakeeditor >>expect
 795echo '-----BEGIN PGP SIGNATURE-----' >>expect
 796test_expect_success GPG '-s implies annotated tag' '
 797        GIT_EDITOR=./fakeeditor git tag -s implied-annotate &&
 798        get_tag_msg implied-annotate >actual &&
 799        test_cmp expect actual
 800'
 801
 802get_tag_header forcesignannotated-implied-sign $commit commit $time >expect
 803echo "A message" >>expect
 804echo '-----BEGIN PGP SIGNATURE-----' >>expect
 805test_expect_success GPG \
 806        'git tag -s implied if configured with tag.forcesignannotated' \
 807        'test_config tag.forcesignannotated true &&
 808        git tag -m "A message" forcesignannotated-implied-sign &&
 809        get_tag_msg forcesignannotated-implied-sign >actual &&
 810        test_cmp expect actual
 811'
 812
 813test_expect_success GPG \
 814        'lightweight with no message when configured with tag.forcesignannotated' \
 815        'test_config tag.forcesignannotated true &&
 816        git tag forcesignannotated-lightweight &&
 817        tag_exists forcesignannotated-lightweight &&
 818        test_must_fail git tag -v forcesignannotated-no-message
 819'
 820
 821get_tag_header forcesignannotated-annotate $commit commit $time >expect
 822echo "A message" >>expect
 823test_expect_success GPG \
 824        'git tag -a disable configured tag.forcesignannotated' \
 825        'test_config tag.forcesignannotated true &&
 826        git tag -a -m "A message" forcesignannotated-annotate &&
 827        get_tag_msg forcesignannotated-annotate >actual &&
 828        test_cmp expect actual &&
 829        test_must_fail git tag -v forcesignannotated-annotate
 830'
 831
 832get_tag_header forcesignannotated-disabled $commit commit $time >expect
 833echo "A message" >>expect
 834echo '-----BEGIN PGP SIGNATURE-----' >>expect
 835test_expect_success GPG \
 836        'git tag --sign enable GPG sign' \
 837        'test_config tag.forcesignannotated false &&
 838        git tag --sign -m "A message" forcesignannotated-disabled &&
 839        get_tag_msg forcesignannotated-disabled >actual &&
 840        test_cmp expect actual
 841'
 842
 843test_expect_success GPG \
 844        'trying to create a signed tag with non-existing -F file should fail' '
 845        ! test -f nonexistingfile &&
 846        ! tag_exists nosigtag &&
 847        test_must_fail git tag -s -F nonexistingfile nosigtag &&
 848        ! tag_exists nosigtag
 849'
 850
 851test_expect_success GPG 'verifying a signed tag should succeed' \
 852        'git tag -v signed-tag'
 853
 854test_expect_success GPG 'verifying two signed tags in one command should succeed' \
 855        'git tag -v signed-tag file-signed-tag'
 856
 857test_expect_success GPG \
 858        'verifying many signed and non-signed tags should fail' '
 859        test_must_fail git tag -v signed-tag annotated-tag &&
 860        test_must_fail git tag -v file-annotated-tag file-signed-tag &&
 861        test_must_fail git tag -v annotated-tag \
 862                file-signed-tag file-annotated-tag &&
 863        test_must_fail git tag -v signed-tag annotated-tag file-signed-tag
 864'
 865
 866test_expect_success GPG 'verifying a forged tag should fail' '
 867        forged=$(git cat-file tag signed-tag |
 868                sed -e "s/signed-tag/forged-tag/" |
 869                git mktag) &&
 870        git tag forged-tag $forged &&
 871        test_must_fail git tag -v forged-tag
 872'
 873
 874test_expect_success 'verifying a proper tag with --format pass and format accordingly' '
 875        cat >expect <<-\EOF
 876        tagname : signed-tag
 877        EOF &&
 878        git tag -v --format="tagname : %(tag)" "signed-tag" >actual &&
 879        test_cmp expect actual
 880'
 881
 882test_expect_success 'verifying a forged tag with --format fail and format accordingly' '
 883        cat >expect <<-\EOF
 884        tagname : forged-tag
 885        EOF &&
 886        test_must_fail git tag -v --format="tagname : %(tag)" "forged-tag" >actual &&
 887        test_cmp expect actual
 888'
 889
 890# blank and empty messages for signed tags:
 891
 892get_tag_header empty-signed-tag $commit commit $time >expect
 893echo '-----BEGIN PGP SIGNATURE-----' >>expect
 894test_expect_success GPG \
 895        'creating a signed tag with an empty -m message should succeed' '
 896        git tag -s -m "" empty-signed-tag &&
 897        get_tag_msg empty-signed-tag >actual &&
 898        test_cmp expect actual &&
 899        git tag -v empty-signed-tag
 900'
 901
 902>sigemptyfile
 903get_tag_header emptyfile-signed-tag $commit commit $time >expect
 904echo '-----BEGIN PGP SIGNATURE-----' >>expect
 905test_expect_success GPG \
 906        'creating a signed tag with an empty -F messagefile should succeed' '
 907        git tag -s -F sigemptyfile emptyfile-signed-tag &&
 908        get_tag_msg emptyfile-signed-tag >actual &&
 909        test_cmp expect actual &&
 910        git tag -v emptyfile-signed-tag
 911'
 912
 913printf '\n\n  \n\t\nLeading blank lines\n' > sigblanksfile
 914printf '\n\t \t  \nRepeated blank lines\n' >>sigblanksfile
 915printf '\n\n\nTrailing spaces      \t  \n' >>sigblanksfile
 916printf '\nTrailing blank lines\n\n\t \n\n' >>sigblanksfile
 917get_tag_header blanks-signed-tag $commit commit $time >expect
 918cat >>expect <<EOF
 919Leading blank lines
 920
 921Repeated blank lines
 922
 923Trailing spaces
 924
 925Trailing blank lines
 926EOF
 927echo '-----BEGIN PGP SIGNATURE-----' >>expect
 928test_expect_success GPG \
 929        'extra blanks in the message for a signed tag should be removed' '
 930        git tag -s -F sigblanksfile blanks-signed-tag &&
 931        get_tag_msg blanks-signed-tag >actual &&
 932        test_cmp expect actual &&
 933        git tag -v blanks-signed-tag
 934'
 935
 936get_tag_header blank-signed-tag $commit commit $time >expect
 937echo '-----BEGIN PGP SIGNATURE-----' >>expect
 938test_expect_success GPG \
 939        'creating a signed tag with a blank -m message should succeed' '
 940        git tag -s -m "     " blank-signed-tag &&
 941        get_tag_msg blank-signed-tag >actual &&
 942        test_cmp expect actual &&
 943        git tag -v blank-signed-tag
 944'
 945
 946echo '     ' >sigblankfile
 947echo ''      >>sigblankfile
 948echo '  '    >>sigblankfile
 949get_tag_header blankfile-signed-tag $commit commit $time >expect
 950echo '-----BEGIN PGP SIGNATURE-----' >>expect
 951test_expect_success GPG \
 952        'creating a signed tag with blank -F file with spaces should succeed' '
 953        git tag -s -F sigblankfile blankfile-signed-tag &&
 954        get_tag_msg blankfile-signed-tag >actual &&
 955        test_cmp expect actual &&
 956        git tag -v blankfile-signed-tag
 957'
 958
 959printf '      ' >sigblanknonlfile
 960get_tag_header blanknonlfile-signed-tag $commit commit $time >expect
 961echo '-----BEGIN PGP SIGNATURE-----' >>expect
 962test_expect_success GPG \
 963        'creating a signed tag with spaces and no newline should succeed' '
 964        git tag -s -F sigblanknonlfile blanknonlfile-signed-tag &&
 965        get_tag_msg blanknonlfile-signed-tag >actual &&
 966        test_cmp expect actual &&
 967        git tag -v signed-tag
 968'
 969
 970# messages with commented lines for signed tags:
 971
 972cat >sigcommentsfile <<EOF
 973# A comment
 974
 975############
 976The message.
 977############
 978One line.
 979
 980
 981# commented lines
 982# commented lines
 983
 984Another line.
 985# comments
 986
 987Last line.
 988EOF
 989get_tag_header comments-signed-tag $commit commit $time >expect
 990cat >>expect <<EOF
 991The message.
 992One line.
 993
 994Another line.
 995
 996Last line.
 997EOF
 998echo '-----BEGIN PGP SIGNATURE-----' >>expect
 999test_expect_success GPG \
1000        'creating a signed tag with a -F file with #comments should succeed' '
1001        git tag -s -F sigcommentsfile comments-signed-tag &&
1002        get_tag_msg comments-signed-tag >actual &&
1003        test_cmp expect actual &&
1004        git tag -v comments-signed-tag
1005'
1006
1007get_tag_header comment-signed-tag $commit commit $time >expect
1008echo '-----BEGIN PGP SIGNATURE-----' >>expect
1009test_expect_success GPG \
1010        'creating a signed tag with #commented -m message should succeed' '
1011        git tag -s -m "#comment" comment-signed-tag &&
1012        get_tag_msg comment-signed-tag >actual &&
1013        test_cmp expect actual &&
1014        git tag -v comment-signed-tag
1015'
1016
1017echo '#comment' >sigcommentfile
1018echo ''         >>sigcommentfile
1019echo '####'     >>sigcommentfile
1020get_tag_header commentfile-signed-tag $commit commit $time >expect
1021echo '-----BEGIN PGP SIGNATURE-----' >>expect
1022test_expect_success GPG \
1023        'creating a signed tag with #commented -F messagefile should succeed' '
1024        git tag -s -F sigcommentfile commentfile-signed-tag &&
1025        get_tag_msg commentfile-signed-tag >actual &&
1026        test_cmp expect actual &&
1027        git tag -v commentfile-signed-tag
1028'
1029
1030printf '#comment' >sigcommentnonlfile
1031get_tag_header commentnonlfile-signed-tag $commit commit $time >expect
1032echo '-----BEGIN PGP SIGNATURE-----' >>expect
1033test_expect_success GPG \
1034        'creating a signed tag with a #comment and no newline should succeed' '
1035        git tag -s -F sigcommentnonlfile commentnonlfile-signed-tag &&
1036        get_tag_msg commentnonlfile-signed-tag >actual &&
1037        test_cmp expect actual &&
1038        git tag -v commentnonlfile-signed-tag
1039'
1040
1041# listing messages for signed tags:
1042
1043test_expect_success GPG \
1044        'listing the one-line message of a signed tag should succeed' '
1045        git tag -s -m "A message line signed" stag-one-line &&
1046
1047        echo "stag-one-line" >expect &&
1048        git tag -l | grep "^stag-one-line" >actual &&
1049        test_cmp expect actual &&
1050        git tag -n0 -l | grep "^stag-one-line" >actual &&
1051        test_cmp expect actual &&
1052        git tag -n0 -l stag-one-line >actual &&
1053        test_cmp expect actual &&
1054
1055        echo "stag-one-line   A message line signed" >expect &&
1056        git tag -n1 -l | grep "^stag-one-line" >actual &&
1057        test_cmp expect actual &&
1058        git tag -n -l | grep "^stag-one-line" >actual &&
1059        test_cmp expect actual &&
1060        git tag -n1 -l stag-one-line >actual &&
1061        test_cmp expect actual &&
1062        git tag -n2 -l stag-one-line >actual &&
1063        test_cmp expect actual &&
1064        git tag -n999 -l stag-one-line >actual &&
1065        test_cmp expect actual
1066'
1067
1068test_expect_success GPG \
1069        'listing the zero-lines message of a signed tag should succeed' '
1070        git tag -s -m "" stag-zero-lines &&
1071
1072        echo "stag-zero-lines" >expect &&
1073        git tag -l | grep "^stag-zero-lines" >actual &&
1074        test_cmp expect actual &&
1075        git tag -n0 -l | grep "^stag-zero-lines" >actual &&
1076        test_cmp expect actual &&
1077        git tag -n0 -l stag-zero-lines >actual &&
1078        test_cmp expect actual &&
1079
1080        echo "stag-zero-lines " >expect &&
1081        git tag -n1 -l | grep "^stag-zero-lines" >actual &&
1082        test_cmp expect actual &&
1083        git tag -n -l | grep "^stag-zero-lines" >actual &&
1084        test_cmp expect actual &&
1085        git tag -n1 -l stag-zero-lines >actual &&
1086        test_cmp expect actual &&
1087        git tag -n2 -l stag-zero-lines >actual &&
1088        test_cmp expect actual &&
1089        git tag -n999 -l stag-zero-lines >actual &&
1090        test_cmp expect actual
1091'
1092
1093echo 'stag line one' >sigtagmsg
1094echo 'stag line two' >>sigtagmsg
1095echo 'stag line three' >>sigtagmsg
1096test_expect_success GPG \
1097        'listing many message lines of a signed tag should succeed' '
1098        git tag -s -F sigtagmsg stag-lines &&
1099
1100        echo "stag-lines" >expect &&
1101        git tag -l | grep "^stag-lines" >actual &&
1102        test_cmp expect actual &&
1103        git tag -n0 -l | grep "^stag-lines" >actual &&
1104        test_cmp expect actual &&
1105        git tag -n0 -l stag-lines >actual &&
1106        test_cmp expect actual &&
1107
1108        echo "stag-lines      stag line one" >expect &&
1109        git tag -n1 -l | grep "^stag-lines" >actual &&
1110        test_cmp expect actual &&
1111        git tag -n -l | grep "^stag-lines" >actual &&
1112        test_cmp expect actual &&
1113        git tag -n1 -l stag-lines >actual &&
1114        test_cmp expect actual &&
1115
1116        echo "    stag line two" >>expect &&
1117        git tag -n2 -l | grep "^ *stag.line" >actual &&
1118        test_cmp expect actual &&
1119        git tag -n2 -l stag-lines >actual &&
1120        test_cmp expect actual &&
1121
1122        echo "    stag line three" >>expect &&
1123        git tag -n3 -l | grep "^ *stag.line" >actual &&
1124        test_cmp expect actual &&
1125        git tag -n3 -l stag-lines >actual &&
1126        test_cmp expect actual &&
1127        git tag -n4 -l | grep "^ *stag.line" >actual &&
1128        test_cmp expect actual &&
1129        git tag -n4 -l stag-lines >actual &&
1130        test_cmp expect actual &&
1131        git tag -n99 -l | grep "^ *stag.line" >actual &&
1132        test_cmp expect actual &&
1133        git tag -n99 -l stag-lines >actual &&
1134        test_cmp expect actual
1135'
1136
1137# tags pointing to objects different from commits:
1138
1139tree=$(git rev-parse HEAD^{tree})
1140blob=$(git rev-parse HEAD:foo)
1141tag=$(git rev-parse signed-tag 2>/dev/null)
1142
1143get_tag_header tree-signed-tag $tree tree $time >expect
1144echo "A message for a tree" >>expect
1145echo '-----BEGIN PGP SIGNATURE-----' >>expect
1146test_expect_success GPG \
1147        'creating a signed tag pointing to a tree should succeed' '
1148        git tag -s -m "A message for a tree" tree-signed-tag HEAD^{tree} &&
1149        get_tag_msg tree-signed-tag >actual &&
1150        test_cmp expect actual
1151'
1152
1153get_tag_header blob-signed-tag $blob blob $time >expect
1154echo "A message for a blob" >>expect
1155echo '-----BEGIN PGP SIGNATURE-----' >>expect
1156test_expect_success GPG \
1157        'creating a signed tag pointing to a blob should succeed' '
1158        git tag -s -m "A message for a blob" blob-signed-tag HEAD:foo &&
1159        get_tag_msg blob-signed-tag >actual &&
1160        test_cmp expect actual
1161'
1162
1163get_tag_header tag-signed-tag $tag tag $time >expect
1164echo "A message for another tag" >>expect
1165echo '-----BEGIN PGP SIGNATURE-----' >>expect
1166test_expect_success GPG \
1167        'creating a signed tag pointing to another tag should succeed' '
1168        git tag -s -m "A message for another tag" tag-signed-tag signed-tag &&
1169        get_tag_msg tag-signed-tag >actual &&
1170        test_cmp expect actual
1171'
1172
1173# usage with rfc1991 signatures
1174get_tag_header rfc1991-signed-tag $commit commit $time >expect
1175echo "RFC1991 signed tag" >>expect
1176echo '-----BEGIN PGP MESSAGE-----' >>expect
1177test_expect_success GPG,RFC1991 \
1178        'creating a signed tag with rfc1991' '
1179        echo "rfc1991" >gpghome/gpg.conf &&
1180        git tag -s -m "RFC1991 signed tag" rfc1991-signed-tag $commit &&
1181        get_tag_msg rfc1991-signed-tag >actual &&
1182        test_cmp expect actual
1183'
1184
1185cat >fakeeditor <<'EOF'
1186#!/bin/sh
1187cp "$1" actual
1188EOF
1189chmod +x fakeeditor
1190
1191test_expect_success GPG,RFC1991 \
1192        'reediting a signed tag body omits signature' '
1193        echo "rfc1991" >gpghome/gpg.conf &&
1194        echo "RFC1991 signed tag" >expect &&
1195        GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1196        test_cmp expect actual
1197'
1198
1199test_expect_success GPG,RFC1991 \
1200        'verifying rfc1991 signature' '
1201        echo "rfc1991" >gpghome/gpg.conf &&
1202        git tag -v rfc1991-signed-tag
1203'
1204
1205test_expect_success GPG,RFC1991 \
1206        'list tag with rfc1991 signature' '
1207        echo "rfc1991" >gpghome/gpg.conf &&
1208        echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1209        git tag -l -n1 rfc1991-signed-tag >actual &&
1210        test_cmp expect actual &&
1211        git tag -l -n2 rfc1991-signed-tag >actual &&
1212        test_cmp expect actual &&
1213        git tag -l -n999 rfc1991-signed-tag >actual &&
1214        test_cmp expect actual
1215'
1216
1217rm -f gpghome/gpg.conf
1218
1219test_expect_success GPG,RFC1991 \
1220        'verifying rfc1991 signature without --rfc1991' '
1221        git tag -v rfc1991-signed-tag
1222'
1223
1224test_expect_success GPG,RFC1991 \
1225        'list tag with rfc1991 signature without --rfc1991' '
1226        echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1227        git tag -l -n1 rfc1991-signed-tag >actual &&
1228        test_cmp expect actual &&
1229        git tag -l -n2 rfc1991-signed-tag >actual &&
1230        test_cmp expect actual &&
1231        git tag -l -n999 rfc1991-signed-tag >actual &&
1232        test_cmp expect actual
1233'
1234
1235test_expect_success GPG,RFC1991 \
1236        'reediting a signed tag body omits signature' '
1237        echo "RFC1991 signed tag" >expect &&
1238        GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1239        test_cmp expect actual
1240'
1241
1242# try to sign with bad user.signingkey
1243test_expect_success GPG \
1244        'git tag -s fails if gpg is misconfigured (bad key)' \
1245        'test_config user.signingkey BobTheMouse &&
1246        test_must_fail git tag -s -m tail tag-gpg-failure'
1247
1248# try to produce invalid signature
1249test_expect_success GPG \
1250        'git tag -s fails if gpg is misconfigured (bad signature format)' \
1251        'test_config gpg.program echo &&
1252         test_must_fail git tag -s -m tail tag-gpg-failure'
1253
1254
1255# try to verify without gpg:
1256
1257rm -rf gpghome
1258test_expect_success GPG \
1259        'verify signed tag fails when public key is not present' \
1260        'test_must_fail git tag -v signed-tag'
1261
1262test_expect_success \
1263        'git tag -a fails if tag annotation is empty' '
1264        ! (GIT_EDITOR=cat git tag -a initial-comment)
1265'
1266
1267test_expect_success \
1268        'message in editor has initial comment' '
1269        ! (GIT_EDITOR=cat git tag -a initial-comment > actual)
1270'
1271
1272test_expect_success 'message in editor has initial comment: first line' '
1273        # check the first line --- should be empty
1274        echo >first.expect &&
1275        sed -e 1q <actual >first.actual &&
1276        test_i18ncmp first.expect first.actual
1277'
1278
1279test_expect_success \
1280        'message in editor has initial comment: remainder' '
1281        # remove commented lines from the remainder -- should be empty
1282        >rest.expect &&
1283        sed -e 1d -e "/^#/d" <actual >rest.actual &&
1284        test_cmp rest.expect rest.actual
1285'
1286
1287get_tag_header reuse $commit commit $time >expect
1288echo "An annotation to be reused" >> expect
1289test_expect_success \
1290        'overwriting an annoted tag should use its previous body' '
1291        git tag -a -m "An annotation to be reused" reuse &&
1292        GIT_EDITOR=true git tag -f -a reuse &&
1293        get_tag_msg reuse >actual &&
1294        test_cmp expect actual
1295'
1296
1297test_expect_success 'filename for the message is relative to cwd' '
1298        mkdir subdir &&
1299        echo "Tag message in top directory" >msgfile-5 &&
1300        echo "Tag message in sub directory" >subdir/msgfile-5 &&
1301        (
1302                cd subdir &&
1303                git tag -a -F msgfile-5 tag-from-subdir
1304        ) &&
1305        git cat-file tag tag-from-subdir | grep "in sub directory"
1306'
1307
1308test_expect_success 'filename for the message is relative to cwd' '
1309        echo "Tag message in sub directory" >subdir/msgfile-6 &&
1310        (
1311                cd subdir &&
1312                git tag -a -F msgfile-6 tag-from-subdir-2
1313        ) &&
1314        git cat-file tag tag-from-subdir-2 | grep "in sub directory"
1315'
1316
1317# create a few more commits to test --contains
1318
1319hash1=$(git rev-parse HEAD)
1320
1321test_expect_success 'creating second commit and tag' '
1322        echo foo-2.0 >foo &&
1323        git add foo &&
1324        git commit -m second &&
1325        git tag v2.0
1326'
1327
1328hash2=$(git rev-parse HEAD)
1329
1330test_expect_success 'creating third commit without tag' '
1331        echo foo-dev >foo &&
1332        git add foo &&
1333        git commit -m third
1334'
1335
1336hash3=$(git rev-parse HEAD)
1337
1338# simple linear checks of --continue
1339
1340cat > expected <<EOF
1341v0.2.1
1342v1.0
1343v1.0.1
1344v1.1.3
1345v2.0
1346EOF
1347
1348test_expect_success 'checking that first commit is in all tags (hash)' "
1349        git tag -l --contains $hash1 v* >actual &&
1350        test_cmp expected actual
1351"
1352
1353# other ways of specifying the commit
1354test_expect_success 'checking that first commit is in all tags (tag)' "
1355        git tag -l --contains v1.0 v* >actual &&
1356        test_cmp expected actual
1357"
1358
1359test_expect_success 'checking that first commit is in all tags (relative)' "
1360        git tag -l --contains HEAD~2 v* >actual &&
1361        test_cmp expected actual
1362"
1363
1364cat > expected <<EOF
1365v2.0
1366EOF
1367
1368test_expect_success 'checking that second commit only has one tag' "
1369        git tag -l --contains $hash2 v* >actual &&
1370        test_cmp expected actual
1371"
1372
1373
1374cat > expected <<EOF
1375EOF
1376
1377test_expect_success 'checking that third commit has no tags' "
1378        git tag -l --contains $hash3 v* >actual &&
1379        test_cmp expected actual
1380"
1381
1382# how about a simple merge?
1383
1384test_expect_success 'creating simple branch' '
1385        git branch stable v2.0 &&
1386        git checkout stable &&
1387        echo foo-3.0 > foo &&
1388        git commit foo -m fourth &&
1389        git tag v3.0
1390'
1391
1392hash4=$(git rev-parse HEAD)
1393
1394cat > expected <<EOF
1395v3.0
1396EOF
1397
1398test_expect_success 'checking that branch head only has one tag' "
1399        git tag -l --contains $hash4 v* >actual &&
1400        test_cmp expected actual
1401"
1402
1403test_expect_success 'merging original branch into this branch' '
1404        git merge --strategy=ours master &&
1405        git tag v4.0
1406'
1407
1408cat > expected <<EOF
1409v4.0
1410EOF
1411
1412test_expect_success 'checking that original branch head has one tag now' "
1413        git tag -l --contains $hash3 v* >actual &&
1414        test_cmp expected actual
1415"
1416
1417cat > expected <<EOF
1418v0.2.1
1419v1.0
1420v1.0.1
1421v1.1.3
1422v2.0
1423v3.0
1424v4.0
1425EOF
1426
1427test_expect_success 'checking that initial commit is in all tags' "
1428        git tag -l --contains $hash1 v* >actual &&
1429        test_cmp expected actual
1430"
1431
1432# mixing modes and options:
1433
1434test_expect_success 'mixing incompatibles modes and options is forbidden' '
1435        test_must_fail git tag -a &&
1436        test_must_fail git tag -l -v &&
1437        test_must_fail git tag -n 100 &&
1438        test_must_fail git tag -l -m msg &&
1439        test_must_fail git tag -l -F some file &&
1440        test_must_fail git tag -v -s
1441'
1442
1443# check points-at
1444
1445test_expect_success '--points-at cannot be used in non-list mode' '
1446        test_must_fail git tag --points-at=v4.0 foo
1447'
1448
1449test_expect_success '--points-at finds lightweight tags' '
1450        echo v4.0 >expect &&
1451        git tag --points-at v4.0 >actual &&
1452        test_cmp expect actual
1453'
1454
1455test_expect_success '--points-at finds annotated tags of commits' '
1456        git tag -m "v4.0, annotated" annotated-v4.0 v4.0 &&
1457        echo annotated-v4.0 >expect &&
1458        git tag -l --points-at v4.0 "annotated*" >actual &&
1459        test_cmp expect actual
1460'
1461
1462test_expect_success '--points-at finds annotated tags of tags' '
1463        git tag -m "describing the v4.0 tag object" \
1464                annotated-again-v4.0 annotated-v4.0 &&
1465        cat >expect <<-\EOF &&
1466        annotated-again-v4.0
1467        annotated-v4.0
1468        EOF
1469        git tag --points-at=annotated-v4.0 >actual &&
1470        test_cmp expect actual
1471'
1472
1473test_expect_success 'multiple --points-at are OR-ed together' '
1474        cat >expect <<-\EOF &&
1475        v2.0
1476        v3.0
1477        EOF
1478        git tag --points-at=v2.0 --points-at=v3.0 >actual &&
1479        test_cmp expect actual
1480'
1481
1482test_expect_success 'lexical sort' '
1483        git tag foo1.3 &&
1484        git tag foo1.6 &&
1485        git tag foo1.10 &&
1486        git tag -l --sort=refname "foo*" >actual &&
1487        cat >expect <<-\EOF &&
1488        foo1.10
1489        foo1.3
1490        foo1.6
1491        EOF
1492        test_cmp expect actual
1493'
1494
1495test_expect_success 'version sort' '
1496        git tag -l --sort=version:refname "foo*" >actual &&
1497        cat >expect <<-\EOF &&
1498        foo1.3
1499        foo1.6
1500        foo1.10
1501        EOF
1502        test_cmp expect actual
1503'
1504
1505test_expect_success 'reverse version sort' '
1506        git tag -l --sort=-version:refname "foo*" >actual &&
1507        cat >expect <<-\EOF &&
1508        foo1.10
1509        foo1.6
1510        foo1.3
1511        EOF
1512        test_cmp expect actual
1513'
1514
1515test_expect_success 'reverse lexical sort' '
1516        git tag -l --sort=-refname "foo*" >actual &&
1517        cat >expect <<-\EOF &&
1518        foo1.6
1519        foo1.3
1520        foo1.10
1521        EOF
1522        test_cmp expect actual
1523'
1524
1525test_expect_success 'configured lexical sort' '
1526        test_config tag.sort "v:refname" &&
1527        git tag -l "foo*" >actual &&
1528        cat >expect <<-\EOF &&
1529        foo1.3
1530        foo1.6
1531        foo1.10
1532        EOF
1533        test_cmp expect actual
1534'
1535
1536test_expect_success 'option override configured sort' '
1537        test_config tag.sort "v:refname" &&
1538        git tag -l --sort=-refname "foo*" >actual &&
1539        cat >expect <<-\EOF &&
1540        foo1.6
1541        foo1.3
1542        foo1.10
1543        EOF
1544        test_cmp expect actual
1545'
1546
1547test_expect_success 'invalid sort parameter on command line' '
1548        test_must_fail git tag -l --sort=notvalid "foo*" >actual
1549'
1550
1551test_expect_success 'invalid sort parameter in configuratoin' '
1552        test_config tag.sort "v:notvalid" &&
1553        test_must_fail git tag -l "foo*"
1554'
1555
1556test_expect_success 'version sort with prerelease reordering' '
1557        test_config versionsort.prereleaseSuffix -rc &&
1558        git tag foo1.6-rc1 &&
1559        git tag foo1.6-rc2 &&
1560        git tag -l --sort=version:refname "foo*" >actual &&
1561        cat >expect <<-\EOF &&
1562        foo1.3
1563        foo1.6-rc1
1564        foo1.6-rc2
1565        foo1.6
1566        foo1.10
1567        EOF
1568        test_cmp expect actual
1569'
1570
1571test_expect_success 'reverse version sort with prerelease reordering' '
1572        test_config versionsort.prereleaseSuffix -rc &&
1573        git tag -l --sort=-version:refname "foo*" >actual &&
1574        cat >expect <<-\EOF &&
1575        foo1.10
1576        foo1.6
1577        foo1.6-rc2
1578        foo1.6-rc1
1579        foo1.3
1580        EOF
1581        test_cmp expect actual
1582'
1583
1584test_expect_success 'version sort with prerelease reordering and common leading character' '
1585        test_config versionsort.prereleaseSuffix -before &&
1586        git tag foo1.7-before1 &&
1587        git tag foo1.7 &&
1588        git tag foo1.7-after1 &&
1589        git tag -l --sort=version:refname "foo1.7*" >actual &&
1590        cat >expect <<-\EOF &&
1591        foo1.7-before1
1592        foo1.7
1593        foo1.7-after1
1594        EOF
1595        test_cmp expect actual
1596'
1597
1598test_expect_success 'version sort with prerelease reordering, multiple suffixes and common leading character' '
1599        test_config versionsort.prereleaseSuffix -before &&
1600        git config --add versionsort.prereleaseSuffix -after &&
1601        git tag -l --sort=version:refname "foo1.7*" >actual &&
1602        cat >expect <<-\EOF &&
1603        foo1.7-before1
1604        foo1.7-after1
1605        foo1.7
1606        EOF
1607        test_cmp expect actual
1608'
1609
1610test_expect_success 'version sort with prerelease reordering, multiple suffixes match the same tag' '
1611        test_config versionsort.prereleaseSuffix -bar &&
1612        git config --add versionsort.prereleaseSuffix -foo-baz &&
1613        git config --add versionsort.prereleaseSuffix -foo-bar &&
1614        git tag foo1.8-foo-bar &&
1615        git tag foo1.8-foo-baz &&
1616        git tag foo1.8 &&
1617        git tag -l --sort=version:refname "foo1.8*" >actual &&
1618        cat >expect <<-\EOF &&
1619        foo1.8-foo-baz
1620        foo1.8-foo-bar
1621        foo1.8
1622        EOF
1623        test_cmp expect actual
1624'
1625
1626test_expect_success 'version sort with prerelease reordering, multiple suffixes match starting at the same position' '
1627        test_config versionsort.prereleaseSuffix -pre &&
1628        git config --add versionsort.prereleaseSuffix -prerelease &&
1629        git tag foo1.9-pre1 &&
1630        git tag foo1.9-pre2 &&
1631        git tag foo1.9-prerelease1 &&
1632        git tag -l --sort=version:refname "foo1.9*" >actual &&
1633        cat >expect <<-\EOF &&
1634        foo1.9-pre1
1635        foo1.9-pre2
1636        foo1.9-prerelease1
1637        EOF
1638        test_cmp expect actual
1639'
1640
1641test_expect_success 'version sort with general suffix reordering' '
1642        test_config versionsort.suffix -alpha &&
1643        git config --add versionsort.suffix -beta &&
1644        git config --add versionsort.suffix ""  &&
1645        git config --add versionsort.suffix -gamma &&
1646        git config --add versionsort.suffix -delta &&
1647        git tag foo1.10-alpha &&
1648        git tag foo1.10-beta &&
1649        git tag foo1.10-gamma &&
1650        git tag foo1.10-delta &&
1651        git tag foo1.10-unlisted-suffix &&
1652        git tag -l --sort=version:refname "foo1.10*" >actual &&
1653        cat >expect <<-\EOF &&
1654        foo1.10-alpha
1655        foo1.10-beta
1656        foo1.10
1657        foo1.10-unlisted-suffix
1658        foo1.10-gamma
1659        foo1.10-delta
1660        EOF
1661        test_cmp expect actual
1662'
1663
1664test_expect_success 'versionsort.suffix overrides versionsort.prereleaseSuffix' '
1665        test_config versionsort.suffix -before &&
1666        test_config versionsort.prereleaseSuffix -after &&
1667        git tag -l --sort=version:refname "foo1.7*" >actual &&
1668        cat >expect <<-\EOF &&
1669        foo1.7-before1
1670        foo1.7
1671        foo1.7-after1
1672        EOF
1673        test_cmp expect actual
1674'
1675
1676test_expect_success 'version sort with very long prerelease suffix' '
1677        test_config versionsort.prereleaseSuffix -very-looooooooooooooooooooooooong-prerelease-suffix &&
1678        git tag -l --sort=version:refname
1679'
1680
1681run_with_limited_stack () {
1682        (ulimit -s 128 && "$@")
1683}
1684
1685test_lazy_prereq ULIMIT_STACK_SIZE 'run_with_limited_stack true'
1686
1687# we require ulimit, this excludes Windows
1688test_expect_success ULIMIT_STACK_SIZE '--contains works in a deep repo' '
1689        >expect &&
1690        i=1 &&
1691        while test $i -lt 8000
1692        do
1693                echo "commit refs/heads/master
1694committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
1695data <<EOF
1696commit #$i
1697EOF"
1698                test $i = 1 && echo "from refs/heads/master^0"
1699                i=$(($i + 1))
1700        done | git fast-import &&
1701        git checkout master &&
1702        git tag far-far-away HEAD^ &&
1703        run_with_limited_stack git tag --contains HEAD >actual &&
1704        test_cmp expect actual
1705'
1706
1707test_expect_success '--format should list tags as per format given' '
1708        cat >expect <<-\EOF &&
1709        refname : refs/tags/v1.0
1710        refname : refs/tags/v1.0.1
1711        refname : refs/tags/v1.1.3
1712        EOF
1713        git tag -l --format="refname : %(refname)" "v1*" >actual &&
1714        test_cmp expect actual
1715'
1716
1717test_expect_success 'setup --merged test tags' '
1718        git tag mergetest-1 HEAD~2 &&
1719        git tag mergetest-2 HEAD~1 &&
1720        git tag mergetest-3 HEAD
1721'
1722
1723test_expect_success '--merged cannot be used in non-list mode' '
1724        test_must_fail git tag --merged=mergetest-2 foo
1725'
1726
1727test_expect_success '--merged shows merged tags' '
1728        cat >expect <<-\EOF &&
1729        mergetest-1
1730        mergetest-2
1731        EOF
1732        git tag -l --merged=mergetest-2 mergetest-* >actual &&
1733        test_cmp expect actual
1734'
1735
1736test_expect_success '--no-merged show unmerged tags' '
1737        cat >expect <<-\EOF &&
1738        mergetest-3
1739        EOF
1740        git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
1741        test_cmp expect actual
1742'
1743
1744test_expect_success 'ambiguous branch/tags not marked' '
1745        git tag ambiguous &&
1746        git branch ambiguous &&
1747        echo ambiguous >expect &&
1748        git tag -l ambiguous >actual &&
1749        test_cmp expect actual
1750'
1751
1752test_done