Skip to content

Commit 72cdb2f

Browse files
committed
submodule.c: use get_git_dir() instead of get_git_common_dir()
Ever since df56607 (git-common-dir: make "modules/" per-working-directory directory, 2014-11-30), submodules in linked worktrees are cloned to $GIT_DIR/modules, i.e. $GIT_COMMON_DIR/worktrees/<name>/modules. However, this convention was not followed when the worktree updater commands checkout, reset and read-tree learned to recurse into submodules. Specifically, submodule.c::submodule_move_head, introduced in 6e3c159 (update submodules: add submodule_move_head, 2017-03-14) and submodule.c::submodule_unset_core_worktree, (re)introduced in 898c2e6 (submodule: unset core.worktree if no working tree is present, 2018-12-14) use get_git_common_dir() instead of get_git_dir() to get the path of the submodule repository. This means that, for example, 'git checkout --recurse-submodules <branch>' in a linked worktree will correctly checkout <branch>, detach the submodule's HEAD at the commit recorded in <branch> and update the submodule working tree, but the submodule HEAD that will be moved is the one in $GIT_COMMON_DIR/modules/<name>/, i.e. the submodule repository of the main superproject working tree. It will also rewrite the gitfile in the submodule working tree of the linked worktree to point to $GIT_COMMON_DIR/modules/<name>/. This leads to an incorrect (and confusing!) state in the submodule working tree of the main superproject worktree. Additionnally, if switching to a commit where the submodule is not present, submodule_unset_core_worktree will be called and will incorrectly remove 'core.wortree' from the config file of the submodule in the main superproject worktree, $GIT_COMMON_DIR/modules/<name>/config. Fix this by constructing the path to the submodule repository using get_git_dir() in both submodule_move_head and submodule_unset_core_worktree. Signed-off-by: Philippe Blain <[email protected]>
1 parent 6e0e664 commit 72cdb2f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

submodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ int bad_to_remove_submodule(const char *path, unsigned flags)
18111811
void submodule_unset_core_worktree(const struct submodule *sub)
18121812
{
18131813
char *config_path = xstrfmt("%s/modules/%s/config",
1814-
get_git_common_dir(), sub->name);
1814+
get_git_dir(), sub->name);
18151815

18161816
if (git_config_set_in_file_gently(config_path, "core.worktree", NULL))
18171817
warning(_("Could not unset core.worktree setting in submodule '%s'"),
@@ -1914,7 +1914,7 @@ int submodule_move_head(const char *path,
19141914
ABSORB_GITDIR_RECURSE_SUBMODULES);
19151915
} else {
19161916
char *gitdir = xstrfmt("%s/modules/%s",
1917-
get_git_common_dir(), sub->name);
1917+
get_git_dir(), sub->name);
19181918
connect_work_tree_and_git_dir(path, gitdir, 0);
19191919
free(gitdir);
19201920

@@ -1924,7 +1924,7 @@ int submodule_move_head(const char *path,
19241924

19251925
if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
19261926
char *gitdir = xstrfmt("%s/modules/%s",
1927-
get_git_common_dir(), sub->name);
1927+
get_git_dir(), sub->name);
19281928
connect_work_tree_and_git_dir(path, gitdir, 1);
19291929
free(gitdir);
19301930
}

t/t2405-worktree-submodule.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test_expect_success 'setup: create origin repos' '
1010
git init origin/sub &&
1111
test_commit -C origin/sub file1 &&
1212
git init origin/main &&
13+
test_commit -C origin/main first &&
1314
git -C origin/main submodule add ../sub &&
1415
git -C origin/main commit -m "add sub" &&
1516
test_commit -C origin/sub "file1-updated" file1 file1updated &&
@@ -54,4 +55,25 @@ test_expect_success 'submodule is checked out after manually adding submodule wo
5455
grep "file1-updated" out
5556
'
5657

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+
cat checkout-recurse/sub/.git > expect-gitfile &&
62+
git -C main/sub rev-parse HEAD > expect-head-main &&
63+
git -C checkout-recurse checkout --recurse-submodules HEAD~1 &&
64+
cat checkout-recurse/sub/.git > actual-gitfile &&
65+
git -C main/sub rev-parse HEAD > actual-head-main &&
66+
test_cmp expect-gitfile actual-gitfile &&
67+
test_cmp expect-head-main actual-head-main
68+
'
69+
70+
test_expect_success 'core.worktree is removed in $GIT_DIR/modules/<name>/config, not in $GIT_COMMON_DIR/modules/<name>/config' '
71+
git -C main/sub config --get core.worktree > expect &&
72+
git -C checkout-recurse checkout --recurse-submodules first &&
73+
test_might_fail git -C main/.git/worktrees/checkout-recurse/modules/sub config --get core.worktree > linked-config &&
74+
test_must_be_empty linked-config &&
75+
test_might_fail git -C main/sub config --get core.worktree > actual &&
76+
test_cmp expect actual
77+
'
78+
5779
test_done

0 commit comments

Comments
 (0)