Skip to content

submodule: support worktree-specific configuration - #2306

Open
yoichi wants to merge 4 commits into
gitgitgadget:masterfrom
yoichi:submodule-with-worktree
Open

yoichi wants to merge 4 commits into
gitgitgadget:masterfrom
yoichi:submodule-with-worktree

Conversation

@yoichi

@yoichi yoichi commented Nov 9, 2026 •

Copy link
Copy Markdown

When using multiple worktrees, submodule settings stored in the repository config are shared between all worktrees, even when they need to differ.

For example, different branches may use different URLs for the same submodule. More importantly, sharing submodule.<name>.gitdir causes submodule worktrees to share the same HEAD and index, even when the corresponding superproject worktrees record different commits for the submodule.

This series makes submodule settings worktree-specific when extensions.worktreeConfig is enabled. It also keeps submodule.<name>.gitdir relative for linked worktrees, so it continues to work when the worktree is moved along with the main worktree.

Included commits:

  • [1/4] Keep submodule.<name>.gitdir relative for linked worktrees.
  • [2/4] Resolve symlinks in submodule.<name>.gitdir.
  • [3/4] Store submodule settings in the worktree config when extensions.worktreeConfig is enabled.
  • [4/4] Make git submodule init copy an existing gitdir setting to the current worktree config only when it belongs to that worktree.

Changes in v4

  • [4/4] Resolve symlinks in submodule.<name>.gitdir value
  • [4/4] Fix the warning message

Changes in v3

  • [1/4] Copy the path early, as relative_path may continue to use the provided buffer
  • [2/4] Add new commit to resolve symlinks in submodule.<name>.gitdir value
  • [3/4] Handle duplicate submodule.<name>.gitdir on 'git worktree add'
  • [3/4] Add more explanations
  • [4/4] Resolve symlinks in submodule_git_dir_matches_current_worktree()

Changes in v2

  • Update the cover letter to better explain the problems this series addresses.
  • [1/3] Copy the string returned by worktree_git_path() early.
  • [2/3] Expand the explanation in the commit message for why the gitdir config is excluded from worktree add.
  • [3/3] Explain the case where the repository config contains a gitdir setting for another worktree, and warn when the setting is not copied.

Included commits:

  • [1/3] Keep submodule.<name>.gitdir relative for linked worktrees.
  • [2/3] Store submodule settings in the worktree config when extensions.worktreeConfig is enabled.
  • [3/3] Make git submodule init copy an existing gitdir setting to the current worktree config only when it belongs to that worktree.

cc: Yoichi NAKAYAMA yoichi.nakayama@gmail.com

@yoichi
yoichi force-pushed the submodule-with-worktree branch 6 times, most recently from 8e9091a to cdf089b Compare November 12, 2026 11:04
@yoichi

yoichi commented Nov 12, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 12, 2026

Copy link
Copy Markdown

Submitted as pull.2306.git.1794484985.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2306/yoichi/submodule-with-worktree-v1

To fetch this version to local tag pr-2306/yoichi/submodule-with-worktree-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2306/yoichi/submodule-with-worktree-v1

@@ -37,6 +37,7 @@
#include "wildmatch.h"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yoichi NAKAYAMA wrote on the Git mailing list (how to reply to this email):

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yoichi NAKAYAMA wrote on the Git mailing list (how to reply to this email):

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

@gitgitgadget

gitgitgadget Bot commented Nov 14, 2026

Copy link
Copy Markdown

User Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> has been added to the cc: list.

@yoichi
yoichi force-pushed the submodule-with-worktree branch 2 times, most recently from a9eee2c to b5d203d Compare November 21, 2026 06:47
@yoichi

yoichi commented Nov 21, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 21, 2026

Copy link
Copy Markdown

Submitted as pull.2306.v2.git.1795273853.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2306/yoichi/submodule-with-worktree-v2

To fetch this version to local tag pr-2306/yoichi/submodule-with-worktree-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2306/yoichi/submodule-with-worktree-v2

@@ -37,6 +37,7 @@
#include "wildmatch.h"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yoichi NAKAYAMA wrote on the Git mailing list (how to reply to this email):

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yoichi NAKAYAMA wrote on the Git mailing list (how to reply to this email):

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

@@ -58,7 +58,8 @@ submodule.<name>.gitdir::
respected when `extensions.submodulePathConfig` is enabled, otherwise it

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yoichi NAKAYAMA wrote on the Git mailing list (how to reply to this email):

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

@@ -479,78 +480,177 @@ struct init_cb {
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"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).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yoichi NAKAYAMA wrote on the Git mailing list (how to reply to this email):

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

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>
@yoichi
yoichi force-pushed the submodule-with-worktree branch 3 times, most recently from 080ca51 to 0ae440d Compare November 28, 2026 08:58
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>
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>
@yoichi
yoichi force-pushed the submodule-with-worktree branch from 0ae440d to 3e2c161 Compare November 28, 2026 11:13
@yoichi

yoichi commented Nov 28, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Nov 28, 2026

Copy link
Copy Markdown

Submitted as pull.2306.v3.git.1795903557.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2306/yoichi/submodule-with-worktree-v3

To fetch this version to local tag pr-2306/yoichi/submodule-with-worktree-v3:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2306/yoichi/submodule-with-worktree-v3

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junio C Hamano wrote on the Git mailing list (how to reply to this email):

"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?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yoichi NAKAYAMA wrote on the Git mailing list (how to reply to this email):

On Sun, Nov 29, 2026 at 5:32 PM 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 = 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.

I understand that an issue remains if a path containing a symlink is
already stored in `submodule.<name>.gitdir`. I will add a test and
replace `strbuf_normalize_path(&submodule)` with
`strbuf_realpath_forgiving(&submodule, submodule.buf, 0)`.
It can convert submodule to an absolute path even if the submodule's
gitdir and its parent directory ("modules") have not created yet.

> > +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.

I was assuming a case where the user has specified a custom path,
rather than another worktree's submodule. However, the final sentence would
be nonsensical if the value is for another worktree's submodule, and
it is unnecessary
even when a custom path is specified. I will remove that final sentence.

> > ...
> > +                     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?

Yes. I'll put _().

Thanks,
-- 
Yoichi NAKAYAMA

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>
@yoichi
yoichi force-pushed the submodule-with-worktree branch from 3e2c161 to cfee53f Compare December 2, 2026 12:43
@yoichi

yoichi commented Dec 2, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Dec 2, 2026

Copy link
Copy Markdown

Submitted as pull.2306.v4.git.1796245612.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2306/yoichi/submodule-with-worktree-v4

To fetch this version to local tag pr-2306/yoichi/submodule-with-worktree-v4:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2306/yoichi/submodule-with-worktree-v4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant