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