From: Yoichi NAKAYAMA <yoic…@gmail.com> When extensions.worktreeConfig is enabled, an existing `submodule.<name>.gitdir` setting in the repository config may have been written by 'git submodule init' in another worktree. Reusing that setting would cause the current worktree to use the submodule gitdir of the other worktree. Make 'git submodule init' copy an existing setting to the worktree config only if it belongs to the current worktree; otherwise, generate a new setting. Warn when an existing setting does not match the current worktree and is not copied to the worktree config. Signed-off-by: Yoichi NAKAYAMA <yoic…@gmail.com> --- Documentation/config/submodule.adoc | 5 +- builtin/submodule--helper.c | 113 ++++++++++++++++++--- t/t7425-submodule-gitdir-path-extension.sh | 69 ++++++++++++- 3 files changed, 171 insertions(+), 16 deletions(-) diff --git a/Documentation/config/submodule.adoc b/Documentation/config/submodule.adoc index 6931888b71..e6b2bb4d3f 100644 --- a/Documentation/config/submodule.adoc +++ b/Documentation/config/submodule.adoc @@ -68,8 +68,9 @@ submodule.<name>.gitdir:: has no effect. When enabled, this config becomes the single source of truth for submodule gitdir paths and Git will error if it is missing. When `extensions.worktreeConfig` is enabled, 'git submodule init' stores - this setting in each worktree's config. See linkgit:git-config[1] for - details. + this setting in each worktree's config. If this setting already exists + in the repository config, copy the value only if it belongs to the + current worktree. See linkgit:git-config[1] for details. submodule.active:: A repeated field which contains a pathspec used to match against a diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 34783c71d1..8e2dfe204c 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -480,10 +480,45 @@ struct init_cb { }; #define INIT_CB_INIT { 0 } +static bool submodule_git_dir_matches_current_worktree(const char *gitdir) +{ + struct strbuf tmp = STRBUF_INIT; + struct strbuf submodule = STRBUF_INIT; + char *super; + bool matches; + + if (is_absolute_path(gitdir)) + strbuf_addstr(&submodule, gitdir); + else if (!repo_worktree_path_append(the_repository, &submodule, + "%s", gitdir)) + return false; + + if (strbuf_normalize_path(&submodule)) { + strbuf_release(&submodule); + return false; + } + + super = real_pathdup(repo_get_git_dir(the_repository), 1); + matches = starts_with(relative_path(submodule.buf, super, &tmp), + "modules/"); + + strbuf_release(&submodule); + strbuf_release(&tmp); + free(super); + return matches; +} + +static const char submodule_mismatch_warning[] = N_( +"config '%s' exists but was not copied to the worktree config because " +"it does not point to a gitdir for the current worktree. If you want " +"to keep using this setting, copy it to the worktree config." +); + static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, - const char *submodule_name) + const char *submodule_name, + struct worktree *wt) { - const char *value; + char *value = NULL; char *key; if (validate_submodule_git_dir(gitdir_path->buf, submodule_name)) @@ -491,18 +526,64 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, key = xstrfmt("submodule.%s.gitdir", submodule_name); - /* Nothing to do if the config already exists. */ - if (!repo_config_get_string_tmp(the_repository, key, &value)) { - free(key); - return 0; + if (the_repository->repository_format_worktree_config) { + struct config_set cs = { 0 }; + const char *filename = worktree_git_path(wt, "config.worktree"); + char *tmp; + + git_configset_init(&cs); + git_configset_add_file(&cs, filename); + + /* + * Nothing to do if the config already exists in the config set + * for the current worktree. + */ + if (!git_configset_get(&cs, key)) { + git_configset_clear(&cs); + free(key); + return 0; + } + + git_configset_clear(&cs); + + /* + * If the worktree config does not have this key, check the + * effective value from lower-priority config scopes and copy + * it to the worktree config if it belongs to the current + * worktree. + */ + if (!repo_config_get_string(the_repository, key, &tmp)) { + bool matches = + submodule_git_dir_matches_current_worktree(tmp); + + if (!matches || + validate_submodule_git_dir(tmp, submodule_name)) { + if (!matches) + warning(submodule_mismatch_warning, key); + free(tmp); + } else { + value = tmp; + } + } + } else { + const char *value_; + + /* Nothing to do if the config already exists. */ + if (!repo_config_get_string_tmp(the_repository, key, &value_)) { + free(key); + return 0; + } } - if (repo_config_set_worktree_gently(the_repository, key, gitdir_path->buf)) { + if (repo_config_set_worktree_gently(the_repository, key, + value ? value : gitdir_path->buf)) { free(key); + free(value); return -1; } free(key); + free(value); return 0; } @@ -526,7 +607,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr(&gitdir_path, submodule_name); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */ @@ -534,7 +616,8 @@ static void create_default_gitdir_config(const char *submodule_name) 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)) + submodule_name, + wt)) goto cleanup; /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */ @@ -542,7 +625,8 @@ static void create_default_gitdir_config(const char *submodule_name) 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)) + submodule_name, + wt)) goto cleanup; /* Case 2.3: Try some derived gitdir names, see if one sticks */ @@ -552,7 +636,8 @@ static void create_default_gitdir_config(const char *submodule_name) 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)) + submodule_name, + wt)) goto cleanup; strbuf_reset(&gitdir_path); @@ -560,7 +645,8 @@ static void create_default_gitdir_config(const char *submodule_name) 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)) + submodule_name, + wt)) goto cleanup; } @@ -577,7 +663,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr(&gitdir_path, hex_name_hash); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; /* Case 3: nothing worked, error out */ diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh index 590febcafd..06d67c654e 100755 --- a/t/t7425-submodule-gitdir-path-extension.sh +++ b/t/t7425-submodule-gitdir-path-extension.sh @@ -338,7 +338,49 @@ test_expect_success '`git clone --recurse-submodules` respects init.defaultSubmo ) ' -test_expect_success 'git submodule init make gitdir relative' ' +test_expect_success 'git submodule init inherits related repository config for main' ' + test_when_finished "rm -rf repo-clone-init-main" && + git clone upstream repo-clone-init-main && + ( + cd repo-clone-init-main && + git config core.repositoryformatversion 1 && + git config extensions.submodulePathConfig true && + git submodule init && + git config submodule.sub1.gitdir >actual && + echo ".git/modules/sub1" >expect && + test_cmp expect actual && + git config submodule.sub2.gitdir >actual && + echo ".git/modules/sub2" >expect && + test_cmp expect actual && + + # Modify values so that they can be distinguished from the default + git config submodule.sub1.gitdir .git/modules/sub1_custom && + git config submodule.sub2.gitdir .git/modules/sub2_custom && + + git config extensions.worktreeConfig true && + git submodule init 2>err && + test_must_be_empty err && + git config --worktree submodule.sub1.gitdir >actual && + echo ".git/modules/sub1_custom" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo ".git/modules/sub2_custom" >expect && + test_cmp expect actual && + + git worktree add wt && + cd wt && + git submodule init 2>err && + test_grep "does not point to a gitdir for the current worktree" err && + git config --worktree submodule.sub1.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub1" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub2" >expect && + test_cmp expect actual + ) +' + +test_expect_success 'git submodule init inherits related repository config for worktree' ' test_when_finished "rm -rf repo-clone-init-wt" && git clone upstream repo-clone-init-wt && ( @@ -353,6 +395,31 @@ test_expect_success 'git submodule init make gitdir relative' ' test_cmp expect actual && git config submodule.sub2.gitdir >actual && echo "../.git/worktrees/wt/modules/sub2" >expect && + test_cmp expect actual && + + # Modify values so that they can be distinguished from the default + git config submodule.sub1.gitdir ../.git/worktrees/wt/modules/sub1_custom && + # Use an absolute path to simulate a value written by older Git + git config submodule.sub2.gitdir "$(cd .. && pwd -P)/.git/worktrees/wt/modules/sub2_custom" && + + git config extensions.worktreeConfig true && + git submodule init 2>err && + test_must_be_empty err && + git config --worktree submodule.sub1.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub1_custom" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo "$(cd .. && pwd -P)/.git/worktrees/wt/modules/sub2_custom" >expect && + test_cmp expect actual && + + cd .. && + git submodule init 2>err && + test_grep "does not point to a gitdir for the current worktree" err && + git config --worktree submodule.sub1.gitdir >actual && + echo ".git/modules/sub1" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo ".git/modules/sub2" >expect && test_cmp expect actual ) '
-- gitgitgadget