t / t6300-for-each-ref.shon commit ref-filter: make %(upstream:track) prints "[gone]" for invalid upstreams (ffd921d)
   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 push:track '[ahead 1]'
 376test_atom head push:trackshort '>'
 377
 378test_expect_success 'Check that :track[short] cannot be used with other atoms' '
 379        test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
 380        test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
 381'
 382
 383test_expect_success 'Check that :track[short] works when upstream is invalid' '
 384        cat >expected <<-\EOF &&
 385        [gone]
 386
 387        EOF
 388        test_when_finished "git config branch.master.merge refs/heads/master" &&
 389        git config branch.master.merge refs/heads/does-not-exist &&
 390        git for-each-ref \
 391                --format="%(upstream:track)$LF%(upstream:trackshort)" \
 392                refs/heads >actual &&
 393        test_cmp expected actual
 394'
 395
 396test_expect_success 'Check for invalid refname format' '
 397        test_must_fail git for-each-ref --format="%(refname:INVALID)"
 398'
 399
 400get_color ()
 401{
 402        git config --get-color no.such.slot "$1"
 403}
 404
 405cat >expected <<EOF
 406$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
 407$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
 408$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
 409$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
 410EOF
 411
 412test_expect_success 'Check %(color:...) ' '
 413        git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
 414        test_cmp expected actual
 415'
 416
 417cat >expected <<\EOF
 418heads/master
 419tags/master
 420EOF
 421
 422test_expect_success 'Check ambiguous head and tag refs (strict)' '
 423        git config --bool core.warnambiguousrefs true &&
 424        git checkout -b newtag &&
 425        echo "Using $datestamp" > one &&
 426        git add one &&
 427        git commit -m "Branch" &&
 428        setdate_and_increment &&
 429        git tag -m "Tagging at $datestamp" master &&
 430        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 431        test_cmp expected actual
 432'
 433
 434cat >expected <<\EOF
 435heads/master
 436master
 437EOF
 438
 439test_expect_success 'Check ambiguous head and tag refs (loose)' '
 440        git config --bool core.warnambiguousrefs false &&
 441        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 442        test_cmp expected actual
 443'
 444
 445cat >expected <<\EOF
 446heads/ambiguous
 447ambiguous
 448EOF
 449
 450test_expect_success 'Check ambiguous head and tag refs II (loose)' '
 451        git checkout master &&
 452        git tag ambiguous testtag^0 &&
 453        git branch ambiguous testtag^0 &&
 454        git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
 455        test_cmp expected actual
 456'
 457
 458test_expect_success 'an unusual tag with an incomplete line' '
 459
 460        git tag -m "bogo" bogo &&
 461        bogo=$(git cat-file tag bogo) &&
 462        bogo=$(printf "%s" "$bogo" | git mktag) &&
 463        git tag -f bogo "$bogo" &&
 464        git for-each-ref --format "%(body)" refs/tags/bogo
 465
 466'
 467
 468test_expect_success 'create tag with subject and body content' '
 469        cat >>msg <<-\EOF &&
 470                the subject line
 471
 472                first body line
 473                second body line
 474        EOF
 475        git tag -F msg subject-body
 476'
 477test_atom refs/tags/subject-body subject 'the subject line'
 478test_atom refs/tags/subject-body body 'first body line
 479second body line
 480'
 481test_atom refs/tags/subject-body contents 'the subject line
 482
 483first body line
 484second body line
 485'
 486
 487test_expect_success 'create tag with multiline subject' '
 488        cat >msg <<-\EOF &&
 489                first subject line
 490                second subject line
 491
 492                first body line
 493                second body line
 494        EOF
 495        git tag -F msg multiline
 496'
 497test_atom refs/tags/multiline subject 'first subject line second subject line'
 498test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
 499test_atom refs/tags/multiline body 'first body line
 500second body line
 501'
 502test_atom refs/tags/multiline contents:body 'first body line
 503second body line
 504'
 505test_atom refs/tags/multiline contents:signature ''
 506test_atom refs/tags/multiline contents 'first subject line
 507second subject line
 508
 509first body line
 510second body line
 511'
 512
 513test_expect_success GPG 'create signed tags' '
 514        git tag -s -m "" signed-empty &&
 515        git tag -s -m "subject line" signed-short &&
 516        cat >msg <<-\EOF &&
 517        subject line
 518
 519        body contents
 520        EOF
 521        git tag -s -F msg signed-long
 522'
 523
 524sig='-----BEGIN PGP SIGNATURE-----
 525-----END PGP SIGNATURE-----
 526'
 527
 528PREREQ=GPG
 529test_atom refs/tags/signed-empty subject ''
 530test_atom refs/tags/signed-empty contents:subject ''
 531test_atom refs/tags/signed-empty body "$sig"
 532test_atom refs/tags/signed-empty contents:body ''
 533test_atom refs/tags/signed-empty contents:signature "$sig"
 534test_atom refs/tags/signed-empty contents "$sig"
 535
 536test_atom refs/tags/signed-short subject 'subject line'
 537test_atom refs/tags/signed-short contents:subject 'subject line'
 538test_atom refs/tags/signed-short body "$sig"
 539test_atom refs/tags/signed-short contents:body ''
 540test_atom refs/tags/signed-short contents:signature "$sig"
 541test_atom refs/tags/signed-short contents "subject line
 542$sig"
 543
 544test_atom refs/tags/signed-long subject 'subject line'
 545test_atom refs/tags/signed-long contents:subject 'subject line'
 546test_atom refs/tags/signed-long body "body contents
 547$sig"
 548test_atom refs/tags/signed-long contents:body 'body contents
 549'
 550test_atom refs/tags/signed-long contents:signature "$sig"
 551test_atom refs/tags/signed-long contents "subject line
 552
 553body contents
 554$sig"
 555
 556cat >expected <<EOF
 557$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
 558$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
 559EOF
 560
 561test_expect_success 'Verify sort with multiple keys' '
 562        git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
 563                refs/tags/bogo refs/tags/master > actual &&
 564        test_cmp expected actual
 565'
 566
 567test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
 568        test_when_finished "git checkout master" &&
 569        git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 570        sed -e "s/^\* /  /" actual >expect &&
 571        git checkout --orphan HEAD &&
 572        git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 573        test_cmp expect actual
 574'
 575
 576cat >trailers <<EOF
 577Reviewed-by: A U Thor <author@example.com>
 578Signed-off-by: A U Thor <author@example.com>
 579EOF
 580
 581test_expect_success 'basic atom: head contents:trailers' '
 582        echo "Some contents" > two &&
 583        git add two &&
 584        git commit -F - <<-EOF &&
 585        trailers: this commit message has trailers
 586
 587        Some message contents
 588
 589        $(cat trailers)
 590        EOF
 591        git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
 592        sanitize_pgp <actual >actual.clean &&
 593        # git for-each-ref ends with a blank line
 594        cat >expect <<-EOF &&
 595        $(cat trailers)
 596
 597        EOF
 598        test_cmp expect actual.clean
 599'
 600
 601test_done