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