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