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 v3 0/4] submodule: support worktree-specific configuration
Date: Sat, 28 Nov 2026 22:05:53 +0000 [thread overview]
Message-ID: <pull.2306.v3.git.1795903557.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.
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 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 | 147 +++++++++++++-
7 files changed, 530 insertions(+), 57 deletions(-)
base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2306%2Fyoichi%2Fsubmodule-with-worktree-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2306/yoichi/submodule-with-worktree-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/2306
Range-diff vs v2:
1: 435ca42ec9 ! 1: 6ed1297d8f submodule: make submodule.<name>.gitdir relative for worktrees
@@ Metadata
Author: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
## Commit message ##
- submodule: make submodule.<name>.gitdir relative for worktrees
+ submodule init: make submodule.<name>.gitdir relative for worktrees
- For a linked worktree, always keep `submodule.<name>.gitdir` relative
- to ensure it continues to work when the worktree is moved along with
- the main worktree. (This benefit also requires
- worktree.useRelativePaths to be set to true.)
+ 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 <yoichi.nakayama@gmail.com>
@@ builtin/submodule--helper.c: static int validate_and_set_submodule_gitdir(struct
static void create_default_gitdir_config(const char *submodule_name)
{
+ struct worktree *wt = get_current_worktree(the_repository);
-+ const char *modules_dir;
++ 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;
-- /* Case 1: try the plain module name */
++ 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;
-+ 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 */
+- }
+ 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/");
@@ builtin/submodule--helper.c: static void create_default_gitdir_config(const char
+
+cleanup:
+ strbuf_release(&gitdir_path);
-+ strbuf_release(&tmp);
++ free(modules_dir);
+ free_worktree(wt);
}
-: ---------- > 2: 594eefeceb submodule init: resolve symlinks in submodule.<name>.gitdir
2: e816484923 ! 3: f5d3f71748 submodule: store settings in worktree config when enabled
@@ Commit message
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
+ ## Documentation/config/extensions.adoc ##
+@@ Documentation/config/extensions.adoc: these config values on a per-worktree basis, and uses the
+ worktree independently. See linkgit:git-sparse-checkout[1] for more
+ details.
+ +
++Submodule configuration is also affected by this extension.
++The `submodule.<name>.gitdir` variable must be worktree-specific, as
++each worktree must use a separate gitdir for the same submodule.
++Other submodule configuration variables may be shared between worktrees,
++but Git commands that update submodule configuration store these
++variables in the worktree-specific `config.worktree` file when this
++extension is enabled, so that each worktree can have its own submodule
++configuration.
+++
+ For historical reasons, this extension is respected regardless of the
+ `core.repositoryFormatVersion` setting.
+
## Documentation/config/submodule.adoc ##
-@@ Documentation/config/submodule.adoc: submodule.<name>.gitdir::
+@@ Documentation/config/submodule.adoc: submodule.<name>.url::
+ set, the presence of this variable is used as a fallback to indicate
+ whether the submodule is of interest to git commands.
+ See linkgit:git-submodule[1] and linkgit:gitmodules[5] for details.
++ When `extensions.worktreeConfig` is enabled, 'git submodule init' and
++ 'git submodule sync' store this setting in each worktree's config.
++ See linkgit:git-config[1] for details.
+
+ submodule.<name>.update::
+ The method by which a submodule is updated by 'git submodule update',
+@@ Documentation/config/submodule.adoc: submodule.<name>.update::
+ and `pull.rebase` are more specific. It is populated by
+ `git submodule init` from the linkgit:gitmodules[5] file.
+ See description of 'update' command in linkgit:git-submodule[1].
++ When `extensions.worktreeConfig` is enabled, 'git submodule init' stores
++ this setting in each worktree's config. See linkgit:git-config[1] for
++ details.
+
+ submodule.<name>.branch::
+ The remote branch name for a submodule, used by `git submodule
+@@ Documentation/config/submodule.adoc: submodule.<name>.active::
+ commands. This config option takes precedence over the
+ submodule.active config option. See linkgit:gitsubmodules[7] for
+ details.
++ When `extensions.worktreeConfig` is enabled, 'git submodule init' stores
++ this setting in each worktree's config. See linkgit:git-config[1] for
++ details.
+
+ submodule.<name>.gitdir::
+ This sets the gitdir path for submodule <name>. This configuration is
respected when `extensions.submodulePathConfig` is enabled, otherwise it
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.
- See linkgit:git-config[1] for details.
-+ When `extensions.worktreeConfig` is enabled, this setting is stored in
-+ each worktree's config. See linkgit:git-config[1] for details.
++ When `extensions.worktreeConfig` is enabled, 'git submodule init' stores
++ this setting in each worktree's config. See linkgit:git-config[1] for
++ details.
submodule.active::
A repeated field which contains a pathspec used to match against a
+ ## Documentation/gitsubmodules.adoc ##
+@@ Documentation/gitsubmodules.adoc: check if the submodule has any changes not published to any remote. The
+ remotes are configured in the submodule as usual in the `$GIT_DIR/config`
+ file.
+
+- * The configuration file `$GIT_DIR/config` in the superproject.
+- Git only recurses into active submodules
++ * The configuration files `$GIT_DIR/config` and, when the
++ `extensions.worktreeConfig` extension is enabled, `$GIT_DIR/config.worktree`
++ in the superproject. Git only recurses into active submodules
+ (see <<ACTIVE_SUBMODULES,"ACTIVE SUBMODULES">> section below).
+ +
+ If the submodule is not yet initialized, then the configuration
+@@ Documentation/gitsubmodules.adoc: If the submodule has never been initialized, this is the only place
+ where submodule configuration is found. It serves as the last fallback
+ to specify where to obtain the submodule from.
+
++MULTIPLE WORKTREES
++------------------
++
++When using multiple worktrees, it is recommended to enable the
++`extensions.worktreeConfig` extension. Without this extension,
++submodule configuration is shared between worktrees, which can
++cause unexpected interactions. For example, if different branches
++use different URLs for a submodule, a URL configured in one
++worktree may be used by another worktree, preventing it from
++fetching the required commits.
++
++Enabling this extension allows Git commands to store submodule
++configuration in each worktree's `config.worktree` file, keeping
++the configuration independent between worktrees.
++
+ [[FORMS]]
+ FORMS
+ -----
+@@ Documentation/gitsubmodules.adoc: The directory which is supposed to be the working directory is empty instead.
+ +
+ A submodule can be deinitialized by running `git submodule deinit`.
+ Besides emptying the working directory, this command only modifies
+-the superproject's `$GIT_DIR/config` file, so the superproject's history
+-is not affected. This can be undone using `git submodule init`.
++the superproject's configuration file (`$GIT_DIR/config` or
++`$GIT_DIR/config.worktree`, depending on whether the
++`extensions.worktreeConfig` extension is enabled), so the superproject's
++history is not affected. This can be undone using `git submodule init`.
+
+ * Deleted submodule: A submodule can be deleted by running
+ `git rm <submodule-path> && git commit`. This can be undone
+
## builtin/submodule--helper.c ##
@@ builtin/submodule--helper.c: static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,
return 0;
@@ builtin/submodule--helper.c: static void deinit_submodule(const char *path, cons
+ char *key = xstrfmt("submodule.%s.active", sub->name);
+
+ repo_config_clear(the_repository);
-+ if (is_submodule_active(the_repository, sub->name))
++ if (is_submodule_active(the_repository, path))
+ repo_config_set_worktree_gently(the_repository,
+ key, "false");
+ free(key);
@@ builtin/worktree.c: static void copy_filtered_worktree_config(const char *worktr
+ string_list_insert(&keys, var);
+ }
+ for_each_string_list_item(item, &keys) {
-+ if (repo_config_set_in_file_gently(the_repository,
-+ to_file, item->string,
-+ NULL, NULL))
++ if (repo_config_set_multivar_in_file_gently(
++ the_repository, to_file, item->string,
++ NULL, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE))
+ error(_("failed to unset '%s' in '%s'"),
+ item->string, to_file);
+ }
@@ t/t2405-worktree-submodule.sh: test_expect_success 'unsetting core.worktree does
+ )
+'
+
++test_expect_success 'deinit overwrites active config (name != path)' '
++ test_when_finished "rm -rf repo-wt-config" &&
++ git clone "$base_path/origin/main" repo-wt-config &&
++ (
++ cd repo-wt-config &&
++ git submodule update --init &&
++ git mv sub sub_ &&
++ git commit -m "move submodule" &&
++ git config extensions.worktreeConfig true &&
++ git worktree add wt &&
++ cat .git/config >.git/expect-config &&
++
++ git submodule deinit sub_ &&
++ test_cmp .git/expect-config .git/config &&
++ echo "false" >expect &&
++ git config --worktree submodule.sub.active >actual &&
++ test_cmp expect actual &&
++
++ cd wt &&
++
++ git submodule deinit sub_ &&
++ test_cmp ../.git/expect-config ../.git/config &&
++ echo "false" >expect &&
++ git config --worktree submodule.sub.active >actual &&
++ test_cmp expect actual
++ )
++'
++
+test_expect_success 'do not copy gitdir in config.worktree' '
+ test_when_finished "rm -rf copy-wt-config" &&
+ git clone "$base_path/origin/main" copy-wt-config &&
3: b5d203d014 ! 4: 3e2c161298 submodule init: copy submodule.<name>.gitdir to config.worktree
@@ Commit message
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
+ ## Documentation/config/submodule.adoc ##
+@@ Documentation/config/submodule.adoc: 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
+
## builtin/submodule--helper.c ##
@@ builtin/submodule--helper.c: struct init_cb {
};
@@ builtin/submodule--helper.c: struct init_cb {
+ return false;
+ }
+
-+ super = absolute_pathdup(repo_get_git_dir(the_repository));
++ super = real_pathdup(repo_get_git_dir(the_repository), 1);
+ matches = starts_with(relative_path(submodule.buf, super, &tmp),
+ "modules/");
+
--
gitgitgadget
next prev parent reply other threads:[~2026-11-28 22:06 UTC|newest]
Thread overview: 24+ 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 ` [PATCH v2 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10 ` [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-24 19:44 ` Junio C Hamano
2026-11-27 5:04 ` Yoichi NAKAYAMA
2026-11-27 5:23 ` Junio C Hamano
2026-11-27 6:15 ` Yoichi NAKAYAMA
2026-11-21 15:10 ` [PATCH v2 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:14 ` Junio C Hamano
2026-11-27 12:35 ` Yoichi NAKAYAMA
2026-11-21 15:10 ` [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:44 ` Junio C Hamano
2026-11-28 0:14 ` Yoichi NAKAYAMA
2026-11-28 22:05 ` Yoichi NAKAYAMA via GitGitGadget [this message]
2026-11-28 22:05 ` [PATCH v3 1/4] submodule init: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05 ` [PATCH v3 2/4] submodule init: resolve symlinks in submodule.<name>.gitdir Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05 ` [PATCH v3 3/4] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05 ` [PATCH v3 4/4] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-29 8:32 ` Junio C Hamano
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.v3.git.1795903557.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