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