t / t7004-tag.shon commit Merge branch 'rs/p5302-create-repositories-before-tests' (cf36a4d)
   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
 882test_expect_success 'verifying a proper tag with --format pass and format accordingly' '
 883        cat >expect <<-\EOF
 884        tagname : signed-tag
 885        EOF &&
 886        git tag -v --format="tagname : %(tag)" "signed-tag" >actual &&
 887        test_cmp expect actual
 888'
 889
 890test_expect_success 'verifying a forged tag with --format fail and format accordingly' '
 891        cat >expect <<-\EOF
 892        tagname : forged-tag
 893        EOF &&
 894        test_must_fail git tag -v --format="tagname : %(tag)" "forged-tag" >actual &&
 895        test_cmp expect actual
 896'
 897
 898# blank and empty messages for signed tags:
 899
 900get_tag_header empty-signed-tag $commit commit $time >expect
 901echo '-----BEGIN PGP SIGNATURE-----' >>expect
 902test_expect_success GPG \
 903        'creating a signed tag with an empty -m message should succeed' '
 904        git tag -s -m "" empty-signed-tag &&
 905        get_tag_msg empty-signed-tag >actual &&
 906        test_cmp expect actual &&
 907        git tag -v empty-signed-tag
 908'
 909
 910>sigemptyfile
 911get_tag_header emptyfile-signed-tag $commit commit $time >expect
 912echo '-----BEGIN PGP SIGNATURE-----' >>expect
 913test_expect_success GPG \
 914        'creating a signed tag with an empty -F messagefile should succeed' '
 915        git tag -s -F sigemptyfile emptyfile-signed-tag &&
 916        get_tag_msg emptyfile-signed-tag >actual &&
 917        test_cmp expect actual &&
 918        git tag -v emptyfile-signed-tag
 919'
 920
 921printf '\n\n  \n\t\nLeading blank lines\n' > sigblanksfile
 922printf '\n\t \t  \nRepeated blank lines\n' >>sigblanksfile
 923printf '\n\n\nTrailing spaces      \t  \n' >>sigblanksfile
 924printf '\nTrailing blank lines\n\n\t \n\n' >>sigblanksfile
 925get_tag_header blanks-signed-tag $commit commit $time >expect
 926cat >>expect <<EOF
 927Leading blank lines
 928
 929Repeated blank lines
 930
 931Trailing spaces
 932
 933Trailing blank lines
 934EOF
 935echo '-----BEGIN PGP SIGNATURE-----' >>expect
 936test_expect_success GPG \
 937        'extra blanks in the message for a signed tag should be removed' '
 938        git tag -s -F sigblanksfile blanks-signed-tag &&
 939        get_tag_msg blanks-signed-tag >actual &&
 940        test_cmp expect actual &&
 941        git tag -v blanks-signed-tag
 942'
 943
 944get_tag_header blank-signed-tag $commit commit $time >expect
 945echo '-----BEGIN PGP SIGNATURE-----' >>expect
 946test_expect_success GPG \
 947        'creating a signed tag with a blank -m message should succeed' '
 948        git tag -s -m "     " blank-signed-tag &&
 949        get_tag_msg blank-signed-tag >actual &&
 950        test_cmp expect actual &&
 951        git tag -v blank-signed-tag
 952'
 953
 954echo '     ' >sigblankfile
 955echo ''      >>sigblankfile
 956echo '  '    >>sigblankfile
 957get_tag_header blankfile-signed-tag $commit commit $time >expect
 958echo '-----BEGIN PGP SIGNATURE-----' >>expect
 959test_expect_success GPG \
 960        'creating a signed tag with blank -F file with spaces should succeed' '
 961        git tag -s -F sigblankfile blankfile-signed-tag &&
 962        get_tag_msg blankfile-signed-tag >actual &&
 963        test_cmp expect actual &&
 964        git tag -v blankfile-signed-tag
 965'
 966
 967printf '      ' >sigblanknonlfile
 968get_tag_header blanknonlfile-signed-tag $commit commit $time >expect
 969echo '-----BEGIN PGP SIGNATURE-----' >>expect
 970test_expect_success GPG \
 971        'creating a signed tag with spaces and no newline should succeed' '
 972        git tag -s -F sigblanknonlfile blanknonlfile-signed-tag &&
 973        get_tag_msg blanknonlfile-signed-tag >actual &&
 974        test_cmp expect actual &&
 975        git tag -v signed-tag
 976'
 977
 978# messages with commented lines for signed tags:
 979
 980cat >sigcommentsfile <<EOF
 981# A comment
 982
 983############
 984The message.
 985############
 986One line.
 987
 988
 989# commented lines
 990# commented lines
 991
 992Another line.
 993# comments
 994
 995Last line.
 996EOF
 997get_tag_header comments-signed-tag $commit commit $time >expect
 998cat >>expect <<EOF
 999The message.
