t / t6300-for-each-ref.shon commit Extend index to save more flags (06aaaa0)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Andy Parkins
   4#
   5
   6test_description='for-each-ref test'
   7
   8. ./test-lib.sh
   9
  10# Mon Jul 3 15:18:43 2006 +0000
  11datestamp=1151939923
  12setdate_and_increment () {
  13    GIT_COMMITTER_DATE="$datestamp +0200"
  14    datestamp=$(expr "$datestamp" + 1)
  15    GIT_AUTHOR_DATE="$datestamp +0200"
  16    datestamp=$(expr "$datestamp" + 1)
  17    export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
  18}
  19
  20test_expect_success 'Create sample commit with known timestamp' '
  21        setdate_and_increment &&
  22        echo "Using $datestamp" > one &&
  23        git add one &&
  24        git commit -m "Initial" &&
  25        setdate_and_increment &&
  26        git tag -a -m "Tagging at $datestamp" testtag
  27'
  28
  29test_atom() {
  30        case "$1" in
  31                head) ref=refs/heads/master ;;
  32                 tag) ref=refs/tags/testtag ;;
  33        esac
  34        printf '%s\n' "$3" >expected
  35        test_expect_${4:-success} "basic atom: $1 $2" "
  36                git for-each-ref --format='%($2)' $ref >actual &&
  37                test_cmp expected actual
  38        "
  39}
  40
  41test_atom head refname refs/heads/master
  42test_atom head objecttype commit
  43test_atom head objectsize 171
  44test_atom head objectname 67a36f10722846e891fbada1ba48ed035de75581
  45test_atom head tree 0e51c00fcb93dffc755546f27593d511e1bdb46f
  46test_atom head parent ''
  47test_atom head numparent 0
  48test_atom head object ''
  49test_atom head type ''
  50test_atom head author 'A U Thor <author@example.com> 1151939924 +0200'
  51test_atom head authorname 'A U Thor'
  52test_atom head authoremail '<author@example.com>'
  53test_atom head authordate 'Mon Jul 3 17:18:44 2006 +0200'
  54test_atom head committer 'C O Mitter <committer@example.com> 1151939923 +0200'
  55test_atom head committername 'C O Mitter'
  56test_atom head committeremail '<committer@example.com>'
  57test_atom head committerdate 'Mon Jul 3 17:18:43 2006 +0200'
  58test_atom head tag ''
  59test_atom head tagger ''
  60test_atom head taggername ''
  61test_atom head taggeremail ''
  62test_atom head taggerdate ''
  63test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
  64test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
  65test_atom head subject 'Initial'
  66test_atom head body ''
  67test_atom head contents 'Initial
  68'
  69
  70test_atom tag refname refs/tags/testtag
  71test_atom tag objecttype tag
  72test_atom tag objectsize 154
  73test_atom tag objectname 98b46b1d36e5b07909de1b3886224e3e81e87322
  74test_atom tag tree ''
  75test_atom tag parent ''
  76test_atom tag numparent ''
  77test_atom tag object '67a36f10722846e891fbada1ba48ed035de75581'
  78test_atom tag type 'commit'
  79test_atom tag author ''
  80test_atom tag authorname ''
  81test_atom tag authoremail ''
  82test_atom tag authordate ''
  83test_atom tag committer ''
  84test_atom tag committername ''
  85test_atom tag committeremail ''
  86test_atom tag committerdate ''
  87test_atom tag tag 'testtag'
  88test_atom tag tagger 'C O Mitter <committer@example.com> 1151939925 +0200'
  89test_atom tag taggername 'C O Mitter'
  90test_atom tag taggeremail '<committer@example.com>'
  91test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
  92test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
  93test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
  94test_atom tag subject 'Tagging at 1151939927'
  95test_atom tag body ''
  96test_atom tag contents 'Tagging at 1151939927
  97'
  98
  99test_expect_success 'Check invalid atoms names are errors' '
 100        test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
 101'
 102
 103test_expect_success 'Check format specifiers are ignored in naming date atoms' '
 104        git for-each-ref --format="%(authordate)" refs/heads &&
 105        git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
 106        git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
 107        git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
 108'
 109
 110test_expect_success 'Check valid format specifiers for date fields' '
 111        git for-each-ref --format="%(authordate:default)" refs/heads &&
 112        git for-each-ref --format="%(authordate:relative)" refs/heads &&
 113        git for-each-ref --format="%(authordate:short)" refs/heads &&
 114        git for-each-ref --format="%(authordate:local)" refs/heads &&
 115        git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
 116        git for-each-ref --format="%(authordate:rfc2822)" refs/heads
 117'
 118
 119test_expect_success 'Check invalid format specifiers are errors' '
 120        test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
 121'
 122
 123cat >expected <<\EOF
 124'refs/heads/master' 'Mon Jul 3 17:18:43 2006 +0200' 'Mon Jul 3 17:18:44 2006 +0200'
 125'refs/tags/testtag' 'Mon Jul 3 17:18:45 2006 +0200'
 126EOF
 127
 128test_expect_success 'Check unformatted date fields output' '
 129        (git for-each-ref --shell --format="%(refname) %(committerdate) %(authordate)" refs/heads &&
 130        git for-each-ref --shell --format="%(refname) %(taggerdate)" refs/tags) >actual &&
 131        test_cmp expected actual
 132'
 133
 134test_expect_success 'Check format "default" formatted date fields output' '
 135        f=default &&
 136        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 137        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 138        test_cmp expected actual
 139'
 140
 141# Don't know how to do relative check because I can't know when this script
 142# is going to be run and can't fake the current time to git, and hence can't
 143# provide expected output.  Instead, I'll just make sure that "relative"
 144# doesn't exit in error
 145#
 146#cat >expected <<\EOF
 147#
 148#EOF
 149#
 150test_expect_success 'Check format "relative" date fields output' '
 151        f=relative &&
 152        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 153        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
 154'
 155
 156cat >expected <<\EOF
 157'refs/heads/master' '2006-07-03' '2006-07-03'
 158'refs/tags/testtag' '2006-07-03'
 159EOF
 160
 161test_expect_success 'Check format "short" date fields output' '
 162        f=short &&
 163        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 164        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 165        test_cmp expected actual
 166'
 167
 168cat >expected <<\EOF
 169'refs/heads/master' 'Mon Jul 3 15:18:43 2006' 'Mon Jul 3 15:18:44 2006'
 170'refs/tags/testtag' 'Mon Jul 3 15:18:45 2006'
 171EOF
 172
 173test_expect_success 'Check format "local" date fields output' '
 174        f=local &&
 175        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 176        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 177        test_cmp expected actual
 178'
 179
 180cat >expected <<\EOF
 181'refs/heads/master' '2006-07-03 17:18:43 +0200' '2006-07-03 17:18:44 +0200'
 182'refs/tags/testtag' '2006-07-03 17:18:45 +0200'
 183EOF
 184
 185test_expect_success 'Check format "iso8601" date fields output' '
 186        f=iso8601 &&
 187        (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
 188        git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual &&
 189        test_cmp expected actual
 190'
 191
 192cat >expected <<\EOF
 193'refs/heads/master' 'Mon, 3 Jul 2006 17:18:43 +0200' 'Mon, 3 Jul 2006 17:18:44 +0200'
 194'refs/tags/testtag' 'Mon, 3 Jul 2006 17:18:45 +0200'
 195EOF
 196
 197test_expect_success 'Check format "rfc2822" date fields output' '
 198        f=rfc2822 &&
 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        test_cmp expected actual
 202'
 203
 204cat >expected <<\EOF
 205refs/heads/master
 206refs/tags/testtag
 207EOF
 208
 209test_expect_success 'Verify ascending sort' '
 210        git for-each-ref --format="%(refname)" --sort=refname >actual &&
 211        test_cmp expected actual
 212'
 213
 214
 215cat >expected <<\EOF
 216refs/tags/testtag
 217refs/heads/master
 218EOF
 219
 220test_expect_success 'Verify descending sort' '
 221        git for-each-ref --format="%(refname)" --sort=-refname >actual &&
 222        test_cmp expected actual
 223'
 224
 225cat >expected <<\EOF
 226'refs/heads/master'
 227'refs/tags/testtag'
 228EOF
 229
 230test_expect_success 'Quoting style: shell' '
 231        git for-each-ref --shell --format="%(refname)" >actual &&
 232        test_cmp expected actual
 233'
 234
 235test_expect_success 'Quoting style: perl' '
 236        git for-each-ref --perl --format="%(refname)" >actual &&
 237        test_cmp expected actual
 238'
 239
 240test_expect_success 'Quoting style: python' '
 241        git for-each-ref --python --format="%(refname)" >actual &&
 242        test_cmp expected actual
 243'
 244
 245cat >expected <<\EOF
 246"refs/heads/master"
 247"refs/tags/testtag"
 248EOF
 249
 250test_expect_success 'Quoting style: tcl' '
 251        git for-each-ref --tcl --format="%(refname)" >actual &&
 252        test_cmp expected actual
 253'
 254
 255for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 256        test_expect_success "more than one quoting style: $i" "
 257                git for-each-ref $i 2>&1 | (read line &&
 258                case \$line in
 259                \"error: more than one quoting style\"*) : happy;;
 260                *) false
 261                esac)
 262        "
 263done
 264
 265cat >expected <<\EOF
 266master
 267testtag
 268EOF
 269
 270test_expect_success 'Check short refname format' '
 271        (git for-each-ref --format="%(refname:short)" refs/heads &&
 272        git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
 273        test_cmp expected actual
 274'
 275
 276test_expect_success 'Check for invalid refname format' '
 277        test_must_fail git for-each-ref --format="%(refname:INVALID)"
 278'
 279
 280cat >expected <<\EOF
 281heads/master
 282master
 283EOF
 284
 285test_expect_success 'Check ambiguous head and tag refs' '
 286        git checkout -b newtag &&
 287        echo "Using $datestamp" > one &&
 288        git add one &&
 289        git commit -m "Branch" &&
 290        setdate_and_increment &&
 291        git tag -m "Tagging at $datestamp" master &&
 292        git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
 293        test_cmp expected actual
 294'
 295
 296cat >expected <<\EOF
 297heads/ambiguous
 298ambiguous
 299EOF
 300
 301test_expect_success 'Check ambiguous head and tag refs II' '
 302        git checkout master &&
 303        git tag ambiguous testtag^0 &&
 304        git branch ambiguous testtag^0 &&
 305        git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
 306        test_cmp expected actual
 307'
 308
 309test_expect_success 'an unusual tag with an incomplete line' '
 310
 311        git tag -m "bogo" bogo &&
 312        bogo=$(git cat-file tag bogo) &&
 313        bogo=$(printf "%s" "$bogo" | git mktag) &&
 314        git tag -f bogo "$bogo" &&
 315        git for-each-ref --format "%(body)" refs/tags/bogo
 316
 317'
 318
 319test_done