t / t7004-tag.shon commit t7004: ship trustdb to avoid gpg warnings (a44c426)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Carlos Rica
   4#
   5
   6test_description='git-tag
   7
   8Basic tests for operations with tags.'
   9
  10. ./test-lib.sh
  11
  12# creating and listing lightweight tags:
  13
  14tag_exists () {
  15        git show-ref --quiet --verify refs/tags/"$1"
  16}
  17
  18# todo: git tag -l now returns always zero, when fixed, change this test
  19test_expect_success 'listing all tags in an empty tree should succeed' \
  20        'git tag -l'
  21
  22test_expect_success 'listing all tags in an empty tree should output nothing' \
  23        'test `git-tag -l | wc -l` -eq 0'
  24
  25test_expect_failure 'looking for a tag in an empty tree should fail' \
  26        'tag_exists mytag'
  27
  28test_expect_success 'creating a tag in an empty tree should fail' '
  29        ! git-tag mynotag &&
  30        ! tag_exists mynotag
  31'
  32
  33test_expect_success 'creating a tag for HEAD in an empty tree should fail' '
  34        ! git-tag mytaghead HEAD &&
  35        ! tag_exists mytaghead
  36'
  37
  38test_expect_success 'creating a tag for an unknown revision should fail' '
  39        ! git-tag mytagnorev aaaaaaaaaaa &&
  40        ! tag_exists mytagnorev
  41'
  42
  43# commit used in the tests, test_tick is also called here to freeze the date:
  44test_expect_success 'creating a tag using default HEAD should succeed' '
  45        test_tick &&
  46        echo foo >foo &&
  47        git add foo &&
  48        git commit -m Foo &&
  49        git tag mytag
  50'
  51
  52test_expect_success 'listing all tags if one exists should succeed' \
  53        'git-tag -l'
  54
  55test_expect_success 'listing all tags if one exists should output that tag' \
  56        'test `git-tag -l` = mytag'
  57
  58# pattern matching:
  59
  60test_expect_success 'listing a tag using a matching pattern should succeed' \
  61        'git-tag -l mytag'
  62
  63test_expect_success \
  64        'listing a tag using a matching pattern should output that tag' \
  65        'test `git-tag -l mytag` = mytag'
  66
  67# todo: git tag -l now returns always zero, when fixed, change this test
  68test_expect_success \
  69        'listing tags using a non-matching pattern should suceed' \
  70        'git-tag -l xxx'
  71
  72test_expect_success \
  73        'listing tags using a non-matching pattern should output nothing' \
  74        'test `git-tag -l xxx | wc -l` -eq 0'
  75
  76# special cases for creating tags:
  77
  78test_expect_failure \
  79        'trying to create a tag with the name of one existing should fail' \
  80        'git tag mytag'
  81
  82test_expect_success \
  83        'trying to create a tag with a non-valid name should fail' '
  84        test `git-tag -l | wc -l` -eq 1 &&
  85        ! git tag "" &&
  86        ! git tag .othertag &&
  87        ! git tag "other tag" &&
  88        ! git tag "othertag^" &&
  89        ! git tag "other~tag" &&
  90        test `git-tag -l | wc -l` -eq 1
  91'
  92
  93test_expect_success 'creating a tag using HEAD directly should succeed' '
  94        git tag myhead HEAD &&
  95        tag_exists myhead
  96'
  97
  98# deleting tags:
  99
 100test_expect_success 'trying to delete an unknown tag should fail' '
 101        ! tag_exists unknown-tag &&
 102        ! git-tag -d unknown-tag
 103'
 104
 105cat >expect <<EOF
 106myhead
 107mytag
 108EOF
 109test_expect_success \
 110        'trying to delete tags without params should succeed and do nothing' '
 111        git tag -l > actual && git diff expect actual &&
 112        git-tag -d &&
 113        git tag -l > actual && git diff expect actual
 114'
 115
 116test_expect_success \
 117        'deleting two existing tags in one command should succeed' '
 118        tag_exists mytag &&
 119        tag_exists myhead &&
 120        git-tag -d mytag myhead &&
 121        ! tag_exists mytag &&
 122        ! tag_exists myhead
 123'
 124
 125test_expect_success \
 126        'creating a tag with the name of another deleted one should succeed' '
 127        ! tag_exists mytag &&
 128        git-tag mytag &&
 129        tag_exists mytag
 130'
 131
 132test_expect_success \
 133        'trying to delete two tags, existing and not, should fail in the 2nd' '
 134        tag_exists mytag &&
 135        ! tag_exists myhead &&
 136        ! git-tag -d mytag anothertag &&
 137        ! tag_exists mytag &&
 138        ! tag_exists myhead
 139'
 140
 141test_expect_failure 'trying to delete an already deleted tag should fail' \
 142        'git-tag -d mytag'
 143
 144# listing various tags with pattern matching:
 145
 146cat >expect <<EOF
 147a1
 148aa1
 149cba
 150t210
 151t211
 152v0.2.1
 153v1.0
 154v1.0.1
 155v1.1.3
 156EOF
 157test_expect_success 'listing all tags should print them ordered' '
 158        git tag v1.0.1 &&
 159        git tag t211 &&
 160        git tag aa1 &&
 161        git tag v0.2.1 &&
 162        git tag v1.1.3 &&
 163        git tag cba &&
 164        git tag a1 &&
 165        git tag v1.0 &&
 166        git tag t210 &&
 167        git tag -l > actual
 168        git diff expect actual
 169'
 170
 171cat >expect <<EOF
 172a1
 173aa1
 174cba
 175EOF
 176test_expect_success \
 177        'listing tags with substring as pattern must print those matching' '
 178        git-tag -l a > actual &&
 179        git-diff expect actual
 180'
 181
 182cat >expect <<EOF
 183v0.2.1
 184v1.0.1
 185v1.1.3
 186EOF
 187test_expect_success \
 188        'listing tags with substring as pattern must print those matching' '
 189        git-tag -l .1 > actual &&
 190        git-diff expect actual
 191'
 192
 193cat >expect <<EOF
 194t210
 195t211
 196EOF
 197test_expect_success \
 198        'listing tags with substring as pattern must print those matching' '
 199        git-tag -l t21 > actual &&
 200        git-diff expect actual
 201'
 202
 203cat >expect <<EOF
 204a1
 205aa1
 206EOF
 207test_expect_success \
 208        'listing tags using a name as pattern must print those matching' '
 209        git-tag -l a1 > actual &&
 210        git-diff expect actual
 211'
 212
 213cat >expect <<EOF
 214v1.0
 215v1.0.1
 216EOF
 217test_expect_success \
 218        'listing tags using a name as pattern must print those matching' '
 219        git-tag -l v1.0 > actual &&
 220        git-diff expect actual
 221'
 222
 223cat >expect <<EOF
 224v1.1.3
 225EOF
 226test_expect_success \
 227        'listing tags with ? in the pattern should print those matching' '
 228        git-tag -l "1.1?" > actual &&
 229        git-diff expect actual
 230'
 231
 232>expect
 233test_expect_success \
 234        'listing tags using v.* should print nothing because none have v.' '
 235        git-tag -l "v.*" > actual &&
 236        git-diff expect actual
 237'
 238
 239cat >expect <<EOF
 240v0.2.1
 241v1.0
 242v1.0.1
 243v1.1.3
 244EOF
 245test_expect_success \
 246        'listing tags using v* should print only those having v' '
 247        git-tag -l "v*" > actual &&
 248        git-diff expect actual
 249'
 250
 251# creating and verifying lightweight tags:
 252
 253test_expect_success \
 254        'a non-annotated tag created without parameters should point to HEAD' '
 255        git-tag non-annotated-tag &&
 256        test $(git-cat-file -t non-annotated-tag) = commit &&
 257        test $(git-rev-parse non-annotated-tag) = $(git-rev-parse HEAD)
 258'
 259
 260test_expect_failure 'trying to verify an unknown tag should fail' \
 261        'git-tag -v unknown-tag'
 262
 263test_expect_failure \
 264        'trying to verify a non-annotated and non-signed tag should fail' \
 265        'git-tag -v non-annotated-tag'
 266
 267# creating annotated tags:
 268
 269get_tag_msg () {
 270        git cat-file tag "$1" | sed -e "/BEGIN PGP/q"
 271}
 272
 273# run test_tick before committing always gives the time in that timezone
 274get_tag_header () {
 275cat <<EOF
 276object $2
 277type $3
 278tag $1
 279tagger C O Mitter <committer@example.com> $4 -0700
 280
 281EOF
 282}
 283
 284commit=$(git rev-parse HEAD)
 285time=$test_tick
 286
 287get_tag_header annotated-tag $commit commit $time >expect
 288echo "A message" >>expect
 289test_expect_success \
 290        'creating an annotated tag with -m message should succeed' '
 291        git-tag -m "A message" annotated-tag &&
 292        get_tag_msg annotated-tag >actual &&
 293        git diff expect actual
 294'
 295
 296cat >msgfile <<EOF
 297Another message
 298in a file.
 299EOF
 300get_tag_header file-annotated-tag $commit commit $time >expect
 301cat msgfile >>expect
 302test_expect_success \
 303        'creating an annotated tag with -F messagefile should succeed' '
 304        git-tag -F msgfile file-annotated-tag &&
 305        get_tag_msg file-annotated-tag >actual &&
 306        git diff expect actual
 307'
 308
 309# blank and empty messages:
 310
 311get_tag_header empty-annotated-tag $commit commit $time >expect
 312test_expect_success \
 313        'creating a tag with an empty -m message should succeed' '
 314        git-tag -m "" empty-annotated-tag &&
 315        get_tag_msg empty-annotated-tag >actual &&
 316        git diff expect actual
 317'
 318
 319>emptyfile
 320get_tag_header emptyfile-annotated-tag $commit commit $time >expect
 321test_expect_success \
 322        'creating a tag with an empty -F messagefile should succeed' '
 323        git-tag -F emptyfile emptyfile-annotated-tag &&
 324        get_tag_msg emptyfile-annotated-tag >actual &&
 325        git diff expect actual
 326'
 327
 328printf '\n\n  \n\t\nLeading blank lines\n' >blanksfile
 329printf '\n\t \t  \nRepeated blank lines\n' >>blanksfile
 330printf '\n\n\nTrailing spaces      \t  \n' >>blanksfile
 331printf '\nTrailing blank lines\n\n\t \n\n' >>blanksfile
 332get_tag_header blanks-annotated-tag $commit commit $time >expect
 333cat >>expect <<EOF
 334Leading blank lines
 335
 336Repeated blank lines
 337
 338Trailing spaces
 339
 340Trailing blank lines
 341EOF
 342test_expect_success \
 343        'extra blanks in the message for an annotated tag should be removed' '
 344        git-tag -F blanksfile blanks-annotated-tag &&
 345        get_tag_msg blanks-annotated-tag >actual &&
 346        git diff expect actual
 347'
 348
 349get_tag_header blank-annotated-tag $commit commit $time >expect
 350test_expect_success \
 351        'creating a tag with blank -m message with spaces should succeed' '
 352        git-tag -m "     " blank-annotated-tag &&
 353        get_tag_msg blank-annotated-tag >actual &&
 354        git diff expect actual
 355'
 356
 357echo '     ' >blankfile
 358echo ''      >>blankfile
 359echo '  '    >>blankfile
 360get_tag_header blankfile-annotated-tag $commit commit $time >expect
 361test_expect_success \
 362        'creating a tag with blank -F messagefile with spaces should succeed' '
 363        git-tag -F blankfile blankfile-annotated-tag &&
 364        get_tag_msg blankfile-annotated-tag >actual &&
 365        git diff expect actual
 366'
 367
 368printf '      ' >blanknonlfile
 369get_tag_header blanknonlfile-annotated-tag $commit commit $time >expect
 370test_expect_success \
 371        'creating a tag with -F file of spaces and no newline should succeed' '
 372        git-tag -F blanknonlfile blanknonlfile-annotated-tag &&
 373        get_tag_msg blanknonlfile-annotated-tag >actual &&
 374        git diff expect actual
 375'
 376
 377# messages with commented lines:
 378
 379cat >commentsfile <<EOF
 380# A comment
 381
 382############
 383The message.
 384############
 385One line.
 386
 387
 388# commented lines
 389# commented lines
 390
 391Another line.
 392# comments
 393
 394Last line.
 395EOF
 396get_tag_header comments-annotated-tag $commit commit $time >expect
 397cat >>expect <<EOF
 398The message.
 399One line.
 400
 401Another line.
 402
 403Last line.
 404EOF
 405test_expect_success \
 406        'creating a tag using a -F messagefile with #comments should succeed' '
 407        git-tag -F commentsfile comments-annotated-tag &&
 408        get_tag_msg comments-annotated-tag >actual &&
 409        git diff expect actual
 410'
 411
 412get_tag_header comment-annotated-tag $commit commit $time >expect
 413test_expect_success \
 414        'creating a tag with a #comment in the -m message should succeed' '
 415        git-tag -m "#comment" comment-annotated-tag &&
 416        get_tag_msg comment-annotated-tag >actual &&
 417        git diff expect actual
 418'
 419
 420echo '#comment' >commentfile
 421echo ''         >>commentfile
 422echo '####'     >>commentfile
 423get_tag_header commentfile-annotated-tag $commit commit $time >expect
 424test_expect_success \
 425        'creating a tag with #comments in the -F messagefile should succeed' '
 426        git-tag -F commentfile commentfile-annotated-tag &&
 427        get_tag_msg commentfile-annotated-tag >actual &&
 428        git diff expect actual
 429'
 430
 431printf '#comment' >commentnonlfile
 432get_tag_header commentnonlfile-annotated-tag $commit commit $time >expect
 433test_expect_success \
 434        'creating a tag with a file of #comment and no newline should succeed' '
 435        git-tag -F commentnonlfile commentnonlfile-annotated-tag &&
 436        get_tag_msg commentnonlfile-annotated-tag >actual &&
 437        git diff expect actual
 438'
 439
 440# trying to verify annotated non-signed tags:
 441
 442test_expect_success \
 443        'trying to verify an annotated non-signed tag should fail' '
 444        tag_exists annotated-tag &&
 445        ! git-tag -v annotated-tag
 446'
 447
 448test_expect_success \
 449        'trying to verify a file-annotated non-signed tag should fail' '
 450        tag_exists file-annotated-tag &&
 451        ! git-tag -v file-annotated-tag
 452'
 453
 454# creating and verifying signed tags:
 455
 456gpg --version >/dev/null
 457if [ $? -eq 127 ]; then
 458        echo "Skipping signed tags tests, because gpg was not found"
 459        test_done
 460        exit
 461fi
 462
 463# key generation info: gpg --homedir t/t7004 --gen-key
 464# Type DSA and Elgamal, size 2048 bits, no expiration date.
 465# Name and email: C O Mitter <committer@example.com>
 466# No password given, to enable non-interactive operation.
 467
 468cp -R ../t7004 ./gpghome
 469chmod 0700 gpghome
 470export GNUPGHOME="$(pwd)/gpghome"
 471
 472get_tag_header signed-tag $commit commit $time >expect
 473echo 'A signed tag message' >>expect
 474echo '-----BEGIN PGP SIGNATURE-----' >>expect
 475test_expect_success 'creating a signed tag with -m message should succeed' '
 476        git-tag -s -m "A signed tag message" signed-tag &&
 477        get_tag_msg signed-tag >actual &&
 478        git-diff expect actual
 479'
 480
 481test_expect_success 'verifying a signed tag should succeed' \
 482        'git-tag -v signed-tag'
 483
 484test_expect_success 'verifying a forged tag should fail' '
 485        forged=$(git cat-file tag signed-tag |
 486                sed -e "s/signed-tag/forged-tag/" |
 487                git mktag) &&
 488        git tag forged-tag $forged &&
 489        ! git-tag -v forged-tag
 490'
 491
 492# blank and empty messages for signed tags:
 493
 494get_tag_header empty-signed-tag $commit commit $time >expect
 495echo '-----BEGIN PGP SIGNATURE-----' >>expect
 496test_expect_success \
 497        'creating a signed tag with an empty -m message should succeed' '
 498        git-tag -s -m "" empty-signed-tag &&
 499        get_tag_msg empty-signed-tag >actual &&
 500        git diff expect actual &&
 501        git-tag -v empty-signed-tag
 502'
 503
 504>sigemptyfile
 505get_tag_header emptyfile-signed-tag $commit commit $time >expect
 506echo '-----BEGIN PGP SIGNATURE-----' >>expect
 507test_expect_success \
 508        'creating a signed tag with an empty -F messagefile should succeed' '
 509        git-tag -s -F sigemptyfile emptyfile-signed-tag &&
 510        get_tag_msg emptyfile-signed-tag >actual &&
 511        git diff expect actual &&
 512        git-tag -v emptyfile-signed-tag
 513'
 514
 515printf '\n\n  \n\t\nLeading blank lines\n' > sigblanksfile
 516printf '\n\t \t  \nRepeated blank lines\n' >>sigblanksfile
 517printf '\n\n\nTrailing spaces      \t  \n' >>sigblanksfile
 518printf '\nTrailing blank lines\n\n\t \n\n' >>sigblanksfile
 519get_tag_header blanks-signed-tag $commit commit $time >expect
 520cat >>expect <<EOF
 521Leading blank lines
 522
 523Repeated blank lines
 524
 525Trailing spaces
 526
 527Trailing blank lines
 528EOF
 529echo '-----BEGIN PGP SIGNATURE-----' >>expect
 530test_expect_success \
 531        'extra blanks in the message for a signed tag should be removed' '
 532        git-tag -s -F sigblanksfile blanks-signed-tag &&
 533        get_tag_msg blanks-signed-tag >actual &&
 534        git diff expect actual &&
 535        git-tag -v blanks-signed-tag
 536'
 537
 538get_tag_header blank-signed-tag $commit commit $time >expect
 539echo '-----BEGIN PGP SIGNATURE-----' >>expect
 540test_expect_success \
 541        'creating a signed tag with a blank -m message should succeed' '
 542        git-tag -s -m "     " blank-signed-tag &&
 543        get_tag_msg blank-signed-tag >actual &&
 544        git diff expect actual &&
 545        git-tag -v blank-signed-tag
 546'
 547
 548echo '     ' >sigblankfile
 549echo ''      >>sigblankfile
 550echo '  '    >>sigblankfile
 551get_tag_header blankfile-signed-tag $commit commit $time >expect
 552echo '-----BEGIN PGP SIGNATURE-----' >>expect
 553test_expect_success \
 554        'creating a signed tag with blank -F file with spaces should succeed' '
 555        git-tag -s -F sigblankfile blankfile-signed-tag &&
 556        get_tag_msg blankfile-signed-tag >actual &&
 557        git diff expect actual &&
 558        git-tag -v blankfile-signed-tag
 559'
 560
 561printf '      ' >sigblanknonlfile
 562get_tag_header blanknonlfile-signed-tag $commit commit $time >expect
 563echo '-----BEGIN PGP SIGNATURE-----' >>expect
 564test_expect_success \
 565        'creating a signed tag with spaces and no newline should succeed' '
 566        git-tag -s -F sigblanknonlfile blanknonlfile-signed-tag &&
 567        get_tag_msg blanknonlfile-signed-tag >actual &&
 568        git diff expect actual &&
 569        git-tag -v signed-tag
 570'
 571
 572# messages with commented lines for signed tags:
 573
 574cat >sigcommentsfile <<EOF
 575# A comment
 576
 577############
 578The message.
 579############
 580One line.
 581
 582
 583# commented lines
 584# commented lines
 585
 586Another line.
 587# comments
 588
 589Last line.
 590EOF
 591get_tag_header comments-signed-tag $commit commit $time >expect
 592cat >>expect <<EOF
 593The message.
 594One line.
 595
 596Another line.
 597
 598Last line.
 599EOF
 600echo '-----BEGIN PGP SIGNATURE-----' >>expect
 601test_expect_success \
 602        'creating a signed tag with a -F file with #comments should succeed' '
 603        git-tag -s -F sigcommentsfile comments-signed-tag &&
 604        get_tag_msg comments-signed-tag >actual &&
 605        git diff expect actual &&
 606        git-tag -v comments-signed-tag
 607'
 608
 609get_tag_header comment-signed-tag $commit commit $time >expect
 610echo '-----BEGIN PGP SIGNATURE-----' >>expect
 611test_expect_success \
 612        'creating a signed tag with #commented -m message should succeed' '
 613        git-tag -s -m "#comment" comment-signed-tag &&
 614        get_tag_msg comment-signed-tag >actual &&
 615        git diff expect actual &&
 616        git-tag -v comment-signed-tag
 617'
 618
 619echo '#comment' >sigcommentfile
 620echo ''         >>sigcommentfile
 621echo '####'     >>sigcommentfile
 622get_tag_header commentfile-signed-tag $commit commit $time >expect
 623echo '-----BEGIN PGP SIGNATURE-----' >>expect
 624test_expect_success \
 625        'creating a signed tag with #commented -F messagefile should succeed' '
 626        git-tag -s -F sigcommentfile commentfile-signed-tag &&
 627        get_tag_msg commentfile-signed-tag >actual &&
 628        git diff expect actual &&
 629        git-tag -v commentfile-signed-tag
 630'
 631
 632printf '#comment' >sigcommentnonlfile
 633get_tag_header commentnonlfile-signed-tag $commit commit $time >expect
 634echo '-----BEGIN PGP SIGNATURE-----' >>expect
 635test_expect_success \
 636        'creating a signed tag with a #comment and no newline should succeed' '
 637        git-tag -s -F sigcommentnonlfile commentnonlfile-signed-tag &&
 638        get_tag_msg commentnonlfile-signed-tag >actual &&
 639        git diff expect actual &&
 640        git-tag -v commentnonlfile-signed-tag
 641'
 642
 643# tags pointing to objects different from commits:
 644
 645tree=$(git rev-parse HEAD^{tree})
 646blob=$(git rev-parse HEAD:foo)
 647tag=$(git rev-parse signed-tag)
 648
 649get_tag_header tree-signed-tag $tree tree $time >expect
 650echo "A message for a tree" >>expect
 651echo '-----BEGIN PGP SIGNATURE-----' >>expect
 652test_expect_success \
 653        'creating a signed tag pointing to a tree should succeed' '
 654        git-tag -s -m "A message for a tree" tree-signed-tag HEAD^{tree} &&
 655        get_tag_msg tree-signed-tag >actual &&
 656        git diff expect actual
 657'
 658
 659get_tag_header blob-signed-tag $blob blob $time >expect
 660echo "A message for a blob" >>expect
 661echo '-----BEGIN PGP SIGNATURE-----' >>expect
 662test_expect_success \
 663        'creating a signed tag pointing to a blob should succeed' '
 664        git-tag -s -m "A message for a blob" blob-signed-tag HEAD:foo &&
 665        get_tag_msg blob-signed-tag >actual &&
 666        git diff expect actual
 667'
 668
 669get_tag_header tag-signed-tag $tag tag $time >expect
 670echo "A message for another tag" >>expect
 671echo '-----BEGIN PGP SIGNATURE-----' >>expect
 672test_expect_success \
 673        'creating a signed tag pointing to another tag should succeed' '
 674        git-tag -s -m "A message for another tag" tag-signed-tag signed-tag &&
 675        get_tag_msg tag-signed-tag >actual &&
 676        git diff expect actual
 677'
 678
 679# try to verify without gpg:
 680
 681rm -rf gpghome
 682test_expect_failure \
 683        'verify signed tag fails when public key is not present' \
 684        'git-tag -v signed-tag'
 685
 686test_done