| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Combination of submodules and multiple worktrees' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | base_path=$(pwd -P) |
| 11 | |
| 12 | test_expect_success 'setup: create origin repos' ' |
| 13 | git config --global protocol.file.allow always && |
| 14 | git init origin/sub && |
| 15 | test_commit -C origin/sub file1 && |
| 16 | git init origin/main && |
| 17 | test_commit -C origin/main first && |
| 18 | git -C origin/main submodule add ../sub && |
| 19 | git -C origin/main commit -m "add sub" && |
| 20 | test_commit -C origin/sub "file1 updated" file1 file1updated file1updated && |
| 21 | git -C origin/main/sub pull && |
| 22 | git -C origin/main add sub && |
| 23 | git -C origin/main commit -m "sub updated" |
| 24 | ' |
| 25 | |
| 26 | test_expect_success 'setup: clone superproject to create main worktree' ' |
| 27 | git clone --recursive "$base_path/origin/main" main |
| 28 | ' |
| 29 | |
| 30 | rev1_hash_main=$(git --git-dir=origin/main/.git show --pretty=format:%h -q "HEAD~1") |
| 31 | rev1_hash_sub=$(git --git-dir=origin/sub/.git show --pretty=format:%h -q "HEAD~1") |
| 32 | |
| 33 | test_expect_success 'add superproject worktree' ' |
| 34 | git -C main worktree add "$base_path/worktree" "$rev1_hash_main" |
| 35 | ' |
| 36 | |
| 37 | test_expect_failure 'submodule is checked out just after worktree add' ' |
| 38 | git -C worktree diff --submodule main"^!" >out && |
| 39 | grep "file1 updated" out |
| 40 | ' |
| 41 | |
| 42 | test_expect_success 'add superproject worktree and initialize submodules' ' |
| 43 | git -C main worktree add "$base_path/worktree-submodule-update" "$rev1_hash_main" && |
| 44 | git -C worktree-submodule-update submodule update |
| 45 | ' |
| 46 | |
| 47 | test_expect_success 'submodule is checked out just after submodule update in linked worktree' ' |
| 48 | git -C worktree-submodule-update diff --submodule main"^!" >out && |
| 49 | grep "file1 updated" out |
| 50 | ' |
| 51 | |
| 52 | test_expect_success 'add superproject worktree and manually add submodule worktree' ' |
| 53 | git -C main worktree add "$base_path/linked_submodule" "$rev1_hash_main" && |
| 54 | git -C main/sub worktree add "$base_path/linked_submodule/sub" "$rev1_hash_sub" |
| 55 | ' |
| 56 | |
| 57 | test_expect_success 'submodule is checked out after manually adding submodule worktree' ' |
| 58 | git -C linked_submodule diff --submodule main"^!" >out && |
| 59 | grep "file1 updated" out |
| 60 | ' |
| 61 | |
| 62 | test_expect_success 'checkout --recurse-submodules uses $GIT_DIR for submodules in a linked worktree' ' |
| 63 | git -C main worktree add "$base_path/checkout-recurse" --detach && |
| 64 | git -C checkout-recurse submodule update --init && |
| 65 | echo "gitdir: ../../main/.git/worktrees/checkout-recurse/modules/sub" >expect-gitfile && |
| 66 | cat checkout-recurse/sub/.git >actual-gitfile && |
| 67 | test_cmp expect-gitfile actual-gitfile && |
| 68 | git -C main/sub rev-parse HEAD >expect-head-main && |
| 69 | git -C checkout-recurse checkout --recurse-submodules HEAD~1 && |
| 70 | cat checkout-recurse/sub/.git >actual-gitfile && |
| 71 | git -C main/sub rev-parse HEAD >actual-head-main && |
| 72 | test_cmp expect-gitfile actual-gitfile && |
| 73 | test_cmp expect-head-main actual-head-main |
| 74 | ' |
| 75 | |
| 76 | test_expect_success 'core.worktree is removed in $GIT_DIR/modules/<name>/config, not in $GIT_COMMON_DIR/modules/<name>/config' ' |
| 77 | echo "../../../sub" >expect-main && |
| 78 | git -C main/sub config --get core.worktree >actual-main && |
| 79 | test_cmp expect-main actual-main && |
| 80 | echo "../../../../../../checkout-recurse/sub" >expect-linked && |
| 81 | git -C checkout-recurse/sub config --get core.worktree >actual-linked && |
| 82 | test_cmp expect-linked actual-linked && |
| 83 | git -C checkout-recurse checkout --recurse-submodules first && |
| 84 | test_expect_code 1 git -C main/.git/worktrees/checkout-recurse/modules/sub config --get core.worktree >linked-config && |
| 85 | test_must_be_empty linked-config && |
| 86 | git -C main/sub config --get core.worktree >actual-main && |
| 87 | test_cmp expect-main actual-main |
| 88 | ' |
| 89 | |
| 90 | test_expect_success 'unsetting core.worktree does not prevent running commands directly against the submodule repository' ' |
| 91 | git -C main/.git/worktrees/checkout-recurse/modules/sub log |
| 92 | ' |
| 93 | |
| 94 | test_done |