097fdf21fe1964f888c8c49147cf869ae2a15278
   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. "$TEST_DIRECTORY"/lib-terminal.sh
  11
  12# Mon Jul 3 23:18:43 2006 +0000
  13datestamp=1151968723
  14setdate_and_increment () {
  15    GIT_COMMITTER_DATE="$datestamp +0200"
  16    datestamp=$(expr "$datestamp" + 1)
  17    GIT_AUTHOR_DATE="$datestamp +0200"
  18    datestamp=$(expr "$datestamp" + 1)
  19    export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
  20}
  21
  22test_expect_success setup '
  23        setdate_and_increment &&
  24        echo "Using $datestamp" > one &&
  25        git add one &&
  26        git commit -m "Initial" &&
  27        setdate_and_increment &&
  28        git tag -a -m "Tagging at $datestamp" testtag &&
  29        git update-ref refs/remotes/origin/master master &&
  30        git remote add origin nowhere &&
  31        git config branch.master.remote origin &&
  32        git config branch.master.merge refs/heads/master &&
  33        git remote add myfork elsewhere &&
  34        git config remote.pushdefault myfork &&
  35        git config push.default current
  36'
  37
  38test_atom() {
  39        case "$1" in
  40                head) ref=refs/heads/master ;;
  41                 tag) ref=refs/tags/testtag ;;
  42                 sym) ref=refs/heads/sym ;;
  43                   *) ref=$1 ;;
  44        esac
  45        printf '%s\n' "$3" >expected
  46        test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
  47                git for-each-ref --format='%($2)' $ref >actual &&
  48                sanitize_pgp <actual >actual.clean &&
  49                test_cmp expected actual.clean
  50        "
  51}
  52
  53test_atom head refname refs/heads/master
  54test_atom head refname: refs/heads/master
  55test_atom head refname:short master
  56test_atom head refname:lstrip=1 heads/master
  57test_atom head refname:lstrip=2 master
  58test_atom head refname:lstrip=-1 master
  59test_atom head refname:lstrip=-2 heads/master
  60test_atom head refname:rstrip=1 refs/heads
  61test_atom head refname:rstrip=2 refs
  62test_atom head refname:rstrip=-1 refs
  63test_atom head refname:rstrip=-2 refs/heads
  64test_atom head refname:strip=1 heads/master
  65test_atom head refname:strip=2 master
  66test_atom head refname:strip=-1 master
  67test_atom head refname:strip=-2 heads/master
  68test_atom head upstream refs/remotes/origin/master
  69test_atom head upstream:short origin/master
  70test_atom head upstream:lstrip=2 origin/master
  71test_atom head upstream:lstrip=-2 origin/master
  72test_atom head upstream:rstrip=2 refs/remotes
  73test_atom head upstream:rstrip=-2 refs/remotes
  74test_atom head upstream:strip=2 origin/master
  75test_atom head upstream:strip=-2 origin/master
  76test_atom head push refs/remotes/myfork/master
  77test_atom head push:short myfork/master
  78test_atom head push:lstrip=1 remotes/myfork/master
  79test_atom head push:lstrip=-1 master
  80test_atom head push:rstrip=1 refs/remotes/myfork
  81test_atom head push:rstrip=-1 refs
  82test_atom head push:strip=1 remotes/myfork/master
  83test_atom head push:strip=-1 master
  84test_atom head objecttype commit
  85test_atom head objectsize 171
  86test_atom head objectsize:disk 138
  87test_atom head objectname $(git rev-parse refs/heads/master)
  88test_atom head objectname:short $(git rev-parse --short refs/heads/master)
  89test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
  90test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
  91test_atom head tree $(git rev-parse refs/heads/master^{tree})
  92test_atom head parent ''
  93test_atom head numparent 0
  94test_atom head object ''
  95test_atom head type ''
  96test_atom head '*objectname' ''
  97test_atom head '*objecttype' ''
  98test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
  99test_atom head authorname 'A U Thor'
 100test_atom head authoremail '<author@example.com>'
 101test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
 102test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
 103test_atom head committername 'C O Mitter'
 104test_atom head committeremail '<committer@example.com>'
 105test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
 106test_atom head tag ''
 107test_atom head tagger ''
 108test_atom head taggername ''
 109test_atom head taggeremail ''
 110test_atom head taggerdate ''
 111test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
 112test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
 113test_atom head subject 'Initial'
 114test_atom head contents:subject 'Initial'
 115test_atom head body ''
 116test_atom head contents:body ''
 117test_atom head contents:signature ''
 118test_atom head contents 'Initial
 119'
 120test_atom head HEAD '*'
 121
 122test_atom tag refname refs/tags/testtag
 123test_atom tag refname:short testtag
 124test_atom tag upstream ''
 125test_atom tag push ''
 126test_atom tag objecttype tag
 127test_atom tag objectsize 154
 128test_atom tag objectsize:disk 138
 129test_atom tag '*objectsize:disk' 138
 130test_atom tag objectname $(git rev-parse refs/tags/testtag)
 131test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
 132test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
 133test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
 134test_atom tag tree ''
 135test_atom tag parent ''
 136test_atom tag numparent ''
 137test_atom tag object $(git rev-parse refs/tags/testtag^0)
 138test_atom tag type 'commit'
 139test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
 140test_atom tag '*objecttype' 'commit'
 141test_atom tag author ''
 142test_atom tag authorname ''
 143test_atom tag authoremail ''
 144test_atom tag authordate ''
 145test_atom tag committer ''
 146test_atom tag committername ''
 147test_atom tag committeremail ''
 148test_atom tag committerdate ''
 149test_atom tag tag 'testtag'
 150test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
 151test_atom tag taggername 'C O Mitter'
 152test_atom tag taggeremail '<committer@example.com>'
 153test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
 154test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
 155test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
 156test_atom tag subject 'Tagging at 1151968727'
 157test_atom tag contents:subject 'Tagging at 1151968727'
 158test_atom tag body ''
 159test_atom tag contents:body ''
 160test_atom tag contents:signature ''
 161test_atom tag contents 'Tagging at 1151968727
 162'
 163test_atom tag HEAD ' '
 164
 165test_expect_success 'Check invalid atoms names are errors' '
 166        test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
 167'
 168
 169test_expect_success 'Check format specifiers are ignored in naming date atoms' '
 170        git for-each-ref --format="%(authordate)" refs/heads &&
 171        git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
 172        git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
 173        git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
 174'
 175
 176test_expect_success 'Check valid format specifiers for date fields' '
 177        git for-each-ref --format="%(authordate:default)" refs/heads &&
 178        git for-each-ref --format="%(authordate:relative)" refs/heads &&
 179        git for-each-ref --format="%(authordate:short)" refs/heads &&
 180        git for-each-ref --format="%(authordate:local)" refs/heads &&
 181        git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
 182        git for-each-ref --format="%(authordate:rfc2822)" refs/heads
 183'
 184
 185test_expect_success 'Check invalid format specifiers are errors' '
 186        test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
 187'
 188
 189test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
 190        test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
 191        test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
 192        test_must_fail git for-each-ref --format="%(objectname:short=foo)"
 193'
 194
 195test_date () {
 196        f=$1 &&
 197        committer_date=$2 &&
 198        author_date=$3 &&
 199        tagger_date=$4 &&
 200        cat >expected <<-EOF &&
 201        'refs/heads/master' '$committer_date' '$author_date'
 202        'refs/tags/testtag' '$tagger_date'
 203        EOF
 204        (
 205                git for-each-ref --shell \
 206                        --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
 207                        refs/heads &&
 208                git for-each-ref --shell \
 209                        --format="%(refname) %(taggerdate${f:+:$f})" \
 210                        refs/tags
 211        ) >actual &&
 212        test_cmp expected actual
 213}
 214
 215test_expect_success 'Check unformatted date fields output' '
 216        test_date "" \
 217                "Tue Jul 4 01:18:43 2006 +0200" \
 218                "Tue Jul 4 01:18:44 2006 +0200" \
 219                "Tue Jul 4 01:18:45 2006 +0200"
 220'
 221
 222test_expect_success 'Check format "default" formatted date fields output' '
 223        test_date default \
 224                "Tue Jul 4 01:18:43 2006 +0200" \
 225                "Tue Jul 4 01:18:44 2006 +0200" \
 226                "Tue Jul 4 01:18:45 2006 +0200"
 227'
 228
 229test_expect_success 'Check format "default-local" date fields output' '
 230        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"
 231'
 232
 233# Don't know how to do relative check because I can't know when this script
 234# is going to be run and can't fake the current time to git, and hence can't
 235# provide expected output.  Instead, I'll just make sure that "relative"
 236# doesn't exit in error
 237test_expect_success 'Check format "relative" date fields output' '
 238        f=relative &&
 239        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 240        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
 241'
 242
 243# We just check that this is the same as "relative" for now.
 244test_expect_success 'Check format "relative-local" date fields output' '
 245        test_date relative-local \
 246                "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
 247                "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
 248                "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
 249'
 250
 251test_expect_success 'Check format "short" date fields output' '
 252        test_date short 2006-07-04 2006-07-04 2006-07-04
 253'
 254
 255test_expect_success 'Check format "short-local" date fields output' '
 256        test_date short-local 2006-07-03 2006-07-03 2006-07-03
 257'
 258
 259test_expect_success 'Check format "local" date fields output' '
 260        test_date local \
 261                "Mon Jul 3 23:18:43 2006" \
 262                "Mon Jul 3 23:18:44 2006" \
 263                "Mon Jul 3 23:18:45 2006"
 264'
 265
 266test_expect_success 'Check format "iso8601" date fields output' '
 267        test_date iso8601 \
 268                "2006-07-04 01:18:43 +0200" \
 269                "2006-07-04 01:18:44 +0200" \
 270                "2006-07-04 01:18:45 +0200"
 271'
 272
 273test_expect_success 'Check format "iso8601-local" date fields output' '
 274        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"
 275'
 276
 277test_expect_success 'Check format "rfc2822" date fields output' '
 278        test_date rfc2822 \
 279                "Tue, 4 Jul 2006 01:18:43 +0200" \
 280                "Tue, 4 Jul 2006 01:18:44 +0200" \
 281                "Tue, 4 Jul 2006 01:18:45 +0200"
 282'
 283
 284test_expect_success 'Check format "rfc2822-local" date fields output' '
 285        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"
 286'
 287
 288test_expect_success 'Check format "raw" date fields output' '
 289        test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
 290'
 291
 292test_expect_success 'Check format "raw-local" date fields output' '
 293        test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
 294'
 295
 296test_expect_success 'Check format of strftime date fields' '
 297        echo "my date is 2006-07-04" >expected &&
 298        git for-each-ref \
 299          --format="%(authordate:format:my date is %Y-%m-%d)" \
 300          refs/heads >actual &&
 301        test_cmp expected actual
 302'
 303
 304test_expect_success 'Check format of strftime-local date fields' '
 305        echo "my date is 2006-07-03" >expected &&
 306        git for-each-ref \
 307          --format="%(authordate:format-local:my date is %Y-%m-%d)" \
 308          refs/heads >actual &&
 309        test_cmp expected actual
 310'
 311
 312test_expect_success 'exercise strftime with odd fields' '
 313        echo >expected &&
 314        git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
 315        test_cmp expected actual &&
 316        long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
 317        echo $long >expected &&
 318        git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
 319        test_cmp expected actual
 320'
 321
 322cat >expected <<\EOF
 323refs/heads/master
 324refs/remotes/origin/master
 325refs/tags/testtag
 326EOF
 327
 328test_expect_success 'Verify ascending sort' '
 329        git for-each-ref --format="%(refname)" --sort=refname >actual &&
 330        test_cmp expected actual
 331'
 332
 333
 334cat >expected <<\EOF
 335refs/tags/testtag
 336refs/remotes/origin/master
 337refs/heads/master
 338EOF
 339
 340test_expect_success 'Verify descending sort' '
 341        git for-each-ref --format="%(refname)" --sort=-refname >actual &&
 342        test_cmp expected actual
 343'
 344
 345cat >expected <<\EOF
 346'refs/heads/master'
 347'refs/remotes/origin/master'
 348'refs/tags/testtag'
 349EOF
 350
 351test_expect_success 'Quoting style: shell' '
 352        git for-each-ref --shell --format="%(refname)" >actual &&
 353        test_cmp expected actual
 354'
 355
 356test_expect_success 'Quoting style: perl' '
 357        git for-each-ref --perl --format="%(refname)" >actual &&
 358        test_cmp expected actual
 359'
 360
 361test_expect_success 'Quoting style: python' '
 362        git for-each-ref --python --format="%(refname)" >actual &&
 363        test_cmp expected actual
 364'
 365
 366cat >expected <<\EOF
 367"refs/heads/master"
 368"refs/remotes/origin/master"
 369"refs/tags/testtag"
 370EOF
 371
 372test_expect_success 'Quoting style: tcl' '
 373        git for-each-ref --tcl --format="%(refname)" >actual &&
 374        test_cmp expected actual
 375'
 376
 377for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 378        test_expect_success "more than one quoting style: $i" "
 379                test_must_fail git for-each-ref $i 2>err &&
 380                grep '^error: more than one quoting style' err
 381        "
 382done
 383
 384test_expect_success 'setup for upstream:track[short]' '
 385        test_commit two
 386'
 387
 388test_atom head upstream:track '[ahead 1]'
 389test_atom head upstream:trackshort '>'
 390test_atom head upstream:track,nobracket 'ahead 1'
 391test_atom head upstream:nobracket,track 'ahead 1'
 392test_atom head push:track '[ahead 1]'
 393test_atom head push:trackshort '>'
 394
 395test_expect_success 'Check that :track[short] cannot be used with other atoms' '
 396        test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
 397        test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
 398'
 399
 400test_expect_success 'Check that :track[short] works when upstream is invalid' '
 401        cat >expected <<-\EOF &&
 402        [gone]
 403
 404        EOF
 405        test_when_finished "git config branch.master.merge refs/heads/master" &&
 406        git config branch.master.merge refs/heads/does-not-exist &&
 407        git for-each-ref \
 408                --format="%(upstream:track)$LF%(upstream:trackshort)" \
 409                refs/heads >actual &&
 410        test_cmp expected actual
 411'
 412
 413test_expect_success 'Check for invalid refname format' '
 414        test_must_fail git for-each-ref --format="%(refname:INVALID)"
 415'
 416
 417test_expect_success 'set up color tests' '
 418        cat >expected.color <<-EOF &&
 419        $(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
 420        $(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
 421        $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
 422        $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
 423        EOF
 424        sed "s/<[^>]*>//g" <expected.color >expected.bare &&
 425        color_format="%(objectname:short) %(color:green)%(refname:short)"
 426'
 427
 428test_expect_success TTY '%(color) shows color with a tty' '
 429        test_terminal git for-each-ref --format="$color_format" >actual.raw &&
 430        test_decode_color <actual.raw >actual &&
 431        test_cmp expected.color actual
 432'
 433
 434test_expect_success '%(color) does not show color without tty' '
 435        TERM=vt100 git for-each-ref --format="$color_format" >actual &&
 436        test_cmp expected.bare actual
 437'
 438
 439test_expect_success '--color can override tty check' '
 440        git for-each-ref --color --format="$color_format" >actual.raw &&
 441        test_decode_color <actual.raw >actual &&
 442        test_cmp expected.color actual
 443'
 444
 445test_expect_success 'color.ui=always does not override tty check' '
 446        git -c color.ui=always for-each-ref --format="$color_format" >actual &&
 447        test_cmp expected.bare actual
 448'
 449
 450cat >expected <<\EOF
 451heads/master
 452tags/master
 453EOF
 454
 455test_expect_success 'Check ambiguous head and tag refs (strict)' '
 456        git config --bool core.warnambiguousrefs true &&
 457        git checkout -b newtag &&
 458        echo "Using $datestamp" > one &&
 459        git add one &&
 460        git commit -m "Branch" &&
 461        setdate_and_increment &&
 462        git tag -m "Tagging at $datestamp" master &&
 463        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 464        test_cmp expected actual
 465'
 466
 467cat >expected <<\EOF
 468heads/master
 469master
 470EOF
 471
 472test_expect_success 'Check ambiguous head and tag refs (loose)' '
 473        git config --bool core.warnambiguousrefs false &&
 474        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 475        test_cmp expected actual
 476'
 477
 478cat >expected <<\EOF
 479heads/ambiguous
 480ambiguous
 481EOF
 482
 483test_expect_success 'Check ambiguous head and tag refs II (loose)' '
 484        git checkout master &&
 485        git tag ambiguous testtag^0 &&
 486        git branch ambiguous testtag^0 &&
 487        git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
 488        test_cmp expected actual
 489'
 490
 491test_expect_success 'an unusual tag with an incomplete line' '
 492
 493        git tag -m "bogo" bogo &&
 494        bogo=$(git cat-file tag bogo) &&
 495        bogo=$(printf "%s" "$bogo" | git mktag) &&
 496        git tag -f bogo "$bogo" &&
 497        git for-each-ref --format "%(body)" refs/tags/bogo
 498
 499'
 500
 501test_expect_success 'create tag with subject and body content' '
 502        cat >>msg <<-\EOF &&
 503                the subject line
 504
 505                first body line
 506                second body line
 507        EOF
 508        git tag -F msg subject-body
 509'
 510test_atom refs/tags/subject-body subject 'the subject line'
 511test_atom refs/tags/subject-body body 'first body line
 512second body line
 513'
 514test_atom refs/tags/subject-body contents 'the subject line
 515
 516first body line
 517second body line
 518'
 519
 520test_expect_success 'create tag with multiline subject' '
 521        cat >msg <<-\EOF &&
 522                first subject line
 523                second subject line
 524
 525                first body line
 526                second body line
 527        EOF
 528        git tag -F msg multiline
 529'
 530test_atom refs/tags/multiline subject 'first subject line second subject line'
 531test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
 532test_atom refs/tags/multiline body 'first body line
 533second body line
 534'
 535test_atom refs/tags/multiline contents:body 'first body line
 536second body line
 537'
 538test_atom refs/tags/multiline contents:signature ''
 539test_atom refs/tags/multiline contents 'first subject line
 540second subject line
 541
 542first body line
 543second body line
 544'
 545
 546test_expect_success GPG 'create signed tags' '
 547        git tag -s -m "" signed-empty &&
 548        git tag -s -m "subject line" signed-short &&
 549        cat >msg <<-\EOF &&
 550        subject line
 551
 552        body contents
 553        EOF
 554        git tag -s -F msg signed-long
 555'
 556
 557sig='-----BEGIN PGP SIGNATURE-----
 558-----END PGP SIGNATURE-----
 559'
 560
 561PREREQ=GPG
 562test_atom refs/tags/signed-empty subject ''
 563test_atom refs/tags/signed-empty contents:subject ''
 564test_atom refs/tags/signed-empty body "$sig"
 565test_atom refs/tags/signed-empty contents:body ''
 566test_atom refs/tags/signed-empty contents:signature "$sig"
 567test_atom refs/tags/signed-empty contents "$sig"
 568
 569test_atom refs/tags/signed-short subject 'subject line'
 570test_atom refs/tags/signed-short contents:subject 'subject line'
 571test_atom refs/tags/signed-short body "$sig"
 572test_atom refs/tags/signed-short contents:body ''
 573test_atom refs/tags/signed-short contents:signature "$sig"
 574test_atom refs/tags/signed-short contents "subject line
 575$sig"
 576
 577test_atom refs/tags/signed-long subject 'subject line'
 578test_atom refs/tags/signed-long contents:subject 'subject line'
 579test_atom refs/tags/signed-long body "body contents
 580$sig"
 581test_atom refs/tags/signed-long contents:body 'body contents
 582'
 583test_atom refs/tags/signed-long contents:signature "$sig"
 584test_atom refs/tags/signed-long contents "subject line
 585
 586body contents
 587$sig"
 588
 589cat >expected <<EOF
 590$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
 591$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
 592EOF
 593
 594test_expect_success 'Verify sort with multiple keys' '
 595        git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
 596                refs/tags/bogo refs/tags/master > actual &&
 597        test_cmp expected actual
 598'
 599
 600
 601test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
 602        test_when_finished "git checkout master" &&
 603        git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 604        sed -e "s/^\* /  /" actual >expect &&
 605        git checkout --orphan orphaned-branch &&
 606        git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 607        test_cmp expect actual
 608'
 609
 610cat >trailers <<EOF
 611Reviewed-by: A U Thor <author@example.com>
 612Signed-off-by: A U Thor <author@example.com>
 613[ v2 updated patch description ]
 614Acked-by: A U Thor
 615  <author@example.com>
 616EOF
 617
 618unfold () {
 619        perl -0pe 's/\n\s+/ /g'
 620}
 621
 622test_expect_success 'set up trailers for next test' '
 623        echo "Some contents" > two &&
 624        git add two &&
 625        git commit -F - <<-EOF
 626        trailers: this commit message has trailers
 627
 628        Some message contents
 629
 630        $(cat trailers)
 631        EOF
 632'
 633
 634test_expect_success '%(trailers:unfold) unfolds trailers' '
 635        git for-each-ref --format="%(trailers:unfold)" refs/heads/master >actual &&
 636        {
 637                unfold <trailers
 638                echo
 639        } >expect &&
 640        test_cmp expect actual
 641'
 642
 643test_expect_success '%(trailers:only) shows only "key: value" trailers' '
 644        git for-each-ref --format="%(trailers:only)" refs/heads/master >actual &&
 645        {
 646                grep -v patch.description <trailers &&
 647                echo
 648        } >expect &&
 649        test_cmp expect actual
 650'
 651
 652test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
 653        git for-each-ref --format="%(trailers:only,unfold)" refs/heads/master >actual &&
 654        git for-each-ref --format="%(trailers:unfold,only)" refs/heads/master >reverse &&
 655        test_cmp actual reverse &&
 656        {
 657                grep -v patch.description <trailers | unfold &&
 658                echo
 659        } >expect &&
 660        test_cmp expect actual
 661'
 662
 663test_expect_success '%(contents:trailers:unfold) unfolds trailers' '
 664        git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/master >actual &&
 665        {
 666                unfold <trailers
 667                echo
 668        } >expect &&
 669        test_cmp expect actual
 670'
 671
 672test_expect_success '%(contents:trailers:only) shows only "key: value" trailers' '
 673        git for-each-ref --format="%(contents:trailers:only)" refs/heads/master >actual &&
 674        {
 675                grep -v patch.description <trailers &&
 676                echo
 677        } >expect &&
 678        test_cmp expect actual
 679'
 680
 681test_expect_success '%(contents:trailers:only) and %(contents:trailers:unfold) work together' '
 682        git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/master >actual &&
 683        git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/master >reverse &&
 684        test_cmp actual reverse &&
 685        {
 686                grep -v patch.description <trailers | unfold &&
 687                echo
 688        } >expect &&
 689        test_cmp expect actual
 690'
 691
 692test_expect_success '%(trailers) rejects unknown trailers arguments' '
 693        # error message cannot be checked under i18n
 694        cat >expect <<-EOF &&
 695        fatal: unknown %(trailers) argument: unsupported
 696        EOF
 697        test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
 698        test_i18ncmp expect actual
 699'
 700
 701test_expect_success '%(contents:trailers) rejects unknown trailers arguments' '
 702        # error message cannot be checked under i18n
 703        cat >expect <<-EOF &&
 704        fatal: unknown %(trailers) argument: unsupported
 705        EOF
 706        test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
 707        test_i18ncmp expect actual
 708'
 709
 710test_expect_success 'basic atom: head contents:trailers' '
 711        git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
 712        sanitize_pgp <actual >actual.clean &&
 713        # git for-each-ref ends with a blank line
 714        cat >expect <<-EOF &&
 715        $(cat trailers)
 716
 717        EOF
 718        test_cmp expect actual.clean
 719'
 720
 721test_expect_success 'trailer parsing not fooled by --- line' '
 722        git commit --allow-empty -F - <<-\EOF &&
 723        this is the subject
 724
 725        This is the body. The message has a "---" line which would confuse a
 726        message+patch parser. But here we know we have only a commit message,
 727        so we get it right.
 728
 729        trailer: wrong
 730        ---
 731        This is more body.
 732
 733        trailer: right
 734        EOF
 735
 736        {
 737                echo "trailer: right" &&
 738                echo
 739        } >expect &&
 740        git for-each-ref --format="%(trailers)" refs/heads/master >actual &&
 741        test_cmp expect actual
 742'
 743
 744test_expect_success 'Add symbolic ref for the following tests' '
 745        git symbolic-ref refs/heads/sym refs/heads/master
 746'
 747
 748cat >expected <<EOF
 749refs/heads/master
 750EOF
 751
 752test_expect_success 'Verify usage of %(symref) atom' '
 753        git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
 754        test_cmp expected actual
 755'
 756
 757cat >expected <<EOF
 758heads/master
 759EOF
 760
 761test_expect_success 'Verify usage of %(symref:short) atom' '
 762        git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
 763        test_cmp expected actual
 764'
 765
 766cat >expected <<EOF
 767master
 768heads/master
 769EOF
 770
 771test_expect_success 'Verify usage of %(symref:lstrip) atom' '
 772        git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
 773        git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
 774        test_cmp expected actual &&
 775
 776        git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
 777        git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
 778        test_cmp expected actual
 779'
 780
 781cat >expected <<EOF
 782refs
 783refs/heads
 784EOF
 785
 786test_expect_success 'Verify usage of %(symref:rstrip) atom' '
 787        git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
 788        git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
 789        test_cmp expected actual
 790'
 791
 792test_expect_success ':remotename and :remoteref' '
 793        git init remote-tests &&
 794        (
 795                cd remote-tests &&
 796                test_commit initial &&
 797                git remote add from fifth.coffee:blub &&
 798                git config branch.master.remote from &&
 799                git config branch.master.merge refs/heads/stable &&
 800                git remote add to southridge.audio:repo &&
 801                git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
 802                git config branch.master.pushRemote to &&
 803                for pair in "%(upstream)=refs/remotes/from/stable" \
 804                        "%(upstream:remotename)=from" \
 805                        "%(upstream:remoteref)=refs/heads/stable" \
 806                        "%(push)=refs/remotes/to/pushed/master" \
 807                        "%(push:remotename)=to" \
 808                        "%(push:remoteref)=refs/heads/pushed/master"
 809                do
 810                        echo "${pair#*=}" >expect &&
 811                        git for-each-ref --format="${pair%=*}" \
 812                                refs/heads/master >actual &&
 813                        test_cmp expect actual
 814                done &&
 815                git branch push-simple &&
 816                git config branch.push-simple.pushRemote from &&
 817                actual="$(git for-each-ref \
 818                        --format="%(push:remotename),%(push:remoteref)" \
 819                        refs/heads/push-simple)" &&
 820                test from, = "$actual"
 821        )
 822'
 823
 824test_expect_success 'for-each-ref --ignore-case ignores case' '
 825        git for-each-ref --format="%(refname)" refs/heads/MASTER >actual &&
 826        test_must_be_empty actual &&
 827
 828        echo refs/heads/master >expect &&
 829        git for-each-ref --format="%(refname)" --ignore-case \
 830                refs/heads/MASTER >actual &&
 831        test_cmp expect actual
 832'
 833
 834test_done