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