859b2374269e630db3653fc153267bb9382fe748
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Andy Parkins
   4#
   5
   6test_description='for-each-ref test'
   7
   8. ./test-lib.sh
   9. "$TEST_DIRECTORY"/lib-gpg.sh
  10
  11# Mon Jul 3 23:18:43 2006 +0000
  12datestamp=1151968723
  13setdate_and_increment () {
  14    GIT_COMMITTER_DATE="$datestamp +0200"
  15    datestamp=$(expr "$datestamp" + 1)
  16    GIT_AUTHOR_DATE="$datestamp +0200"
  17    datestamp=$(expr "$datestamp" + 1)
  18    export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
  19}
  20
  21test_expect_success setup '
  22        setdate_and_increment &&
  23        echo "Using $datestamp" > one &&
  24        git add one &&
  25        git commit -m "Initial" &&
  26        setdate_and_increment &&
  27        git tag -a -m "Tagging at $datestamp" testtag &&
  28        git update-ref refs/remotes/origin/master master &&
  29        git remote add origin nowhere &&
  30        git config branch.master.remote origin &&
  31        git config branch.master.merge refs/heads/master &&
  32        git remote add myfork elsewhere &&
  33        git config remote.pushdefault myfork &&
  34        git config push.default current
  35'
  36
  37test_atom() {
  38        case "$1" in
  39                head) ref=refs/heads/master ;;
  40                 tag) ref=refs/tags/testtag ;;
  41                   *) ref=$1 ;;
  42        esac
  43        printf '%s\n' "$3" >expected
  44        test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
  45                git for-each-ref --format='%($2)' $ref >actual &&
  46                sanitize_pgp <actual >actual.clean &&
  47                test_cmp expected actual.clean
  48        "
  49}
  50
  51test_atom head refname refs/heads/master
  52test_atom head refname:short master
  53test_atom head upstream refs/remotes/origin/master
  54test_atom head upstream:short origin/master
  55test_atom head push refs/remotes/myfork/master
  56test_atom head push:short myfork/master
  57test_atom head objecttype commit
  58test_atom head objectsize 171
  59test_atom head objectname $(git rev-parse refs/heads/master)
  60test_atom head objectname:short $(git rev-parse --short refs/heads/master)
  61test_atom head tree $(git rev-parse refs/heads/master^{tree})
  62test_atom head parent ''
  63test_atom head numparent 0
  64test_atom head object ''
  65test_atom head type ''
  66test_atom head '*objectname' ''
  67test_atom head '*objecttype' ''
  68test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
  69test_atom head authorname 'A U Thor'
  70test_atom head authoremail '<author@example.com>'
  71test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
  72test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
  73test_atom head committername 'C O Mitter'
  74test_atom head committeremail '<committer@example.com>'
  75test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
  76test_atom head tag ''
  77test_atom head tagger ''
  78test_atom head taggername ''
  79test_atom head taggeremail ''
  80test_atom head taggerdate ''
  81test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
  82test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
  83test_atom head subject 'Initial'
  84test_atom head contents:subject 'Initial'
  85test_atom head body ''
  86test_atom head contents:body ''
  87test_atom head contents:signature ''
  88test_atom head contents 'Initial
  89'
  90test_atom head HEAD '*'
  91
  92test_atom tag refname refs/tags/testtag
  93test_atom tag refname:short testtag
  94test_atom tag upstream ''
  95test_atom tag push ''
  96test_atom tag objecttype tag
  97test_atom tag objectsize 154
  98test_atom tag objectname $(git rev-parse refs/tags/testtag)
  99test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
 100test_atom tag tree ''
 101test_atom tag parent ''
 102test_atom tag numparent ''
 103test_atom tag object $(git rev-parse refs/tags/testtag^0)
 104test_atom tag type 'commit'
 105test_atom tag '*objectname' 'ea122842f48be4afb2d1fc6a4b96c05885ab7463'
 106test_atom tag '*objecttype' 'commit'
 107test_atom tag author ''
 108test_atom tag authorname ''
 109test_atom tag authoremail ''
 110test_atom tag authordate ''
 111test_atom tag committer ''
 112test_atom tag committername ''
 113test_atom tag committeremail ''
 114test_atom tag committerdate ''
 115test_atom tag tag 'testtag'
 116test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
 117test_atom tag taggername 'C O Mitter'
 118test_atom tag taggeremail '<committer@example.com>'
 119test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
 120test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
 121test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
 122test_atom tag subject 'Tagging at 1151968727'
 123test_atom tag contents:subject 'Tagging at 1151968727'
 124test_atom tag body ''
 125test_atom tag contents:body ''
 126test_atom tag contents:signature ''
 127test_atom tag contents 'Tagging at 1151968727
 128'
 129test_atom tag HEAD ' '
 130
 131test_expect_success 'Check invalid atoms names are errors' '
 132        test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
 133'
 134
 135test_expect_success 'Check format specifiers are ignored in naming date atoms' '
 136        git for-each-ref --format="%(authordate)" refs/heads &&
 137        git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
 138        git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
 139        git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
 140'
 141
 142test_expect_success 'Check valid format specifiers for date fields' '
 143        git for-each-ref --format="%(authordate:default)" refs/heads &&
 144        git for-each-ref --format="%(authordate:relative)" refs/heads &&
 145        git for-each-ref --format="%(authordate:short)" refs/heads &&
 146        git for-each-ref --format="%(authordate:local)" refs/heads &&
 147        git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
 148        git for-each-ref --format="%(authordate:rfc2822)" refs/heads
 149'
 150
 151test_expect_success 'Check invalid format specifiers are errors' '
 152        test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
 153'
 154
 155test_date () {
 156        f=$1 &&
 157        committer_date=$2 &&
 158        author_date=$3 &&
 159        tagger_date=$4 &&
 160        cat >expected <<-EOF &&
 161        'refs/heads/master' '$committer_date' '$author_date'
 162        'refs/tags/testtag' '$tagger_date'
 163        EOF
 164        (
 165                git for-each-ref --shell \
 166                        --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
 167                        refs/heads &&
 168                git for-each-ref --shell \
 169                        --format="%(refname) %(taggerdate${f:+:$f})" \
 170                        refs/tags
 171        ) >actual &&
 172        test_cmp expected actual
 173}
 174
 175test_expect_success 'Check unformatted date fields output' '
 176        test_date "" \
 177                "Tue Jul 4 01:18:43 2006 +0200" \
 178                "Tue Jul 4 01:18:44 2006 +0200" \
 179                "Tue Jul 4 01:18:45 2006 +0200"
 180'
 181
 182test_expect_success 'Check format "default" formatted date fields output' '
 183        test_date default \
 184                "Tue Jul 4 01:18:43 2006 +0200" \
 185                "Tue Jul 4 01:18:44 2006 +0200" \
 186                "Tue Jul 4 01:18:45 2006 +0200"
 187'
 188
 189test_expect_success 'Check format "default-local" date fields output' '
 190        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"
 191'
 192
 193# Don't know how to do relative check because I can't know when this script
 194# is going to be run and can't fake the current time to git, and hence can't
 195# provide expected output.  Instead, I'll just make sure that "relative"
 196# doesn't exit in error
 197test_expect_success 'Check format "relative" date fields output' '
 198        f=relative &&
 199        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 200        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
 201'
 202
 203# We just check that this is the same as "relative" for now.
 204test_expect_success 'Check format "relative-local" date fields output' '
 205        test_date relative-local \
 206                "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
 207                "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
 208                "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
 209'
 210
 211test_expect_success 'Check format "short" date fields output' '
 212        test_date short 2006-07-04 2006-07-04 2006-07-04
 213'
 214
 215test_expect_success 'Check format "short-local" date fields output' '
 216        test_date short-local 2006-07-03 2006-07-03 2006-07-03
 217'
 218
 219test_expect_success 'Check format "local" date fields output' '
 220        test_date local \
 221                "Mon Jul 3 23:18:43 2006" \
 222                "Mon Jul 3 23:18:44 2006" \
 223                "Mon Jul 3 23:18:45 2006"
 224'
 225
 226test_expect_success 'Check format "iso8601" date fields output' '
 227        test_date iso8601 \
 228                "2006-07-04 01:18:43 +0200" \
 229                "2006-07-04 01:18:44 +0200" \
 230                "2006-07-04 01:18:45 +0200"
 231'
 232
 233test_expect_success 'Check format "iso8601-local" date fields output' '
 234        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"
 235'
 236
 237test_expect_success 'Check format "rfc2822" date fields output' '
 238        test_date rfc2822 \
 239                "Tue, 4 Jul 2006 01:18:43 +0200" \
 240                "Tue, 4 Jul 2006 01:18:44 +0200" \
 241                "Tue, 4 Jul 2006 01:18:45 +0200"
 242'
 243
 244test_expect_success 'Check format "rfc2822-local" date fields output' '
 245        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"
 246'
 247
 248test_expect_success 'Check format "raw" date fields output' '
 249        test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
 250'
 251
 252test_expect_success 'Check format "raw-local" date fields output' '
 253        test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
 254'
 255
 256test_expect_success 'Check format of strftime date fields' '
 257        echo "my date is 2006-07-04" >expected &&
 258        git for-each-ref \
 259          --format="%(authordate:format:my date is %Y-%m-%d)" \
 260          refs/heads >actual &&
 261        test_cmp expected actual
 262'
 263
 264test_expect_success 'Check format of strftime-local date fields' '
 265        echo "my date is 2006-07-03" >expected &&
 266        git for-each-ref \
 267          --format="%(authordate:format-local:my date is %Y-%m-%d)" \
 268          refs/heads >actual &&
 269        test_cmp expected actual
 270'
 271
 272test_expect_success 'exercise strftime with odd fields' '
 273        echo >expected &&
 274        git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
 275        test_cmp expected actual &&
 276        long="long format -- $_z40$_z40$_z40$_z40$_z40$_z40$_z40" &&
 277        echo $long >expected &&
 278        git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
 279        test_cmp expected actual
 280'
 281
 282cat >expected <<\EOF
 283refs/heads/master
 284refs/remotes/origin/master
 285refs/tags/testtag
 286EOF
 287
 288test_expect_success 'Verify ascending sort' '
 289        git for-each-ref --format="%(refname)" --sort=refname >actual &&
 290        test_cmp expected actual
 291'
 292
 293
 294cat >expected <<\EOF
 295refs/tags/testtag
 296refs/remotes/origin/master
 297refs/heads/master
 298EOF
 299
 300test_expect_success 'Verify descending sort' '
 301        git for-each-ref --format="%(refname)" --sort=-refname >actual &&
 302        test_cmp expected actual
 303'
 304
 305cat >expected <<\EOF
 306'refs/heads/master'
 307'refs/remotes/origin/master'
 308'refs/tags/testtag'
 309EOF
 310
 311test_expect_success 'Quoting style: shell' '
 312        git for-each-ref --shell --format="%(refname)" >actual &&
 313        test_cmp expected actual
 314'
 315
 316test_expect_success 'Quoting style: perl' '
 317        git for-each-ref --perl --format="%(refname)" >actual &&
 318        test_cmp expected actual
 319'
 320
 321test_expect_success 'Quoting style: python' '
 322        git for-each-ref --python --format="%(refname)" >actual &&
 323        test_cmp expected actual
 324'
 325
 326cat >expected <<\EOF
 327"refs/heads/master"
 328"refs/remotes/origin/master"
 329"refs/tags/testtag"
 330EOF
 331
 332test_expect_success 'Quoting style: tcl' '
 333        git for-each-ref --tcl --format="%(refname)" >actual &&
 334        test_cmp expected actual
 335'
 336
 337for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 338        test_expect_success "more than one quoting style: $i" "
 339                git for-each-ref $i 2>&1 | (read line &&
 340                case \$line in
 341                \"error: more than one quoting style\"*) : happy;;
 342                *) false
 343                esac)
 344        "
 345done
 346
 347test_expect_success 'setup for upstream:track[short]' '
 348        test_commit two
 349'
 350
 351test_atom head upstream:track '[ahead 1]'
 352test_atom head upstream:trackshort '>'
 353test_atom head push:track '[ahead 1]'
 354test_atom head push:trackshort '>'
 355
 356test_expect_success 'Check that :track[short] cannot be used with other atoms' '
 357        test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
 358        test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
 359'
 360
 361test_expect_success 'Check that :track[short] works when upstream is invalid' '
 362        cat >expected <<-\EOF &&
 363
 364
 365        EOF
 366        test_when_finished "git config branch.master.merge refs/heads/master" &&
 367        git config branch.master.merge refs/heads/does-not-exist &&
 368        git for-each-ref \
 369                --format="%(upstream:track)$LF%(upstream:trackshort)" \
 370                refs/heads >actual &&
 371        test_cmp expected actual
 372'
 373
 374test_expect_success 'Check for invalid refname format' '
 375        test_must_fail git for-each-ref --format="%(refname:INVALID)"
 376'
 377
 378get_color ()
 379{
 380        git config --get-color no.such.slot "$1"
 381}
 382
 383cat >expected <<EOF
 384$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
 385$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
 386$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
 387$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
 388EOF
 389
 390test_expect_success 'Check %(color:...) ' '
 391        git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)" >actual &&
 392        test_cmp expected actual
 393'
 394
 395cat >expected <<\EOF
 396heads/master
 397tags/master
 398EOF
 399
 400test_expect_success 'Check ambiguous head and tag refs (strict)' '
 401        git config --bool core.warnambiguousrefs true &&
 402        git checkout -b newtag &&
 403        echo "Using $datestamp" > one &&
 404        git add one &&
 405        git commit -m "Branch" &&
 406        setdate_and_increment &&
 407        git tag -m "Tagging at $datestamp" master &&
 408        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 409        test_cmp expected actual
 410'
 411
 412cat >expected <<\EOF
 413heads/master
 414master
 415EOF
 416
 417test_expect_success 'Check ambiguous head and tag refs (loose)' '
 418        git config --bool core.warnambiguousrefs false &&
 419        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 420        test_cmp expected actual
 421'
 422
 423cat >expected <<\EOF
 424heads/ambiguous
 425ambiguous
 426EOF
 427
 428test_expect_success 'Check ambiguous head and tag refs II (loose)' '
 429        git checkout master &&
 430        git tag ambiguous testtag^0 &&
 431        git branch ambiguous testtag^0 &&
 432        git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
 433        test_cmp expected actual
 434'
 435
 436test_expect_success 'an unusual tag with an incomplete line' '
 437
 438        git tag -m "bogo" bogo &&
 439        bogo=$(git cat-file tag bogo) &&
 440        bogo=$(printf "%s" "$bogo" | git mktag) &&
 441        git tag -f bogo "$bogo" &&
 442        git for-each-ref --format "%(body)" refs/tags/bogo
 443
 444'
 445
 446test_expect_success 'create tag with subject and body content' '
 447        cat >>msg <<-\EOF &&
 448                the subject line
 449
 450                first body line
 451                second body line
 452        EOF
 453        git tag -F msg subject-body
 454'
 455test_atom refs/tags/subject-body subject 'the subject line'
 456test_atom refs/tags/subject-body body 'first body line
 457second body line
 458'
 459test_atom refs/tags/subject-body contents 'the subject line
 460
 461first body line
 462second body line
 463'
 464
 465test_expect_success 'create tag with multiline subject' '
 466        cat >msg <<-\EOF &&
 467                first subject line
 468                second subject line
 469
 470                first body line
 471                second body line
 472        EOF
 473        git tag -F msg multiline
 474'
 475test_atom refs/tags/multiline subject 'first subject line second subject line'
 476test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
 477test_atom refs/tags/multiline body 'first body line
 478second body line
 479'
 480test_atom refs/tags/multiline contents:body 'first body line
 481second body line
 482'
 483test_atom refs/tags/multiline contents:signature ''
 484test_atom refs/tags/multiline contents 'first subject line
 485second subject line
 486
 487first body line
 488second body line
 489'
 490
 491test_expect_success GPG 'create signed tags' '
 492        git tag -s -m "" signed-empty &&
 493        git tag -s -m "subject line" signed-short &&
 494        cat >msg <<-\EOF &&
 495        subject line
 496
 497        body contents
 498        EOF
 499        git tag -s -F msg signed-long
 500'
 501
 502sig='-----BEGIN PGP SIGNATURE-----
 503-----END PGP SIGNATURE-----
 504'
 505
 506PREREQ=GPG
 507test_atom refs/tags/signed-empty subject ''
 508test_atom refs/tags/signed-empty contents:subject ''
 509test_atom refs/tags/signed-empty body "$sig"
 510test_atom refs/tags/signed-empty contents:body ''
 511test_atom refs/tags/signed-empty contents:signature "$sig"
 512test_atom refs/tags/signed-empty contents "$sig"
 513
 514test_atom refs/tags/signed-short subject 'subject line'
 515test_atom refs/tags/signed-short contents:subject 'subject line'
 516test_atom refs/tags/signed-short body "$sig"
 517test_atom refs/tags/signed-short contents:body ''
 518test_atom refs/tags/signed-short contents:signature "$sig"
 519test_atom refs/tags/signed-short contents "subject line
 520$sig"
 521
 522test_atom refs/tags/signed-long subject 'subject line'
 523test_atom refs/tags/signed-long contents:subject 'subject line'
 524test_atom refs/tags/signed-long body "body contents
 525$sig"
 526test_atom refs/tags/signed-long contents:body 'body contents
 527'
 528test_atom refs/tags/signed-long contents:signature "$sig"
 529test_atom refs/tags/signed-long contents "subject line
 530
 531body contents
 532$sig"
 533
 534cat >expected <<EOF
 535$(git rev-parse refs/tags/bogo) <committer@example.com> refs/tags/bogo
 536$(git rev-parse refs/tags/master) <committer@example.com> refs/tags/master
 537EOF
 538
 539test_expect_success 'Verify sort with multiple keys' '
 540        git for-each-ref --format="%(objectname) %(taggeremail) %(refname)" --sort=objectname --sort=taggeremail \
 541                refs/tags/bogo refs/tags/master > actual &&
 542        test_cmp expected actual
 543'
 544test_done