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