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