t / t6300-for-each-ref.shon commit for-each-ref: introduce %(HEAD) asterisk marker (7a48b83)
   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 15:18:43 2006 +0000
  12datestamp=1151939923
  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'
  33
  34test_atom() {
  35        case "$1" in
  36                head) ref=refs/heads/master ;;
  37                 tag) ref=refs/tags/testtag ;;
  38                   *) ref=$1 ;;
  39        esac
  40        printf '%s\n' "$3" >expected
  41        test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
  42                git for-each-ref --format='%($2)' $ref >actual &&
  43                sanitize_pgp <actual >actual.clean &&
  44                test_cmp expected actual.clean
  45        "
  46}
  47
  48test_atom head refname refs/heads/master
  49test_atom head upstream refs/remotes/origin/master
  50test_atom head objecttype commit
  51test_atom head objectsize 171
  52test_atom head objectname $(git rev-parse refs/heads/master)
  53test_atom head tree $(git rev-parse refs/heads/master^{tree})
  54test_atom head parent ''
  55test_atom head numparent 0
  56test_atom head object ''
  57test_atom head type ''
  58test_atom head author 'A U Thor <author@example.com> 1151939924 +0200'
  59test_atom head authorname 'A U Thor'
  60test_atom head authoremail '<author@example.com>'
  61test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
  62test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
  63test_atom head committername 'C O Mitter'
  64test_atom head committeremail '<committer@example.com>'
  65test_atom head committerdate 'Mon Jul 3 17:18:43 2006 +0200'
  66test_atom head tag ''
  67test_atom head tagger ''
  68test_atom head taggername ''
  69test_atom head taggeremail ''
  70test_atom head taggerdate ''
  71test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
  72test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
  73test_atom head subject 'Initial'
  74test_atom head contents:subject 'Initial'
  75test_atom head body ''
  76test_atom head contents:body ''
  77test_atom head contents:signature ''
  78test_atom head contents 'Initial
  79'
  80test_atom head HEAD '*'
  81
  82test_atom tag refname refs/tags/testtag
  83test_atom tag upstream ''
  84test_atom tag objecttype tag
  85test_atom tag objectsize 154
  86test_atom tag objectname $(git rev-parse refs/tags/testtag)
  87test_atom tag tree ''
  88test_atom tag parent ''
  89test_atom tag numparent ''
  90test_atom tag object $(git rev-parse refs/tags/testtag^0)
  91test_atom tag type 'commit'
  92test_atom tag author ''
  93test_atom tag authorname ''
  94test_atom tag authoremail ''
  95test_atom tag authordate ''
  96test_atom tag committer ''
  97test_atom tag committername ''
  98test_atom tag committeremail ''
  99test_atom tag committerdate ''
 100test_atom tag tag 'testtag'
 101test_atom tag tagger 'C O Mitter <committer@example.com> 1151939925 +0200'
 102test_atom tag taggername 'C O Mitter'
 103test_atom tag taggeremail '<committer@example.com>'
 104test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
 105test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
 106test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
 107test_atom tag subject 'Tagging at 1151939927'
 108test_atom tag contents:subject 'Tagging at 1151939927'
 109test_atom tag body ''
 110test_atom tag contents:body ''
 111test_atom tag contents:signature ''
 112test_atom tag contents 'Tagging at 1151939927
 113'
 114test_atom tag HEAD ' '
 115
 116test_expect_success 'Check invalid atoms names are errors' '
 117        test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
 118'
 119
 120test_expect_success 'Check format specifiers are ignored in naming date atoms' '
 121        git for-each-ref --format="%(authordate)" refs/heads &&
 122        git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
 123        git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
 124        git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
 125'
 126
 127test_expect_success 'Check valid format specifiers for date fields' '
 128        git for-each-ref --format="%(authordate:default)" refs/heads &&
 129        git for-each-ref --format="%(authordate:relative)" refs/heads &&
 130        git for-each-ref --format="%(authordate:short)" refs/heads &&
 131        git for-each-ref --format="%(authordate:local)" refs/heads &&
 132        git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
 133        git for-each-ref --format="%(authordate:rfc2822)" refs/heads
 134'
 135
 136test_expect_success 'Check invalid format specifiers are errors' '
 137        test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
 138'
 139
 140cat >expected <<\EOF
 141'refs/heads/master' 'Mon Jul 3 17:18:43 2006 +0200' 'Mon Jul 3 17:18:44 2006 +0200'
 142'refs/tags/testtag' 'Mon Jul 3 17:18:45 2006 +0200'
 143EOF
 144
 145test_expect_success 'Check unformatted date fields output' '
 146        (git for-each-ref --shell --format="%(refname) %(committerdate) %(authordate)" refs/heads &&
 147        git for-each-ref --shell --format="%(refname) %(taggerdate)" refs/tags) >actual &&
 148        test_cmp expected actual
 149'
 150
 151test_expect_success 'Check format "default" formatted date fields output' '
 152        f=default &&
 153        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 154        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 155        test_cmp expected actual
 156'
 157
 158# Don't know how to do relative check because I can't know when this script
 159# is going to be run and can't fake the current time to git, and hence can't
 160# provide expected output.  Instead, I'll just make sure that "relative"
 161# doesn't exit in error
 162#
 163#cat >expected <<\EOF
 164#
 165#EOF
 166#
 167test_expect_success 'Check format "relative" date fields output' '
 168        f=relative &&
 169        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 170        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
 171'
 172
 173cat >expected <<\EOF
 174'refs/heads/master' '2006-07-03' '2006-07-03'
 175'refs/tags/testtag' '2006-07-03'
 176EOF
 177
 178test_expect_success 'Check format "short" date fields output' '
 179        f=short &&
 180        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 181        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 182        test_cmp expected actual
 183'
 184
 185cat >expected <<\EOF
 186'refs/heads/master' 'Mon Jul 3 15:18:43 2006' 'Mon Jul 3 15:18:44 2006'
 187'refs/tags/testtag' 'Mon Jul 3 15:18:45 2006'
 188EOF
 189
 190test_expect_success 'Check format "local" date fields output' '
 191        f=local &&
 192        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 193        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 194        test_cmp expected actual
 195'
 196
 197cat >expected <<\EOF
 198'refs/heads/master' '2006-07-03 17:18:43 +0200' '2006-07-03 17:18:44 +0200'
 199'refs/tags/testtag' '2006-07-03 17:18:45 +0200'
 200EOF
 201
 202test_expect_success 'Check format "iso8601" date fields output' '
 203        f=iso8601 &&
 204        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 205        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 206        test_cmp expected actual
 207'
 208
 209cat >expected <<\EOF
 210'refs/heads/master' 'Mon, 3 Jul 2006 17:18:43 +0200' 'Mon, 3 Jul 2006 17:18:44 +0200'
 211'refs/tags/testtag' 'Mon, 3 Jul 2006 17:18:45 +0200'
 212EOF
 213
 214test_expect_success 'Check format "rfc2822" date fields output' '
 215        f=rfc2822 &&
 216        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 217        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 218        test_cmp expected actual
 219'
 220
 221cat >expected <<\EOF
 222refs/heads/master
 223refs/remotes/origin/master
 224refs/tags/testtag
 225EOF
 226
 227test_expect_success 'Verify ascending sort' '
 228        git for-each-ref --format="%(refname)" --sort=refname >actual &&
 229        test_cmp expected actual
 230'
 231
 232
 233cat >expected <<\EOF
 234refs/tags/testtag
 235refs/remotes/origin/master
 236refs/heads/master
 237EOF
 238
 239test_expect_success 'Verify descending sort' '
 240        git for-each-ref --format="%(refname)" --sort=-refname >actual &&
 241        test_cmp expected actual
 242'
 243
 244cat >expected <<\EOF
 245'refs/heads/master'
 246'refs/remotes/origin/master'
 247'refs/tags/testtag'
 248EOF
 249
 250test_expect_success 'Quoting style: shell' '
 251        git for-each-ref --shell --format="%(refname)" >actual &&
 252        test_cmp expected actual
 253'
 254
 255test_expect_success 'Quoting style: perl' '
 256        git for-each-ref --perl --format="%(refname)" >actual &&
 257        test_cmp expected actual
 258'
 259
 260test_expect_success 'Quoting style: python' '
 261        git for-each-ref --python --format="%(refname)" >actual &&
 262        test_cmp expected actual
 263'
 264
 265cat >expected <<\EOF
 266"refs/heads/master"
 267"refs/remotes/origin/master"
 268"refs/tags/testtag"
 269EOF
 270
 271test_expect_success 'Quoting style: tcl' '
 272        git for-each-ref --tcl --format="%(refname)" >actual &&
 273        test_cmp expected actual
 274'
 275
 276for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 277        test_expect_success "more than one quoting style: $i" "
 278                git for-each-ref $i 2>&1 | (read line &&
 279                case \$line in
 280                \"error: more than one quoting style\"*) : happy;;
 281                *) false
 282                esac)
 283        "
 284done
 285
 286cat >expected <<\EOF
 287master
 288testtag
 289EOF
 290
 291test_expect_success 'Check short refname format' '
 292        (git for-each-ref --format="%(refname:short)" refs/heads &&
 293        git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
 294        test_cmp expected actual
 295'
 296
 297cat >expected <<EOF
 298origin/master
 299EOF
 300
 301test_expect_success 'Check short upstream format' '
 302        git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
 303        test_cmp expected actual
 304'
 305
 306cat >expected <<EOF
 307$(git rev-parse --short HEAD)
 308EOF
 309
 310test_expect_success 'Check short objectname format' '
 311        git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
 312        test_cmp expected actual
 313'
 314
 315test_expect_success 'Check for invalid refname format' '
 316        test_must_fail git for-each-ref --format="%(refname:INVALID)"
 317'
 318
 319cat >expected <<\EOF
 320heads/master
 321tags/master
 322EOF
 323
 324test_expect_success 'Check ambiguous head and tag refs (strict)' '
 325        git config --bool core.warnambiguousrefs true &&
 326        git checkout -b newtag &&
 327        echo "Using $datestamp" > one &&
 328        git add one &&
 329        git commit -m "Branch" &&
 330        setdate_and_increment &&
 331        git tag -m "Tagging at $datestamp" master &&
 332        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 333        test_cmp expected actual
 334'
 335
 336cat >expected <<\EOF
 337heads/master
 338master
 339EOF
 340
 341test_expect_success 'Check ambiguous head and tag refs (loose)' '
 342        git config --bool core.warnambiguousrefs false &&
 343        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 344        test_cmp expected actual
 345'
 346
 347cat >expected <<\EOF
 348heads/ambiguous
 349ambiguous
 350EOF
 351
 352test_expect_success 'Check ambiguous head and tag refs II (loose)' '
 353        git checkout master &&
 354        git tag ambiguous testtag^0 &&
 355        git branch ambiguous testtag^0 &&
 356        git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
 357        test_cmp expected actual
 358'
 359
 360test_expect_success 'an unusual tag with an incomplete line' '
 361
 362        git tag -m "bogo" bogo &&
 363        bogo=$(git cat-file tag bogo) &&
 364        bogo=$(printf "%s" "$bogo" | git mktag) &&
 365        git tag -f bogo "$bogo" &&
 366        git for-each-ref --format "%(body)" refs/tags/bogo
 367
 368'
 369
 370test_expect_success 'create tag with subject and body content' '
 371        cat >>msg <<-\EOF &&
 372                the subject line
 373
 374                first body line
 375                second body line
 376        EOF
 377        git tag -F msg subject-body
 378'
 379test_atom refs/tags/subject-body subject 'the subject line'
 380test_atom refs/tags/subject-body body 'first body line
 381second body line
 382'
 383test_atom refs/tags/subject-body contents 'the subject line
 384
 385first body line
 386second body line
 387'
 388
 389test_expect_success 'create tag with multiline subject' '
 390        cat >msg <<-\EOF &&
 391                first subject line
 392                second subject line
 393
 394                first body line
 395                second body line
 396        EOF
 397        git tag -F msg multiline
 398'
 399test_atom refs/tags/multiline subject 'first subject line second subject line'
 400test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
 401test_atom refs/tags/multiline body 'first body line
 402second body line
 403'
 404test_atom refs/tags/multiline contents:body 'first body line
 405second body line
 406'
 407test_atom refs/tags/multiline contents:signature ''
 408test_atom refs/tags/multiline contents 'first subject line
 409second subject line
 410
 411first body line
 412second body line
 413'
 414
 415test_expect_success GPG 'create signed tags' '
 416        git tag -s -m "" signed-empty &&
 417        git tag -s -m "subject line" signed-short &&
 418        cat >msg <<-\EOF &&
 419        subject line
 420
 421        body contents
 422        EOF
 423        git tag -s -F msg signed-long
 424'
 425
 426sig='-----BEGIN PGP SIGNATURE-----
 427-----END PGP SIGNATURE-----
 428'
 429
 430PREREQ=GPG
 431test_atom refs/tags/signed-empty subject ''
 432test_atom refs/tags/signed-empty contents:subject ''
 433test_atom refs/tags/signed-empty body "$sig"
 434test_atom refs/tags/signed-empty contents:body ''
 435test_atom refs/tags/signed-empty contents:signature "$sig"
 436test_atom refs/tags/signed-empty contents "$sig"
 437
 438test_atom refs/tags/signed-short subject 'subject line'
 439test_atom refs/tags/signed-short contents:subject 'subject line'
 440test_atom refs/tags/signed-short body "$sig"
 441test_atom refs/tags/signed-short contents:body ''
 442test_atom refs/tags/signed-short contents:signature "$sig"
 443test_atom refs/tags/signed-short contents "subject line
 444$sig"
 445
 446test_atom refs/tags/signed-long subject 'subject line'
 447test_atom refs/tags/signed-long contents:subject 'subject line'
 448test_atom refs/tags/signed-long body "body contents
 449$sig"
 450test_atom refs/tags/signed-long contents:body 'body contents
 451'
 452test_atom refs/tags/signed-long contents:signature "$sig"
 453test_atom refs/tags/signed-long contents "subject line
 454
 455body contents
 456$sig"
 457
 458cat >expected <<EOF
 459$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
 460$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
 461EOF
 462
 463test_expect_success 'Verify sort with multiple keys' '
 464        git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
 465                refs/tags/bogo refs/tags/master > actual &&
 466        test_cmp expected actual
 467'
 468test_done