Skip to content

Commit ff5134b

Browse files
committed
Merge branch 'pb/recurse-submodule-in-worktree-fix'
The "--recurse-submodules" option of various subcommands did not work well when run in an alternate worktree, which has been corrected. * pb/recurse-submodule-in-worktree-fix: submodule.c: use get_git_dir() instead of get_git_common_dir() t2405: clarify test descriptions and simplify test t2405: use git -C and test_commit -C instead of subshells t7410: rename to t2405-worktree-submodule.sh
2 parents b5c71cc + a9472af commit ff5134b

File tree

3 files changed

+93
-80
lines changed

3 files changed

+93
-80
lines changed

submodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ int bad_to_remove_submodule(const char *path, unsigned flags)
18221822
void submodule_unset_core_worktree(const struct submodule *sub)
18231823
{
18241824
char *config_path = xstrfmt("%s/modules/%s/config",
1825-
get_git_common_dir(), sub->name);
1825+
get_git_dir(), sub->name);
18261826

18271827
if (git_config_set_in_file_gently(config_path, "core.worktree", NULL))
18281828
warning(_("Could not unset core.worktree setting in submodule '%s'"),
@@ -1925,7 +1925,7 @@ int submodule_move_head(const char *path,
19251925
ABSORB_GITDIR_RECURSE_SUBMODULES);
19261926
} else {
19271927
char *gitdir = xstrfmt("%s/modules/%s",
1928-
get_git_common_dir(), sub->name);
1928+
get_git_dir(), sub->name);
19291929
connect_work_tree_and_git_dir(path, gitdir, 0);
19301930
free(gitdir);
19311931

@@ -1935,7 +1935,7 @@ int submodule_move_head(const char *path,
19351935

19361936
if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
19371937
char *gitdir = xstrfmt("%s/modules/%s",
1938-
get_git_common_dir(), sub->name);
1938+
get_git_dir(), sub->name);
19391939
connect_work_tree_and_git_dir(path, gitdir, 1);
19401940
free(gitdir);
19411941
}

t/t2405-worktree-submodule.sh

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

t/t7410-submodule-checkout-to.sh

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)