@matheusbsbs / matheus-bagliano / commits / a9472afb63
Fork 0

submodule.c: use get_git_dir() instead of get_git_common_dir()

Ever since df56607dff (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 6e3c1595c6 (update submodules: add submodule_move_head, 2017-03-14) and submodule.c::submodule_unset_core_worktree, (re)introduced in 898c2e65b7 (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. Additionally, 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 <levraiphilippeblain@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Philippe Blain committed Jan 21, 2020 at 15:01 UTC a9472afb6328e22cdaea4225d3b36b97d2e9e704
2 files changed +36 -3
submodule.c
+3 -3
@@ -1811,7 +1811,7 @@ out:
1811 void submodule_unset_core_worktree(const struct submodule *sub)
1812 {
1813 char *config_path = xstrfmt("%s/modules/%s/config",
1814 - get_git_common_dir(), sub->name);
1814 + get_git_dir(), sub->name);
1815
1816 if (git_config_set_in_file_gently(config_path, "core.worktree", NULL))
1817 warning(_("Could not unset core.worktree setting in submodule '%s'"),
@@ -1914,7 +1914,7 @@ int submodule_move_head(const char *path,
1914 ABSORB_GITDIR_RECURSE_SUBMODULES);
1915 } else {
1916 char *gitdir = xstrfmt("%s/modules/%s",
1917 - get_git_common_dir(), sub->name);
1917 + get_git_dir(), sub->name);
1918 connect_work_tree_and_git_dir(path, gitdir, 0);
1919 free(gitdir);
1920
@@ -1924,7 +1924,7 @@ int submodule_move_head(const char *path,
1924
1925 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1926 char *gitdir = xstrfmt("%s/modules/%s",
1927 - get_git_common_dir(), sub->name);
1927 + get_git_dir(), sub->name);
1928 connect_work_tree_and_git_dir(path, gitdir, 1);
1929 free(gitdir);
1930 }
t/t2405-worktree-submodule.sh
+33
@@ -10,6 +10,7 @@ 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 &&
@@ -54,4 +55,36 @@ test_expect_success 'submodule is checked out after manually adding submodule wo
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