From: Yoichi NAKAYAMA <yoic…@gmail.com> Currently, repo_git_path_append() can produce an absolute path when running in a linked worktree or when GIT_DIR is set to an absolute path. As a result, `submodule.<name>.gitdir` can be stored as an absolute path. Always store `submodule.<name>.gitdir` as a relative path to ensure that it continues to work when the worktree is moved along with the main worktree. The worktree must also use relative paths for this benefit, which requires `worktree.useRelativePaths` to be set to true. Signed-off-by: Yoichi NAKAYAMA <yoic…@gmail.com> --- builtin/submodule--helper.c | 70 ++++++++++++++-------- t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++ 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 461b682d0d..c20540497b 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -37,6 +37,7 @@ #include "wildmatch.h" #include "strbuf.h" #include "url.h" +#include "worktree.h" #define OPT_QUIET (1 << 0) #define OPT_CACHED (1 << 1) @@ -507,50 +508,62 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, static void create_default_gitdir_config(const char *submodule_name) { + struct worktree *wt = get_current_worktree(the_repository); + const char *raw_modules_dir; + struct strbuf tmp = STRBUF_INIT; + char *modules_dir; struct strbuf gitdir_path = STRBUF_INIT; struct git_hash_ctx ctx; char hex_name_hash[GIT_MAX_HEXSZ + 1], header[128]; unsigned char raw_name_hash[GIT_MAX_RAWSZ]; int header_len; + raw_modules_dir = worktree_git_path(wt, "modules/"); + modules_dir = xstrdup(is_absolute_path(raw_modules_dir) ? + relative_path(raw_modules_dir, wt->path, &tmp) : + raw_modules_dir); + strbuf_release(&tmp); + /* Case 1: try the plain module name */ - repo_git_path_append(the_repository, &gitdir_path, "modules/%s", submodule_name); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) { - strbuf_release(&gitdir_path); - return; - } + strbuf_addstr(&gitdir_path, modules_dir); + strbuf_addstr(&gitdir_path, submodule_name); + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */ strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); + strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) { - strbuf_release(&gitdir_path); - return; - } + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */ strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); + strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) - return; + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; /* Case 2.3: Try some derived gitdir names, see if one sticks */ for (char c = '0'; c <= '9'; c++) { strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); + strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved); strbuf_addch(&gitdir_path, c); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) - return; + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); + strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved); strbuf_addch(&gitdir_path, c); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) - return; + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; } /* Case 2.4: If all the above failed, try a hash of the name as a last resort */ @@ -563,17 +576,22 @@ static void create_default_gitdir_config(const char *submodule_name) git_hash_final(raw_name_hash, &ctx); hash_to_hex_algop_r(hex_name_hash, raw_name_hash, the_hash_algo); strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/%s", hex_name_hash); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) { - strbuf_release(&gitdir_path); - return; - } + strbuf_addstr(&gitdir_path, modules_dir); + strbuf_addstr(&gitdir_path, hex_name_hash); + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; /* Case 3: nothing worked, error out */ die(_("failed to set a valid default config for 'submodule.%s.gitdir'. " "Please ensure it is set, for example by running something like: " - "'git config submodule.%s.gitdir .git/modules/%s'"), - submodule_name, submodule_name, submodule_name); + "'git config submodule.%s.gitdir %s%s'"), + submodule_name, submodule_name, modules_dir, submodule_name); + +cleanup: + strbuf_release(&gitdir_path); + free(modules_dir); + free_worktree(wt); } static void init_submodule(const char *path, const char *prefix, diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh index ea86ecf7ee..02123b8468 100755 --- a/t/t7425-submodule-gitdir-path-extension.sh +++ b/t/t7425-submodule-gitdir-path-extension.sh @@ -279,6 +279,25 @@ test_expect_success '`git clone --recurse-submodules` respects init.defaultSubmo ) ' +test_expect_success 'git submodule init make gitdir relative' ' + test_when_finished "rm -rf repo-clone-init-wt" && + git clone upstream repo-clone-init-wt && + ( + cd repo-clone-init-wt && + git config core.repositoryformatversion 1 && + git config extensions.submodulePathConfig true && + git worktree add wt && + cd wt && + git submodule init && + git config submodule.sub1.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub1" >expect && + test_cmp expect actual && + git config submodule.sub2.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub2" >expect && + test_cmp expect actual + ) +' + test_expect_success 'submodule--helper migrates legacy modules' ' ( cd upstream &&
-- gitgitgadget