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