When using multiple worktrees, submodule settings stored in the repository config are shared between all worktrees, even when they need to differ. For example, different branches may use different URLs for the same submodule. More importantly, sharing submodule.<name>.gitdir causes submodule worktrees to share the same HEAD and index, even when the corresponding superproject worktrees record different commits for the submodule. This series makes submodule settings worktree-specific when extensions.worktreeConfig is enabled. It also keeps submodule.<name>.gitdir relative for linked worktrees, so it continues to work when the worktree is moved along with the main worktree. Included commits: * [1/4] Keep submodule.<name>.gitdir relative for linked worktrees. * [2/4] Resolve symlinks in submodule.<name>.gitdir. * [3/4] Store submodule settings in the worktree config when extensions.worktreeConfig is enabled. * [4/4] Make git submodule init copy an existing gitdir setting to the current worktree config only when it belongs to that worktree. Changes in v4 ============= * [4/4] Resolve symlinks in submodule.<name>.gitdir value * [4/4] Fix the warning message Changes in v3 ============= * [1/4] Copy the path early, as relative_path may continue to use the provided buffer * [2/4] Add new commit to resolve symlinks in submodule.<name>.gitdir value * [3/4] Handle duplicate submodule.<name>.gitdir on 'git worktree add' * [3/4] Add more explanations * [4/4] Resolve symlinks in submodule_git_dir_matches_current_worktree() Changes in v2 ============= * Update the cover letter to better explain the problems this series addresses. * [1/3] Copy the string returned by worktree_git_path() early. * [2/3] Expand the explanation in the commit message for why the gitdir config is excluded from worktree add. * [3/3] Explain the case where the repository config contains a gitdir setting for another worktree, and warn when the setting is not copied. Included commits: * [1/3] Keep submodule.<name>.gitdir relative for linked worktrees. * [2/3] Store submodule settings in the worktree config when extensions.worktreeConfig is enabled. * [3/3] Make git submodule init copy an existing gitdir setting to the current worktree config only when it belongs to that worktree. Yoichi NAKAYAMA (4): submodule init: make submodule.<name>.gitdir relative for worktrees submodule init: resolve symlinks in submodule.<name>.gitdir submodule: store settings in worktree config when enabled submodule init: copy submodule.<name>.gitdir to config.worktree Documentation/config/extensions.adoc | 9 + Documentation/config/submodule.adoc | 14 +- Documentation/gitsubmodules.adoc | 26 ++- builtin/submodule--helper.c | 211 ++++++++++++++++----- builtin/worktree.c | 23 +++ t/t2405-worktree-submodule.sh | 157 +++++++++++++++ t/t7425-submodule-gitdir-path-extension.sh | 180 +++++++++++++++++- 7 files changed, 563 insertions(+), 57 deletions(-) base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2306%2Fyoichi%2Fsubmodule-with-worktree-v4 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2306/yoichi/submodule-with-worktree-v4 Pull-Request: https://github.com/gitgitgadget/git/pull/2306 Range-diff vs v3: 1: 6ed1297d8f = 1: 6ed1297d8f submodule init: make submodule.<name>.gitdir relative for worktrees 2: 594eefeceb = 2: 594eefeceb submodule init: resolve symlinks in submodule.<name>.gitdir 3: f5d3f71748 = 3: f5d3f71748 submodule: store settings in worktree config when enabled 4: 3e2c161298 ! 4: cfee53fc44 submodule init: copy submodule.<name>.gitdir to config.worktree @@ builtin/submodule--helper.c: struct init_cb { + "%s", gitdir)) + return false; + -+ if (strbuf_normalize_path(&submodule)) { ++ if (!strbuf_realpath_forgiving(&submodule, submodule.buf, 0)) { + strbuf_release(&submodule); + return false; + } @@ builtin/submodule--helper.c: struct init_cb { + +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." ++"it does not point to a gitdir for the current worktree." +); + static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, @@ builtin/submodule--helper.c: static int validate_and_set_submodule_gitdir(struct + if (!matches || + validate_submodule_git_dir(tmp, submodule_name)) { + if (!matches) -+ warning(submodule_mismatch_warning, key); ++ warning(_(submodule_mismatch_warning), ++ key); + free(tmp); + } else { + value = tmp; @@ t/t7425-submodule-gitdir-path-extension.sh: test_expect_success 'git submodule i + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo ".git/modules/sub2" >expect && ++ test_cmp expect actual ++ ) ++' ++ ++test_expect_success SYMLINKS 'git submodule init inherits related repository config by resolving symlinks' ' ++ test_when_finished "rm -rf repo-clone-init-symlink" && ++ git clone upstream repo-clone-init-symlink && ++ ( ++ cd repo-clone-init-symlink && ++ 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 && ++ ++ ln -s ../repo-clone-init-symlink link && ++ git config submodule.sub2.gitdir "$(pwd -P)/link/.git/modules/sub2" && ++ git config extensions.worktreeConfig true && ++ ++ GIT_DIR=link/.git git submodule init sub1 2>err && ++ test_must_be_empty err && ++ git config --worktree submodule.sub1.gitdir >actual && ++ git config --local submodule.sub1.gitdir >expect && ++ test_cmp expect actual && ++ ++ git submodule init sub2 2>err && ++ test_must_be_empty err && ++ git config --worktree submodule.sub2.gitdir >actual && ++ git config --local submodule.sub2.gitdir >expect && test_cmp expect actual ) '
-- gitgitgadget