* [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
2026-11-12 12:03 [PATCH 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
@ 2026-11-12 12:03 ` Yoichi NAKAYAMA via GitGitGadget
2026-11-14 15:27 ` Yoichi NAKAYAMA
2026-11-12 12:03 ` [PATCH 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
` (3 subsequent siblings)
4 siblings, 1 reply; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-12 12:03 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
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.)
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
builtin/submodule--helper.c | 68 +++++++++++++---------
t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++
2 files changed, 61 insertions(+), 26 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 461b682d0d..56d7720e93 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,60 @@ 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 *modules_dir;
+ struct strbuf tmp = STRBUF_INIT;
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 */
- 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);
}
+ /* 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/");
+ 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 +574,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);
+ strbuf_release(&tmp);
+ 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
^ permalink raw reply related [flat|nested] 24+ messages in thread* Re: [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
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
0 siblings, 1 reply; 24+ messages in thread
From: Yoichi NAKAYAMA @ 2026-11-14 15:27 UTC (permalink / raw)
To: git
On Thu, Nov 12, 2026 at 9:03 PM Yoichi NAKAYAMA via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
>
> 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.)
>
> Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
> ---
> builtin/submodule--helper.c | 68 +++++++++++++---------
> t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++
> 2 files changed, 61 insertions(+), 26 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 461b682d0d..56d7720e93 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,60 @@ 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 *modules_dir;
> + struct strbuf tmp = STRBUF_INIT;
> 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 */
> - 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);
> }
I noticed that worktree_git_path() returns a pointer to a static buffer managed
by get_pathname(). I'll copy the result into tmp immediately, before calling
other functions that might overwrite the buffer:
modules_dir = worktree_git_path(wt, "modules/");
if (is_absolute_path(modules_dir))
relative_path(modules_dir, wt->path, &tmp);
else
strbuf_addstr(&tmp, modules_dir);
modules_dir = tmp.buf;
Thanks,
--
Yoichi NAKAYAMA
^ permalink raw reply [flat|nested] 24+ messages in thread* Re: [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
2026-11-14 15:27 ` Yoichi NAKAYAMA
@ 2026-11-14 15:47 ` Yoichi NAKAYAMA
0 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA @ 2026-11-14 15:47 UTC (permalink / raw)
To: git
On Sun, Nov 15, 2026 at 12:27 AM Yoichi NAKAYAMA
<yoichi.nakayama@gmail.com> wrote:
>
> On Thu, Nov 12, 2026 at 9:03 PM Yoichi NAKAYAMA via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
> >
> > From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
> >
> > 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.)
> >
> > Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
> > ---
> > builtin/submodule--helper.c | 68 +++++++++++++---------
> > t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++
> > 2 files changed, 61 insertions(+), 26 deletions(-)
> >
> > diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> > index 461b682d0d..56d7720e93 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,60 @@ 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 *modules_dir;
> > + struct strbuf tmp = STRBUF_INIT;
> > 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 */
> > - 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);
> > }
>
> I noticed that worktree_git_path() returns a pointer to a static buffer managed
> by get_pathname(). I'll copy the result into tmp immediately, before calling
> other functions that might overwrite the buffer:
>
> modules_dir = worktree_git_path(wt, "modules/");
> if (is_absolute_path(modules_dir))
> relative_path(modules_dir, wt->path, &tmp);
> else
> strbuf_addstr(&tmp, modules_dir);
> modules_dir = tmp.buf;
Correction: relative_path() does not always store the result in tmp.
The code should be:
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;
}
Thanks,
--
Yoichi NAKAYAMA
^ permalink raw reply [flat|nested] 24+ messages in thread
* [PATCH 2/3] submodule: store settings in worktree config when enabled
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-12 12:03 ` 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
` (2 subsequent siblings)
4 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-12 12:03 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Submodule URLs can differ between branches. For example, one branch
may use the upstream repository while another uses a fork for the same
submodule. When these branches are checked out in separate worktrees,
storing `submodule.<name>.url` in the shared repository config
prevents each worktree from using its own URL.
Store submodule settings such as `submodule.<name>.active` and
`submodule.<name>.url` in the worktree config when
`extensions.worktreeConfig` is enabled.
Settings shared between worktrees can remain in the repository
config. When such a setting exists there, 'git submodule deinit' sets
`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
worktree add'.
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
Documentation/config/submodule.adoc | 3 +-
builtin/submodule--helper.c | 43 ++++---
builtin/worktree.c | 23 ++++
t/t2405-worktree-submodule.sh | 129 +++++++++++++++++++++
t/t7425-submodule-gitdir-path-extension.sh | 49 ++++++++
5 files changed, 227 insertions(+), 20 deletions(-)
diff --git a/Documentation/config/submodule.adoc b/Documentation/config/submodule.adoc
index 4c424017b4..1e294147db 100644
--- a/Documentation/config/submodule.adoc
+++ b/Documentation/config/submodule.adoc
@@ -58,7 +58,8 @@ submodule.<name>.gitdir::
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.
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 56d7720e93..f249582ca6 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -497,7 +497,7 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,
return 0;
}
- if (repo_config_set_gently(the_repository, key, gitdir_path->buf)) {
+ if (repo_config_set_worktree_gently(the_repository, key, gitdir_path->buf)) {
free(key);
return -1;
}
@@ -609,15 +609,10 @@ static void init_submodule(const char *path, const char *prefix,
die(_("No url found for submodule path '%s' in .gitmodules"),
displaypath);
- /*
- * NEEDSWORK: In a multi-working-tree world, this needs to be
- * set in the per-worktree config.
- *
- * Set active flag for the submodule being initialized
- */
+ /* Set active flag for the submodule being initialized */
if (!is_submodule_active(the_repository, path)) {
strbuf_addf(&sb, "submodule.%s.active", sub->name);
- repo_config_set_gently(the_repository, sb.buf, "true");
+ repo_config_set_worktree_gently(the_repository, sb.buf, "true");
strbuf_reset(&sb);
}
@@ -643,7 +638,7 @@ static void init_submodule(const char *path, const char *prefix,
free(oldurl);
}
- if (repo_config_set_gently(the_repository, sb.buf, url))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, url))
die(_("Failed to register url for submodule path '%s'"),
displaypath);
if (!(flags & OPT_QUIET))
@@ -665,7 +660,7 @@ static void init_submodule(const char *path, const char *prefix,
upd = submodule_update_type_to_string(sub->update_strategy.type);
}
- if (repo_config_set_gently(the_repository, sb.buf, upd))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, upd))
die(_("Failed to register update mode for submodule path '%s'"), displaypath);
}
@@ -1491,7 +1486,7 @@ static void sync_submodule(const char *path, const char *prefix,
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name);
- if (repo_config_set_gently(the_repository, sb.buf, super_config_url))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, super_config_url))
die(_("failed to register url for submodule path '%s'"),
displaypath);
@@ -1670,17 +1665,31 @@ static void deinit_submodule(const char *path, const char *prefix,
/* remove the .git/config entries (unless the user already did it) */
if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
+ char *file = the_repository->repository_format_worktree_config ?
+ repo_git_path(the_repository, "config.worktree") :
+ repo_git_path(the_repository, "config");
char *sub_key = xstrfmt("submodule.%s", sub->name);
/*
* remove the whole section so we have a clean state when
* the user later decides to init this submodule again
*/
- repo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);
+ repo_config_rename_section_in_file(the_repository, file, sub_key, NULL);
if (!(flags & OPT_QUIET))
printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
sub->name, sub->url, displaypath);
+ free(file);
free(sub_key);
+
+ if (the_repository->repository_format_worktree_config) {
+ char *key = xstrfmt("submodule.%s.active", sub->name);
+
+ repo_config_clear(the_repository);
+ if (is_submodule_active(the_repository, sub->name))
+ repo_config_set_worktree_gently(the_repository,
+ key, "false");
+ free(key);
+ }
}
cleanup:
@@ -3580,7 +3589,7 @@ static void configure_added_submodule(struct add_data *add_data)
int matched = 0;
key = xstrfmt("submodule.%s.url", add_data->sm_name);
- repo_config_set_gently(the_repository, key, add_data->realrepo);
+ repo_config_set_worktree_gently(the_repository, key, add_data->realrepo);
free(key);
add_submod.git_cmd = 1;
@@ -3610,10 +3619,6 @@ static void configure_added_submodule(struct add_data *add_data)
if (run_command(&add_gitmodules))
die(_("Failed to register submodule '%s'"), add_data->sm_path);
- /*
- * NEEDSWORK: In a multi-working-tree world this needs to be
- * set in the per-worktree config.
- */
/*
* NEEDSWORK: In the longer run, we need to get rid of this
* pattern of querying "submodule.active" before calling
@@ -3627,7 +3632,7 @@ static void configure_added_submodule(struct add_data *add_data)
* current configured pathspec, set the submodule's active flag
*/
key = xstrfmt("submodule.%s.active", add_data->sm_name);
- repo_config_set_gently(the_repository, key, "true");
+ repo_config_set_worktree_gently(the_repository, key, "true");
free(key);
} else {
for (size_t i = 0; i < values->nr; i++) {
@@ -3639,7 +3644,7 @@ static void configure_added_submodule(struct add_data *add_data)
}
if (!matched) { /* no pattern matched -> force-enable */
key = xstrfmt("submodule.%s.active", add_data->sm_name);
- repo_config_set_gently(the_repository, key, "true");
+ repo_config_set_worktree_gently(the_repository, key, "true");
free(key);
}
}
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..ef03ef979a 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -363,6 +363,9 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
if (file_exists(from_file)) {
struct config_set cs = { { 0 } };
int bare;
+ struct string_list keys = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+ int i;
if (safe_create_leading_directories(the_repository, to_file) ||
copy_file(the_repository, to_file, from_file, 0666)) {
@@ -386,6 +389,26 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
error(_("failed to unset '%s' in '%s'"),
"core.worktree", to_file);
+ /* unset "submodule.*.gitdir" */
+ for (i = 0; i < cs.list.nr; i++) {
+ const char *var = cs.list.items[i].e->key;
+ const char *subsection, *key;
+ size_t subsection_len;
+
+ if (!parse_config_key(var, "submodule",
+ &subsection, &subsection_len, &key) &&
+ subsection && !strcmp(key, "gitdir"))
+ 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))
+ error(_("failed to unset '%s' in '%s'"),
+ item->string, to_file);
+ }
+ string_list_clear(&keys, 0);
+
git_configset_clear(&cs);
}
diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh
index f5c94d490b..d46d565ad6 100755
--- a/t/t2405-worktree-submodule.sh
+++ b/t/t2405-worktree-submodule.sh
@@ -91,4 +91,133 @@ test_expect_success 'unsetting core.worktree does not prevent running commands d
git -C main/.git/worktrees/checkout-recurse/modules/sub log
'
+test_expect_success 'submodule config in worktree config' '
+ test_when_finished "rm -rf wt-config" &&
+ git clone "$base_path/origin/main" wt-config &&
+ (
+ cd wt-config &&
+ git config extensions.worktreeConfig true &&
+ git worktree add wt &&
+ cat .git/config >.git/expect-config &&
+
+ git submodule update --init &&
+ test_cmp .git/expect-config .git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+
+ git submodule deinit sub &&
+ test_cmp .git/expect-config .git/config &&
+ test_must_be_empty .git/config.worktree &&
+
+ git submodule add ../sub sub2 &&
+ test_cmp .git/expect-config .git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub2.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ git config --file=.gitmodules submodule.sub2.url new_url &&
+ git submodule sync &&
+ test_cmp .git/expect-config .git/config &&
+ echo "new_url" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ cd wt &&
+
+ git submodule update --init &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+
+ git submodule deinit sub &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ test_must_be_empty ../.git/worktrees/wt/config.worktree &&
+
+ git submodule add ../sub sub2 &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub2.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ git config --file=.gitmodules submodule.sub2.url new_url &&
+ git submodule sync &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "new_url" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'deinit overwrites active config' '
+ 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 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 &&
+ (
+ cd copy-wt-config &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ git submodule update --init &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+ echo ".git/modules/sub" >expect &&
+ git config --worktree submodule.sub.gitdir >actual &&
+ test_cmp expect actual &&
+
+ git worktree add wt &&
+ cd wt &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config --worktree submodule.sub.gitdir
+ )
+'
+
test_done
diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh
index 02123b8468..3bae4c4ef1 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -78,6 +78,55 @@ test_expect_success 'gitdir config path is relative for both absolute and relati
)
'
+test_expect_success 'gitdir for the main worktree is stored in worktree config' '
+ test_when_finished "rm -rf relative-cfg-path-test-main" &&
+ git init -b main relative-cfg-path-test-main &&
+ (
+ cd relative-cfg-path-test-main &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ # Test with absolute URL
+ git submodule add "$TRASH_DIRECTORY/new-sub" sub-abs &&
+ git config --worktree submodule.sub-abs.gitdir >actual &&
+ echo ".git/modules/sub-abs" >expect &&
+ test_cmp expect actual &&
+
+ # Test with relative URL
+ git submodule add ../new-sub sub-rel &&
+ git config --worktree submodule.sub-rel.gitdir >actual &&
+ echo ".git/modules/sub-rel" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'gitdir for a linked worktree is stored in worktree config' '
+ test_when_finished "rm -rf relative-cfg-path-test-wt" &&
+ git init -b main relative-cfg-path-test-wt &&
+ (
+ cd relative-cfg-path-test-wt &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ git worktree add wt &&
+ cd wt &&
+
+ # Test with absolute URL
+ git submodule add "$TRASH_DIRECTORY/new-sub" sub-abs &&
+ git config --worktree submodule.sub-abs.gitdir >actual &&
+ echo "../.git/worktrees/wt/modules/sub-abs" >expect &&
+ test_cmp expect actual &&
+
+ # Test with relative URL
+ git submodule add ../../new-sub sub-rel &&
+ git config --worktree submodule.sub-rel.gitdir >actual &&
+ echo "../.git/worktrees/wt/modules/sub-rel" >expect &&
+ test_cmp expect actual
+ )
+'
+
test_expect_success 'clone from repo with both legacy and new-style submodules' '
git clone --recurse-submodules main cloned-non-extension &&
(
--
gitgitgadget
^ permalink raw reply related [flat|nested] 24+ messages in thread* [PATCH 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree
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-12 12:03 ` [PATCH 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
@ 2026-11-12 12:03 ` Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10 ` [PATCH v2 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
4 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-12 12:03 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
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.
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
builtin/submodule--helper.c | 100 ++++++++++++++++++---
t/t7425-submodule-gitdir-path-extension.sh | 65 +++++++++++++-
2 files changed, 151 insertions(+), 14 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index f249582ca6..f34a22238f 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -480,10 +480,41 @@ struct init_cb {
};
#define INIT_CB_INIT { 0 }
+static int validate_submodule_git_dir_with_worktree(const char *gitdir)
+{
+ struct strbuf tmp = STRBUF_INIT;
+ struct strbuf submodule = STRBUF_INIT;
+ char *super;
+ int ret = 1;
+
+ if (is_absolute_path(gitdir))
+ strbuf_addstr(&submodule, gitdir);
+ else if (!repo_worktree_path_append(the_repository, &submodule,
+ "%s", gitdir))
+ return -1;
+
+ if (strbuf_normalize_path(&submodule)) {
+ strbuf_release(&submodule);
+ return -1;
+ }
+
+ super = absolute_pathdup(repo_get_git_dir(the_repository));
+
+ if (starts_with(relative_path(submodule.buf, super, &tmp), "modules/"))
+ ret = 0;
+
+ strbuf_release(&submodule);
+ strbuf_release(&tmp);
+ free(super);
+ return ret;
+}
+
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;
+ const char *value = gitdir_path->buf;
+ char *value_to_free = NULL;
char *key;
if (validate_submodule_git_dir(gitdir_path->buf, submodule_name))
@@ -491,18 +522,55 @@ 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)) {
+ if (validate_submodule_git_dir_with_worktree(tmp) ||
+ validate_submodule_git_dir(tmp, submodule_name))
+ free(tmp);
+ else
+ value = value_to_free = tmp;
+ }
+ } else {
+ /* 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)) {
free(key);
+ free(value_to_free);
return -1;
}
free(key);
+ free(value_to_free);
return 0;
}
@@ -526,7 +594,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 +603,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 +612,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 +623,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 +632,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 +650,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 3bae4c4ef1..36aac0f578 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -328,7 +328,47 @@ 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 &&
+ 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 &&
+ 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 &&
(
@@ -343,6 +383,29 @@ 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 &&
+ 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 &&
+ 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
^ permalink raw reply related [flat|nested] 24+ messages in thread* [PATCH v2 0/3] submodule: support worktree-specific configuration
2026-11-12 12:03 [PATCH 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
` (2 preceding siblings ...)
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
2026-11-21 15:10 ` [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
` (2 more replies)
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
4 siblings, 3 replies; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-21 15:10 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA
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
^ permalink raw reply [flat|nested] 24+ messages in thread* [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
2026-11-21 15:10 ` [PATCH v2 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
@ 2026-11-21 15:10 ` Yoichi NAKAYAMA via GitGitGadget
2026-11-24 19:44 ` Junio C Hamano
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
2 siblings, 1 reply; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-21 15:10 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
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.)
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
builtin/submodule--helper.c | 71 ++++++++++++++--------
t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++
2 files changed, 64 insertions(+), 26 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 461b682d0d..c3b3a2865f 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,63 @@ 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 *modules_dir;
+ struct strbuf tmp = STRBUF_INIT;
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 */
- 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/");
+ 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 +577,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);
+ strbuf_release(&tmp);
+ 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
^ permalink raw reply related [flat|nested] 24+ messages in thread* Re: [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
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
0 siblings, 1 reply; 24+ messages in thread
From: Junio C Hamano @ 2026-11-24 19:44 UTC (permalink / raw)
To: Yoichi NAKAYAMA via GitGitGadget; +Cc: git, Yoichi NAKAYAMA
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
>
> 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.)
Perhaps state the status quo first for future readers, that
repo_git_path_append() produces an absolute path for linked worktrees
unless relative paths are enabled. This change applies to the main
worktree as well whenever worktree_git_path() returns an absolute path
(such as when GIT_DIR is absolute), right? That may be worth noting
here, too.
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 461b682d0d..c3b3a2865f 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -507,50 +508,63 @@
> ...
> + 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;
> }
Copying 'modules_dir' in the 'else' branch avoids overwriting the static
buffer from worktree_git_path(), and converting the early returns in the
later cases 2.X to 'goto cleanup' nicely plugs pre-existing strbuf
leaks.
However, doesn't the 'if (is_absolute_path(modules_dir))' branch still
have the same static-buffer hazard? As documented above relative_path()
in path.c:
The strbuf may or may not be used, so do not assume it contains the
returned path.
When 'wt->path' is a directory prefix of 'modules_dir' (for instance, in
the main worktree with GIT_DIR=/abs/repo/.git GIT_WORK_TREE=/abs/repo),
relative_path() advances 'in' (initialized from modules_dir we gave) and
returns a pointer directly into worktree_git_path()'s 4-slot ring buffer
without touching 'tmp'. Once patch 3 calls worktree_git_path(wt,
"config.worktree") inside validate_and_set_submodule_gitdir(), four
consecutive failures across Case 1, 2.1, 2.2, and 2.3 (e.g., if
config.worktree is locked or unwritable) will wrap that 4-slot buffer
and overwrite 'modules_dir' while create_default_gitdir_config() is
still using it, no?
Unconditionally owning the copy in 'modules_dir' (or 'tmp') closes that
hole cleanly:
const char *raw = worktree_git_path(wt, "modules/");
char *modules_dir = xstrdup(is_absolute_path(raw) ?
relative_path(raw, wt->path, &tmp) : raw);
Of course, if we go that route, freeing 'modules_dir' under 'cleanup:'
would become necessary.
Thanks.
^ permalink raw reply [flat|nested] 24+ messages in thread* Re: [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
2026-11-24 19:44 ` Junio C Hamano
@ 2026-11-27 5:04 ` Yoichi NAKAYAMA
2026-11-27 5:23 ` Junio C Hamano
0 siblings, 1 reply; 24+ messages in thread
From: Yoichi NAKAYAMA @ 2026-11-27 5:04 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Yoichi NAKAYAMA via GitGitGadget, git
On Wed, Nov 25, 2026 at 4:44 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
> >
> > 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.)
>
> Perhaps state the status quo first for future readers, that
> repo_git_path_append() produces an absolute path for linked worktrees
> unless relative paths are enabled. This change applies to the main
> worktree as well whenever worktree_git_path() returns an absolute path
> (such as when GIT_DIR is absolute), right? That may be worth noting
> here, too.
I'll update the commit message body as follows:
Currently, repo_git_path_append() can produce an absolute path
even when worktree.useRelativePaths is enabled, such as 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. (This benefit also requires worktree.useRelativePaths
to be set to true.)
> > diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> > index 461b682d0d..c3b3a2865f 100644
> > --- a/builtin/submodule--helper.c
> > +++ b/builtin/submodule--helper.c
> > @@ -507,50 +508,63 @@
> > ...
> > + 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;
> > }
>
> Copying 'modules_dir' in the 'else' branch avoids overwriting the static
> buffer from worktree_git_path(), and converting the early returns in the
> later cases 2.X to 'goto cleanup' nicely plugs pre-existing strbuf
> leaks.
>
> However, doesn't the 'if (is_absolute_path(modules_dir))' branch still
> have the same static-buffer hazard? As documented above relative_path()
> in path.c:
>
> The strbuf may or may not be used, so do not assume it contains the
> returned path.
>
> When 'wt->path' is a directory prefix of 'modules_dir' (for instance, in
> the main worktree with GIT_DIR=/abs/repo/.git GIT_WORK_TREE=/abs/repo),
> relative_path() advances 'in' (initialized from modules_dir we gave) and
> returns a pointer directly into worktree_git_path()'s 4-slot ring buffer
> without touching 'tmp'. Once patch 3 calls worktree_git_path(wt,
> "config.worktree") inside validate_and_set_submodule_gitdir(), four
> consecutive failures across Case 1, 2.1, 2.2, and 2.3 (e.g., if
> config.worktree is locked or unwritable) will wrap that 4-slot buffer
> and overwrite 'modules_dir' while create_default_gitdir_config() is
> still using it, no?
>
> Unconditionally owning the copy in 'modules_dir' (or 'tmp') closes that
> hole cleanly:
>
> const char *raw = worktree_git_path(wt, "modules/");
> char *modules_dir = xstrdup(is_absolute_path(raw) ?
> relative_path(raw, wt->path, &tmp) : raw);
>
> Of course, if we go that route, freeing 'modules_dir' under 'cleanup:'
> would become necessary.
You're right. I hadn't accounted for cases where `relative_path` doesn't
use `strbuf`. I'll fix it so that it performs a copy at the beginning.
Thanks,
--
Yoichi NAKAYAMA
^ permalink raw reply [flat|nested] 24+ messages in thread* Re: [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
2026-11-27 5:04 ` Yoichi NAKAYAMA
@ 2026-11-27 5:23 ` Junio C Hamano
2026-11-27 6:15 ` Yoichi NAKAYAMA
0 siblings, 1 reply; 24+ messages in thread
From: Junio C Hamano @ 2026-11-27 5:23 UTC (permalink / raw)
To: Yoichi NAKAYAMA; +Cc: Yoichi NAKAYAMA via GitGitGadget, git
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> writes:
> I'll update the commit message body as follows:
>
> Currently, repo_git_path_append() can produce an absolute path
> even when worktree.useRelativePaths is enabled, such as 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. (This benefit also requires worktree.useRelativePaths
> to be set to true.)
"Always store"?
I am not a heavy user of submodule & secondary worktree user, but if
worktree.useRelativePaths configuration exists, should relativifying the
gitdir still be done unconditionally, or should it be done only when the
configuration variable is set to true?
Thanks.
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees
2026-11-27 5:23 ` Junio C Hamano
@ 2026-11-27 6:15 ` Yoichi NAKAYAMA
0 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA @ 2026-11-27 6:15 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Yoichi NAKAYAMA via GitGitGadget, git
On Fri, Nov 27, 2026 at 2:23 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> writes:
>
> > I'll update the commit message body as follows:
> >
> > Currently, repo_git_path_append() can produce an absolute path
> > even when worktree.useRelativePaths is enabled, such as 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. (This benefit also requires worktree.useRelativePaths
> > to be set to true.)
>
> "Always store"?
>
> I am not a heavy user of submodule & secondary worktree user, but if
> worktree.useRelativePaths configuration exists, should relativifying the
> gitdir still be done unconditionally, or should it be done only when the
> configuration variable is set to true?
My understanding is that the expected behavior-regarding the
interlinking between a submodule and its management directory,
as well as the `submodule.<name>.gitdir` configuration-is for these
to be recorded using relative paths, regardless of specific configuration
settings. This is, in fact, the current behavior for submodules belonging
to the main worktree.
With this change, `submodule.<name>.gitdir` will now always be
stored as a relative path, even for submodules that belong to a worktree.
The `worktree.useRelativePaths` configuration does govern the links
between a worktree’s own `.git` file and its administrative directory (i.e.
`$GIT_DIR/worktrees/{id}`).
This configuration does not directly affect the behavior of submodule
links contained within a worktree.
I mentioned `worktree.useRelativePaths` because, even if submodule
links are set to use relative paths, Git’s behavior after a directory move
would still become problematic if the links for the worktree itself were
absolute paths (the default behavior, where `worktree.useRelativePaths=false`).
Thanks,
--
Yoichi NAKAYAMA
^ permalink raw reply [flat|nested] 24+ messages in thread
* [PATCH v2 2/3] submodule: store settings in worktree config when enabled
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-21 15:10 ` Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:14 ` Junio C Hamano
2026-11-21 15:10 ` [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2 siblings, 1 reply; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-21 15:10 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Submodule URLs can differ between branches. For example, one branch
may use the upstream repository while another uses a fork for the same
submodule. When these branches are checked out in separate worktrees,
storing `submodule.<name>.url` in the shared repository config
prevents each worktree from using its own URL.
Store submodule settings such as `submodule.<name>.active` and
`submodule.<name>.url` in the worktree config when
`extensions.worktreeConfig` is enabled.
Settings shared between worktrees can remain in the repository
config. When such a setting exists there, 'git submodule deinit' sets
`submodule.<name>.active` to false in the worktree config to
unregister the submodule.
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>
---
Documentation/config/submodule.adoc | 3 +-
builtin/submodule--helper.c | 43 ++++---
builtin/worktree.c | 23 ++++
t/t2405-worktree-submodule.sh | 129 +++++++++++++++++++++
t/t7425-submodule-gitdir-path-extension.sh | 49 ++++++++
5 files changed, 227 insertions(+), 20 deletions(-)
diff --git a/Documentation/config/submodule.adoc b/Documentation/config/submodule.adoc
index 4c424017b4..1e294147db 100644
--- a/Documentation/config/submodule.adoc
+++ b/Documentation/config/submodule.adoc
@@ -58,7 +58,8 @@ submodule.<name>.gitdir::
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.
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 c3b3a2865f..2cd6f58cac 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -497,7 +497,7 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,
return 0;
}
- if (repo_config_set_gently(the_repository, key, gitdir_path->buf)) {
+ if (repo_config_set_worktree_gently(the_repository, key, gitdir_path->buf)) {
free(key);
return -1;
}
@@ -612,15 +612,10 @@ static void init_submodule(const char *path, const char *prefix,
die(_("No url found for submodule path '%s' in .gitmodules"),
displaypath);
- /*
- * NEEDSWORK: In a multi-working-tree world, this needs to be
- * set in the per-worktree config.
- *
- * Set active flag for the submodule being initialized
- */
+ /* Set active flag for the submodule being initialized */
if (!is_submodule_active(the_repository, path)) {
strbuf_addf(&sb, "submodule.%s.active", sub->name);
- repo_config_set_gently(the_repository, sb.buf, "true");
+ repo_config_set_worktree_gently(the_repository, sb.buf, "true");
strbuf_reset(&sb);
}
@@ -646,7 +641,7 @@ static void init_submodule(const char *path, const char *prefix,
free(oldurl);
}
- if (repo_config_set_gently(the_repository, sb.buf, url))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, url))
die(_("Failed to register url for submodule path '%s'"),
displaypath);
if (!(flags & OPT_QUIET))
@@ -668,7 +663,7 @@ static void init_submodule(const char *path, const char *prefix,
upd = submodule_update_type_to_string(sub->update_strategy.type);
}
- if (repo_config_set_gently(the_repository, sb.buf, upd))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, upd))
die(_("Failed to register update mode for submodule path '%s'"), displaypath);
}
@@ -1494,7 +1489,7 @@ static void sync_submodule(const char *path, const char *prefix,
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name);
- if (repo_config_set_gently(the_repository, sb.buf, super_config_url))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, super_config_url))
die(_("failed to register url for submodule path '%s'"),
displaypath);
@@ -1673,17 +1668,31 @@ static void deinit_submodule(const char *path, const char *prefix,
/* remove the .git/config entries (unless the user already did it) */
if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
+ char *file = the_repository->repository_format_worktree_config ?
+ repo_git_path(the_repository, "config.worktree") :
+ repo_git_path(the_repository, "config");
char *sub_key = xstrfmt("submodule.%s", sub->name);
/*
* remove the whole section so we have a clean state when
* the user later decides to init this submodule again
*/
- repo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);
+ repo_config_rename_section_in_file(the_repository, file, sub_key, NULL);
if (!(flags & OPT_QUIET))
printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
sub->name, sub->url, displaypath);
+ free(file);
free(sub_key);
+
+ if (the_repository->repository_format_worktree_config) {
+ char *key = xstrfmt("submodule.%s.active", sub->name);
+
+ repo_config_clear(the_repository);
+ if (is_submodule_active(the_repository, sub->name))
+ repo_config_set_worktree_gently(the_repository,
+ key, "false");
+ free(key);
+ }
}
cleanup:
@@ -3583,7 +3592,7 @@ static void configure_added_submodule(struct add_data *add_data)
int matched = 0;
key = xstrfmt("submodule.%s.url", add_data->sm_name);
- repo_config_set_gently(the_repository, key, add_data->realrepo);
+ repo_config_set_worktree_gently(the_repository, key, add_data->realrepo);
free(key);
add_submod.git_cmd = 1;
@@ -3613,10 +3622,6 @@ static void configure_added_submodule(struct add_data *add_data)
if (run_command(&add_gitmodules))
die(_("Failed to register submodule '%s'"), add_data->sm_path);
- /*
- * NEEDSWORK: In a multi-working-tree world this needs to be
- * set in the per-worktree config.
- */
/*
* NEEDSWORK: In the longer run, we need to get rid of this
* pattern of querying "submodule.active" before calling
@@ -3630,7 +3635,7 @@ static void configure_added_submodule(struct add_data *add_data)
* current configured pathspec, set the submodule's active flag
*/
key = xstrfmt("submodule.%s.active", add_data->sm_name);
- repo_config_set_gently(the_repository, key, "true");
+ repo_config_set_worktree_gently(the_repository, key, "true");
free(key);
} else {
for (size_t i = 0; i < values->nr; i++) {
@@ -3642,7 +3647,7 @@ static void configure_added_submodule(struct add_data *add_data)
}
if (!matched) { /* no pattern matched -> force-enable */
key = xstrfmt("submodule.%s.active", add_data->sm_name);
- repo_config_set_gently(the_repository, key, "true");
+ repo_config_set_worktree_gently(the_repository, key, "true");
free(key);
}
}
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..ef03ef979a 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -363,6 +363,9 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
if (file_exists(from_file)) {
struct config_set cs = { { 0 } };
int bare;
+ struct string_list keys = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+ int i;
if (safe_create_leading_directories(the_repository, to_file) ||
copy_file(the_repository, to_file, from_file, 0666)) {
@@ -386,6 +389,26 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
error(_("failed to unset '%s' in '%s'"),
"core.worktree", to_file);
+ /* unset "submodule.*.gitdir" */
+ for (i = 0; i < cs.list.nr; i++) {
+ const char *var = cs.list.items[i].e->key;
+ const char *subsection, *key;
+ size_t subsection_len;
+
+ if (!parse_config_key(var, "submodule",
+ &subsection, &subsection_len, &key) &&
+ subsection && !strcmp(key, "gitdir"))
+ 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))
+ error(_("failed to unset '%s' in '%s'"),
+ item->string, to_file);
+ }
+ string_list_clear(&keys, 0);
+
git_configset_clear(&cs);
}
diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh
index f5c94d490b..d46d565ad6 100755
--- a/t/t2405-worktree-submodule.sh
+++ b/t/t2405-worktree-submodule.sh
@@ -91,4 +91,133 @@ test_expect_success 'unsetting core.worktree does not prevent running commands d
git -C main/.git/worktrees/checkout-recurse/modules/sub log
'
+test_expect_success 'submodule config in worktree config' '
+ test_when_finished "rm -rf wt-config" &&
+ git clone "$base_path/origin/main" wt-config &&
+ (
+ cd wt-config &&
+ git config extensions.worktreeConfig true &&
+ git worktree add wt &&
+ cat .git/config >.git/expect-config &&
+
+ git submodule update --init &&
+ test_cmp .git/expect-config .git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+
+ git submodule deinit sub &&
+ test_cmp .git/expect-config .git/config &&
+ test_must_be_empty .git/config.worktree &&
+
+ git submodule add ../sub sub2 &&
+ test_cmp .git/expect-config .git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub2.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ git config --file=.gitmodules submodule.sub2.url new_url &&
+ git submodule sync &&
+ test_cmp .git/expect-config .git/config &&
+ echo "new_url" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ cd wt &&
+
+ git submodule update --init &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+
+ git submodule deinit sub &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ test_must_be_empty ../.git/worktrees/wt/config.worktree &&
+
+ git submodule add ../sub sub2 &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub2.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ git config --file=.gitmodules submodule.sub2.url new_url &&
+ git submodule sync &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "new_url" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'deinit overwrites active config' '
+ 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 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 &&
+ (
+ cd copy-wt-config &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ git submodule update --init &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+ echo ".git/modules/sub" >expect &&
+ git config --worktree submodule.sub.gitdir >actual &&
+ test_cmp expect actual &&
+
+ git worktree add wt &&
+ cd wt &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config --worktree submodule.sub.gitdir
+ )
+'
+
test_done
diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh
index 02123b8468..3bae4c4ef1 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -78,6 +78,55 @@ test_expect_success 'gitdir config path is relative for both absolute and relati
)
'
+test_expect_success 'gitdir for the main worktree is stored in worktree config' '
+ test_when_finished "rm -rf relative-cfg-path-test-main" &&
+ git init -b main relative-cfg-path-test-main &&
+ (
+ cd relative-cfg-path-test-main &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ # Test with absolute URL
+ git submodule add "$TRASH_DIRECTORY/new-sub" sub-abs &&
+ git config --worktree submodule.sub-abs.gitdir >actual &&
+ echo ".git/modules/sub-abs" >expect &&
+ test_cmp expect actual &&
+
+ # Test with relative URL
+ git submodule add ../new-sub sub-rel &&
+ git config --worktree submodule.sub-rel.gitdir >actual &&
+ echo ".git/modules/sub-rel" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'gitdir for a linked worktree is stored in worktree config' '
+ test_when_finished "rm -rf relative-cfg-path-test-wt" &&
+ git init -b main relative-cfg-path-test-wt &&
+ (
+ cd relative-cfg-path-test-wt &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ git worktree add wt &&
+ cd wt &&
+
+ # Test with absolute URL
+ git submodule add "$TRASH_DIRECTORY/new-sub" sub-abs &&
+ git config --worktree submodule.sub-abs.gitdir >actual &&
+ echo "../.git/worktrees/wt/modules/sub-abs" >expect &&
+ test_cmp expect actual &&
+
+ # Test with relative URL
+ git submodule add ../../new-sub sub-rel &&
+ git config --worktree submodule.sub-rel.gitdir >actual &&
+ echo "../.git/worktrees/wt/modules/sub-rel" >expect &&
+ test_cmp expect actual
+ )
+'
+
test_expect_success 'clone from repo with both legacy and new-style submodules' '
git clone --recurse-submodules main cloned-non-extension &&
(
--
gitgitgadget
^ permalink raw reply related [flat|nested] 24+ messages in thread* Re: [PATCH v2 2/3] submodule: store settings in worktree config when enabled
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
0 siblings, 1 reply; 24+ messages in thread
From: Junio C Hamano @ 2026-11-24 20:14 UTC (permalink / raw)
To: Yoichi NAKAYAMA via GitGitGadget; +Cc: git, Yoichi NAKAYAMA
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
> + /* Set active flag for the submodule being initialized */
> if (!is_submodule_active(the_repository, path)) {
> strbuf_addf(&sb, "submodule.%s.active", sub->name);
> - repo_config_set_gently(the_repository, sb.buf, "true");
> + repo_config_set_worktree_gently(the_repository, sb.buf, "true");
> strbuf_reset(&sb);
> }
Mental note. The is_submodule_active() function is designed to take
the path of the submodule, and not the name of the submodule. That
is why we use path and not 'sub->name' here.
A submodule's name may not necessarily match its path (e.g., you may
add a submodule 'sub' when it was at the path 'sub' and its name and
path may be identical, but then you can move 'sub' to 'mod/sub' and
now its path is 'mod/sub' while the name is still 'sub' to preserve
its identity across such a rename in the superproject). We need to
be careful which one is used by which API functions.
> @@ -1673,17 +1668,31 @@ static void deinit_submodule(const char *path, const char *prefix,
> ...
> + if (the_repository->repository_format_worktree_config) {
> + char *key = xstrfmt("submodule.%s.active", sub->name);
> +
> + repo_config_clear(the_repository);
> + if (is_submodule_active(the_repository, sub->name))
> + repo_config_set_worktree_gently(the_repository,
> + key, "false");
This call to is_submodule_active() using 'sub->name' would break when
the name and path of the submodule are different.
It would also be worth covering a submodule whose name differs from its
path in t/t2405-worktree-submodule.sh.
> @@ -386,6 +389,26 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
> error(_("failed to unset '%s' in '%s'"),
> "core.worktree", to_file);
>
> + /* unset "submodule.*.gitdir" */
> + for (i = 0; i < cs.list.nr; i++) {
> + const char *var = cs.list.items[i].e->key;
> + const char *subsection, *key;
> + size_t subsection_len;
> +
> + if (!parse_config_key(var, "submodule",
> + &subsection, &subsection_len, &key) &&
> + subsection && !strcmp(key, "gitdir"))
> + 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))
> + error(_("failed to unset '%s' in '%s'"),
> + item->string, to_file);
> + }
This code is trying to copy the worktree config, but this removal would
not work if the file happens to have duplicate 'submodule.<name>.gitdir'
entries. Perhaps use repo_config_set_multivar_in_file_gently() with
CONFIG_FLAGS_MULTI_REPLACE as you want to unset all of these?
Do we need to say something about how various 'submodule.<name>.*'
entries this patch touches interact with 'extensions.worktreeConfig' in
the documentation, perhaps in Documentation/config/submodule.adoc or
somewhere around there?
Thanks.
^ permalink raw reply [flat|nested] 24+ messages in thread* Re: [PATCH v2 2/3] submodule: store settings in worktree config when enabled
2026-11-24 20:14 ` Junio C Hamano
@ 2026-11-27 12:35 ` Yoichi NAKAYAMA
0 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA @ 2026-11-27 12:35 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Yoichi NAKAYAMA via GitGitGadget, git
On Wed, Nov 25, 2026 at 5:14 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > + /* Set active flag for the submodule being initialized */
> > if (!is_submodule_active(the_repository, path)) {
> > strbuf_addf(&sb, "submodule.%s.active", sub->name);
> > - repo_config_set_gently(the_repository, sb.buf, "true");
> > + repo_config_set_worktree_gently(the_repository, sb.buf, "true");
> > strbuf_reset(&sb);
> > }
>
> Mental note. The is_submodule_active() function is designed to take
> the path of the submodule, and not the name of the submodule. That
> is why we use path and not 'sub->name' here.
>
> A submodule's name may not necessarily match its path (e.g., you may
> add a submodule 'sub' when it was at the path 'sub' and its name and
> path may be identical, but then you can move 'sub' to 'mod/sub' and
> now its path is 'mod/sub' while the name is still 'sub' to preserve
> its identity across such a rename in the superproject). We need to
> be careful which one is used by which API functions.
>
> > @@ -1673,17 +1668,31 @@ static void deinit_submodule(const char *path, const char *prefix,
> > ...
> > + if (the_repository->repository_format_worktree_config) {
> > + char *key = xstrfmt("submodule.%s.active", sub->name);
> > +
> > + repo_config_clear(the_repository);
> > + if (is_submodule_active(the_repository, sub->name))
> > + repo_config_set_worktree_gently(the_repository,
> > + key, "false");
>
> This call to is_submodule_active() using 'sub->name' would break when
> the name and path of the submodule are different.
>
> It would also be worth covering a submodule whose name differs from its
> path in t/t2405-worktree-submodule.sh.
I've confirmed the problem. I'll fix it and add a test in
t/t2405-worktree-submodule.sh.
> > @@ -386,6 +389,26 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
> > error(_("failed to unset '%s' in '%s'"),
> > "core.worktree", to_file);
> >
> > + /* unset "submodule.*.gitdir" */
> > + for (i = 0; i < cs.list.nr; i++) {
> > + const char *var = cs.list.items[i].e->key;
> > + const char *subsection, *key;
> > + size_t subsection_len;
> > +
> > + if (!parse_config_key(var, "submodule",
> > + &subsection, &subsection_len, &key) &&
> > + subsection && !strcmp(key, "gitdir"))
> > + 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))
> > + error(_("failed to unset '%s' in '%s'"),
> > + item->string, to_file);
> > + }
>
> This code is trying to copy the worktree config, but this removal would
> not work if the file happens to have duplicate 'submodule.<name>.gitdir'
> entries. Perhaps use repo_config_set_multivar_in_file_gently() with
> CONFIG_FLAGS_MULTI_REPLACE as you want to unset all of these?
I've confirmed that duplicate 'submodule.<name>.gitdir' causes
warning: submodule.sub.gitdir has multiple values
error: failed to unset 'submodule.sub.gitdir' in
'.git/worktrees/foo/config.worktree'
and it is fixed by using repo_config_set_multivar_in_file_gently().
> Do we need to say something about how various 'submodule.<name>.*'
> entries this patch touches interact with 'extensions.worktreeConfig' in
> the documentation, perhaps in Documentation/config/submodule.adoc or
> somewhere around there?
I will update `Documentation/config/submodule.adoc` to note that
`submodule.<name>.{url,update,active,gitdir}` settings are written
to the worktree config when running `git submodule {init,sync}`.
I intend to omit a detailed explanation regarding the behavior in cases
where these items already exist in the repository settings.
However, regarding the inheritance of `submodule.<name>.gitdir`,
I will consider adding further explanation in a subsequent commit.
Thanks,
--
Yoichi NAKAYAMA
^ permalink raw reply [flat|nested] 24+ messages in thread
* [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree
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-21 15:10 ` [PATCH v2 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
@ 2026-11-21 15:10 ` Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:44 ` Junio C Hamano
2 siblings, 1 reply; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-21 15:10 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@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 <yoichi.nakayama@gmail.com>
---
builtin/submodule--helper.c | 113 ++++++++++++++++++---
t/t7425-submodule-gitdir-path-extension.sh | 69 ++++++++++++-
2 files changed, 168 insertions(+), 14 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 2cd6f58cac..9eba2e3aeb 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 = absolute_pathdup(repo_get_git_dir(the_repository));
+ 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;
}
@@ -529,7 +610,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 */
@@ -537,7 +619,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 */
@@ -545,7 +628,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 */
@@ -555,7 +639,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);
@@ -563,7 +648,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;
}
@@ -580,7 +666,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 3bae4c4ef1..ca55b78c07 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -328,7 +328,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 &&
(
@@ -343,6 +385,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
^ permalink raw reply related [flat|nested] 24+ messages in thread* Re: [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree
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
0 siblings, 1 reply; 24+ messages in thread
From: Junio C Hamano @ 2026-11-24 20:44 UTC (permalink / raw)
To: Yoichi NAKAYAMA via GitGitGadget; +Cc: git, Yoichi NAKAYAMA
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 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.
This makes the transition to extensions.worktreeConfig much safer for
existing repositories, and the new warning plus test cases covering both
main-worktree and linked-worktree migration look solid.
> +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 = absolute_pathdup(repo_get_git_dir(the_repository));
> + matches = starts_with(relative_path(submodule.buf, super, &tmp),
> + "modules/");
Isn't there a subtle symlink / normalization mismatch here?
repo_worktree_path_append() uses 'the_repository->worktree', which is
canonicalized via real_pathdup() during repository setup. In contrast,
absolute_pathdup(repo_get_git_dir(the_repository)) uses 'PWD' (if
matching 'getcwd()') without resolving symlinks when the gitdir is
relative, and copies an explicit absolute GIT_DIR verbatim without
resolving symlinks or normalizing '.' / '..'.
While 'git submodule' normally runs cd_to_toplevel() (resetting PWD)
when GIT_DIR is unset, running 'GIT_DIR=.git git submodule init' from a
symlinked path to the worktree (or 'GIT_DIR=.git/../.git', or invoking
'git submodule--helper init' directly via a symlink) causes 'super' and
'submodule' to diverge, triggering a false mismatch warning and
generating a new gitdir instead of copying the existing one, no?
Using real_pathdup(repo_get_git_dir(the_repository), 1) for 'super' (and
strbuf_realpath_forgiving() for 'submodule' in case an existing absolute
'gitdir' config contains symlinks) keeps both sides canonicalized, which
might make it safer (though the resulting path may become uglier, as it
often is the reason why people use symlinks that are more comfortable to
the eyes and "cd" to a directory through them).
^ permalink raw reply [flat|nested] 24+ messages in thread* Re: [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree
2026-11-24 20:44 ` Junio C Hamano
@ 2026-11-28 0:14 ` Yoichi NAKAYAMA
0 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA @ 2026-11-28 0:14 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Yoichi NAKAYAMA via GitGitGadget, git
On Wed, Nov 25, 2026 at 5:44 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > +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 = absolute_pathdup(repo_get_git_dir(the_repository));
> > + matches = starts_with(relative_path(submodule.buf, super, &tmp),
> > + "modules/");
>
> Isn't there a subtle symlink / normalization mismatch here?
>
> repo_worktree_path_append() uses 'the_repository->worktree', which is
> canonicalized via real_pathdup() during repository setup. In contrast,
> absolute_pathdup(repo_get_git_dir(the_repository)) uses 'PWD' (if
> matching 'getcwd()') without resolving symlinks when the gitdir is
> relative, and copies an explicit absolute GIT_DIR verbatim without
> resolving symlinks or normalizing '.' / '..'.
>
> While 'git submodule' normally runs cd_to_toplevel() (resetting PWD)
> when GIT_DIR is unset, running 'GIT_DIR=.git git submodule init' from a
> symlinked path to the worktree (or 'GIT_DIR=.git/../.git', or invoking
> 'git submodule--helper init' directly via a symlink) causes 'super' and
> 'submodule' to diverge, triggering a false mismatch warning and
> generating a new gitdir instead of copying the existing one, no?
>
> Using real_pathdup(repo_get_git_dir(the_repository), 1) for 'super' (and
> strbuf_realpath_forgiving() for 'submodule' in case an existing absolute
> 'gitdir' config contains symlinks) keeps both sides canonicalized, which
> might make it safer (though the resulting path may become uglier, as it
> often is the reason why people use symlinks that are more comfortable to
> the eyes and "cd" to a directory through them).
I have confirmed that issues arise when a path containing a symbolic link is
specified for `GIT_DIR`. Furthermore, I have discovered that symbolic links
are not being resolved within the value of `submodule.<name>.gitdir`. I will
address this by adding a new commit, and I will also update
`submodule_git_dir_matches_current_worktree()` to use
`real_pathdup(repo_get_git_dir(the_repository), 1)`.
Thanks,
--
Yoichi NAKAYAMA
^ permalink raw reply [flat|nested] 24+ messages in thread
* [PATCH v3 0/4] submodule: support worktree-specific configuration
2026-11-12 12:03 [PATCH 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
` (3 preceding siblings ...)
2026-11-21 15:10 ` [PATCH v2 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
@ 2026-11-28 22:05 ` Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05 ` [PATCH v3 1/4] submodule init: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
` (3 more replies)
4 siblings, 4 replies; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-28 22:05 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA
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
^ permalink raw reply [flat|nested] 24+ messages in thread* [PATCH v3 1/4] submodule init: make submodule.<name>.gitdir relative for worktrees
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
@ 2026-11-28 22:05 ` 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
` (2 subsequent siblings)
3 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-28 22:05 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@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 <yoichi.nakayama@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
^ permalink raw reply related [flat|nested] 24+ messages in thread* [PATCH v3 2/4] submodule init: resolve symlinks in submodule.<name>.gitdir
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
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 ` 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
3 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-28 22:05 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
When GIT_DIR is specified with a path containing symbolic links,
'git submodule init' generates a non-canonical path for
`submodule.<name>.gitdir`. Use strbuf_realpath_forgiving to
resolve the symlinks.
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
builtin/submodule--helper.c | 15 +++++++--------
t/t7425-submodule-gitdir-path-extension.sh | 14 ++++++++++++--
2 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index c20540497b..9faa532854 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -509,20 +509,18 @@ 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 raw_modules_dir = STRBUF_INIT;
struct strbuf tmp = STRBUF_INIT;
- char *modules_dir;
+ const 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);
+ strbuf_realpath_forgiving(&raw_modules_dir, worktree_git_path(wt, "modules"), 1);
+ strbuf_addch(&raw_modules_dir, '/');
+ modules_dir = relative_path(raw_modules_dir.buf, wt->path, &tmp);
/* Case 1: try the plain module name */
strbuf_addstr(&gitdir_path, modules_dir);
@@ -590,7 +588,8 @@ static void create_default_gitdir_config(const char *submodule_name)
cleanup:
strbuf_release(&gitdir_path);
- free(modules_dir);
+ strbuf_release(&tmp);
+ strbuf_release(&raw_modules_dir);
free_worktree(wt);
}
diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh
index 02123b8468..65a61edb09 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -57,7 +57,7 @@ test_expect_success 'manual add and verify legacy submodule gitdir config' '
'
test_expect_success 'gitdir config path is relative for both absolute and relative urls' '
- test_when_finished "rm -rf relative-cfg-path-test" &&
+ test_when_finished "rm -rf relative-cfg-path-test relative-cfg-path-test-link" &&
git init -b main relative-cfg-path-test &&
(
cd relative-cfg-path-test &&
@@ -74,7 +74,17 @@ test_expect_success 'gitdir config path is relative for both absolute and relati
git submodule add ../new-sub sub-rel &&
git config submodule.sub-rel.gitdir >actual &&
echo ".git/modules/sub-rel" >expect &&
- test_cmp expect actual
+ test_cmp expect actual &&
+
+ # Test with GIT_DIR containing symlink
+ if test_have_prereq SYMLINKS
+ then
+ ln -s relative-cfg-path-test ../relative-cfg-path-test-link &&
+ GIT_DIR=../relative-cfg-path-test-link/.git git submodule add ../new-sub sub-sym &&
+ git config submodule.sub-sym.gitdir >actual &&
+ echo ".git/modules/sub-sym" >expect &&
+ test_cmp expect actual
+ fi
)
'
--
gitgitgadget
^ permalink raw reply related [flat|nested] 24+ messages in thread* [PATCH v3 3/4] submodule: store settings in worktree config when enabled
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
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 ` 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
3 siblings, 0 replies; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-28 22:05 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Submodule URLs can differ between branches. For example, one branch
may use the upstream repository while another uses a fork for the same
submodule. When these branches are checked out in separate worktrees,
storing `submodule.<name>.url` in the shared repository config
prevents each worktree from using its own URL.
Store submodule settings such as `submodule.<name>.active` and
`submodule.<name>.url` in the worktree config when
`extensions.worktreeConfig` is enabled.
Settings shared between worktrees can remain in the repository
config. When such a setting exists there, 'git submodule deinit' sets
`submodule.<name>.active` to false in the worktree config to
unregister the submodule.
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>
---
Documentation/config/extensions.adoc | 9 ++
Documentation/config/submodule.adoc | 13 +-
Documentation/gitsubmodules.adoc | 26 +++-
builtin/submodule--helper.c | 43 +++---
builtin/worktree.c | 23 +++
t/t2405-worktree-submodule.sh | 157 +++++++++++++++++++++
t/t7425-submodule-gitdir-path-extension.sh | 49 +++++++
7 files changed, 296 insertions(+), 24 deletions(-)
diff --git a/Documentation/config/extensions.adoc b/Documentation/config/extensions.adoc
index 4f3a66a454..5dc6475cfe 100644
--- a/Documentation/config/extensions.adoc
+++ b/Documentation/config/extensions.adoc
@@ -169,5 +169,14 @@ 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.
diff --git a/Documentation/config/submodule.adoc b/Documentation/config/submodule.adoc
index 4c424017b4..6931888b71 100644
--- a/Documentation/config/submodule.adoc
+++ b/Documentation/config/submodule.adoc
@@ -6,6 +6,9 @@ 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',
@@ -16,6 +19,9 @@ 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
@@ -52,13 +58,18 @@ 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, '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
diff --git a/Documentation/gitsubmodules.adoc b/Documentation/gitsubmodules.adoc
index 8e8165637b..a18d565bc6 100644
--- a/Documentation/gitsubmodules.adoc
+++ b/Documentation/gitsubmodules.adoc
@@ -102,8 +102,9 @@ 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
@@ -123,6 +124,21 @@ 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
-----
@@ -150,8 +166,10 @@ 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
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 9faa532854..34783c71d1 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -497,7 +497,7 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,
return 0;
}
- if (repo_config_set_gently(the_repository, key, gitdir_path->buf)) {
+ if (repo_config_set_worktree_gently(the_repository, key, gitdir_path->buf)) {
free(key);
return -1;
}
@@ -610,15 +610,10 @@ static void init_submodule(const char *path, const char *prefix,
die(_("No url found for submodule path '%s' in .gitmodules"),
displaypath);
- /*
- * NEEDSWORK: In a multi-working-tree world, this needs to be
- * set in the per-worktree config.
- *
- * Set active flag for the submodule being initialized
- */
+ /* Set active flag for the submodule being initialized */
if (!is_submodule_active(the_repository, path)) {
strbuf_addf(&sb, "submodule.%s.active", sub->name);
- repo_config_set_gently(the_repository, sb.buf, "true");
+ repo_config_set_worktree_gently(the_repository, sb.buf, "true");
strbuf_reset(&sb);
}
@@ -644,7 +639,7 @@ static void init_submodule(const char *path, const char *prefix,
free(oldurl);
}
- if (repo_config_set_gently(the_repository, sb.buf, url))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, url))
die(_("Failed to register url for submodule path '%s'"),
displaypath);
if (!(flags & OPT_QUIET))
@@ -666,7 +661,7 @@ static void init_submodule(const char *path, const char *prefix,
upd = submodule_update_type_to_string(sub->update_strategy.type);
}
- if (repo_config_set_gently(the_repository, sb.buf, upd))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, upd))
die(_("Failed to register update mode for submodule path '%s'"), displaypath);
}
@@ -1492,7 +1487,7 @@ static void sync_submodule(const char *path, const char *prefix,
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.url", sub->name);
- if (repo_config_set_gently(the_repository, sb.buf, super_config_url))
+ if (repo_config_set_worktree_gently(the_repository, sb.buf, super_config_url))
die(_("failed to register url for submodule path '%s'"),
displaypath);
@@ -1671,17 +1666,31 @@ static void deinit_submodule(const char *path, const char *prefix,
/* remove the .git/config entries (unless the user already did it) */
if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
+ char *file = the_repository->repository_format_worktree_config ?
+ repo_git_path(the_repository, "config.worktree") :
+ repo_git_path(the_repository, "config");
char *sub_key = xstrfmt("submodule.%s", sub->name);
/*
* remove the whole section so we have a clean state when
* the user later decides to init this submodule again
*/
- repo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);
+ repo_config_rename_section_in_file(the_repository, file, sub_key, NULL);
if (!(flags & OPT_QUIET))
printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
sub->name, sub->url, displaypath);
+ free(file);
free(sub_key);
+
+ if (the_repository->repository_format_worktree_config) {
+ char *key = xstrfmt("submodule.%s.active", sub->name);
+
+ repo_config_clear(the_repository);
+ if (is_submodule_active(the_repository, path))
+ repo_config_set_worktree_gently(the_repository,
+ key, "false");
+ free(key);
+ }
}
cleanup:
@@ -3581,7 +3590,7 @@ static void configure_added_submodule(struct add_data *add_data)
int matched = 0;
key = xstrfmt("submodule.%s.url", add_data->sm_name);
- repo_config_set_gently(the_repository, key, add_data->realrepo);
+ repo_config_set_worktree_gently(the_repository, key, add_data->realrepo);
free(key);
add_submod.git_cmd = 1;
@@ -3611,10 +3620,6 @@ static void configure_added_submodule(struct add_data *add_data)
if (run_command(&add_gitmodules))
die(_("Failed to register submodule '%s'"), add_data->sm_path);
- /*
- * NEEDSWORK: In a multi-working-tree world this needs to be
- * set in the per-worktree config.
- */
/*
* NEEDSWORK: In the longer run, we need to get rid of this
* pattern of querying "submodule.active" before calling
@@ -3628,7 +3633,7 @@ static void configure_added_submodule(struct add_data *add_data)
* current configured pathspec, set the submodule's active flag
*/
key = xstrfmt("submodule.%s.active", add_data->sm_name);
- repo_config_set_gently(the_repository, key, "true");
+ repo_config_set_worktree_gently(the_repository, key, "true");
free(key);
} else {
for (size_t i = 0; i < values->nr; i++) {
@@ -3640,7 +3645,7 @@ static void configure_added_submodule(struct add_data *add_data)
}
if (!matched) { /* no pattern matched -> force-enable */
key = xstrfmt("submodule.%s.active", add_data->sm_name);
- repo_config_set_gently(the_repository, key, "true");
+ repo_config_set_worktree_gently(the_repository, key, "true");
free(key);
}
}
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..71cea4813a 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -363,6 +363,9 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
if (file_exists(from_file)) {
struct config_set cs = { { 0 } };
int bare;
+ struct string_list keys = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+ int i;
if (safe_create_leading_directories(the_repository, to_file) ||
copy_file(the_repository, to_file, from_file, 0666)) {
@@ -386,6 +389,26 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)
error(_("failed to unset '%s' in '%s'"),
"core.worktree", to_file);
+ /* unset "submodule.*.gitdir" */
+ for (i = 0; i < cs.list.nr; i++) {
+ const char *var = cs.list.items[i].e->key;
+ const char *subsection, *key;
+ size_t subsection_len;
+
+ if (!parse_config_key(var, "submodule",
+ &subsection, &subsection_len, &key) &&
+ subsection && !strcmp(key, "gitdir"))
+ string_list_insert(&keys, var);
+ }
+ for_each_string_list_item(item, &keys) {
+ 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);
+ }
+ string_list_clear(&keys, 0);
+
git_configset_clear(&cs);
}
diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh
index f5c94d490b..9bd8fda77c 100755
--- a/t/t2405-worktree-submodule.sh
+++ b/t/t2405-worktree-submodule.sh
@@ -91,4 +91,161 @@ test_expect_success 'unsetting core.worktree does not prevent running commands d
git -C main/.git/worktrees/checkout-recurse/modules/sub log
'
+test_expect_success 'submodule config in worktree config' '
+ test_when_finished "rm -rf wt-config" &&
+ git clone "$base_path/origin/main" wt-config &&
+ (
+ cd wt-config &&
+ git config extensions.worktreeConfig true &&
+ git worktree add wt &&
+ cat .git/config >.git/expect-config &&
+
+ git submodule update --init &&
+ test_cmp .git/expect-config .git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+
+ git submodule deinit sub &&
+ test_cmp .git/expect-config .git/config &&
+ test_must_be_empty .git/config.worktree &&
+
+ git submodule add ../sub sub2 &&
+ test_cmp .git/expect-config .git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub2.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ git config --file=.gitmodules submodule.sub2.url new_url &&
+ git submodule sync &&
+ test_cmp .git/expect-config .git/config &&
+ echo "new_url" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ cd wt &&
+
+ git submodule update --init &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+
+ git submodule deinit sub &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ test_must_be_empty ../.git/worktrees/wt/config.worktree &&
+
+ git submodule add ../sub sub2 &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub2.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual &&
+
+ git config --file=.gitmodules submodule.sub2.url new_url &&
+ git submodule sync &&
+ test_cmp ../.git/expect-config ../.git/config &&
+ echo "new_url" >expect &&
+ git config --worktree submodule.sub2.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'deinit overwrites active config' '
+ 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 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 '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 &&
+ (
+ cd copy-wt-config &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ git submodule update --init &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+ echo ".git/modules/sub" >expect &&
+ git config --worktree submodule.sub.gitdir >actual &&
+ test_cmp expect actual &&
+
+ git worktree add wt &&
+ cd wt &&
+ echo "true" >expect &&
+ git config --worktree submodule.sub.active >actual &&
+ test_cmp expect actual &&
+ echo "$base_path/origin/sub" >expect &&
+ git config --worktree submodule.sub.url >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config --worktree submodule.sub.gitdir
+ )
+'
+
test_done
diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh
index 65a61edb09..590febcafd 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -88,6 +88,55 @@ test_expect_success 'gitdir config path is relative for both absolute and relati
)
'
+test_expect_success 'gitdir for the main worktree is stored in worktree config' '
+ test_when_finished "rm -rf relative-cfg-path-test-main" &&
+ git init -b main relative-cfg-path-test-main &&
+ (
+ cd relative-cfg-path-test-main &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ # Test with absolute URL
+ git submodule add "$TRASH_DIRECTORY/new-sub" sub-abs &&
+ git config --worktree submodule.sub-abs.gitdir >actual &&
+ echo ".git/modules/sub-abs" >expect &&
+ test_cmp expect actual &&
+
+ # Test with relative URL
+ git submodule add ../new-sub sub-rel &&
+ git config --worktree submodule.sub-rel.gitdir >actual &&
+ echo ".git/modules/sub-rel" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'gitdir for a linked worktree is stored in worktree config' '
+ test_when_finished "rm -rf relative-cfg-path-test-wt" &&
+ git init -b main relative-cfg-path-test-wt &&
+ (
+ cd relative-cfg-path-test-wt &&
+ git config core.repositoryformatversion 1 &&
+ git config extensions.submodulePathConfig true &&
+ git config extensions.worktreeConfig true &&
+
+ git worktree add wt &&
+ cd wt &&
+
+ # Test with absolute URL
+ git submodule add "$TRASH_DIRECTORY/new-sub" sub-abs &&
+ git config --worktree submodule.sub-abs.gitdir >actual &&
+ echo "../.git/worktrees/wt/modules/sub-abs" >expect &&
+ test_cmp expect actual &&
+
+ # Test with relative URL
+ git submodule add ../../new-sub sub-rel &&
+ git config --worktree submodule.sub-rel.gitdir >actual &&
+ echo "../.git/worktrees/wt/modules/sub-rel" >expect &&
+ test_cmp expect actual
+ )
+'
+
test_expect_success 'clone from repo with both legacy and new-style submodules' '
git clone --recurse-submodules main cloned-non-extension &&
(
--
gitgitgadget
^ permalink raw reply related [flat|nested] 24+ messages in thread* [PATCH v3 4/4] submodule init: copy submodule.<name>.gitdir to config.worktree
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
` (2 preceding siblings ...)
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 ` Yoichi NAKAYAMA via GitGitGadget
2026-11-29 8:32 ` Junio C Hamano
3 siblings, 1 reply; 24+ messages in thread
From: Yoichi NAKAYAMA via GitGitGadget @ 2026-11-28 22:05 UTC (permalink / raw)
To: git; +Cc: Yoichi NAKAYAMA, Yoichi NAKAYAMA, Yoichi NAKAYAMA
From: Yoichi NAKAYAMA <yoichi.nakayama@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 <yoichi.nakayama@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
^ permalink raw reply related [flat|nested] 24+ messages in thread* Re: [PATCH v3 4/4] submodule init: copy submodule.<name>.gitdir to config.worktree
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
0 siblings, 0 replies; 24+ messages in thread
From: Junio C Hamano @ 2026-11-29 8:32 UTC (permalink / raw)
To: Yoichi NAKAYAMA via GitGitGadget; +Cc: git, Yoichi NAKAYAMA
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
> +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/");
Isn't there still a symlink asymmetry here?
While 'super' now resolves symlinks via real_pathdup(), 'submodule' only
goes through strbuf_normalize_path(), which performs purely lexical
normalization without calling lstat() or readlink().
Suppose a repository 'repo' has a symlink 'repo-link' pointing to it,
and an older Git (before [PATCH 2/4]) wrote an absolute path through the
symlink into the repository config, such as
"/path/to/repo-link/.git/modules/sub1_custom" (or a value containing a
symlinked parent directory like /tmp or /var).
When the user later enables extensions.worktreeConfig and runs "git
submodule init" in 'repo':
1. strbuf_normalize_path() leaves the symlink component in 'submodule.buf'
unresolved, so it stays "/path/to/repo-link/.git/modules/sub1_custom".
2. real_pathdup() resolves 'super' to "/path/to/repo/.git".
3. relative_path() compares the two strings byte-by-byte, diverges at
'repo-link' vs 'repo', and returns "../../repo-link/.git/modules/sub1_custom"
instead of "modules/sub1_custom".
4. submodule_git_dir_matches_current_worktree() then returns false,
emitting the mismatch warning and overwriting the custom setting in
config.worktree with ".git/modules/sub1".
The new test in t7425 uses "pwd -P", which may hide this case.
Using strbuf_realpath_forgiving(&submodule, submodule.buf, 0) instead of
(or before) strbuf_normalize_path(&submodule) keeps both sides
canonicalized even when the final submodule gitdir directory does not
exist yet, avoids collapsing "<symlink>/.." lexically before resolving
the symlink, and lets invalid paths return false cleanly.
> +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."
> +);
The sentence "If you want to..." feels a bit odd, doesn't it? "git
submodule init" just wrote a fallback setting into config.worktree and
sharing another worktree's gitdir is usually not what you want to do.
Saying "If you want to use this setting here, set it in the worktree
config.' or dropping that sentence altogether may make the message less
confusing. I dunno.
> ...
> + if (!matches ||
> + validate_submodule_git_dir(tmp, submodule_name)) {
> + if (!matches)
> + warning(submodule_mismatch_warning, key);
You'd need _() around submodule_mismatch_warning don't you?
^ permalink raw reply [flat|nested] 24+ messages in thread