1000One line.
1001
1002Another line.
1003
1004Last line.
1005EOF
1006echo '-----BEGIN PGP SIGNATURE-----' >>expect
1007test_expect_success GPG \
1008        'creating a signed tag with a -F file with #comments should succeed' '
1009        git tag -s -F sigcommentsfile comments-signed-tag &&
1010        get_tag_msg comments-signed-tag >actual &&
1011        test_cmp expect actual &&
1012        git tag -v comments-signed-tag
1013'
1014
1015get_tag_header comment-signed-tag $commit commit $time >expect
1016echo '-----BEGIN PGP SIGNATURE-----' >>expect
1017test_expect_success GPG \
1018        'creating a signed tag with #commented -m message should succeed' '
1019        git tag -s -m "#comment" comment-signed-tag &&
1020        get_tag_msg comment-signed-tag >actual &&
1021        test_cmp expect actual &&
1022        git tag -v comment-signed-tag
1023'
1024
1025echo '#comment' >sigcommentfile
1026echo ''         >>sigcommentfile
1027echo '####'     >>sigcommentfile
1028get_tag_header commentfile-signed-tag $commit commit $time >expect
1029echo '-----BEGIN PGP SIGNATURE-----' >>expect
1030test_expect_success GPG \
1031        'creating a signed tag with #commented -F messagefile should succeed' '
1032        git tag -s -F sigcommentfile commentfile-signed-tag &&
1033        get_tag_msg commentfile-signed-tag >actual &&
1034        test_cmp expect actual &&
1035        git tag -v commentfile-signed-tag
1036'
1037
1038printf '#comment' >sigcommentnonlfile
1039get_tag_header commentnonlfile-signed-tag $commit commit $time >expect
1040echo '-----BEGIN PGP SIGNATURE-----' >>expect
1041test_expect_success GPG \
1042        'creating a signed tag with a #comment and no newline should succeed' '
1043        git tag -s -F sigcommentnonlfile commentnonlfile-signed-tag &&
1044        get_tag_msg commentnonlfile-signed-tag >actual &&
1045        test_cmp expect actual &&
1046        git tag -v commentnonlfile-signed-tag
1047'
1048
1049# listing messages for signed tags:
1050
1051test_expect_success GPG \
1052        'listing the one-line message of a signed tag should succeed' '
1053        git tag -s -m "A message line signed" stag-one-line &&
1054
1055        echo "stag-one-line" >expect &&
1056        git tag -l | grep "^stag-one-line" >actual &&
1057        test_cmp expect actual &&
1058        git tag -n0 -l | grep "^stag-one-line" >actual &&
1059        test_cmp expect actual &&
1060        git tag -n0 -l stag-one-line >actual &&
1061        test_cmp expect actual &&
1062
1063        echo "stag-one-line   A message line signed" >expect &&
1064        git tag -n1 -l | grep "^stag-one-line" >actual &&
1065        test_cmp expect actual &&
1066        git tag -n -l | grep "^stag-one-line" >actual &&
1067        test_cmp expect actual &&
1068        git tag -n1 -l stag-one-line >actual &&
1069        test_cmp expect actual &&
1070        git tag -n2 -l stag-one-line >actual &&
1071        test_cmp expect actual &&
1072        git tag -n999 -l stag-one-line >actual &&
1073        test_cmp expect actual
1074'
1075
1076test_expect_success GPG \
1077        'listing the zero-lines message of a signed tag should succeed' '
1078        git tag -s -m "" stag-zero-lines &&
1079
1080        echo "stag-zero-lines" >expect &&
1081        git tag -l | grep "^stag-zero-lines" >actual &&
1082        test_cmp expect actual &&
1083        git tag -n0 -l | grep "^stag-zero-lines" >actual &&
1084        test_cmp expect actual &&
1085        git tag -n0 -l stag-zero-lines >actual &&
1086        test_cmp expect actual &&
1087
1088        echo "stag-zero-lines " >expect &&
1089        git tag -n1 -l | grep "^stag-zero-lines" >actual &&
1090        test_cmp expect actual &&
1091        git tag -n -l | grep "^stag-zero-lines" >actual &&
1092        test_cmp expect actual &&
1093        git tag -n1 -l stag-zero-lines >actual &&
1094        test_cmp expect actual &&
1095        git tag -n2 -l stag-zero-lines >actual &&
1096        test_cmp expect actual &&
1097        git tag -n999 -l stag-zero-lines >actual &&
1098        test_cmp expect actual
1099'
1100
1101echo 'stag line one' >sigtagmsg
1102echo 'stag line two' >>sigtagmsg
1103echo 'stag line three' >>sigtagmsg
1104test_expect_success GPG \
1105        'listing many message lines of a signed tag should succeed' '
1106        git tag -s -F sigtagmsg stag-lines &&
1107
1108        echo "stag-lines" >expect &&
1109        git tag -l | grep "^stag-lines" >actual &&
1110        test_cmp expect actual &&
1111        git tag -n0 -l | grep "^stag-lines" >actual &&
1112        test_cmp expect actual &&
1113        git tag -n0 -l stag-lines >actual &&
1114        test_cmp expect actual &&
1115
1116        echo "stag-lines      stag line one" >expect &&
1117        git tag -n1 -l | grep "^stag-lines" >actual &&
1118        test_cmp expect actual &&
1119        git tag -n -l | grep "^stag-lines" >actual &&
1120        test_cmp expect actual &&
1121        git tag -n1 -l stag-lines >actual &&
1122        test_cmp expect actual &&
1123
1124        echo "    stag line two" >>expect &&
1125        git tag -n2 -l | grep "^ *stag.line" >actual &&
1126        test_cmp expect actual &&
1127        git tag -n2 -l stag-lines >actual &&
1128        test_cmp expect actual &&
1129
1130        echo "    stag line three" >>expect &&
1131        git tag -n3 -l | grep "^ *stag.line" >actual &&
1132        test_cmp expect actual &&
1133        git tag -n3 -l stag-lines >actual &&
1134        test_cmp expect actual &&
1135        git tag -n4 -l | grep "^ *stag.line" >actual &&
1136        test_cmp expect actual &&
1137        git tag -n4 -l stag-lines >actual &&
1138        test_cmp expect actual &&
1139        git tag -n99 -l | grep "^ *stag.line" >actual &&
1140        test_cmp expect actual &&
1141        git tag -n99 -l stag-lines >actual &&
1142        test_cmp expect actual
1143'
1144
1145# tags pointing to objects different from commits:
1146
1147tree=$(git rev-parse HEAD^{tree})
1148blob=$(git rev-parse HEAD:foo)
1149tag=$(git rev-parse signed-tag 2>/dev/null)
1150
1151get_tag_header tree-signed-tag $tree tree $time >expect
1152echo "A message for a tree" >>expect
1153echo '-----BEGIN PGP SIGNATURE-----' >>expect
1154test_expect_success GPG \
1155        'creating a signed tag pointing to a tree should succeed' '
1156        git tag -s -m "A message for a tree" tree-signed-tag HEAD^{tree} &&
1157        get_tag_msg tree-signed-tag >actual &&
1158        test_cmp expect actual
1159'
1160
1161get_tag_header blob-signed-tag $blob blob $time >expect
1162echo "A message for a blob" >>expect
1163echo '-----BEGIN PGP SIGNATURE-----' >>expect
1164test_expect_success GPG \
1165        'creating a signed tag pointing to a blob should succeed' '
1166        git tag -s -m "A message for a blob" blob-signed-tag HEAD:foo &&
1167        get_tag_msg blob-signed-tag >actual &&
1168        test_cmp expect actual
1169'
1170
1171get_tag_header tag-signed-tag $tag tag $time >expect
1172echo "A message for another tag" >>expect
1173echo '-----BEGIN PGP SIGNATURE-----' >>expect
1174test_expect_success GPG \
1175        'creating a signed tag pointing to another tag should succeed' '
1176        git tag -s -m "A message for another tag" tag-signed-tag signed-tag &&
1177        get_tag_msg tag-signed-tag >actual &&
1178        test_cmp expect actual
1179'
1180
1181# usage with rfc1991 signatures
1182get_tag_header rfc1991-signed-tag $commit commit $time >expect
1183echo "RFC1991 signed tag" >>expect
1184echo '-----BEGIN PGP MESSAGE-----' >>expect
1185test_expect_success GPG,RFC1991 \
1186        'creating a signed tag with rfc1991' '
1187        echo "rfc1991" >gpghome/gpg.conf &&
1188        git tag -s -m "RFC1991 signed tag" rfc1991-signed-tag $commit &&
1189        get_tag_msg rfc1991-signed-tag >actual &&
1190        test_cmp expect actual
1191'
1192
1193cat >fakeeditor <<'EOF'
1194#!/bin/sh
1195cp "$1" actual
1196EOF
1197chmod +x fakeeditor
1198
1199test_expect_success GPG,RFC1991 \
1200        'reediting a signed tag body omits signature' '
1201        echo "rfc1991" >gpghome/gpg.conf &&
1202        echo "RFC1991 signed tag" >expect &&
1203        GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1204        test_cmp expect actual
1205'
1206
1207test_expect_success GPG,RFC1991 \
1208        'verifying rfc1991 signature' '
1209        echo "rfc1991" >gpghome/gpg.conf &&
1210        git tag -v rfc1991-signed-tag
1211'
1212
1213test_expect_success GPG,RFC1991 \
1214        'list tag with rfc1991 signature' '
1215        echo "rfc1991" >gpghome/gpg.conf &&
1216        echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1217        git tag -l -n1 rfc1991-signed-tag >actual &&
1218        test_cmp expect actual &&
1219        git tag -l -n2 rfc1991-signed-tag >actual &&
1220        test_cmp expect actual &&
1221        git tag -l -n999 rfc1991-signed-tag >actual &&
1222        test_cmp expect actual
1223'
1224
1225rm -f gpghome/gpg.conf
1226
1227test_expect_success GPG,RFC1991 \
1228        'verifying rfc1991 signature without --rfc1991' '
1229        git tag -v rfc1991-signed-tag
1230'
1231
1232test_expect_success GPG,RFC1991 \
1233        'list tag with rfc1991 signature without --rfc1991' '
1234        echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1235        git tag -l -n1 rfc1991-signed-tag >actual &&
1236        test_cmp expect actual &&
1237        git tag -l -n2 rfc1991-signed-tag >actual &&
1238        test_cmp expect actual &&
1239        git tag -l -n999 rfc1991-signed-tag >actual &&
1240        test_cmp expect actual
1241'
1242
1243test_expect_success GPG,RFC1991 \
1244        'reediting a signed tag body omits signature' '
1245        echo "RFC1991 signed tag" >expect &&
1246        GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1247        test_cmp expect actual
1248'
1249
1250# try to sign with bad user.signingkey
1251test_expect_success GPG \
1252        'git tag -s fails if gpg is misconfigured (bad key)' \
1253        'test_config user.signingkey BobTheMouse &&
1254        test_must_fail git tag -s -m tail tag-gpg-failure'
1255
1256# try to produce invalid signature
1257test_expect_success GPG \
1258        'git tag -s fails if gpg is misconfigured (bad signature format)' \
1259        'test_config gpg.program echo &&
1260         test_must_fail git tag -s -m tail tag-gpg-failure'
1261
1262
1263# try to verify without gpg:
1264
1265rm -rf gpghome
1266test_expect_success GPG \
1267        'verify signed tag fails when public key is not present' \
1268        'test_must_fail git tag -v signed-tag'
1269
1270test_expect_success \
1271        'git tag -a fails if tag annotation is empty' '
1272        ! (GIT_EDITOR=cat git tag -a initial-comment)
1273'
1274
1275test_expect_success \
1276        'message in editor has initial comment' '
1277        ! (GIT_EDITOR=cat git tag -a initial-comment > actual)
1278'
1279
1280test_expect_success 'message in editor has initial comment: first line' '
1281        # check the first line --- should be empty
1282        echo >first.expect &&
1283        sed -e 1q <actual >first.actual &&
1284        test_i18ncmp first.expect first.actual
1285'
1286
1287test_expect_success \
1288        'message in editor has initial comment: remainder' '
1289        # remove commented lines from the remainder -- should be empty
1290        >rest.expect &&
1291        sed -e 1d -e "/^#/d" <actual >rest.actual &&
1292        test_cmp rest.expect rest.actual
1293'
1294
1295get_tag_header reuse $commit commit $time >expect
1296echo "An annotation to be reused" >> expect
1297test_expect_success \
1298        'overwriting an annoted tag should use its previous body' '
1299        git tag -a -m "An annotation to be reused" reuse &&
1300        GIT_EDITOR=true git tag -f -a reuse &&
1301        get_tag_msg reuse >actual &&
1302        test_cmp expect actual
1303'
1304
1305test_expect_success 'filename for the message is relative to cwd' '
1306        mkdir subdir &&
1307        echo "Tag message in top directory" >msgfile-5 &&
1308        echo "Tag message in sub directory" >subdir/msgfile-5 &&
1309        (
1310                cd subdir &&
1311                git tag -a -F msgfile-5 tag-from-subdir
1312        ) &&
1313        git cat-file tag tag-from-subdir | grep "in sub directory"
1314'
1315
1316test_expect_success 'filename for the message is relative to cwd' '
1317        echo "Tag message in sub directory" >subdir/msgfile-6 &&
1318        (
1319                cd subdir &&
1320                git tag -a -F msgfile-6 tag-from-subdir-2
1321        ) &&
1322        git cat-file tag tag-from-subdir-2 | grep "in sub directory"
1323'
1324
1325# create a few more commits to test --contains
1326
1327hash1=$(git rev-parse HEAD)
1328
1329test_expect_success 'creating second commit and tag' '
1330        echo foo-2.0 >foo &&
1331        git add foo &&
1332        git commit -m second &&
1333        git tag v2.0
1334'
1335
1336hash2=$(git rev-parse HEAD)
1337
1338test_expect_success 'creating third commit without tag' '
1339        echo foo-dev >foo &&
1340        git add foo &&
1341        git commit -m third
1342'
1343
1344hash3=$(git rev-parse HEAD)
1345
1346# simple linear checks of --continue
1347
1348cat > expected <<EOF
1349v0.2.1
1350v1.0
1351v1.0.1
1352v1.1.3
1353v2.0
1354EOF
1355
1356test_expect_success 'checking that first commit is in all tags (hash)' "
1357        git tag -l --contains $hash1 v* >actual &&
1358        test_cmp expected actual
1359"
1360
1361# other ways of specifying the commit
1362test_expect_success 'checking that first commit is in all tags (tag)' "
1363        git tag -l --contains v1.0 v* >actual &&
1364        test_cmp expected actual
1365"
1366
1367test_expect_success 'checking that first commit is in all tags (relative)' "
1368        git tag -l --contains HEAD~2 v* >actual &&
1369        test_cmp expected actual
1370"
1371
1372cat > expected <<EOF
1373v2.0
1374EOF
1375
1376test_expect_success 'checking that second commit only has one tag' "
1377        git tag -l --contains $hash2 v* >actual &&
1378        test_cmp expected actual
1379"
1380
1381
1382cat > expected <<EOF
1383EOF
1384
1385test_expect_success 'checking that third commit has no tags' "
1386        git tag -l --contains $hash3 v* >actual &&
1387        test_cmp expected actual
1388"
1389
1390# how about a simple merge?
1391
1392test_expect_success 'creating simple branch' '
1393        git branch stable v2.0 &&
1394        git checkout stable &&
1395        echo foo-3.0 > foo &&
1396        git commit foo -m fourth &&
1397        git tag v3.0
1398'
1399
1400hash4=$(git rev-parse HEAD)
1401
1402cat > expected <<EOF
1403v3.0
1404EOF
1405
1406test_expect_success 'checking that branch head only has one tag' "
1407        git tag -l --contains $hash4 v* >actual &&
1408        test_cmp expected actual
1409"
1410
1411test_expect_success 'merging original branch into this branch' '
1412        git merge --strategy=ours master &&
1413        git tag v4.0
1414'
1415
1416cat > expected <<EOF
1417v4.0
1418EOF
1419
1420test_expect_success 'checking that original branch head has one tag now' "
1421        git tag -l --contains $hash3 v* >actual &&
1422        test_cmp expected actual
1423"
1424
1425cat > expected <<EOF
1426v0.2.1
1427v1.0
1428v1.0.1
1429v1.1.3
1430v2.0
1431v3.0
1432v4.0
1433EOF
1434
1435test_expect_success 'checking that initial commit is in all tags' "
1436        git tag -l --contains $hash1 v* >actual &&
1437        test_cmp expected actual
1438"
1439
1440# mixing modes and options:
1441
1442test_expect_success 'mixing incompatibles modes and options is forbidden' '
1443        test_must_fail git tag -a &&
1444        test_must_fail git tag -l -v &&
1445        test_must_fail git tag -n 100 &&
1446        test_must_fail git tag -l -m msg &&
1447        test_must_fail git tag -l -F some file &&
1448        test_must_fail git tag -v -s
1449'
1450
1451# check points-at
1452
1453test_expect_success '--points-at cannot be used in non-list mode' '
1454        test_must_fail git tag --points-at=v4.0 foo
1455'
1456
1457test_expect_success '--points-at finds lightweight tags' '
1458        echo v4.0 >expect &&
1459        git tag --points-at v4.0 >actual &&
1460        test_cmp expect actual
1461'
1462
1463test_expect_success '--points-at finds annotated tags of commits' '
1464        git tag -m "v4.0, annotated" annotated-v4.0 v4.0 &&
1465        echo annotated-v4.0 >expect &&
1466        git tag -l --points-at v4.0 "annotated*" >actual &&
1467        test_cmp expect actual
1468'
1469
1470test_expect_success '--points-at finds annotated tags of tags' '
1471        git tag -m "describing the v4.0 tag object" \
1472                annotated-again-v4.0 annotated-v4.0 &&
1473        cat >expect <<-\EOF &&
1474        annotated-again-v4.0
1475        annotated-v4.0
1476        EOF
1477        git tag --points-at=annotated-v4.0 >actual &&
1478        test_cmp expect actual
1479'
1480
1481test_expect_success 'multiple --points-at are OR-ed together' '
1482        cat >expect <<-\EOF &&
1483        v2.0
1484        v3.0
1485        EOF
1486        git tag --points-at=v2.0 --points-at=v3.0 >actual &&
1487        test_cmp expect actual
1488'
1489
1490test_expect_success 'lexical sort' '
1491        git tag foo1.3 &&
1492        git tag foo1.6 &&
1493        git tag foo1.10 &&
1494        git tag -l --sort=refname "foo*" >actual &&
1495        cat >expect <<-\EOF &&
1496        foo1.10
1497        foo1.3
1498        foo1.6
1499        EOF
1500        test_cmp expect actual
1501'
1502
1503test_expect_success 'version sort' '
1504        git tag -l --sort=version:refname "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 'reverse version sort' '
1514        git tag -l --sort=-version:refname "foo*" >actual &&
1515        cat >expect <<-\EOF &&
1516        foo1.10
1517        foo1.6
1518        foo1.3
1519        EOF
1520        test_cmp expect actual
1521'
1522
1523test_expect_success 'reverse lexical sort' '
1524        git tag -l --sort=-refname "foo*" >actual &&
1525        cat >expect <<-\EOF &&
1526        foo1.6
1527        foo1.3
1528        foo1.10
1529        EOF
1530        test_cmp expect actual
1531'
1532
1533test_expect_success 'configured lexical sort' '
1534        test_config tag.sort "v:refname" &&
1535        git tag -l "foo*" >actual &&
1536        cat >expect <<-\EOF &&
1537        foo1.3
1538        foo1.6
1539        foo1.10
1540        EOF
1541        test_cmp expect actual
1542'
1543
1544test_expect_success 'option override configured sort' '
1545        test_config tag.sort "v:refname" &&
1546        git tag -l --sort=-refname "foo*" >actual &&
1547        cat >expect <<-\EOF &&
1548        foo1.6
1549        foo1.3
1550        foo1.10
1551        EOF
1552        test_cmp expect actual
1553'
1554
1555test_expect_success 'invalid sort parameter on command line' '
1556        test_must_fail git tag -l --sort=notvalid "foo*" >actual
1557'
1558
1559test_expect_success 'invalid sort parameter in configuratoin' '
1560        test_config tag.sort "v:notvalid" &&
1561        test_must_fail git tag -l "foo*"
1562'
1563
1564test_expect_success 'version sort with prerelease reordering' '
1565        test_config versionsort.prereleaseSuffix -rc &&
1566        git tag foo1.6-rc1 &&
1567        git tag foo1.6-rc2 &&
1568        git tag -l --sort=version:refname "foo*" >actual &&
1569        cat >expect <<-\EOF &&
1570        foo1.3
1571        foo1.6-rc1
1572        foo1.6-rc2
1573        foo1.6
1574        foo1.10
1575        EOF
1576        test_cmp expect actual
1577'
1578
1579test_expect_success 'reverse version sort with prerelease reordering' '
1580        test_config versionsort.prereleaseSuffix -rc &&
1581        git tag -l --sort=-version:refname "foo*" >actual &&
1582        cat >expect <<-\EOF &&
1583        foo1.10
1584        foo1.6
1585        foo1.6-rc2
1586        foo1.6-rc1
1587        foo1.3
1588        EOF
1589        test_cmp expect actual
1590'
1591
1592test_expect_success 'version sort with prerelease reordering and common leading character' '
1593        test_config versionsort.prereleaseSuffix -before &&
1594        git tag foo1.7-before1 &&
1595        git tag foo1.7 &&
1596        git tag foo1.7-after1 &&
1597        git tag -l --sort=version:refname "foo1.7*" >actual &&
1598        cat >expect <<-\EOF &&
1599        foo1.7-before1
1600        foo1.7
1601        foo1.7-after1
1602        EOF
1603        test_cmp expect actual
1604'
1605
1606test_expect_success 'version sort with prerelease reordering, multiple suffixes and common leading character' '
1607        test_config versionsort.prereleaseSuffix -before &&
1608        git config --add versionsort.prereleaseSuffix -after &&
1609        git tag -l --sort=version:refname "foo1.7*" >actual &&
1610        cat >expect <<-\EOF &&
1611        foo1.7-before1
1612        foo1.7-after1
1613        foo1.7
1614        EOF
1615        test_cmp expect actual
1616'
1617
1618test_expect_success 'version sort with prerelease reordering, multiple suffixes match the same tag' '
1619        test_config versionsort.prereleaseSuffix -bar &&
1620        git config --add versionsort.prereleaseSuffix -foo-baz &&
1621        git config --add versionsort.prereleaseSuffix -foo-bar &&
1622        git tag foo1.8-foo-bar &&
1623        git tag foo1.8-foo-baz &&
1624        git tag foo1.8 &&
1625        git tag -l --sort=version:refname "foo1.8*" >actual &&
1626        cat >expect <<-\EOF &&
1627        foo1.8-foo-baz
1628        foo1.8-foo-bar
1629        foo1.8
1630        EOF
1631        test_cmp expect actual
1632'
1633
1634test_expect_success 'version sort with prerelease reordering, multiple suffixes match starting at the same position' '
1635        test_config versionsort.prereleaseSuffix -pre &&
1636        git config --add versionsort.prereleaseSuffix -prerelease &&
1637        git tag foo1.9-pre1 &&
1638        git tag foo1.9-pre2 &&
1639        git tag foo1.9-prerelease1 &&
1640        git tag -l --sort=version:refname "foo1.9*" >actual &&
1641        cat >expect <<-\EOF &&
1642        foo1.9-pre1
1643        foo1.9-pre2
1644        foo1.9-prerelease1
1645        EOF
1646        test_cmp expect actual
1647'
1648
1649test_expect_success 'version sort with general suffix reordering' '
1650        test_config versionsort.suffix -alpha &&
1651        git config --add versionsort.suffix -beta &&
1652        git config --add versionsort.suffix ""  &&
1653        git config --add versionsort.suffix -gamma &&
1654        git config --add versionsort.suffix -delta &&
1655        git tag foo1.10-alpha &&
1656        git tag foo1.10-beta &&
1657        git tag foo1.10-gamma &&
1658        git tag foo1.10-delta &&
1659        git tag foo1.10-unlisted-suffix &&
1660        git tag -l --sort=version:refname "foo1.10*" >actual &&
1661        cat >expect <<-\EOF &&
1662        foo1.10-alpha
1663        foo1.10-beta
1664        foo1.10
1665        foo1.10-unlisted-suffix
1666        foo1.10-gamma
1667        foo1.10-delta
1668        EOF
1669        test_cmp expect actual
1670'
1671
1672test_expect_success 'versionsort.suffix overrides versionsort.prereleaseSuffix' '
1673        test_config versionsort.suffix -before &&
1674        test_config versionsort.prereleaseSuffix -after &&
1675        git tag -l --sort=version:refname "foo1.7*" >actual &&
1676        cat >expect <<-\EOF &&
1677        foo1.7-before1
1678        foo1.7
1679        foo1.7-after1
1680        EOF
1681        test_cmp expect actual
1682'
1683
1684test_expect_success 'version sort with very long prerelease suffix' '
1685        test_config versionsort.prereleaseSuffix -very-looooooooooooooooooooooooong-prerelease-suffix &&
1686        git tag -l --sort=version:refname
1687'
1688
1689run_with_limited_stack () {
1690        (ulimit -s 128 && "$@")
1691}
1692
1693test_lazy_prereq ULIMIT_STACK_SIZE 'run_with_limited_stack true'
1694
1695# we require ulimit, this excludes Windows
1696test_expect_success ULIMIT_STACK_SIZE '--contains works in a deep repo' '
1697        >expect &&
1698        i=1 &&
1699        while test $i -lt 8000
1700        do
1701                echo "commit refs/heads/master
1702committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
1703data <<EOF
1704commit #$i
1705EOF"
1706                test $i = 1 && echo "from refs/heads/master^0"
1707                i=$(($i + 1))
1708        done | git fast-import &&
1709        git checkout master &&
1710        git tag far-far-away HEAD^ &&
1711        run_with_limited_stack git tag --contains HEAD >actual &&
1712        test_cmp expect actual
1713'
1714
1715test_expect_success '--format should list tags as per format given' '
1716        cat >expect <<-\EOF &&
1717        refname : refs/tags/v1.0
1718        refname : refs/tags/v1.0.1
1719        refname : refs/tags/v1.1.3
1720        EOF
1721        git tag -l --format="refname : %(refname)" "v1*" >actual &&
1722        test_cmp expect actual
1723'
1724
1725test_expect_success 'setup --merged test tags' '
1726        git tag mergetest-1 HEAD~2 &&
1727        git tag mergetest-2 HEAD~1 &&
1728        git tag mergetest-3 HEAD
1729'
1730
1731test_expect_success '--merged cannot be used in non-list mode' '
1732        test_must_fail git tag --merged=mergetest-2 foo
1733'
1734
1735test_expect_success '--merged shows merged tags' '
1736        cat >expect <<-\EOF &&
1737        mergetest-1
1738        mergetest-2
1739        EOF
1740        git tag -l --merged=mergetest-2 mergetest-* >actual &&
1741        test_cmp expect actual
1742'
1743
1744test_expect_success '--no-merged show unmerged tags' '
1745        cat >expect <<-\EOF &&
1746        mergetest-3
1747        EOF
1748        git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
1749        test_cmp expect actual
1750'
1751
1752test_expect_success 'ambiguous branch/tags not marked' '
1753        git tag ambiguous &&
1754        git branch ambiguous &&
1755        echo ambiguous >expect &&
1756        git tag -l ambiguous >actual &&
1757        test_cmp expect actual
1758'
1759
1760test_done