| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2007 Lars Hjemli |
| 4 | # |
| 5 | |
| 6 | test_description='Basic porcelain support for submodules |
| 7 | |
| 8 | This test tries to verify basic sanity of the init, update and status |
| 9 | subcommands of git submodule. |
| 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 | test_expect_success 'setup - enable local submodules' ' |
| 18 | git config --global protocol.file.allow always |
| 19 | ' |
| 20 | |
| 21 | test_expect_success 'submodule usage: -h' ' |
| 22 | git submodule -h >out 2>err && |
| 23 | test_grep "^usage: git submodule" out && |
| 24 | test_must_be_empty err |
| 25 | ' |
| 26 | |
| 27 | test_expect_success 'submodule usage: --recursive' ' |
| 28 | test_expect_code 1 git submodule --recursive >out 2>err && |
| 29 | test_grep "^usage: git submodule" err && |
| 30 | test_must_be_empty out |
| 31 | ' |
| 32 | |
| 33 | test_expect_success 'submodule usage: status --' ' |
| 34 | test_expect_code 1 git submodule -- && |
| 35 | test_expect_code 1 git submodule --end-of-options |
| 36 | ' |
| 37 | |
| 38 | for opt in '--quiet' '--cached' |
| 39 | do |
| 40 | test_expect_success "submodule usage: status $opt" ' |
| 41 | git submodule $opt && |
| 42 | git submodule status $opt && |
| 43 | git submodule $opt status |
| 44 | ' |
| 45 | done |
| 46 | |
| 47 | test_expect_success 'submodule deinit works on empty repository' ' |
| 48 | git submodule deinit --all |
| 49 | ' |
| 50 | |
| 51 | test_expect_success 'submodule add with incomplete .gitmodules' ' |
| 52 | test_when_finished "rm -f expect actual" && |
| 53 | test_when_finished "git config remove-section submodule.one" && |
| 54 | test_when_finished "git rm -f one .gitmodules" && |
| 55 | git init one && |
| 56 | git -C one commit --allow-empty -m one-initial && |
| 57 | git config -f .gitmodules submodule.one.ignore all && |
| 58 | |
| 59 | git submodule add ./one && |
| 60 | |
| 61 | for var in ignore path url |
| 62 | do |
| 63 | git config -f .gitmodules --get "submodule.one.$var" || |
| 64 | return 1 |
| 65 | done >actual && |
| 66 | test_write_lines all one ./one >expect && |
| 67 | test_cmp expect actual |
| 68 | ' |
| 69 | |
| 70 | test_expect_success 'setup - initial commit' ' |
| 71 | >t && |
| 72 | git add t && |
| 73 | git commit -m "initial commit" && |
| 74 | git branch initial |
| 75 | ' |
| 76 | |
| 77 | test_expect_success 'submodule init aborts on missing .gitmodules file' ' |
| 78 | test_when_finished "git update-index --remove sub" && |
| 79 | git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub && |
| 80 | # missing the .gitmodules file here |
| 81 | test_must_fail git submodule init 2>actual && |
| 82 | test_grep "No url found for submodule path" actual |
| 83 | ' |
| 84 | |
| 85 | test_expect_success 'submodule update aborts on missing .gitmodules file' ' |
| 86 | test_when_finished "git update-index --remove sub" && |
| 87 | git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub && |
| 88 | # missing the .gitmodules file here |
| 89 | git submodule update sub 2>actual && |
| 90 | test_grep "Submodule path .sub. not initialized" actual |
| 91 | ' |
| 92 | |
| 93 | test_expect_success 'submodule update aborts on missing gitmodules url' ' |
| 94 | test_when_finished "git update-index --remove sub" && |
| 95 | git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub && |
| 96 | test_when_finished "rm -f .gitmodules" && |
| 97 | git config -f .gitmodules submodule.s.path sub && |
| 98 | test_must_fail git submodule init |
| 99 | ' |
| 100 | |
| 101 | test_expect_success 'add aborts on repository with no commits' ' |
| 102 | cat >expect <<-\EOF && |
| 103 | fatal: '"'repo-no-commits'"' does not have a commit checked out |
| 104 | EOF |
| 105 | git init repo-no-commits && |
| 106 | test_must_fail git submodule add ../a ./repo-no-commits 2>actual && |
| 107 | test_cmp expect actual |
| 108 | ' |
| 109 | |
| 110 | test_expect_success 'status should ignore inner git repo when not added' ' |
| 111 | rm -fr inner && |
| 112 | mkdir inner && |
| 113 | ( |
| 114 | cd inner && |
| 115 | git init && |
| 116 | >t && |
| 117 | git add t && |
| 118 | git commit -m "initial" |
| 119 | ) && |
| 120 | test_must_fail git submodule status inner 2>output.err && |
| 121 | rm -fr inner && |
| 122 | test_grep "^error: .*did not match any file(s) known to git" output.err |
| 123 | ' |
| 124 | |
| 125 | test_expect_success 'setup - repository in init subdirectory' ' |
| 126 | mkdir init && |
| 127 | ( |
| 128 | cd init && |
| 129 | git init && |
| 130 | echo a >a && |
| 131 | git add a && |
| 132 | git commit -m "submodule commit 1" && |
| 133 | git tag -a -m "rev-1" rev-1 |
| 134 | ) |
| 135 | ' |
| 136 | |
| 137 | test_expect_success 'setup - commit with gitlink' ' |
| 138 | echo a >a && |
| 139 | echo z >z && |
| 140 | git add a init z && |
| 141 | git commit -m "super commit 1" |
| 142 | ' |
| 143 | |
| 144 | test_expect_success 'setup - hide init subdirectory' ' |
| 145 | mv init .subrepo |
| 146 | ' |
| 147 | |
| 148 | test_expect_success 'setup - repository to add submodules to' ' |
| 149 | git init addtest && |
| 150 | git init addtest-ignore |
| 151 | ' |
| 152 | |
| 153 | # The 'submodule add' tests need some repository to add as a submodule. |
| 154 | # The trash directory is a good one as any. We need to canonicalize |
| 155 | # the name, though, as some tests compare it to the absolute path git |
| 156 | # generates, which will expand symbolic links. |
| 157 | submodurl=$(pwd -P) |
| 158 | |
| 159 | listbranches() { |
| 160 | git for-each-ref --format='%(refname)' 'refs/heads/*' |
| 161 | } |
| 162 | |
| 163 | inspect() { |
| 164 | dir=$1 && |
| 165 | dotdot="${2:-..}" && |
| 166 | |
| 167 | ( |
| 168 | cd "$dir" && |
| 169 | listbranches >"$dotdot/heads" && |
| 170 | { git symbolic-ref HEAD || :; } >"$dotdot/head" && |
| 171 | git rev-parse HEAD >"$dotdot/head-sha1" && |
| 172 | git update-index --refresh && |
| 173 | git diff-files --exit-code && |
| 174 | git clean -n -d -x >"$dotdot/untracked" |
| 175 | ) |
| 176 | } |
| 177 | |
| 178 | test_expect_success 'submodule add' ' |
| 179 | echo "refs/heads/main" >expect && |
| 180 | |
| 181 | ( |
| 182 | cd addtest && |
| 183 | git submodule add -q "$submodurl" submod >actual && |
| 184 | test_must_be_empty actual && |
| 185 | echo "gitdir: ../.git/modules/submod" >expect && |
| 186 | test_cmp expect submod/.git && |
| 187 | ( |
| 188 | cd submod && |
| 189 | git config core.worktree >actual && |
| 190 | echo "../../../submod" >expect && |
| 191 | test_cmp expect actual && |
| 192 | rm -f actual expect |
| 193 | ) && |
| 194 | git submodule init |
| 195 | ) && |
| 196 | |
| 197 | rm -f heads head untracked && |
| 198 | inspect addtest/submod ../.. && |
| 199 | test_cmp expect heads && |
| 200 | test_cmp expect head && |
| 201 | test_must_be_empty untracked |
| 202 | ' |
| 203 | |
| 204 | test_expect_success !WINDOWS 'submodule add (absolute path)' ' |
| 205 | test_when_finished "git reset --hard" && |
| 206 | git submodule add "$submodurl" "$submodurl/add-abs" |
| 207 | ' |
| 208 | |
| 209 | test_expect_success 'setup parent and one repository' ' |
| 210 | test_create_repo parent && |
| 211 | test_commit -C parent one |
| 212 | ' |
| 213 | |
| 214 | test_expect_success 'redirected submodule add does not show progress' ' |
| 215 | git -C addtest submodule add "file://$submodurl/parent" submod-redirected \ |
| 216 | 2>err && |
| 217 | test_grep ! % err && |
| 218 | test_grep ! "Checking connectivity" err |
| 219 | ' |
| 220 | |
| 221 | test_expect_success 'redirected submodule add --progress does show progress' ' |
| 222 | git -C addtest submodule add --progress "file://$submodurl/parent" \ |
| 223 | submod-redirected-progress 2>err && \ |
| 224 | test_grep % err |
| 225 | ' |
| 226 | |
| 227 | test_expect_success 'submodule add to .gitignored path fails' ' |
| 228 | ( |
| 229 | cd addtest-ignore && |
| 230 | cat <<-\EOF >expect && |
| 231 | The following paths are ignored by one of your .gitignore files: |
| 232 | submod |
| 233 | hint: Use -f if you really want to add them. |
| 234 | hint: Disable this message with "git config set advice.addIgnoredFile false" |
| 235 | EOF |
| 236 | # Does not use test_commit due to the ignore |
| 237 | echo "*" > .gitignore && |
| 238 | git add --force .gitignore && |
| 239 | git commit -m"Ignore everything" && |
| 240 | ! git submodule add "$submodurl" submod >actual 2>&1 && |
| 241 | test_cmp expect actual |
| 242 | ) |
| 243 | ' |
| 244 | |
| 245 | test_expect_success 'submodule add to .gitignored path with --force' ' |
| 246 | ( |
| 247 | cd addtest-ignore && |
| 248 | git submodule add --force "$submodurl" submod |
| 249 | ) |
| 250 | ' |
| 251 | |
| 252 | test_expect_success 'submodule add to path with tracked content fails' ' |
| 253 | ( |
| 254 | cd addtest && |
| 255 | echo "fatal: '\''dir-tracked'\'' already exists in the index" >expect && |
| 256 | mkdir dir-tracked && |
| 257 | test_commit foo dir-tracked/bar && |
| 258 | test_must_fail git submodule add "$submodurl" dir-tracked >actual 2>&1 && |
| 259 | test_cmp expect actual |
| 260 | ) |
| 261 | ' |
| 262 | |
| 263 | test_expect_success 'submodule add to reconfigure existing submodule with --force' ' |
| 264 | ( |
| 265 | cd addtest-ignore && |
| 266 | bogus_url="$(pwd)/bogus-url" && |
| 267 | git submodule add --force "$bogus_url" submod && |
| 268 | git submodule add --force -b initial "$submodurl" submod-branch && |
| 269 | test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" && |
| 270 | test "$bogus_url" = "$(git config submodule.submod.url)" && |
| 271 | # Restore the url |
| 272 | git submodule add --force "$submodurl" submod && |
| 273 | test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" && |
| 274 | test "$submodurl" = "$(git config submodule.submod.url)" |
| 275 | ) |
| 276 | ' |
| 277 | |
| 278 | test_expect_success 'submodule add relays add --dry-run stderr' ' |
| 279 | test_when_finished "rm -rf addtest/.git/index.lock" && |
| 280 | ( |
| 281 | cd addtest && |
| 282 | : >.git/index.lock && |
| 283 | ! git submodule add "$submodurl" sub-while-locked 2>output.err && |
| 284 | test_grep "^fatal: .*index\.lock" output.err && |
| 285 | test_path_is_missing sub-while-locked |
| 286 | ) |
| 287 | ' |
| 288 | |
| 289 | test_expect_success 'submodule add --branch' ' |
| 290 | echo "refs/heads/initial" >expect-head && |
| 291 | cat <<-\EOF >expect-heads && |
| 292 | refs/heads/initial |
| 293 | refs/heads/main |
| 294 | EOF |
| 295 | |
| 296 | ( |
| 297 | cd addtest && |
| 298 | git submodule add -b initial "$submodurl" submod-branch && |
| 299 | test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" && |
| 300 | git submodule init |
| 301 | ) && |
| 302 | |
| 303 | rm -f heads head untracked && |
| 304 | inspect addtest/submod-branch ../.. && |
| 305 | test_cmp expect-heads heads && |
| 306 | test_cmp expect-head head && |
| 307 | test_must_be_empty untracked |
| 308 | ' |
| 309 | |
| 310 | test_expect_success 'submodule add with ./ in path' ' |
| 311 | echo "refs/heads/main" >expect && |
| 312 | |
| 313 | ( |
| 314 | cd addtest && |
| 315 | git submodule add "$submodurl" ././dotsubmod/./frotz/./ && |
| 316 | git submodule init |
| 317 | ) && |
| 318 | |
| 319 | rm -f heads head untracked && |
| 320 | inspect addtest/dotsubmod/frotz ../../.. && |
| 321 | test_cmp expect heads && |
| 322 | test_cmp expect head && |
| 323 | test_must_be_empty untracked |
| 324 | ' |
| 325 | |
| 326 | test_expect_success 'submodule add with /././ in path' ' |
| 327 | echo "refs/heads/main" >expect && |
| 328 | |
| 329 | ( |
| 330 | cd addtest && |
| 331 | git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ && |
| 332 | git submodule init |
| 333 | ) && |
| 334 | |
| 335 | rm -f heads head untracked && |
| 336 | inspect addtest/dotslashdotsubmod/frotz ../../.. && |
| 337 | test_cmp expect heads && |
| 338 | test_cmp expect head && |
| 339 | test_must_be_empty untracked |
| 340 | ' |
| 341 | |
| 342 | test_expect_success 'submodule add with // in path' ' |
| 343 | echo "refs/heads/main" >expect && |
| 344 | |
| 345 | ( |
| 346 | cd addtest && |
| 347 | git submodule add "$submodurl" slashslashsubmod///frotz// && |
| 348 | git submodule init |
| 349 | ) && |
| 350 | |
| 351 | rm -f heads head untracked && |
| 352 | inspect addtest/slashslashsubmod/frotz ../../.. && |
| 353 | test_cmp expect heads && |
| 354 | test_cmp expect head && |
| 355 | test_must_be_empty untracked |
| 356 | ' |
| 357 | |
| 358 | test_expect_success 'submodule add with /.. in path' ' |
| 359 | echo "refs/heads/main" >expect && |
| 360 | |
| 361 | ( |
| 362 | cd addtest && |
| 363 | git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. && |
| 364 | git submodule init |
| 365 | ) && |
| 366 | |
| 367 | rm -f heads head untracked && |
| 368 | inspect addtest/realsubmod ../.. && |
| 369 | test_cmp expect heads && |
| 370 | test_cmp expect head && |
| 371 | test_must_be_empty untracked |
| 372 | ' |
| 373 | |
| 374 | test_expect_success 'submodule add with ./, /.. and // in path' ' |
| 375 | echo "refs/heads/main" >expect && |
| 376 | |
| 377 | ( |
| 378 | cd addtest && |
| 379 | git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. && |
| 380 | git submodule init |
| 381 | ) && |
| 382 | |
| 383 | rm -f heads head untracked && |
| 384 | inspect addtest/realsubmod2 ../.. && |
| 385 | test_cmp expect heads && |
| 386 | test_cmp expect head && |
| 387 | test_must_be_empty untracked |
| 388 | ' |
| 389 | |
| 390 | test_expect_success !CYGWIN 'submodule add with \\ in path' ' |
| 391 | test_when_finished "rm -rf parent sub\\with\\backslash" && |
| 392 | |
| 393 | # Initialize a repo with a backslash in its name |
| 394 | git init sub\\with\\backslash && |
| 395 | touch sub\\with\\backslash/empty.file && |
| 396 | git -C sub\\with\\backslash add empty.file && |
| 397 | git -C sub\\with\\backslash commit -m "Added empty.file" && |
| 398 | |
| 399 | # Add that repository as a submodule |
| 400 | git init parent && |
| 401 | git -C parent submodule add ../sub\\with\\backslash |
| 402 | ' |
| 403 | |
| 404 | test_expect_success 'submodule add in subdirectory' ' |
| 405 | echo "refs/heads/main" >expect && |
| 406 | |
| 407 | mkdir addtest/sub && |
| 408 | ( |
| 409 | cd addtest/sub && |
| 410 | git submodule add "$submodurl" ../realsubmod3 && |
| 411 | git submodule init |
| 412 | ) && |
| 413 | |
| 414 | rm -f heads head untracked && |
| 415 | inspect addtest/realsubmod3 ../.. && |
| 416 | test_cmp expect heads && |
| 417 | test_cmp expect head && |
| 418 | test_must_be_empty untracked |
| 419 | ' |
| 420 | |
| 421 | test_expect_success 'submodule add in subdirectory with relative path should fail' ' |
| 422 | ( |
| 423 | cd addtest/sub && |
| 424 | test_must_fail git submodule add ../../ submod3 2>../../output.err |
| 425 | ) && |
| 426 | test_grep toplevel output.err |
| 427 | ' |
| 428 | |
| 429 | test_expect_success 'submodule add of a different algorithm fails' ' |
| 430 | git init --object-format=sha256 sha256 && |
| 431 | ( |
| 432 | cd sha256 && |
| 433 | test_commit abc && |
| 434 | git init --object-format=sha1 submodule && |
| 435 | test_commit -C submodule def && |
| 436 | test_must_fail git submodule add "$submodurl" submodule 2>err && |
| 437 | test_grep "cannot add a submodule of a different hash algorithm" err && |
| 438 | git ls-files --stage >entries && |
| 439 | test_grep ! ^160000 entries |
| 440 | ) && |
| 441 | git init --object-format=sha1 sha1 && |
| 442 | ( |
| 443 | cd sha1 && |
| 444 | test_commit abc && |
| 445 | git init --object-format=sha256 submodule && |
| 446 | test_commit -C submodule def && |
| 447 | test_must_fail git submodule add "$submodurl" submodule 2>err && |
| 448 | test_grep "cannot add a submodule of a different hash algorithm" err && |
| 449 | git ls-files --stage >entries && |
| 450 | test_grep ! ^160000 entries |
| 451 | ) |
| 452 | ' |
| 453 | |
| 454 | test_expect_success 'setup - add an example entry to .gitmodules' ' |
| 455 | git config --file=.gitmodules submodule.example.url git://example.com/init.git |
| 456 | ' |
| 457 | |
| 458 | test_expect_success 'status should fail for unmapped paths' ' |
| 459 | test_must_fail git submodule status |
| 460 | ' |
| 461 | |
| 462 | test_expect_success 'setup - map path in .gitmodules' ' |
| 463 | cat <<\EOF >expect && |
| 464 | [submodule "example"] |
| 465 | url = git://example.com/init.git |
| 466 | path = init |
| 467 | EOF |
| 468 | |
| 469 | git config --file=.gitmodules submodule.example.path init && |
| 470 | |
| 471 | test_cmp expect .gitmodules |
| 472 | ' |
| 473 | |
| 474 | test_expect_success 'status should only print one line' ' |
| 475 | git submodule status >lines && |
| 476 | test_line_count = 1 lines |
| 477 | ' |
| 478 | |
| 479 | test_expect_success 'status from subdirectory should have the same SHA1' ' |
| 480 | test_when_finished "rmdir addtest/subdir" && |
| 481 | ( |
| 482 | cd addtest && |
| 483 | mkdir subdir && |
| 484 | git submodule status >output && |
| 485 | awk "{print \$1}" <output >expect && |
| 486 | cd subdir && |
| 487 | git submodule status >../output && |
| 488 | awk "{print \$1}" <../output >../actual && |
| 489 | test_cmp ../expect ../actual && |
| 490 | git -C ../submod checkout HEAD^ && |
| 491 | git submodule status >../output && |
| 492 | awk "{print \$1}" <../output >../actual2 && |
| 493 | cd .. && |
| 494 | git submodule status >output && |
| 495 | awk "{print \$1}" <output >expect2 && |
| 496 | test_cmp expect2 actual2 && |
| 497 | ! test_cmp actual actual2 |
| 498 | ) |
| 499 | ' |
| 500 | |
| 501 | test_expect_success 'setup - fetch commit name from submodule' ' |
| 502 | rev1=$(cd .subrepo && git rev-parse HEAD) && |
| 503 | printf "rev1: %s\n" "$rev1" && |
| 504 | test -n "$rev1" |
| 505 | ' |
| 506 | |
| 507 | test_expect_success 'status should initially be "missing"' ' |
| 508 | git submodule status >lines && |
| 509 | test_grep "^-$rev1" lines |
| 510 | ' |
| 511 | |
| 512 | test_expect_success 'init should register submodule url in .git/config' ' |
| 513 | echo git://example.com/init.git >expect && |
| 514 | |
| 515 | git submodule init && |
| 516 | git config submodule.example.url >url && |
| 517 | git config submodule.example.url ./.subrepo && |
| 518 | |
| 519 | test_cmp expect url |
| 520 | ' |
| 521 | |
| 522 | test_expect_success 'status should still be "missing" after initializing' ' |
| 523 | rm -fr init && |
| 524 | mkdir init && |
| 525 | git submodule status >lines && |
| 526 | rm -fr init && |
| 527 | test_grep "^-$rev1" lines |
| 528 | ' |
| 529 | |
| 530 | test_failure_with_unknown_submodule () { |
| 531 | test_must_fail git submodule $1 no-such-submodule 2>output.err && |
| 532 | test_grep "^error: .*no-such-submodule" output.err |
| 533 | } |
| 534 | |
| 535 | test_expect_success 'init should fail with unknown submodule' ' |
| 536 | test_failure_with_unknown_submodule init |
| 537 | ' |
| 538 | |
| 539 | test_expect_success 'update should fail with unknown submodule' ' |
| 540 | test_failure_with_unknown_submodule update |
| 541 | ' |
| 542 | |
| 543 | test_expect_success 'status should fail with unknown submodule' ' |
| 544 | test_failure_with_unknown_submodule status |
| 545 | ' |
| 546 | |
| 547 | test_expect_success 'sync should fail with unknown submodule' ' |
| 548 | test_failure_with_unknown_submodule sync |
| 549 | ' |
| 550 | |
| 551 | test_expect_success 'update should fail when path is used by a file' ' |
| 552 | echo hello >expect && |
| 553 | |
| 554 | echo "hello" >init && |
| 555 | test_must_fail git submodule update && |
| 556 | |
| 557 | test_cmp expect init |
| 558 | ' |
| 559 | |
| 560 | test_expect_success 'update should fail when path is used by a nonempty directory' ' |
| 561 | echo hello >expect && |
| 562 | |
| 563 | rm -fr init && |
| 564 | mkdir init && |
| 565 | echo "hello" >init/a && |
| 566 | |
| 567 | test_must_fail git submodule update && |
| 568 | |
| 569 | test_cmp expect init/a |
| 570 | ' |
| 571 | |
| 572 | test_expect_success 'update should work when path is an empty dir' ' |
| 573 | rm -fr init && |
| 574 | rm -f head-sha1 && |
| 575 | echo "$rev1" >expect && |
| 576 | |
| 577 | mkdir init && |
| 578 | git submodule update -q >update.out && |
| 579 | test_must_be_empty update.out && |
| 580 | |
| 581 | inspect init && |
| 582 | test_cmp expect head-sha1 |
| 583 | ' |
| 584 | |
| 585 | test_expect_success 'status should be "up-to-date" after update' ' |
| 586 | git submodule status >list && |
| 587 | test_grep "^ $rev1" list |
| 588 | ' |
| 589 | |
| 590 | test_expect_success 'status "up-to-date" from subdirectory' ' |
| 591 | mkdir -p sub && |
| 592 | ( |
| 593 | cd sub && |
| 594 | git submodule status >../list |
| 595 | ) && |
| 596 | test_grep "^ $rev1" list && |
| 597 | test_grep "\\.\\./init" list |
| 598 | ' |
| 599 | |
| 600 | test_expect_success 'status "up-to-date" from subdirectory with path' ' |
| 601 | mkdir -p sub && |
| 602 | ( |
| 603 | cd sub && |
| 604 | git submodule status ../init >../list |
| 605 | ) && |
| 606 | test_grep "^ $rev1" list && |
| 607 | test_grep "\\.\\./init" list |
| 608 | ' |
| 609 | |
| 610 | test_expect_success 'status should be "modified" after submodule commit' ' |
| 611 | ( |
| 612 | cd init && |
| 613 | echo b >b && |
| 614 | git add b && |
| 615 | git commit -m "submodule commit 2" |
| 616 | ) && |
| 617 | |
| 618 | rev2=$(cd init && git rev-parse HEAD) && |
| 619 | test -n "$rev2" && |
| 620 | git submodule status >list && |
| 621 | |
| 622 | test_grep "^+$rev2" list |
| 623 | ' |
| 624 | |
| 625 | test_expect_success '"submodule --cached" command forms should be identical' ' |
| 626 | git submodule status --cached >expect && |
| 627 | |
| 628 | git submodule --cached >actual && |
| 629 | test_cmp expect actual && |
| 630 | |
| 631 | git submodule --cached status >actual && |
| 632 | test_cmp expect actual |
| 633 | ' |
| 634 | |
| 635 | test_expect_success 'the --cached sha1 should be rev1' ' |
| 636 | git submodule --cached status >list && |
| 637 | test_grep "^+$rev1" list |
| 638 | ' |
| 639 | |
| 640 | test_expect_success 'git diff should report the SHA1 of the new submodule commit' ' |
| 641 | git diff >diff && |
| 642 | test_grep "^+Subproject commit $rev2" diff |
| 643 | ' |
| 644 | |
| 645 | test_expect_success 'update should checkout rev1' ' |
| 646 | rm -f head-sha1 && |
| 647 | echo "$rev1" >expect && |
| 648 | |
| 649 | git submodule update init && |
| 650 | inspect init && |
| 651 | |
| 652 | test_cmp expect head-sha1 |
| 653 | ' |
| 654 | |
| 655 | test_expect_success 'status should be "up-to-date" after update' ' |
| 656 | git submodule status >list && |
| 657 | test_grep "^ $rev1" list |
| 658 | ' |
| 659 | |
| 660 | test_expect_success 'checkout superproject with subproject already present' ' |
| 661 | git checkout initial && |
| 662 | git checkout main |
| 663 | ' |
| 664 | |
| 665 | test_expect_success 'apply submodule diff' ' |
| 666 | git branch second && |
| 667 | ( |
| 668 | cd init && |
| 669 | echo s >s && |
| 670 | git add s && |
| 671 | git commit -m "change subproject" |
| 672 | ) && |
| 673 | git update-index --add init && |
| 674 | git commit -m "change init" && |
| 675 | git format-patch -1 --stdout >P.diff && |
| 676 | git checkout second && |
| 677 | git apply --index P.diff && |
| 678 | |
| 679 | git diff --cached main >staged && |
| 680 | test_must_be_empty staged |
| 681 | ' |
| 682 | |
| 683 | test_expect_success 'update --init' ' |
| 684 | mv init init2 && |
| 685 | git config -f .gitmodules submodule.example.url "$(pwd)/init2" && |
| 686 | git config --remove-section submodule.example && |
| 687 | test_must_fail git config submodule.example.url && |
| 688 | |
| 689 | git submodule update init 2> update.out && |
| 690 | test_grep "not initialized" update.out && |
| 691 | test_must_fail git rev-parse --resolve-git-dir init/.git && |
| 692 | |
| 693 | git submodule update --init init && |
| 694 | git rev-parse --resolve-git-dir init/.git |
| 695 | ' |
| 696 | |
| 697 | test_expect_success 'update --init from subdirectory' ' |
| 698 | mv init init2 && |
| 699 | git config -f .gitmodules submodule.example.url "$(pwd)/init2" && |
| 700 | git config --remove-section submodule.example && |
| 701 | test_must_fail git config submodule.example.url && |
| 702 | |
| 703 | mkdir -p sub && |
| 704 | ( |
| 705 | cd sub && |
| 706 | git submodule update ../init 2>update.out && |
| 707 | test_grep "not initialized" update.out && |
| 708 | test_must_fail git rev-parse --resolve-git-dir ../init/.git && |
| 709 | |
| 710 | git submodule update --init ../init |
| 711 | ) && |
| 712 | git rev-parse --resolve-git-dir init/.git |
| 713 | ' |
| 714 | |
| 715 | test_expect_success 'do not add files from a submodule' ' |
| 716 | |
| 717 | git reset --hard && |
| 718 | test_must_fail git add init/a |
| 719 | |
| 720 | ' |
| 721 | |
| 722 | test_expect_success 'gracefully add/reset submodule with a trailing slash' ' |
| 723 | |
| 724 | git reset --hard && |
| 725 | git commit -m "commit subproject" init && |
| 726 | (cd init && |
| 727 | echo b > a) && |
| 728 | git add init/ && |
| 729 | git diff --exit-code --cached init && |
| 730 | commit=$(cd init && |
| 731 | git commit -m update a >/dev/null && |
| 732 | git rev-parse HEAD) && |
| 733 | git add init/ && |
| 734 | test_must_fail git diff --exit-code --cached init && |
| 735 | test $commit = $(git ls-files --stage | |
| 736 | sed -n "s/^160000 \([^ ]*\).*/\1/p") && |
| 737 | git reset init/ && |
| 738 | git diff --exit-code --cached init |
| 739 | |
| 740 | ' |
| 741 | |
| 742 | test_expect_success 'ls-files gracefully handles trailing slash' ' |
| 743 | |
| 744 | test "init" = "$(git ls-files init/)" |
| 745 | |
| 746 | ' |
| 747 | |
| 748 | test_expect_success 'moving to a commit without submodule does not leave empty dir' ' |
| 749 | rm -rf init && |
| 750 | mkdir init && |
| 751 | git reset --hard && |
| 752 | git checkout initial && |
| 753 | test ! -d init && |
| 754 | git checkout second |
| 755 | ' |
| 756 | |
| 757 | test_expect_success 'submodule <invalid-subcommand> fails' ' |
| 758 | test_must_fail git submodule no-such-subcommand |
| 759 | ' |
| 760 | |
| 761 | test_expect_success 'add submodules without specifying an explicit path' ' |
| 762 | mkdir repo && |
| 763 | ( |
| 764 | cd repo && |
| 765 | git init && |
| 766 | echo r >r && |
| 767 | git add r && |
| 768 | git commit -m "repo commit 1" |
| 769 | ) && |
| 770 | git clone --bare repo/ bare.git && |
| 771 | ( |
| 772 | cd addtest && |
| 773 | git submodule add "$submodurl/repo" && |
| 774 | git config -f .gitmodules submodule.repo.path repo && |
| 775 | git submodule add "$submodurl/bare.git" && |
| 776 | git config -f .gitmodules submodule.bare.path bare |
| 777 | ) |
| 778 | ' |
| 779 | |
| 780 | test_expect_success 'add should fail when path is used by a file' ' |
| 781 | ( |
| 782 | cd addtest && |
| 783 | touch file && |
| 784 | test_must_fail git submodule add "$submodurl/repo" file |
| 785 | ) |
| 786 | ' |
| 787 | |
| 788 | test_expect_success 'add should fail when path is used by an existing directory' ' |
| 789 | ( |
| 790 | cd addtest && |
| 791 | mkdir empty-dir && |
| 792 | test_must_fail git submodule add "$submodurl/repo" empty-dir |
| 793 | ) |
| 794 | ' |
| 795 | |
| 796 | test_expect_success 'use superproject as upstream when path is relative and no url is set there' ' |
| 797 | ( |
| 798 | cd addtest && |
| 799 | git submodule add ../repo relative && |
| 800 | test "$(git config -f .gitmodules submodule.relative.url)" = ../repo && |
| 801 | git submodule sync relative && |
| 802 | test "$(git config submodule.relative.url)" = "$submodurl/repo" |
| 803 | ) |
| 804 | ' |
| 805 | |
| 806 | test_expect_success 'set up for relative path tests' ' |
| 807 | mkdir reltest && |
| 808 | ( |
| 809 | cd reltest && |
| 810 | git init && |
| 811 | mkdir sub && |
| 812 | ( |
| 813 | cd sub && |
| 814 | git init && |
| 815 | test_commit foo |
| 816 | ) && |
| 817 | git add sub && |
| 818 | git config -f .gitmodules submodule.sub.path sub && |
| 819 | git config -f .gitmodules submodule.sub.url ../subrepo && |
| 820 | cp .git/config pristine-.git-config && |
| 821 | cp .gitmodules pristine-.gitmodules |
| 822 | ) |
| 823 | ' |
| 824 | |
| 825 | test_expect_success '../subrepo works with URL - ssh://hostname/repo' ' |
| 826 | ( |
| 827 | cd reltest && |
| 828 | cp pristine-.git-config .git/config && |
| 829 | cp pristine-.gitmodules .gitmodules && |
| 830 | git config remote.origin.url ssh://hostname/repo && |
| 831 | git submodule init && |
| 832 | test "$(git config submodule.sub.url)" = ssh://hostname/subrepo |
| 833 | ) |
| 834 | ' |
| 835 | |
| 836 | test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' ' |
| 837 | ( |
| 838 | cd reltest && |
| 839 | cp pristine-.git-config .git/config && |
| 840 | cp pristine-.gitmodules .gitmodules && |
| 841 | git config remote.origin.url ssh://hostname:22/repo && |
| 842 | git submodule init && |
| 843 | test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo |
| 844 | ) |
| 845 | ' |
| 846 | |
| 847 | # About the choice of the path in the next test: |
| 848 | # - double-slash side-steps path mangling issues on Windows |
| 849 | # - it is still an absolute local path |
| 850 | # - there cannot be a server with a blank in its name just in case the |
| 851 | # path is used erroneously to access a //server/share style path |
| 852 | test_expect_success '../subrepo path works with local path - //somewhere else/repo' ' |
| 853 | ( |
| 854 | cd reltest && |
| 855 | cp pristine-.git-config .git/config && |
| 856 | cp pristine-.gitmodules .gitmodules && |
| 857 | git config remote.origin.url "//somewhere else/repo" && |
| 858 | git submodule init && |
| 859 | test "$(git config submodule.sub.url)" = "//somewhere else/subrepo" |
| 860 | ) |
| 861 | ' |
| 862 | |
| 863 | test_expect_success '../subrepo works with file URL - file:///tmp/repo' ' |
| 864 | ( |
| 865 | cd reltest && |
| 866 | cp pristine-.git-config .git/config && |
| 867 | cp pristine-.gitmodules .gitmodules && |
| 868 | git config remote.origin.url file:///tmp/repo && |
| 869 | git submodule init && |
| 870 | test "$(git config submodule.sub.url)" = file:///tmp/subrepo |
| 871 | ) |
| 872 | ' |
| 873 | |
| 874 | test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' ' |
| 875 | ( |
| 876 | cd reltest && |
| 877 | cp pristine-.git-config .git/config && |
| 878 | cp pristine-.gitmodules .gitmodules && |
| 879 | git config remote.origin.url helper:://hostname/repo && |
| 880 | git submodule init && |
| 881 | test "$(git config submodule.sub.url)" = helper:://hostname/subrepo |
| 882 | ) |
| 883 | ' |
| 884 | |
| 885 | test_expect_success '../subrepo works with scp-style URL - user@host:repo' ' |
| 886 | ( |
| 887 | cd reltest && |
| 888 | cp pristine-.git-config .git/config && |
| 889 | git config remote.origin.url user@host:repo && |
| 890 | git submodule init && |
| 891 | test "$(git config submodule.sub.url)" = user@host:subrepo |
| 892 | ) |
| 893 | ' |
| 894 | |
| 895 | test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' ' |
| 896 | ( |
| 897 | cd reltest && |
| 898 | cp pristine-.git-config .git/config && |
| 899 | cp pristine-.gitmodules .gitmodules && |
| 900 | git config remote.origin.url user@host:path/to/repo && |
| 901 | git submodule init && |
| 902 | test "$(git config submodule.sub.url)" = user@host:path/to/subrepo |
| 903 | ) |
| 904 | ' |
| 905 | |
| 906 | test_expect_success '../subrepo works with relative local path - foo' ' |
| 907 | ( |
| 908 | cd reltest && |
| 909 | cp pristine-.git-config .git/config && |
| 910 | cp pristine-.gitmodules .gitmodules && |
| 911 | git config remote.origin.url foo && |
| 912 | # actual: fails with an error |
| 913 | git submodule init && |
| 914 | test "$(git config submodule.sub.url)" = subrepo |
| 915 | ) |
| 916 | ' |
| 917 | |
| 918 | test_expect_success '../subrepo works with relative local path - foo/bar' ' |
| 919 | ( |
| 920 | cd reltest && |
| 921 | cp pristine-.git-config .git/config && |
| 922 | cp pristine-.gitmodules .gitmodules && |
| 923 | git config remote.origin.url foo/bar && |
| 924 | git submodule init && |
| 925 | test "$(git config submodule.sub.url)" = foo/subrepo |
| 926 | ) |
| 927 | ' |
| 928 | |
| 929 | test_expect_success '../subrepo works with relative local path - ./foo' ' |
| 930 | ( |
| 931 | cd reltest && |
| 932 | cp pristine-.git-config .git/config && |
| 933 | cp pristine-.gitmodules .gitmodules && |
| 934 | git config remote.origin.url ./foo && |
| 935 | git submodule init && |
| 936 | test "$(git config submodule.sub.url)" = subrepo |
| 937 | ) |
| 938 | ' |
| 939 | |
| 940 | test_expect_success '../subrepo works with relative local path - ./foo/bar' ' |
| 941 | ( |
| 942 | cd reltest && |
| 943 | cp pristine-.git-config .git/config && |
| 944 | cp pristine-.gitmodules .gitmodules && |
| 945 | git config remote.origin.url ./foo/bar && |
| 946 | git submodule init && |
| 947 | test "$(git config submodule.sub.url)" = foo/subrepo |
| 948 | ) |
| 949 | ' |
| 950 | |
| 951 | test_expect_success '../subrepo works with relative local path - ../foo' ' |
| 952 | ( |
| 953 | cd reltest && |
| 954 | cp pristine-.git-config .git/config && |
| 955 | cp pristine-.gitmodules .gitmodules && |
| 956 | git config remote.origin.url ../foo && |
| 957 | git submodule init && |
| 958 | test "$(git config submodule.sub.url)" = ../subrepo |
| 959 | ) |
| 960 | ' |
| 961 | |
| 962 | test_expect_success '../subrepo works with relative local path - ../foo/bar' ' |
| 963 | ( |
| 964 | cd reltest && |
| 965 | cp pristine-.git-config .git/config && |
| 966 | cp pristine-.gitmodules .gitmodules && |
| 967 | git config remote.origin.url ../foo/bar && |
| 968 | git submodule init && |
| 969 | test "$(git config submodule.sub.url)" = ../foo/subrepo |
| 970 | ) |
| 971 | ' |
| 972 | |
| 973 | test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' ' |
| 974 | ( |
| 975 | cd reltest && |
| 976 | cp pristine-.git-config .git/config && |
| 977 | cp pristine-.gitmodules .gitmodules && |
| 978 | mkdir -p a/b/c && |
| 979 | (cd a/b/c && git init && test_commit msg) && |
| 980 | git config remote.origin.url ../foo/bar.git && |
| 981 | git submodule add ../bar/a/b/c ./a/b/c && |
| 982 | git submodule init && |
| 983 | test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c |
| 984 | ) |
| 985 | ' |
| 986 | |
| 987 | test_expect_success 'moving the superproject does not break submodules' ' |
| 988 | ( |
| 989 | cd addtest && |
| 990 | git submodule status >expect |
| 991 | ) && |
| 992 | mv addtest addtest2 && |
| 993 | ( |
| 994 | cd addtest2 && |
| 995 | git submodule status >actual && |
| 996 | test_cmp expect actual |
| 997 | ) |
| 998 | ' |
| 999 | |
| 1000 | test_expect_success 'moving the submodule does not break the superproject' ' |
| 1001 | ( |
| 1002 | cd addtest2 && |
| 1003 | git submodule status |
| 1004 | ) >actual && |
| 1005 | sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect && |
| 1006 | mv addtest2/repo addtest2/repo.bak && |
| 1007 | test_when_finished "mv addtest2/repo.bak addtest2/repo" && |
| 1008 | ( |
| 1009 | cd addtest2 && |
| 1010 | git submodule status |
| 1011 | ) >actual && |
| 1012 | test_cmp expect actual |
| 1013 | ' |
| 1014 | |
| 1015 | test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' ' |
| 1016 | ( |
| 1017 | cd addtest2 && |
| 1018 | ( |
| 1019 | cd repo && |
| 1020 | echo "$submodurl/repo" >expect && |
| 1021 | git config remote.origin.url >actual && |
| 1022 | test_cmp expect actual && |
| 1023 | echo "gitdir: ../.git/modules/repo" >expect && |
| 1024 | test_cmp expect .git |
| 1025 | ) && |
| 1026 | rm -rf repo && |
| 1027 | git rm repo && |
| 1028 | git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual && |
| 1029 | test_must_be_empty actual && |
| 1030 | echo "gitdir: ../.git/modules/submod" >expect && |
| 1031 | test_cmp expect submod/.git && |
| 1032 | ( |
| 1033 | cd repo && |
| 1034 | echo "$submodurl/bare.git" >expect && |
| 1035 | git config remote.origin.url >actual && |
| 1036 | test_cmp expect actual && |
| 1037 | echo "gitdir: ../.git/modules/repo_new" >expect && |
| 1038 | test_cmp expect .git |
| 1039 | ) && |
| 1040 | echo "repo" >expect && |
| 1041 | test_must_fail git config -f .gitmodules submodule.repo.path && |
| 1042 | git config -f .gitmodules submodule.repo_new.path >actual && |
| 1043 | test_cmp expect actual && |
| 1044 | echo "$submodurl/repo" >expect && |
| 1045 | test_must_fail git config -f .gitmodules submodule.repo.url && |
| 1046 | echo "$submodurl/bare.git" >expect && |
| 1047 | git config -f .gitmodules submodule.repo_new.url >actual && |
| 1048 | test_cmp expect actual && |
| 1049 | echo "$submodurl/repo" >expect && |
| 1050 | git config submodule.repo.url >actual && |
| 1051 | test_cmp expect actual && |
| 1052 | echo "$submodurl/bare.git" >expect && |
| 1053 | git config submodule.repo_new.url >actual && |
| 1054 | test_cmp expect actual |
| 1055 | ) |
| 1056 | ' |
| 1057 | |
| 1058 | test_expect_success 'recursive relative submodules stay relative' ' |
| 1059 | test_when_finished "rm -rf super clone2 subsub sub3" && |
| 1060 | mkdir subsub && |
| 1061 | ( |
| 1062 | cd subsub && |
| 1063 | git init && |
| 1064 | >t && |
| 1065 | git add t && |
| 1066 | git commit -m "initial commit" |
| 1067 | ) && |
| 1068 | mkdir sub3 && |
| 1069 | ( |
| 1070 | cd sub3 && |
| 1071 | git init && |
| 1072 | >t && |
| 1073 | git add t && |
| 1074 | git commit -m "initial commit" && |
| 1075 | git submodule add ../subsub dirdir/subsub && |
| 1076 | git commit -m "add submodule subsub" |
| 1077 | ) && |
| 1078 | mkdir super && |
| 1079 | ( |
| 1080 | cd super && |
| 1081 | git init && |
| 1082 | >t && |
| 1083 | git add t && |
| 1084 | git commit -m "initial commit" && |
| 1085 | git submodule add ../sub3 && |
| 1086 | git commit -m "add submodule sub" |
| 1087 | ) && |
| 1088 | git clone super clone2 && |
| 1089 | ( |
| 1090 | cd clone2 && |
| 1091 | git submodule update --init --recursive && |
| 1092 | echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect && |
| 1093 | echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect |
| 1094 | ) && |
| 1095 | test_cmp clone2/sub3/.git_expect clone2/sub3/.git && |
| 1096 | test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git |
| 1097 | ' |
| 1098 | |
| 1099 | test_expect_success 'submodule add with an existing name fails unless forced' ' |
| 1100 | ( |
| 1101 | cd addtest2 && |
| 1102 | rm -rf repo && |
| 1103 | git rm repo && |
| 1104 | test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo && |
| 1105 | test ! -d repo && |
| 1106 | test_must_fail git config -f .gitmodules submodule.repo_new.path && |
| 1107 | test_must_fail git config -f .gitmodules submodule.repo_new.url && |
| 1108 | echo "$submodurl/bare.git" >expect && |
| 1109 | git config submodule.repo_new.url >actual && |
| 1110 | test_cmp expect actual && |
| 1111 | git submodule add -f -q --name repo_new "$submodurl/repo.git" repo && |
| 1112 | test -d repo && |
| 1113 | echo "repo" >expect && |
| 1114 | git config -f .gitmodules submodule.repo_new.path >actual && |
| 1115 | test_cmp expect actual && |
| 1116 | echo "$submodurl/repo.git" >expect && |
| 1117 | git config -f .gitmodules submodule.repo_new.url >actual && |
| 1118 | test_cmp expect actual && |
| 1119 | echo "$submodurl/repo.git" >expect && |
| 1120 | git config submodule.repo_new.url >actual && |
| 1121 | test_cmp expect actual |
| 1122 | ) |
| 1123 | ' |
| 1124 | |
| 1125 | test_expect_success 'set up a second submodule' ' |
| 1126 | git submodule add ./init2 example2 && |
| 1127 | git commit -m "submodule example2 added" |
| 1128 | ' |
| 1129 | |
| 1130 | test_expect_success 'submodule deinit works on repository without submodules' ' |
| 1131 | test_when_finished "rm -rf newdirectory" && |
| 1132 | mkdir newdirectory && |
| 1133 | ( |
| 1134 | cd newdirectory && |
| 1135 | git init && |
| 1136 | >file && |
| 1137 | git add file && |
| 1138 | git commit -m "repo should not be empty" && |
| 1139 | git submodule deinit . && |
| 1140 | git submodule deinit --all |
| 1141 | ) |
| 1142 | ' |
| 1143 | |
| 1144 | test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' ' |
| 1145 | git config submodule.example.foo bar && |
| 1146 | git config submodule.example2.frotz nitfol && |
| 1147 | git submodule deinit init && |
| 1148 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1149 | test -n "$(git config --get-regexp "submodule\.example2\.")" && |
| 1150 | test -f example2/.git && |
| 1151 | rmdir init |
| 1152 | ' |
| 1153 | |
| 1154 | test_expect_success 'submodule deinit should unset core.worktree' ' |
| 1155 | test_path_is_file .git/modules/example/config && |
| 1156 | test_must_fail git config -f .git/modules/example/config core.worktree |
| 1157 | ' |
| 1158 | |
| 1159 | test_expect_success 'submodule deinit from subdirectory' ' |
| 1160 | git submodule update --init && |
| 1161 | git config submodule.example.foo bar && |
| 1162 | mkdir -p sub && |
| 1163 | ( |
| 1164 | cd sub && |
| 1165 | git submodule deinit ../init >../output |
| 1166 | ) && |
| 1167 | test_grep "\\.\\./init" output && |
| 1168 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1169 | test -n "$(git config --get-regexp "submodule\.example2\.")" && |
| 1170 | test -f example2/.git && |
| 1171 | rmdir init |
| 1172 | ' |
| 1173 | |
| 1174 | test_expect_success 'submodule deinit . deinits all initialized submodules' ' |
| 1175 | git submodule update --init && |
| 1176 | git config submodule.example.foo bar && |
| 1177 | git config submodule.example2.frotz nitfol && |
| 1178 | test_must_fail git submodule deinit && |
| 1179 | git submodule deinit . >actual && |
| 1180 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1181 | test -z "$(git config --get-regexp "submodule\.example2\.")" && |
| 1182 | test_grep "Cleared directory .init" actual && |
| 1183 | test_grep "Cleared directory .example2" actual && |
| 1184 | rmdir init example2 |
| 1185 | ' |
| 1186 | |
| 1187 | test_expect_success 'submodule deinit --all deinits all initialized submodules' ' |
| 1188 | git submodule update --init && |
| 1189 | git config submodule.example.foo bar && |
| 1190 | git config submodule.example2.frotz nitfol && |
| 1191 | test_must_fail git submodule deinit && |
| 1192 | git submodule deinit --all >actual && |
| 1193 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1194 | test -z "$(git config --get-regexp "submodule\.example2\.")" && |
| 1195 | test_grep "Cleared directory .init" actual && |
| 1196 | test_grep "Cleared directory .example2" actual && |
| 1197 | rmdir init example2 |
| 1198 | ' |
| 1199 | |
| 1200 | test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' ' |
| 1201 | git submodule update --init && |
| 1202 | rm -rf init example2/* example2/.git && |
| 1203 | git submodule deinit init example2 >actual && |
| 1204 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1205 | test -z "$(git config --get-regexp "submodule\.example2\.")" && |
| 1206 | test_grep ! "Cleared directory .init" actual && |
| 1207 | test_grep "Cleared directory .example2" actual && |
| 1208 | rmdir init |
| 1209 | ' |
| 1210 | |
| 1211 | test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' ' |
| 1212 | git submodule update --init && |
| 1213 | echo X >>init/s && |
| 1214 | test_must_fail git submodule deinit init && |
| 1215 | test -n "$(git config --get-regexp "submodule\.example\.")" && |
| 1216 | test -f example2/.git && |
| 1217 | git submodule deinit -f init >actual && |
| 1218 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1219 | test_grep "Cleared directory .init" actual && |
| 1220 | rmdir init |
| 1221 | ' |
| 1222 | |
| 1223 | test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' ' |
| 1224 | git submodule update --init && |
| 1225 | echo X >>init/untracked && |
| 1226 | test_must_fail git submodule deinit init && |
| 1227 | test -n "$(git config --get-regexp "submodule\.example\.")" && |
| 1228 | test -f example2/.git && |
| 1229 | git submodule deinit -f init >actual && |
| 1230 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1231 | test_grep "Cleared directory .init" actual && |
| 1232 | rmdir init |
| 1233 | ' |
| 1234 | |
| 1235 | test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' ' |
| 1236 | git submodule update --init && |
| 1237 | ( |
| 1238 | cd init && |
| 1239 | git checkout HEAD^ |
| 1240 | ) && |
| 1241 | test_must_fail git submodule deinit init && |
| 1242 | test -n "$(git config --get-regexp "submodule\.example\.")" && |
| 1243 | test -f example2/.git && |
| 1244 | git submodule deinit -f init >actual && |
| 1245 | test -z "$(git config --get-regexp "submodule\.example\.")" && |
| 1246 | test_grep "Cleared directory .init" actual && |
| 1247 | rmdir init |
| 1248 | ' |
| 1249 | |
| 1250 | test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' ' |
| 1251 | git submodule update --init && |
| 1252 | git submodule deinit init >actual && |
| 1253 | test_grep "Submodule .example. (.*) unregistered for path .init" actual && |
| 1254 | test_grep "Cleared directory .init" actual && |
| 1255 | git submodule deinit init >actual && |
| 1256 | test_grep ! "Submodule .example. (.*) unregistered for path .init" actual && |
| 1257 | test_grep "Cleared directory .init" actual && |
| 1258 | git submodule deinit . >actual && |
| 1259 | test_grep ! "Submodule .example. (.*) unregistered for path .init" actual && |
| 1260 | test_grep "Submodule .example2. (.*) unregistered for path .example2" actual && |
| 1261 | test_grep "Cleared directory .init" actual && |
| 1262 | git submodule deinit . >actual && |
| 1263 | test_grep ! "Submodule .example. (.*) unregistered for path .init" actual && |
| 1264 | test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual && |
| 1265 | test_grep "Cleared directory .init" actual && |
| 1266 | git submodule deinit --all >actual && |
| 1267 | test_grep ! "Submodule .example. (.*) unregistered for path .init" actual && |
| 1268 | test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual && |
| 1269 | test_grep "Cleared directory .init" actual && |
| 1270 | rmdir init example2 |
| 1271 | ' |
| 1272 | |
| 1273 | test_expect_success 'submodule deinit absorbs .git directory if .git is a directory' ' |
| 1274 | git submodule update --init && |
| 1275 | ( |
| 1276 | cd init && |
| 1277 | rm .git && |
| 1278 | mv ../.git/modules/example .git && |
| 1279 | GIT_WORK_TREE=. git config --unset core.worktree |
| 1280 | ) && |
| 1281 | git submodule deinit init && |
| 1282 | test_path_is_missing init/.git && |
| 1283 | test -z "$(git config --get-regexp "submodule\.example\.")" |
| 1284 | ' |
| 1285 | |
| 1286 | test_expect_success 'submodule with UTF-8 name' ' |
| 1287 | svname=$(printf "\303\245 \303\244\303\266") && |
| 1288 | mkdir "$svname" && |
| 1289 | ( |
| 1290 | cd "$svname" && |
| 1291 | git init && |
| 1292 | >sub && |
| 1293 | git add sub && |
| 1294 | git commit -m "init sub" |
| 1295 | ) && |
| 1296 | git submodule add ./"$svname" && |
| 1297 | git submodule >&2 && |
| 1298 | test -n "$(git submodule | grep "$svname")" |
| 1299 | ' |
| 1300 | |
| 1301 | test_expect_success 'submodule add clone shallow submodule' ' |
| 1302 | mkdir super && |
| 1303 | pwd=$(pwd) && |
| 1304 | ( |
| 1305 | cd super && |
| 1306 | git init && |
| 1307 | git submodule add --depth=1 file://"$pwd"/example2 submodule && |
| 1308 | ( |
| 1309 | cd submodule && |
| 1310 | test 1 = $(git log --oneline | wc -l) |
| 1311 | ) |
| 1312 | ) |
| 1313 | ' |
| 1314 | |
| 1315 | test_expect_success 'setup superproject with submodules' ' |
| 1316 | git init sub1 && |
| 1317 | test_commit -C sub1 test && |
| 1318 | test_commit -C sub1 test2 && |
| 1319 | git init multisuper && |
| 1320 | git -C multisuper submodule add ../sub1 sub0 && |
| 1321 | git -C multisuper submodule add ../sub1 sub1 && |
| 1322 | git -C multisuper submodule add ../sub1 sub2 && |
| 1323 | git -C multisuper submodule add ../sub1 sub3 && |
| 1324 | git -C multisuper commit -m "add some submodules" |
| 1325 | ' |
| 1326 | |
| 1327 | cat >expect <<-EOF |
| 1328 | -sub0 |
| 1329 | sub1 (test2) |
| 1330 | sub2 (test2) |
| 1331 | sub3 (test2) |
| 1332 | EOF |
| 1333 | |
| 1334 | test_expect_success 'submodule update --init with a specification' ' |
| 1335 | test_when_finished "rm -rf multisuper_clone" && |
| 1336 | pwd=$(pwd) && |
| 1337 | git clone file://"$pwd"/multisuper multisuper_clone && |
| 1338 | git -C multisuper_clone submodule update --init . ":(exclude)sub0" && |
| 1339 | git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual && |
| 1340 | test_cmp expect actual |
| 1341 | ' |
| 1342 | |
| 1343 | test_expect_success 'submodule update --init with submodule.active set' ' |
| 1344 | test_when_finished "rm -rf multisuper_clone" && |
| 1345 | pwd=$(pwd) && |
| 1346 | git clone file://"$pwd"/multisuper multisuper_clone && |
| 1347 | git -C multisuper_clone config submodule.active "." && |
| 1348 | git -C multisuper_clone config --add submodule.active ":(exclude)sub0" && |
| 1349 | git -C multisuper_clone submodule update --init && |
| 1350 | git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual && |
| 1351 | test_cmp expect actual |
| 1352 | ' |
| 1353 | |
| 1354 | test_expect_success 'submodule update and setting submodule.<name>.active' ' |
| 1355 | test_when_finished "rm -rf multisuper_clone" && |
| 1356 | pwd=$(pwd) && |
| 1357 | git clone file://"$pwd"/multisuper multisuper_clone && |
| 1358 | git -C multisuper_clone config --bool submodule.sub0.active "true" && |
| 1359 | git -C multisuper_clone config --bool submodule.sub1.active "false" && |
| 1360 | git -C multisuper_clone config --bool submodule.sub2.active "true" && |
| 1361 | |
| 1362 | cat >expect <<-\EOF && |
| 1363 | sub0 (test2) |
| 1364 | -sub1 |
| 1365 | sub2 (test2) |
| 1366 | -sub3 |
| 1367 | EOF |
| 1368 | git -C multisuper_clone submodule update && |
| 1369 | git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual && |
| 1370 | test_cmp expect actual |
| 1371 | ' |
| 1372 | |
| 1373 | test_expect_success 'clone active submodule without submodule url set' ' |
| 1374 | test_when_finished "rm -rf test/test" && |
| 1375 | mkdir test && |
| 1376 | # another dir breaks accidental relative paths still being correct |
| 1377 | git clone file://"$pwd"/multisuper test/test && |
| 1378 | ( |
| 1379 | cd test/test && |
| 1380 | git config submodule.active "." && |
| 1381 | |
| 1382 | # do not pass --init flag, as the submodule is already active: |
| 1383 | git submodule update && |
| 1384 | git submodule status >actual_raw && |
| 1385 | |
| 1386 | cut -d" " -f3- actual_raw >actual && |
| 1387 | cat >expect <<-\EOF && |
| 1388 | sub0 (test2) |
| 1389 | sub1 (test2) |
| 1390 | sub2 (test2) |
| 1391 | sub3 (test2) |
| 1392 | EOF |
| 1393 | test_cmp expect actual |
| 1394 | ) |
| 1395 | ' |
| 1396 | |
| 1397 | test_expect_success 'update submodules without url set in .gitconfig' ' |
| 1398 | test_when_finished "rm -rf multisuper_clone" && |
| 1399 | git clone file://"$pwd"/multisuper multisuper_clone && |
| 1400 | |
| 1401 | git -C multisuper_clone submodule init && |
| 1402 | for s in sub0 sub1 sub2 sub3 |
| 1403 | do |
| 1404 | key=submodule.$s.url && |
| 1405 | git -C multisuper_clone config --local --unset $key && |
| 1406 | git -C multisuper_clone config --file .gitmodules --unset $key || return 1 |
| 1407 | done && |
| 1408 | |
| 1409 | test_must_fail git -C multisuper_clone submodule update 2>err && |
| 1410 | test_grep "cannot clone submodule .sub[0-3]. without a URL" err |
| 1411 | ' |
| 1412 | |
| 1413 | test_expect_success 'clone --recurse-submodules with a pathspec works' ' |
| 1414 | test_when_finished "rm -rf multisuper_clone" && |
| 1415 | cat >expected <<-\EOF && |
| 1416 | sub0 (test2) |
| 1417 | -sub1 |
| 1418 | -sub2 |
| 1419 | -sub3 |
| 1420 | EOF |
| 1421 | |
| 1422 | git clone --recurse-submodules="sub0" multisuper multisuper_clone && |
| 1423 | git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual && |
| 1424 | test_cmp expected actual |
| 1425 | ' |
| 1426 | |
| 1427 | test_expect_success 'clone with multiple --recurse-submodules options' ' |
| 1428 | test_when_finished "rm -rf multisuper_clone" && |
| 1429 | cat >expect <<-\EOF && |
| 1430 | -sub0 |
| 1431 | sub1 (test2) |
| 1432 | -sub2 |
| 1433 | sub3 (test2) |
| 1434 | EOF |
| 1435 | |
| 1436 | git clone --recurse-submodules="." \ |
| 1437 | --recurse-submodules=":(exclude)sub0" \ |
| 1438 | --recurse-submodules=":(exclude)sub2" \ |
| 1439 | multisuper multisuper_clone && |
| 1440 | git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual && |
| 1441 | test_cmp expect actual |
| 1442 | ' |
| 1443 | |
| 1444 | test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' ' |
| 1445 | test_when_finished "rm -rf multisuper_clone" && |
| 1446 | cat <<-\EOF >expect && |
| 1447 | -sub0 |
| 1448 | sub1 (test2) |
| 1449 | -sub2 |
| 1450 | sub3 (test2) |
| 1451 | EOF |
| 1452 | |
| 1453 | cat <<-\EOF >expect2 && |
| 1454 | -sub0 |
| 1455 | sub1 (test2) |
| 1456 | -sub2 |
| 1457 | sub3 (test2) |
| 1458 | -sub4 |
| 1459 | sub5 (test2) |
| 1460 | EOF |
| 1461 | |
| 1462 | git clone --recurse-submodules="." \ |
| 1463 | --recurse-submodules=":(exclude)sub0" \ |
| 1464 | --recurse-submodules=":(exclude)sub2" \ |
| 1465 | --recurse-submodules=":(exclude)sub4" \ |
| 1466 | multisuper multisuper_clone && |
| 1467 | |
| 1468 | git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual && |
| 1469 | test_cmp expect actual && |
| 1470 | |
| 1471 | git -C multisuper submodule add ../sub1 sub4 && |
| 1472 | git -C multisuper submodule add ../sub1 sub5 && |
| 1473 | git -C multisuper commit -m "add more submodules" && |
| 1474 | # obtain the new superproject |
| 1475 | git -C multisuper_clone pull && |
| 1476 | git -C multisuper_clone submodule update --init && |
| 1477 | git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual && |
| 1478 | test_cmp expect2 actual |
| 1479 | ' |
| 1480 | |
| 1481 | test_expect_success 'init properly sets the config' ' |
| 1482 | test_when_finished "rm -rf multisuper_clone" && |
| 1483 | git clone --recurse-submodules="." \ |
| 1484 | --recurse-submodules=":(exclude)sub0" \ |
| 1485 | multisuper multisuper_clone && |
| 1486 | |
| 1487 | git -C multisuper_clone submodule init -- sub0 sub1 && |
| 1488 | git -C multisuper_clone config --get submodule.sub0.active && |
| 1489 | test_must_fail git -C multisuper_clone config --get submodule.sub1.active |
| 1490 | ' |
| 1491 | |
| 1492 | test_expect_success 'recursive clone respects -q' ' |
| 1493 | test_when_finished "rm -rf multisuper_clone" && |
| 1494 | git clone -q --recurse-submodules multisuper multisuper_clone >actual && |
| 1495 | test_must_be_empty actual |
| 1496 | ' |
| 1497 | |
| 1498 | test_expect_success '`submodule init` and `init.templateDir`' ' |
| 1499 | mkdir -p tmpl/hooks && |
| 1500 | write_script tmpl/hooks/post-checkout <<-EOF && |
| 1501 | echo HOOK-RUN >&2 |
| 1502 | echo I was here >hook.run |
| 1503 | exit 1 |
| 1504 | EOF |
| 1505 | |
| 1506 | test_config init.templateDir "$(pwd)/tmpl" && |
| 1507 | test_when_finished \ |
| 1508 | "git config --global --unset init.templateDir || true" && |
| 1509 | ( |
| 1510 | sane_unset GIT_TEMPLATE_DIR && |
| 1511 | NO_SET_GIT_TEMPLATE_DIR=t && |
| 1512 | export NO_SET_GIT_TEMPLATE_DIR && |
| 1513 | |
| 1514 | git config --global init.templateDir "$(pwd)/tmpl" && |
| 1515 | test_must_fail git submodule \ |
| 1516 | add "$submodurl" sub-global 2>err && |
| 1517 | git config --global --unset init.templateDir && |
| 1518 | test_grep HOOK-RUN err && |
| 1519 | test_path_is_file sub-global/hook.run && |
| 1520 | |
| 1521 | git config init.templateDir "$(pwd)/tmpl" && |
| 1522 | git submodule add "$submodurl" sub-local 2>err && |
| 1523 | git config --unset init.templateDir && |
| 1524 | test_grep ! HOOK-RUN err && |
| 1525 | test_path_is_missing sub-local/hook.run |
| 1526 | ) |
| 1527 | ' |
| 1528 | |
| 1529 | test_expect_success 'submodule add fails when name is reused' ' |
| 1530 | git init test-submodule && |
| 1531 | ( |
| 1532 | cd test-submodule && |
| 1533 | git commit --allow-empty -m init && |
| 1534 | |
| 1535 | git init ../child-origin && |
| 1536 | git -C ../child-origin commit --allow-empty -m init && |
| 1537 | |
| 1538 | git submodule add ../child-origin child && |
| 1539 | git commit -m "Add submodule child" && |
| 1540 | |
| 1541 | git mv child child_old && |
| 1542 | git commit -m "Move child to child_old" && |
| 1543 | |
| 1544 | # Now adding a *new* repo at the old name must fail |
| 1545 | git init ../child2-origin && |
| 1546 | git -C ../child2-origin commit --allow-empty -m init && |
| 1547 | test_must_fail git submodule add ../child2-origin child 2>err && |
| 1548 | test_grep "already used for" err |
| 1549 | ) |
| 1550 | ' |
| 1551 | |
| 1552 | test_done |