Git development
 help / color / mirror / Atom feed
From: "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>,
	Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Subject: [PATCH v2 0/3] submodule: support worktree-specific configuration
Date: Sat, 21 Nov 2026 15:10:50 +0000	[thread overview]
Message-ID: <pull.2306.v2.git.1795273853.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2306.git.1794484985.gitgitgadget@gmail.com>

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.

The changes include:

 * [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.


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.

Yoichi NAKAYAMA (3):
  submodule: make submodule.<name>.gitdir relative for worktrees
  submodule: store settings in worktree config when enabled
  submodule init: copy submodule.<name>.gitdir to config.worktree

 Documentation/config/submodule.adoc        |   3 +-
 builtin/submodule--helper.c                | 213 ++++++++++++++++-----
 builtin/worktree.c                         |  23 +++
 t/t2405-worktree-submodule.sh              | 129 +++++++++++++
 t/t7425-submodule-gitdir-path-extension.sh | 135 +++++++++++++
 5 files changed, 451 insertions(+), 52 deletions(-)


base-commit: f7de81456940dd6f3274e461149bba12965de609
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2306%2Fyoichi%2Fsubmodule-with-worktree-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2306/yoichi/submodule-with-worktree-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2306

Range-diff vs v1:

 1:  f9adc6c85c ! 1:  435ca42ec9 submodule: make submodule.<name>.gitdir relative for worktrees
     @@ builtin/submodule--helper.c: static int validate_and_set_submodule_gitdir(struct
      +	modules_dir = worktree_git_path(wt, "modules/");
      +	if (is_absolute_path(modules_dir)) {
      +		modules_dir = relative_path(modules_dir, wt->path, &tmp);
     ++	} else {
     ++		strbuf_addstr(&tmp, modules_dir);
     ++		modules_dir = tmp.buf;
       	}
       
      +	/* Case 1: try the plain module name */
 2:  14b156bc42 ! 2:  e816484923 submodule: store settings in worktree config when enabled
     @@ Commit message
          `submodule.<name>.active` to false in the worktree config to
          unregister the submodule.
      
     -    Unlike other settings, `submodule.<name>.gitdir` should not be shared
     -    between worktrees, so exclude it from the config copied by 'git
     +    Unlike other settings, `submodule.<name>.gitdir` must not be shared
     +    between worktrees. Different commits in the superproject may record
     +    different commits for the same submodule. If the gitdir is shared, the
     +    submodule worktrees end up sharing the same `HEAD` and index, causing
     +    Git commands to behave incorrectly. Therefore, store this setting in
     +    the worktree config and exclude it from the config copied by 'git
          worktree add'.
      
          Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
 3:  cdf089b635 ! 3:  b5d203d014 submodule init: copy submodule.<name>.gitdir to config.worktree
     @@ Metadata
       ## Commit message ##
          submodule init: copy submodule.<name>.gitdir to config.worktree
      
     -    If a repository already has gitdir settings in its repository config
     -    when extensions.worktreeConfig is enabled, those settings belong to a
     -    particular worktree. Make 'git submodule init' copy an existing
     -    setting to the worktree config if it belongs to the current worktree;
     -    otherwise, generate a new setting.
     +    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 <yoichi.nakayama@gmail.com>
      
     @@ builtin/submodule--helper.c: struct init_cb {
       };
       #define INIT_CB_INIT { 0 }
       
     -+static int validate_submodule_git_dir_with_worktree(const char *gitdir)
     ++static bool submodule_git_dir_matches_current_worktree(const char *gitdir)
      +{
      +	struct strbuf tmp = STRBUF_INIT;
      +	struct strbuf submodule = STRBUF_INIT;
      +	char *super;
     -+	int ret = 1;
     ++	bool matches;
      +
      +	if (is_absolute_path(gitdir))
      +		strbuf_addstr(&submodule, gitdir);
      +	else if (!repo_worktree_path_append(the_repository, &submodule,
      +					    "%s", gitdir))
     -+		return -1;
     ++		return false;
      +
      +	if (strbuf_normalize_path(&submodule)) {
      +		strbuf_release(&submodule);
     -+		return -1;
     ++		return false;
      +	}
      +
      +	super = absolute_pathdup(repo_get_git_dir(the_repository));
     -+
     -+	if (starts_with(relative_path(submodule.buf, super, &tmp), "modules/"))
     -+		ret = 0;
     ++	matches = starts_with(relative_path(submodule.buf, super, &tmp),
     ++			      "modules/");
      +
      +	strbuf_release(&submodule);
      +	strbuf_release(&tmp);
      +	free(super);
     -+	return ret;
     ++	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)
     @@ builtin/submodule--helper.c: struct init_cb {
      +					     struct worktree *wt)
       {
      -	const char *value;
     -+	const char *value = gitdir_path->buf;
     -+	char *value_to_free = NULL;
     ++	char *value = NULL;
       	char *key;
       
       	if (validate_submodule_git_dir(gitdir_path->buf, submodule_name))
     @@ builtin/submodule--helper.c: static int validate_and_set_submodule_gitdir(struct
      +		 * worktree.
      +		 */
      +		if (!repo_config_get_string(the_repository, key, &tmp)) {
     -+			if (validate_submodule_git_dir_with_worktree(tmp) ||
     -+			    validate_submodule_git_dir(tmp, submodule_name))
     ++			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 = value_to_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)) {
     ++		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)) {
     ++	if (repo_config_set_worktree_gently(the_repository, key,
     ++					    value ? value : gitdir_path->buf)) {
       		free(key);
     -+		free(value_to_free);
     ++		free(value);
       		return -1;
       	}
       
       	free(key);
     -+	free(value_to_free);
     ++	free(value);
       	return 0;
       }
       
     @@ t/t7425-submodule-gitdir-path-extension.sh: test_expect_success '`git clone --re
      +		git config submodule.sub2.gitdir .git/modules/sub2_custom &&
      +
      +		git config extensions.worktreeConfig true &&
     -+		git submodule init &&
     ++		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 &&
     @@ t/t7425-submodule-gitdir-path-extension.sh: test_expect_success '`git clone --re
      +
      +		git worktree add wt &&
      +		cd wt &&
     -+		git submodule init &&
     ++		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 &&
     @@ t/t7425-submodule-gitdir-path-extension.sh: test_expect_success 'git submodule i
      +		git config submodule.sub2.gitdir "$(cd .. && pwd -P)/.git/worktrees/wt/modules/sub2_custom" &&
      +
      +		git config extensions.worktreeConfig true &&
     -+		git submodule init &&
     ++		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 &&
     @@ t/t7425-submodule-gitdir-path-extension.sh: test_expect_success 'git submodule i
      +		test_cmp expect actual &&
      +
      +		cd .. &&
     -+		git submodule init &&
     ++		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 &&

-- 
gitgitgadget

  parent reply	other threads:[~2026-11-21 15:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 12:03 [PATCH 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-12 12:03 ` [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-14 15:27   ` Yoichi NAKAYAMA
2026-11-14 15:47     ` Yoichi NAKAYAMA
2026-11-12 12:03 ` [PATCH 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-12 12:03 ` [PATCH 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10 ` Yoichi NAKAYAMA via GitGitGadget [this message]
2026-11-21 15:10   ` [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10   ` [PATCH v2 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10   ` [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2306.v2.git.1795273853.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=yoichi.nakayama@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox