t / t7814-grep-recurse-submodules.shon commit Merge branch 'sb/submodule-url-to-absolute' (3fc8522)
   1#!/bin/sh
   2
   3test_description='Test grep recurse-submodules feature
   4
   5This test verifies the recurse-submodules feature correctly greps across
   6submodules.
   7'
   8
   9. ./test-lib.sh
  10
  11test_expect_success 'setup directory structure and submodule' '
  12        echo "(1|2)d(3|4)" >a &&
  13        mkdir b &&
  14        echo "(3|4)" >b/b &&
  15        git add a b &&
  16        git commit -m "add a and b" &&
  17        git init submodule &&
  18        echo "(1|2)d(3|4)" >submodule/a &&
  19        git -C submodule add a &&
  20        git -C submodule commit -m "add a" &&
  21        git submodule add ./submodule &&
  22        git commit -m "added submodule"
  23'
  24
  25test_expect_success 'grep correctly finds patterns in a submodule' '
  26        cat >expect <<-\EOF &&
  27        a:(1|2)d(3|4)
  28        b/b:(3|4)
  29        submodule/a:(1|2)d(3|4)
  30        EOF
  31
  32        git grep -e "(3|4)" --recurse-submodules >actual &&
  33        test_cmp expect actual
  34'
  35
  36test_expect_success 'grep finds patterns in a submodule via config' '
  37        test_config submodule.recurse true &&
  38        # expect from previous test
  39        git grep -e "(3|4)" >actual &&
  40        test_cmp expect actual
  41'
  42
  43test_expect_success 'grep --no-recurse-submodules overrides config' '
  44        test_config submodule.recurse true &&
  45        cat >expect <<-\EOF &&
  46        a:(1|2)d(3|4)
  47        b/b:(3|4)
  48        EOF
  49
  50        git grep -e "(3|4)" --no-recurse-submodules >actual &&
  51        test_cmp expect actual
  52'
  53
  54test_expect_success 'grep and basic pathspecs' '
  55        cat >expect <<-\EOF &&
  56        submodule/a:(1|2)d(3|4)
  57        EOF
  58
  59        git grep -e. --recurse-submodules -- submodule >actual &&
  60        test_cmp expect actual
  61'
  62
  63test_expect_success 'grep and nested submodules' '
  64        git init submodule/sub &&
  65        echo "(1|2)d(3|4)" >submodule/sub/a &&
  66        git -C submodule/sub add a &&
  67        git -C submodule/sub commit -m "add a" &&
  68        git -C submodule submodule add ./sub &&
  69        git -C submodule add sub &&
  70        git -C submodule commit -m "added sub" &&
  71        git add submodule &&
  72        git commit -m "updated submodule" &&
  73
  74        cat >expect <<-\EOF &&
  75        a:(1|2)d(3|4)
  76        b/b:(3|4)
  77        submodule/a:(1|2)d(3|4)
  78        submodule/sub/a:(1|2)d(3|4)
  79        EOF
  80
  81        git grep -e "(3|4)" --recurse-submodules >actual &&
  82        test_cmp expect actual
  83'
  84
  85test_expect_success 'grep and multiple patterns' '
  86        cat >expect <<-\EOF &&
  87        a:(1|2)d(3|4)
  88        submodule/a:(1|2)d(3|4)
  89        submodule/sub/a:(1|2)d(3|4)
  90        EOF
  91
  92        git grep -e "(3|4)" --and -e "(1|2)" --recurse-submodules >actual &&
  93        test_cmp expect actual
  94'
  95
  96test_expect_success 'grep and multiple patterns' '
  97        cat >expect <<-\EOF &&
  98        b/b:(3|4)
  99        EOF
 100
 101        git grep -e "(3|4)" --and --not -e "(1|2)" --recurse-submodules >actual &&
 102        test_cmp expect actual
 103'
 104
 105test_expect_success 'basic grep tree' '
 106        cat >expect <<-\EOF &&
 107        HEAD:a:(1|2)d(3|4)
 108        HEAD:b/b:(3|4)
 109        HEAD:submodule/a:(1|2)d(3|4)
 110        HEAD:submodule/sub/a:(1|2)d(3|4)
 111        EOF
 112
 113        git grep -e "(3|4)" --recurse-submodules HEAD >actual &&
 114        test_cmp expect actual
 115'
 116
 117test_expect_success 'grep tree HEAD^' '
 118        cat >expect <<-\EOF &&
 119        HEAD^:a:(1|2)d(3|4)
 120        HEAD^:b/b:(3|4)
 121        HEAD^:submodule/a:(1|2)d(3|4)
 122        EOF
 123
 124        git grep -e "(3|4)" --recurse-submodules HEAD^ >actual &&
 125        test_cmp expect actual
 126'
 127
 128test_expect_success 'grep tree HEAD^^' '
 129        cat >expect <<-\EOF &&
 130        HEAD^^:a:(1|2)d(3|4)
 131        HEAD^^:b/b:(3|4)
 132        EOF
 133
 134        git grep -e "(3|4)" --recurse-submodules HEAD^^ >actual &&
 135        test_cmp expect actual
 136'
 137
 138test_expect_success 'grep tree and pathspecs' '
 139        cat >expect <<-\EOF &&
 140        HEAD:submodule/a:(1|2)d(3|4)
 141        HEAD:submodule/sub/a:(1|2)d(3|4)
 142        EOF
 143
 144        git grep -e "(3|4)" --recurse-submodules HEAD -- submodule >actual &&
 145        test_cmp expect actual
 146'
 147
 148test_expect_success 'grep tree and pathspecs' '
 149        cat >expect <<-\EOF &&
 150        HEAD:submodule/a:(1|2)d(3|4)
 151        HEAD:submodule/sub/a:(1|2)d(3|4)
 152        EOF
 153
 154        git grep -e "(3|4)" --recurse-submodules HEAD -- "submodule*a" >actual &&
 155        test_cmp expect actual
 156'
 157
 158test_expect_success 'grep tree and more pathspecs' '
 159        cat >expect <<-\EOF &&
 160        HEAD:submodule/a:(1|2)d(3|4)
 161        EOF
 162
 163        git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul?/a" >actual &&
 164        test_cmp expect actual
 165'
 166
 167test_expect_success 'grep tree and more pathspecs' '
 168        cat >expect <<-\EOF &&
 169        HEAD:submodule/sub/a:(1|2)d(3|4)
 170        EOF
 171
 172        git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul*/sub/a" >actual &&
 173        test_cmp expect actual
 174'
 175
 176test_expect_success !MINGW 'grep recurse submodule colon in name' '
 177        git init parent &&
 178        test_when_finished "rm -rf parent" &&
 179        echo "(1|2)d(3|4)" >"parent/fi:le" &&
 180        git -C parent add "fi:le" &&
 181        git -C parent commit -m "add fi:le" &&
 182
 183        git init "su:b" &&
 184        test_when_finished "rm -rf su:b" &&
 185        echo "(1|2)d(3|4)" >"su:b/fi:le" &&
 186        git -C "su:b" add "fi:le" &&
 187        git -C "su:b" commit -m "add fi:le" &&
 188
 189        git -C parent submodule add "../su:b" "su:b" &&
 190        git -C parent commit -m "add submodule" &&
 191
 192        cat >expect <<-\EOF &&
 193        fi:le:(1|2)d(3|4)
 194        su:b/fi:le:(1|2)d(3|4)
 195        EOF
 196        git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
 197        test_cmp expect actual &&
 198
 199        cat >expect <<-\EOF &&
 200        HEAD:fi:le:(1|2)d(3|4)
 201        HEAD:su:b/fi:le:(1|2)d(3|4)
 202        EOF
 203        git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD >actual &&
 204        test_cmp expect actual
 205'
 206
 207test_expect_success 'grep history with moved submoules' '
 208        git init parent &&
 209        test_when_finished "rm -rf parent" &&
 210        echo "(1|2)d(3|4)" >parent/file &&
 211        git -C parent add file &&
 212        git -C parent commit -m "add file" &&
 213
 214        git init sub &&
 215        test_when_finished "rm -rf sub" &&
 216        echo "(1|2)d(3|4)" >sub/file &&
 217        git -C sub add file &&
 218        git -C sub commit -m "add file" &&
 219
 220        git -C parent submodule add ../sub dir/sub &&
 221        git -C parent commit -m "add submodule" &&
 222
 223        cat >expect <<-\EOF &&
 224        dir/sub/file:(1|2)d(3|4)
 225        file:(1|2)d(3|4)
 226        EOF
 227        git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
 228        test_cmp expect actual &&
 229
 230        git -C parent mv dir/sub sub-moved &&
 231        git -C parent commit -m "moved submodule" &&
 232
 233        cat >expect <<-\EOF &&
 234        file:(1|2)d(3|4)
 235        sub-moved/file:(1|2)d(3|4)
 236        EOF
 237        git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
 238        test_cmp expect actual &&
 239
 240        cat >expect <<-\EOF &&
 241        HEAD^:dir/sub/file:(1|2)d(3|4)
 242        HEAD^:file:(1|2)d(3|4)
 243        EOF
 244        git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD^ >actual &&
 245        test_cmp expect actual
 246'
 247
 248test_expect_success 'grep using relative path' '
 249        test_when_finished "rm -rf parent sub" &&
 250        git init sub &&
 251        echo "(1|2)d(3|4)" >sub/file &&
 252        git -C sub add file &&
 253        git -C sub commit -m "add file" &&
 254
 255        git init parent &&
 256        echo "(1|2)d(3|4)" >parent/file &&
 257        git -C parent add file &&
 258        mkdir parent/src &&
 259        echo "(1|2)d(3|4)" >parent/src/file2 &&
 260        git -C parent add src/file2 &&
 261        git -C parent submodule add ../sub &&
 262        git -C parent commit -m "add files and submodule" &&
 263
 264        # From top works
 265        cat >expect <<-\EOF &&
 266        file:(1|2)d(3|4)
 267        src/file2:(1|2)d(3|4)
 268        sub/file:(1|2)d(3|4)
 269        EOF
 270        git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
 271        test_cmp expect actual &&
 272
 273        # Relative path to top
 274        cat >expect <<-\EOF &&
 275        ../file:(1|2)d(3|4)
 276        file2:(1|2)d(3|4)
 277        ../sub/file:(1|2)d(3|4)
 278        EOF
 279        git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- .. >actual &&
 280        test_cmp expect actual &&
 281
 282        # Relative path to submodule
 283        cat >expect <<-\EOF &&
 284        ../sub/file:(1|2)d(3|4)
 285        EOF
 286        git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- ../sub >actual &&
 287        test_cmp expect actual
 288'
 289
 290test_expect_success 'grep from a subdir' '
 291        test_when_finished "rm -rf parent sub" &&
 292        git init sub &&
 293        echo "(1|2)d(3|4)" >sub/file &&
 294        git -C sub add file &&
 295        git -C sub commit -m "add file" &&
 296
 297        git init parent &&
 298        mkdir parent/src &&
 299        echo "(1|2)d(3|4)" >parent/src/file &&
 300        git -C parent add src/file &&
 301        git -C parent submodule add ../sub src/sub &&
 302        git -C parent submodule add ../sub sub &&
 303        git -C parent commit -m "add files and submodules" &&
 304
 305        # Verify grep from root works
 306        cat >expect <<-\EOF &&
 307        src/file:(1|2)d(3|4)
 308        src/sub/file:(1|2)d(3|4)
 309        sub/file:(1|2)d(3|4)
 310        EOF
 311        git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
 312        test_cmp expect actual &&
 313
 314        # Verify grep from a subdir works
 315        cat >expect <<-\EOF &&
 316        file:(1|2)d(3|4)
 317        sub/file:(1|2)d(3|4)
 318        EOF
 319        git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
 320        test_cmp expect actual
 321'
 322
 323test_incompatible_with_recurse_submodules ()
 324{
 325        test_expect_success "--recurse-submodules and $1 are incompatible" "
 326                test_must_fail git grep -e. --recurse-submodules $1 2>actual &&
 327                test_i18ngrep 'not supported with --recurse-submodules' actual
 328        "
 329}
 330
 331test_incompatible_with_recurse_submodules --untracked
 332test_incompatible_with_recurse_submodules --no-index
 333
 334test_expect_success 'grep --recurse-submodules should pass the pattern type along' '
 335        # Fixed
 336        test_must_fail git grep -F --recurse-submodules -e "(.|.)[\d]" &&
 337        test_must_fail git -c grep.patternType=fixed grep --recurse-submodules -e "(.|.)[\d]" &&
 338
 339        # Basic
 340        git grep -G --recurse-submodules -e "(.|.)[\d]" >actual &&
 341        cat >expect <<-\EOF &&
 342        a:(1|2)d(3|4)
 343        submodule/a:(1|2)d(3|4)
 344        submodule/sub/a:(1|2)d(3|4)
 345        EOF
 346        test_cmp expect actual &&
 347        git -c grep.patternType=basic grep --recurse-submodules -e "(.|.)[\d]" >actual &&
 348        test_cmp expect actual &&
 349
 350        # Extended
 351        git grep -E --recurse-submodules -e "(.|.)[\d]" >actual &&
 352        cat >expect <<-\EOF &&
 353        .gitmodules:[submodule "submodule"]
 354        .gitmodules:    path = submodule
 355        .gitmodules:    url = ./submodule
 356        a:(1|2)d(3|4)
 357        submodule/.gitmodules:[submodule "sub"]
 358        submodule/a:(1|2)d(3|4)
 359        submodule/sub/a:(1|2)d(3|4)
 360        EOF
 361        test_cmp expect actual &&
 362        git -c grep.patternType=extended grep --recurse-submodules -e "(.|.)[\d]" >actual &&
 363        test_cmp expect actual &&
 364        git -c grep.extendedRegexp=true grep --recurse-submodules -e "(.|.)[\d]" >actual &&
 365        test_cmp expect actual &&
 366
 367        # Perl
 368        if test_have_prereq PCRE
 369        then
 370                git grep -P --recurse-submodules -e "(.|.)[\d]" >actual &&
 371                cat >expect <<-\EOF &&
 372                a:(1|2)d(3|4)
 373                b/b:(3|4)
 374                submodule/a:(1|2)d(3|4)
 375                submodule/sub/a:(1|2)d(3|4)
 376                EOF
 377                test_cmp expect actual &&
 378                git -c grep.patternType=perl grep --recurse-submodules -e "(.|.)[\d]" >actual &&
 379                test_cmp expect actual
 380        fi
 381'
 382
 383test_done