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