af76dc530212a77e56b86775712be8080510705c
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Andy Parkins
   4#
   5
   6test_description='for-each-ref test'
   7
   8. ./test-lib.sh
   9. "$TEST_DIRECTORY"/lib-gpg.sh
  10
  11# Mon Jul 3 23:18:43 2006 +0000
  12datestamp=1151968723
  13setdate_and_increment () {
  14    GIT_COMMITTER_DATE="$datestamp +0200"
  15    datestamp=$(expr "$datestamp" + 1)
  16    GIT_AUTHOR_DATE="$datestamp +0200"
  17    datestamp=$(expr "$datestamp" + 1)
  18    export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
  19}
  20
  21test_expect_success setup '
  22        setdate_and_increment &&
  23        echo "Using $datestamp" > one &&
  24        git add one &&
  25        git commit -m "Initial" &&
  26        setdate_and_increment &&
  27        git tag -a -m "Tagging at $datestamp" testtag &&
  28        git update-ref refs/remotes/origin/master master &&
  29        git remote add origin nowhere &&
  30        git config branch.master.remote origin &&
  31        git config branch.master.merge refs/heads/master &&
  32        git remote add myfork elsewhere &&
  33        git config remote.pushdefault myfork &&
  34        git config push.default current
  35'
  36
  37test_atom() {
  38        case "$1" in
  39                head) ref=refs/heads/master ;;
  40                 tag) ref=refs/tags/testtag ;;
  41                   *) ref=$1 ;;
  42        esac
  43        printf '%s\n' "$3" >expected
  44        test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
  45                git for-each-ref --format='%($2)' $ref >actual &&
  46                sanitize_pgp <actual >actual.clean &&
  47                test_cmp expected actual.clean
  48        "
  49}
  50
  51test_atom head refname refs/heads/master
  52test_atom head refname:short master
  53test_atom head refname:strip=1 heads/master
  54test_atom head refname:strip=2 master
  55test_atom head upstream refs/remotes/origin/master
  56test_atom head upstream:short origin/master
  57test_atom head push refs/remotes/myfork/master
  58test_atom head push:short myfork/master
  59test_atom head objecttype commit
  60test_atom head objectsize 171
  61test_atom head objectname $(git rev-parse refs/heads/master)
  62test_atom head objectname:short $(git rev-parse --short refs/heads/master)
  63test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
  64test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
  65test_atom head tree $(git rev-parse refs/heads/master^{tree})
  66test_atom head parent ''
  67test_atom head numparent 0
  68test_atom head object ''
  69test_atom head type ''
  70test_atom head '*objectname' ''
  71test_atom head '*objecttype' ''
  72test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
  73test_atom head authorname 'A U Thor'
  74test_atom head authoremail '<author@example.com>'
  75test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
  76test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
  77test_atom head committername 'C O Mitter'
  78test_atom head committeremail '<committer@example.com>'
  79test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
  80test_atom head tag ''
  81test_atom head tagger ''
  82test_atom head taggername ''
  83test_atom head taggeremail ''
  84test_atom head taggerdate ''
  85test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
  86test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
  87test_atom head subject 'Initial'
  88test_atom head contents:subject 'Initial'
  89test_atom head body ''
  90test_atom head contents:body ''
  91test_atom head contents:signature ''
  92test_atom head contents 'Initial
  93'
  94test_atom head HEAD '*'
  95
  96test_atom tag refname refs/tags/testtag
  97test_atom tag refname:short testtag
  98test_atom tag upstream ''
  99test_atom tag push ''
 100test_atom tag objecttype tag
 101test_atom tag objectsize 154
 102test_atom tag objectname $(git rev-parse refs/tags/testtag)
 103test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
 104test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
 105test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
 106test_atom tag tree ''
 107test_atom tag parent ''
 108test_atom tag numparent ''
 109test_atom tag object $(git rev-parse refs/tags/testtag^0)
 110test_atom tag type 'commit'
 111test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
 112test_atom tag '*objecttype' 'commit'
 113test_atom tag author ''
 114test_atom tag authorname ''
 115test_atom tag authoremail ''
 116test_atom tag authordate ''
 117test_atom tag committer ''
 118test_atom tag committername ''
 119test_atom tag committeremail ''
 120test_atom tag committerdate ''
 121test_atom tag tag 'testtag'
 122test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
 123test_atom tag taggername 'C O Mitter'
 124test_atom tag taggeremail '<committer@example.com>'
 125test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
 126test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
 127test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
 128test_atom tag subject 'Tagging at 1151968727'
 129test_atom tag contents:subject 'Tagging at 1151968727'
 130test_atom tag body ''
 131test_atom tag contents:body ''
 132test_atom tag contents:signature ''
 133test_atom tag contents 'Tagging at 1151968727
 134'
 135test_atom tag HEAD ' '
 136
 137test_expect_success 'Check invalid atoms names are errors' '
 138        test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
 139'
 140
 141test_expect_success 'arguments to :strip must be positive integers' '
 142        test_must_fail git for-each-ref --format="%(refname:strip=0)" &&
 143        test_must_fail git for-each-ref --format="%(refname:strip=-1)" &&
 144        test_must_fail git for-each-ref --format="%(refname:strip=foo)"
 145'
 146
 147test_expect_success 'stripping refnames too far gives an error' '
 148        test_must_fail git for-each-ref --format="%(refname:strip=3)"
 149'
 150
 151test_expect_success 'Check format specifiers are ignored in naming date atoms' '
 152        git for-each-ref --format="%(authordate)" refs/heads &&
 153        git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
 154        git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
 155        git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
 156'
 157
 158test_expect_success 'Check valid format specifiers for date fields' '
 159        git for-each-ref --format="%(authordate:default)" refs/heads &&
 160        git for-each-ref --format="%(authordate:relative)" refs/heads &&
 161        git for-each-ref --format="%(authordate:short)" refs/heads &&
 162        git for-each-ref --format="%(authordate:local)" refs/heads &&
 163        git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
 164        git for-each-ref --format="%(authordate:rfc2822)" refs/heads
 165'
 166
 167test_expect_success 'Check invalid format specifiers are errors' '
 168        test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
 169'
 170
 171test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
 172        test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
 173        test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
 174        test_must_fail git for-each-ref --format="%(objectname:short=foo)"
 175'
 176
 177test_date () {
 178        f=$1 &&
 179        committer_date=$2 &&
 180        author_date=$3 &&
 181        tagger_date=$4 &&
 182        cat >expected <<-EOF &&
 183        'refs/heads/master' '$committer_date' '$author_date'
 184        'refs/tags/testtag' '$tagger_date'
 185        EOF
 186        (
 187                git for-each-ref --shell \
 188                        --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
 189                        refs/heads &&
 190                git for-each-ref --shell \
 191                        --format="%(refname) %(taggerdate${f:+:$f})" \
 192                        refs/tags
 193        ) >actual &&
 194        test_cmp expected actual
 195}
 196
 197test_expect_success 'Check unformatted date fields output' '
 198        test_date "" \
 199                "Tue Jul 4 01:18:43 2006 +0200" \
 200                "Tue Jul 4 01:18:44 2006 +0200" \
 201                "Tue Jul 4 01:18:45 2006 +0200"
 202'
 203
 204test_expect_success 'Check format "default" formatted date fields output' '
 205        test_date default \
 206                "Tue Jul 4 01:18:43 2006 +0200" \
 207                "Tue Jul 4 01:18:44 2006 +0200" \
 208                "Tue Jul 4 01:18:45 2006 +0200"
 209'
 210
 211test_expect_success 'Check format "default-local" date fields output' '
 212        test_date default-local "Mon Jul 3 23:18:43 2006" "Mon Jul 3 23:18:44 2006" "Mon Jul 3 23:18:45 2006"
 213'
 214
 215# Don't know how to do relative check because I can't know when this script
 216# is going to be run and can't fake the current time to git, and hence can't
 217# provide expected output.  Instead, I'll just make sure that "relative"
 218# doesn't exit in error
 219test_expect_success 'Check format "relative" date fields output' '
 220        f=relative &&
 221        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 222        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
 223'
 224
 225# We just check that this is the same as "relative" for now.
 226test_expect_success 'Check format "relative-local" date fields output' '
 227        test_date relative-local \
 228                "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
 229                "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
 230                "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
 231'
 232
 233test_expect_success 'Check format "short" date fields output' '
 234        test_date short 2006-07-04 2006-07-04 2006-07-04
 235'
 236
 237test_expect_success 'Check format "short-local" date fields output' '
 238        test_date short-local 2006-07-03 2006-07-03 2006-07-03
 239'
 240
 241test_expect_success 'Check format "local" date fields output' '
 242        test_date local \
 243                "Mon Jul 3 23:18:43 2006" \
 244                "Mon Jul 3 23:18:44 2006" \
 245                "Mon Jul 3 23:18:45 2006"
 246'
 247
 248test_expect_success 'Check format "iso8601" date fields output' '
 249        test_date iso8601 \
 250                "2006-07-04 01:18:43 +0200" \
 251                "2006-07-04 01:18:44 +0200" \
 252                "2006-07-04 01:18:45 +0200"
 253'
 254
 255test_expect_success 'Check format "iso8601-local" date fields output' '
 256        test_date iso8601-local "2006-07-03 23:18:43 +0000" "2006-07-03 23:18:44 +0000" "2006-07-03 23:18:45 +0000"
 257'
 258
 259test_expect_success 'Check format "rfc2822" date fields output' '
 260        test_date rfc2822 \
 261                "Tue, 4 Jul 2006 01:18:43 +0200" \
 262                "Tue, 4 Jul 2006 01:18:44 +0200" \
 263                "Tue, 4 Jul 2006 01:18:45 +0200"
 264'
 265
 266test_expect_success 'Check format "rfc2822-local" date fields output' '
 267        test_date rfc2822-local "Mon, 3 Jul 2006 23:18:43 +0000" "Mon, 3 Jul 2006 23:18:44 +0000" "Mon, 3 Jul 2006 23:18:45 +0000"
 268'
 269
 270test_expect_success 'Check format "raw" date fields output' '
 271        test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
 272'
 273
 274test_expect_success 'Check format "raw-local" date fields output' '
 275        test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
 276'
 277
 278test_expect_success 'Check format of strftime date fields' '
 279        echo "my date is 2006-07-04" >expected &&
 280        git for-each-ref \
 281          --format="%(authordate:format:my date is %Y-%m-%d)" \
 282          refs/heads >actual &&
 283        test_cmp expected actual
 284'
 285
 286test_expect_success 'Check format of strftime-local date fields' '
 287        echo "my date is 2006-07-03" >expected &&
 288        git for-each-ref \
 289          --format="%(authordate:format-local:my date is %Y-%m-%d)" \
 290          refs/heads >actual &&
 291        test_cmp expected actual
 292'
 293
 294test_expect_success 'exercise strftime with odd fields' '
 295        echo >expected &&
 296        git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
 297        test_cmp expected actual &&
 298        long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
 299        echo $long >expected &&
 300        git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
 301        test_cmp expected actual
 302'
 303
 304cat >expected <<\EOF
 305refs/heads/master
 306refs/remotes/origin/master
 307refs/tags/testtag
 308EOF
 309
 310test_expect_success 'Verify ascending sort' '
 311        git for-each-ref --format="%(refname)" --sort=refname >actual &&
 312        test_cmp expected actual
 313'
 314
 315
 316cat >expected <<\EOF
 317refs/tags/testtag
 318refs/remotes/origin/master
 319refs/heads/master
 320EOF
 321
 322test_expect_success 'Verify descending sort' '
 323        git for-each-ref --format="%(refname)" --sort=-refname >actual &&
 324        test_cmp expected actual
 325'
 326
 327cat >expected <<\EOF
 328'refs/heads/master'
 329'refs/remotes/origin/master'
 330'refs/tags/testtag'
 331EOF
 332
 333test_expect_success 'Quoting style: shell' '
 334        git for-each-ref --shell --format="%(refname)" >actual &&
 335        test_cmp expected actual
 336'
 337
 338test_expect_success 'Quoting style: perl' '
 339        git for-each-ref --perl --format="%(refname)" >actual &&
 340        test_cmp expected actual
 341'
 342
 343test_expect_success 'Quoting style: python' '
 344        git for-each-ref --python --format="%(refname)" >actual &&
 345        test_cmp expected actual
 346'
 347
 348cat >expected <<\EOF
 349"refs/heads/master"
 350"refs/remotes/origin/master"
 351"refs/tags/testtag"
 352EOF
 353
 354test_expect_success 'Quoting style: tcl' '
 355        git for-each-ref --tcl --format="%(refname)" >actual &&
 356        test_cmp expected actual
 357'
 358
 359for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 360        test_expect_success "more than one quoting style: $i" "
 361                git for-each-ref $i 2>&1 | (read line &&
 362                case \$line in
 363                \"error: more than one quoting style\"*) : happy;;
 364                *) false
 365                esac)
 366        "
 367done
 368
 369test_expect_success 'setup for upstream:track[short]' '
 370        test_commit two
 371'
 372
 373test_atom head upstream:track '[ahead 1]'
 374test_atom head upstream:trackshort '>'
 375test_atom head upstream:track,nobracket 'ahead 1'
 376test_atom head upstream:nobracket,track 'ahead 1'
 377test_atom head push:track '[ahead 1]'
 378test_atom head push:trackshort '>'
 379
 380test_expect_success 'Check that :track[short] cannot be used with other atoms' '
 381        test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
 382        test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
 383'
 384
 385test_expect_success 'Check that :track[short] works when upstream is invalid' '
 386        cat >expected <<-\EOF &&
 387        [gone]
 388
 389        EOF
 390        test_when_finished "git config branch.master.merge refs/heads/master" &&
 391        git config branch.master.merge refs/heads/does-not-exist &&
 392        git for-each-ref \
 393                --format="%(upstream:track)$LF%(upstream:trackshort)" \
 394                refs/heads >actual &&
 395        test_cmp expected actual
 396'
 397
 398test_expect_success 'Check for invalid refname format' '
 399        test_must_fail git for-each-ref --format="%(refname:INVALID)"
 400'
 401
 402get_color ()
 403{
 404        git config --get-color no.such.slot "$1"
 405}
 406
 407cat >expected <<EOF
 408$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
 409$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
 410$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
 411$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
 412EOF
 413
 414test_expect_success 'Check %(color:...) ' '
 415        git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
 416        test_cmp expected actual
 417'
 418
 419cat >expected <<\EOF
 420heads/master
 421tags/master
 422EOF
 423
 424test_expect_success 'Check ambiguous head and tag refs (strict)' '
 425        git config --bool core.warnambiguousrefs true &&
 426        git checkout -b newtag &&
 427        echo "Using $datestamp" > one &&
 428        git add one &&
 429        git commit -m "Branch" &&
 430        setdate_and_increment &&
 431        git tag -m "Tagging at $datestamp" master &&
 432        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 433        test_cmp expected actual
 434'
 435
 436cat >expected <<\EOF
 437heads/master
 438master
 439EOF
 440
 441test_expect_success 'Check ambiguous head and tag refs (loose)' '
 442        git config --bool core.warnambiguousrefs false &&
 443        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 444        test_cmp expected actual
 445'
 446
 447cat >expected <<\EOF
 448heads/ambiguous
 449ambiguous
 450EOF
 451
 452test_expect_success 'Check ambiguous head and tag refs II (loose)' '
 453        git checkout master &&
 454        git tag ambiguous testtag^0 &&
 455        git branch ambiguous testtag^0 &&
 456        git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
 457        test_cmp expected actual
 458'
 459
 460test_expect_success 'an unusual tag with an incomplete line' '
 461
 462        git tag -m "bogo" bogo &&
 463        bogo=$(git cat-file tag bogo) &&
 464        bogo=$(printf "%s" "$bogo" | git mktag) &&
 465        git tag -f bogo "$bogo" &&
 466        git for-each-ref --format "%(body)" refs/tags/bogo
 467
 468'
 469
 470test_expect_success 'create tag with subject and body content' '
 471        cat >>msg <<-\EOF &&
 472                the subject line
 473
 474                first body line
 475                second body line
 476        EOF
 477        git tag -F msg subject-body
 478'
 479test_atom refs/tags/subject-body subject 'the subject line'
 480test_atom refs/tags/subject-body body 'first body line
 481second body line
 482'
 483test_atom refs/tags/subject-body contents 'the subject line
 484
 485first body line
 486second body line
 487'
 488
 489test_expect_success 'create tag with multiline subject' '
 490        cat >msg <<-\EOF &&
 491                first subject line
 492                second subject line
 493
 494                first body line
 495                second body line
 496        EOF
 497        git tag -F msg multiline
 498'
 499test_atom refs/tags/multiline subject 'first subject line second subject line'
 500test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
 501test_atom refs/tags/multiline body 'first body line
 502second body line
 503'
 504test_atom refs/tags/multiline contents:body 'first body line
 505second body line
 506'
 507test_atom refs/tags/multiline contents:signature ''
 508test_atom refs/tags/multiline contents 'first subject line
 509second subject line
 510
 511first body line
 512second body line
 513'
 514
 515test_expect_success GPG 'create signed tags' '
 516        git tag -s -m "" signed-empty &&
 517        git tag -s -m "subject line" signed-short &&
 518        cat >msg <<-\EOF &&
 519        subject line
 520
 521        body contents
 522        EOF
 523        git tag -s -F msg signed-long
 524'
 525
 526sig='-----BEGIN PGP SIGNATURE-----
 527-----END PGP SIGNATURE-----
 528'
 529
 530PREREQ=GPG
 531test_atom refs/tags/signed-empty subject ''
 532test_atom refs/tags/signed-empty contents:subject ''
 533test_atom refs/tags/signed-empty body "$sig"
 534test_atom refs/tags/signed-empty contents:body ''
 535test_atom refs/tags/signed-empty contents:signature "$sig"
 536test_atom refs/tags/signed-empty contents "$sig"
 537
 538test_atom refs/tags/signed-short subject 'subject line'
 539test_atom refs/tags/signed-short contents:subject 'subject line'
 540test_atom refs/tags/signed-short body "$sig"
 541test_atom refs/tags/signed-short contents:body ''
 542test_atom refs/tags/signed-short contents:signature "$sig"
 543test_atom refs/tags/signed-short contents "subject line
 544$sig"
 545
 546test_atom refs/tags/signed-long subject 'subject line'
 547test_atom refs/tags/signed-long contents:subject 'subject line'
 548test_atom refs/tags/signed-long body "body contents
 549$sig"
 550test_atom refs/tags/signed-long contents:body 'body contents
 551'
 552test_atom refs/tags/signed-long contents:signature "$sig"
 553test_atom refs/tags/signed-long contents "subject line
 554
 555body contents
 556$sig"
 557
 558cat >expected <<EOF
 559$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
 560$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
 561EOF
 562
 563test_expect_success 'Verify sort with multiple keys' '
 564        git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
 565                refs/tags/bogo refs/tags/master > actual &&
 566        test_cmp expected actual
 567'
 568
 569test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
 570        test_when_finished "git checkout master" &&
 571        git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 572        sed -e "s/^\* /  /" actual >expect &&
 573        git checkout --orphan HEAD &&
 574        git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 575        test_cmp expect actual
 576'
 577
 578cat >trailers <<EOF
 579Reviewed-by: A U Thor <author@example.com>
 580Signed-off-by: A U Thor <author@example.com>
 581EOF
 582
 583test_expect_success 'basic atom: head contents:trailers' '
 584        echo "Some contents" > two &&
 585        git add two &&
 586        git commit -F - <<-EOF &&
 587        trailers: this commit message has trailers
 588
 589        Some message contents
 590
 591        $(cat trailers)
 592        EOF
 593        git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
 594        sanitize_pgp <actual >actual.clean &&
 595        # git for-each-ref ends with a blank line
 596        cat >expect <<-EOF &&
 597        $(cat trailers)
 598
 599        EOF
 600        test_cmp expect actual.clean
 601'
 602
 603test_done