t / t6302-for-each-ref-filter.shon commit Merge branch 'rs/wt-status-detached-branch-fix' (a59d1d8)
   1#!/bin/sh
   2
   3test_description='test for-each-refs usage of ref-filter APIs'
   4
   5. ./test-lib.sh
   6. "$TEST_DIRECTORY"/lib-gpg.sh
   7
   8if ! test_have_prereq GPG
   9then
  10        skip_all="skipping for-each-ref tests, GPG not available"
  11        test_done
  12fi
  13
  14test_expect_success 'setup some history and refs' '
  15        test_commit one &&
  16        test_commit two &&
  17        test_commit three &&
  18        git checkout -b side &&
  19        test_commit four &&
  20        git tag -s -m "A signed tag message" signed-tag &&
  21        git tag -s -m "Annonated doubly" double-tag signed-tag &&
  22        git checkout master &&
  23        git update-ref refs/odd/spot master
  24'
  25
  26test_expect_success 'filtering with --points-at' '
  27        cat >expect <<-\EOF &&
  28        refs/heads/master
  29        refs/odd/spot
  30        refs/tags/three
  31        EOF
  32        git for-each-ref --format="%(refname)" --points-at=master >actual &&
  33        test_cmp expect actual
  34'
  35
  36test_expect_success 'check signed tags with --points-at' '
  37        sed -e "s/Z$//" >expect <<-\EOF &&
  38        refs/heads/side Z
  39        refs/tags/four Z
  40        refs/tags/signed-tag four
  41        EOF
  42        git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
  43        test_cmp expect actual
  44'
  45
  46test_expect_success 'filtering with --merged' '
  47        cat >expect <<-\EOF &&
  48        refs/heads/master
  49        refs/odd/spot
  50        refs/tags/one
  51        refs/tags/three
  52        refs/tags/two
  53        EOF
  54        git for-each-ref --format="%(refname)" --merged=master >actual &&
  55        test_cmp expect actual
  56'
  57
  58test_expect_success 'filtering with --no-merged' '
  59        cat >expect <<-\EOF &&
  60        refs/heads/side
  61        refs/tags/double-tag
  62        refs/tags/four
  63        refs/tags/signed-tag
  64        EOF
  65        git for-each-ref --format="%(refname)" --no-merged=master >actual &&
  66        test_cmp expect actual
  67'
  68
  69test_expect_success 'filtering with --contains' '
  70        cat >expect <<-\EOF &&
  71        refs/heads/master
  72        refs/heads/side
  73        refs/odd/spot
  74        refs/tags/double-tag
  75        refs/tags/four
  76        refs/tags/signed-tag
  77        refs/tags/three
  78        refs/tags/two
  79        EOF
  80        git for-each-ref --format="%(refname)" --contains=two >actual &&
  81        test_cmp expect actual
  82'
  83
  84test_expect_success '%(color) must fail' '
  85        test_must_fail git for-each-ref --format="%(color)%(refname)"
  86'
  87
  88test_expect_success 'left alignment is default' '
  89        cat >expect <<-\EOF &&
  90        refname is refs/heads/master  |refs/heads/master
  91        refname is refs/heads/side    |refs/heads/side
  92        refname is refs/odd/spot      |refs/odd/spot
  93        refname is refs/tags/double-tag|refs/tags/double-tag
  94        refname is refs/tags/four     |refs/tags/four
  95        refname is refs/tags/one      |refs/tags/one
  96        refname is refs/tags/signed-tag|refs/tags/signed-tag
  97        refname is refs/tags/three    |refs/tags/three
  98        refname is refs/tags/two      |refs/tags/two
  99        EOF
 100        git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
 101        test_cmp expect actual
 102'
 103
 104test_expect_success 'middle alignment' '
 105        cat >expect <<-\EOF &&
 106        | refname is refs/heads/master |refs/heads/master
 107        |  refname is refs/heads/side  |refs/heads/side
 108        |   refname is refs/odd/spot   |refs/odd/spot
 109        |refname is refs/tags/double-tag|refs/tags/double-tag
 110        |  refname is refs/tags/four   |refs/tags/four
 111        |   refname is refs/tags/one   |refs/tags/one
 112        |refname is refs/tags/signed-tag|refs/tags/signed-tag
 113        |  refname is refs/tags/three  |refs/tags/three
 114        |   refname is refs/tags/two   |refs/tags/two
 115        EOF
 116        git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
 117        test_cmp expect actual
 118'
 119
 120test_expect_success 'right alignment' '
 121        cat >expect <<-\EOF &&
 122        |  refname is refs/heads/master|refs/heads/master
 123        |    refname is refs/heads/side|refs/heads/side
 124        |      refname is refs/odd/spot|refs/odd/spot
 125        |refname is refs/tags/double-tag|refs/tags/double-tag
 126        |     refname is refs/tags/four|refs/tags/four
 127        |      refname is refs/tags/one|refs/tags/one
 128        |refname is refs/tags/signed-tag|refs/tags/signed-tag
 129        |    refname is refs/tags/three|refs/tags/three
 130        |      refname is refs/tags/two|refs/tags/two
 131        EOF
 132        git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
 133        test_cmp expect actual
 134'
 135
 136# Individual atoms inside %(align:...) and %(end) must not be quoted.
 137
 138test_expect_success 'alignment with format quote' "
 139        cat >expect <<-\EOF &&
 140        |'      '\''master| A U Thor'\''      '|
 141        |'       '\''side| A U Thor'\''       '|
 142        |'     '\''odd/spot| A U Thor'\''     '|
 143        |'        '\''double-tag| '\''        '|
 144        |'       '\''four| A U Thor'\''       '|
 145        |'       '\''one| A U Thor'\''        '|
 146        |'        '\''signed-tag| '\''        '|
 147        |'      '\''three| A U Thor'\''       '|
 148        |'       '\''two| A U Thor'\''        '|
 149        EOF
 150        git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
 151        test_cmp expect actual
 152"
 153
 154test_expect_success 'nested alignment with quote formatting' "
 155        cat >expect <<-\EOF &&
 156        |'         master               '|
 157        |'           side               '|
 158        |'       odd/spot               '|
 159        |'     double-tag               '|
 160        |'           four               '|
 161        |'            one               '|
 162        |'     signed-tag               '|
 163        |'          three               '|
 164        |'            two               '|
 165        EOF
 166        git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
 167        test_cmp expect actual
 168"
 169
 170test_expect_success 'check `%(contents:lines=1)`' '
 171        cat >expect <<-\EOF &&
 172        master |three
 173        side |four
 174        odd/spot |three
 175        double-tag |Annonated doubly
 176        four |four
 177        one |one
 178        signed-tag |A signed tag message
 179        three |three
 180        two |two
 181        EOF
 182        git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
 183        test_cmp expect actual
 184'
 185
 186test_expect_success 'check `%(contents:lines=0)`' '
 187        cat >expect <<-\EOF &&
 188        master |
 189        side |
 190        odd/spot |
 191        double-tag |
 192        four |
 193        one |
 194        signed-tag |
 195        three |
 196        two |
 197        EOF
 198        git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
 199        test_cmp expect actual
 200'
 201
 202test_expect_success 'check `%(contents:lines=99999)`' '
 203        cat >expect <<-\EOF &&
 204        master |three
 205        side |four
 206        odd/spot |three
 207        double-tag |Annonated doubly
 208        four |four
 209        one |one
 210        signed-tag |A signed tag message
 211        three |three
 212        two |two
 213        EOF
 214        git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
 215        test_cmp expect actual
 216'
 217
 218test_expect_success '`%(contents:lines=-1)` should fail' '
 219        test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
 220'
 221
 222test_expect_success 'setup for version sort' '
 223        test_commit foo1.3 &&
 224        test_commit foo1.6 &&
 225        test_commit foo1.10
 226'
 227
 228test_expect_success 'version sort' '
 229        git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
 230        cat >expect <<-\EOF &&
 231        foo1.3
 232        foo1.6
 233        foo1.10
 234        EOF
 235        test_cmp expect actual
 236'
 237
 238test_expect_success 'version sort (shortened)' '
 239        git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
 240        cat >expect <<-\EOF &&
 241        foo1.3
 242        foo1.6
 243        foo1.10
 244        EOF
 245        test_cmp expect actual
 246'
 247
 248test_expect_success 'reverse version sort' '
 249        git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
 250        cat >expect <<-\EOF &&
 251        foo1.10
 252        foo1.6
 253        foo1.3
 254        EOF
 255        test_cmp expect actual
 256'
 257
 258test_done