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