t / t7407-submodule-foreach.shon commit grep: recurse in-process using 'struct repository' (f9ee2fc)
   1#!/bin/sh
   2#
   3# Copyright (c) 2009 Johan Herland
   4#
   5
   6test_description='Test "git submodule foreach"
   7
   8This test verifies that "git submodule foreach" correctly visits all submodules
   9that are currently checked out.
  10'
  11
  12. ./test-lib.sh
  13
  14
  15test_expect_success 'setup a submodule tree' '
  16        echo file > file &&
  17        git add file &&
  18        test_tick &&
  19        git commit -m upstream &&
  20        git clone . super &&
  21        git clone super submodule &&
  22        (
  23                cd super &&
  24                git submodule add ../submodule sub1 &&
  25                git submodule add ../submodule sub2 &&
  26                git submodule add ../submodule sub3 &&
  27                git config -f .gitmodules --rename-section \
  28                        submodule.sub1 submodule.foo1 &&
  29                git config -f .gitmodules --rename-section \
  30                        submodule.sub2 submodule.foo2 &&
  31                git config -f .gitmodules --rename-section \
  32                        submodule.sub3 submodule.foo3 &&
  33                git add .gitmodules &&
  34                test_tick &&
  35                git commit -m "submodules" &&
  36                git submodule init sub1 &&
  37                git submodule init sub2 &&
  38                git submodule init sub3
  39        ) &&
  40        (
  41                cd submodule &&
  42                echo different > file &&
  43                git add file &&
  44                test_tick &&
  45                git commit -m "different"
  46        ) &&
  47        (
  48                cd super &&
  49                (
  50                        cd sub3 &&
  51                        git pull
  52                ) &&
  53                git add sub3 &&
  54                test_tick &&
  55                git commit -m "update sub3"
  56        )
  57'
  58
  59sub1sha1=$(cd super/sub1 && git rev-parse HEAD)
  60sub3sha1=$(cd super/sub3 && git rev-parse HEAD)
  61
  62pwd=$(pwd)
  63
  64cat > expect <<EOF
  65Entering 'sub1'
  66$pwd/clone-foo1-sub1-$sub1sha1
  67Entering 'sub3'
  68$pwd/clone-foo3-sub3-$sub3sha1
  69EOF
  70
  71test_expect_success 'test basic "submodule foreach" usage' '
  72        git clone super clone &&
  73        (
  74                cd clone &&
  75                git submodule update --init -- sub1 sub3 &&
  76                git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual &&
  77                git config foo.bar zar &&
  78                git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar"
  79        ) &&
  80        test_i18ncmp expect actual
  81'
  82
  83cat >expect <<EOF
  84Entering '../sub1'
  85$pwd/clone-foo1-../sub1-$sub1sha1
  86Entering '../sub3'
  87$pwd/clone-foo3-../sub3-$sub3sha1
  88EOF
  89
  90test_expect_success 'test "submodule foreach" from subdirectory' '
  91        mkdir clone/sub &&
  92        (
  93                cd clone/sub &&
  94                git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$sha1" >../../actual
  95        ) &&
  96        test_i18ncmp expect actual
  97'
  98
  99test_expect_success 'setup nested submodules' '
 100        git clone submodule nested1 &&
 101        git clone submodule nested2 &&
 102        git clone submodule nested3 &&
 103        (
 104                cd nested3 &&
 105                git submodule add ../submodule submodule &&
 106                test_tick &&
 107                git commit -m "submodule" &&
 108                git submodule init submodule
 109        ) &&
 110        (
 111                cd nested2 &&
 112                git submodule add ../nested3 nested3 &&
 113                test_tick &&
 114                git commit -m "nested3" &&
 115                git submodule init nested3
 116        ) &&
 117        (
 118                cd nested1 &&
 119                git submodule add ../nested2 nested2 &&
 120                test_tick &&
 121                git commit -m "nested2" &&
 122                git submodule init nested2
 123        ) &&
 124        (
 125                cd super &&
 126                git submodule add ../nested1 nested1 &&
 127                test_tick &&
 128                git commit -m "nested1" &&
 129                git submodule init nested1
 130        )
 131'
 132
 133test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' '
 134        git clone super clone2 &&
 135        (
 136                cd clone2 &&
 137                test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
 138                test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
 139                test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
 140                test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
 141                git submodule update --init &&
 142                git rev-parse --resolve-git-dir sub1/.git &&
 143                git rev-parse --resolve-git-dir sub2/.git &&
 144                git rev-parse --resolve-git-dir sub3/.git &&
 145                git rev-parse --resolve-git-dir nested1/.git &&
 146                test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git &&
 147                git submodule foreach "git submodule update --init" &&
 148                git rev-parse --resolve-git-dir nested1/nested2/.git &&
 149                test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git
 150        )
 151'
 152
 153test_expect_success 'use "foreach --recursive" to checkout all submodules' '
 154        (
 155                cd clone2 &&
 156                git submodule foreach --recursive "git submodule update --init" &&
 157                git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
 158                git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 159        )
 160'
 161
 162cat > expect <<EOF
 163Entering 'nested1'
 164Entering 'nested1/nested2'
 165Entering 'nested1/nested2/nested3'
 166Entering 'nested1/nested2/nested3/submodule'
 167Entering 'sub1'
 168Entering 'sub2'
 169Entering 'sub3'
 170EOF
 171
 172test_expect_success 'test messages from "foreach --recursive"' '
 173        (
 174                cd clone2 &&
 175                git submodule foreach --recursive "true" > ../actual
 176        ) &&
 177        test_i18ncmp expect actual
 178'
 179
 180cat > expect <<EOF
 181Entering '../nested1'
 182Entering '../nested1/nested2'
 183Entering '../nested1/nested2/nested3'
 184Entering '../nested1/nested2/nested3/submodule'
 185Entering '../sub1'
 186Entering '../sub2'
 187Entering '../sub3'
 188EOF
 189
 190test_expect_success 'test messages from "foreach --recursive" from subdirectory' '
 191        (
 192                cd clone2 &&
 193                mkdir untracked &&
 194                cd untracked &&
 195                git submodule foreach --recursive >../../actual
 196        ) &&
 197        test_i18ncmp expect actual
 198'
 199
 200cat > expect <<EOF
 201nested1-nested1
 202nested2-nested2
 203nested3-nested3
 204submodule-submodule
 205foo1-sub1
 206foo2-sub2
 207foo3-sub3
 208EOF
 209
 210test_expect_success 'test "foreach --quiet --recursive"' '
 211        (
 212                cd clone2 &&
 213                git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
 214        ) &&
 215        test_cmp expect actual
 216'
 217
 218test_expect_success 'use "update --recursive" to checkout all submodules' '
 219        git clone super clone3 &&
 220        (
 221                cd clone3 &&
 222                test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
 223                test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
 224                test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
 225                test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
 226                git submodule update --init --recursive &&
 227                git rev-parse --resolve-git-dir sub1/.git &&
 228                git rev-parse --resolve-git-dir sub2/.git &&
 229                git rev-parse --resolve-git-dir sub3/.git &&
 230                git rev-parse --resolve-git-dir nested1/.git &&
 231                git rev-parse --resolve-git-dir nested1/nested2/.git &&
 232                git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
 233                git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 234        )
 235'
 236
 237nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
 238nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
 239nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
 240submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
 241sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
 242sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
 243sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
 244sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
 245sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)
 246
 247cat > expect <<EOF
 248 $nested1sha1 nested1 (heads/master)
 249 $nested2sha1 nested1/nested2 (heads/master)
 250 $nested3sha1 nested1/nested2/nested3 (heads/master)
 251 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
 252 $sub1sha1 sub1 ($sub1sha1_short)
 253 $sub2sha1 sub2 ($sub2sha1_short)
 254 $sub3sha1 sub3 (heads/master)
 255EOF
 256
 257test_expect_success 'test "status --recursive"' '
 258        (
 259                cd clone3 &&
 260                git submodule status --recursive > ../actual
 261        ) &&
 262        test_cmp expect actual
 263'
 264
 265cat > expect <<EOF
 266 $nested1sha1 nested1 (heads/master)
 267+$nested2sha1 nested1/nested2 (file2~1)
 268 $nested3sha1 nested1/nested2/nested3 (heads/master)
 269 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
 270EOF
 271
 272test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' '
 273        (
 274                cd clone3 &&
 275                (
 276                        cd nested1/nested2 &&
 277                        test_commit file2
 278                ) &&
 279                git submodule status --cached --recursive -- nested1 > ../actual
 280        ) &&
 281        test_cmp expect actual
 282'
 283
 284nested2sha1=$(git -C clone3/nested1/nested2 rev-parse HEAD)
 285
 286cat > expect <<EOF
 287 $nested1sha1 ../nested1 (heads/master)
 288+$nested2sha1 ../nested1/nested2 (file2)
 289 $nested3sha1 ../nested1/nested2/nested3 (heads/master)
 290 $submodulesha1 ../nested1/nested2/nested3/submodule (heads/master)
 291 $sub1sha1 ../sub1 ($sub1sha1_short)
 292 $sub2sha1 ../sub2 ($sub2sha1_short)
 293 $sub3sha1 ../sub3 (heads/master)
 294EOF
 295
 296test_expect_success 'test "status --recursive" from sub directory' '
 297        (
 298                cd clone3 &&
 299                mkdir tmp && cd tmp &&
 300                git submodule status --recursive > ../../actual
 301        ) &&
 302        test_cmp expect actual
 303'
 304
 305test_expect_success 'use "git clone --recursive" to checkout all submodules' '
 306        git clone --recursive super clone4 &&
 307        (
 308                cd clone4 &&
 309                git rev-parse --resolve-git-dir .git &&
 310                git rev-parse --resolve-git-dir sub1/.git &&
 311                git rev-parse --resolve-git-dir sub2/.git &&
 312                git rev-parse --resolve-git-dir sub3/.git &&
 313                git rev-parse --resolve-git-dir nested1/.git &&
 314                git rev-parse --resolve-git-dir nested1/nested2/.git &&
 315                git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
 316                git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 317        )
 318'
 319
 320test_expect_success 'test "update --recursive" with a flag with spaces' '
 321        git clone super "common objects" &&
 322        git clone super clone5 &&
 323        (
 324                cd clone5 &&
 325                test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
 326                git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
 327                git rev-parse --resolve-git-dir nested1/.git &&
 328                git rev-parse --resolve-git-dir nested1/nested2/.git &&
 329                git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
 330                test -f .git/modules/nested1/objects/info/alternates &&
 331                test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
 332                test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
 333        )
 334'
 335
 336test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
 337        git clone super clone6 &&
 338        (
 339                cd clone6 &&
 340                test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
 341                test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
 342                test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
 343                test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
 344                git submodule update --init --recursive -- nested1 &&
 345                test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
 346                test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
 347                test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
 348                git rev-parse --resolve-git-dir nested1/.git &&
 349                git rev-parse --resolve-git-dir nested1/nested2/.git &&
 350                git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
 351                git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
 352        )
 353'
 354
 355test_expect_success 'command passed to foreach retains notion of stdin' '
 356        (
 357                cd super &&
 358                git submodule foreach echo success >../expected &&
 359                yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
 360        ) &&
 361        test_cmp expected actual
 362'
 363
 364test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
 365        (
 366                cd clone2 &&
 367                git submodule foreach --recursive echo success >../expected &&
 368                yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
 369        ) &&
 370        test_cmp expected actual
 371'
 372
 373test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
 374        (
 375                cd super &&
 376                git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
 377                git submodule foreach echo \"quoted\" > ../actual
 378        ) &&
 379        test_cmp expected actual
 380'
 381
 382test_done