🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
19 messages
19 total messages Started by Andreas Wendleder Mon, 16 Nov 2026 22:51
submodule: linked-worktree submodule hits BUG() in validate_submodule_legacy_git_dir()
#3197
Author: Andreas Wendleder
Date: Mon, 16 Nov 2026 22:51
42 lines
12385 bytes
With submodule.recurse=true, switching branches in a superproject worktree
whose submodule is itself a linked worktree of the submodule repository
aborts:

  BUG: submodule.c:2414: submodule name 'sub' not a suffix of git dir
  '.../.git/modules/sub/worktrees/sub1'

Reproduction (git 2.56.0; the code is the same on master, next and seen):

  #!/bin/sh
  set -e
  export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
  g() { git -c user.name=t -c user.email=t@t -c protocol.file.allow=always "$@"; }
  rm -rf w && mkdir w && cd w
  g init -q -b main sub && g -C sub commit -q --allow-empty -m s1
  g init -q -b main super && cd super
  g submodule add -q ../sub sub && g commit -q -m add
  g -C sub commit -q --allow-empty -m s2 && g add sub && g commit -q -m bump
  g branch other HEAD~1
  g worktree add -q --detach ../wt main
  g -C sub worktree add -q --detach ../../other/sub HEAD  # takes the name 'sub'
  g -C sub worktree add -q --detach ../../wt/sub HEAD     # so this one is 'sub1'
  cd ../wt
  g -c submodule.recurse=true switch other

submodule_move_head() expects validate_submodule_git_dir() to return < 0
and then dies with "refusing to create/use ... Enabling
extensions.submodulePathConfig should fix this", but without the extension
validate_submodule_legacy_git_dir() calls BUG() instead. A linked
worktree's git dir (modules/<name>/worktrees/<id>) never ends in the
submodule name, so this setup always hits it.

With extensions.submodulePathConfig enabled (repositoryformatversion 1),
the same commands no longer crash but die with the "refusing to create/use
..." message, which recommends enabling the extension that is already
enabled.

I would expect a linked worktree of a submodule's repository to be
accepted as the submodule's git dir. If it is meant to be rejected, the
legacy path should return an error instead of calling BUG(), and the hint
should not suggest the extension when it is already on.
Re: submodule: linked-worktree submodule hits BUG() in validate_submodule_legacy_git_dir()
#3254
Author: Adrian Ratiu
Date: Tue, 17 Nov 2026 17:45
46 lines
10825 bytes
Hi Andreas,

On Mon, 16 Nov 2026, Andreas Wendleder <gonsolo@gmail.com> wrote:
> With submodule.recurse=true, switching branches in a superproject worktree
> whose submodule is itself a linked worktree of the submodule repository
> aborts:
>
>   BUG: submodule.c:2414: submodule name 'sub' not a suffix of git dir
>   '.../.git/modules/sub/worktrees/sub1'

Thanks for reporting this and for the reproducer.

The BUG() is older than submodulePathConfig. Users could not reach it
before 9cf8547320 (clone: prevent clashing git dirs when cloning
submodule in parallel, 2024-01-28), which made submodule_move_head()
validate whatever path the submodule's gitfile points to. A path read
from disk can be anything, so it should never hit a BUG().

Your reproducer needs the second worktree because the gitdir ".../sub"
happens to end in the submodule name, so the check passes by accident.

t2405 sets up exactly this layout but never runs a recursvie checkout.

I'll send a fix.

> With extensions.submodulePathConfig enabled (repositoryformatversion 1),
> the same commands no longer crash but die with the "refusing to create/use
> ..." message, which recommends enabling the extension that is already
> enabled.
>
> I would expect a linked worktree of a submodule's repository to be
> accepted as the submodule's git dir. If it is meant to be rejected, the
> legacy path should return an error instead of calling BUG(), and the hint
> should not suggest the extension when it is already on.

Yes, the hint is actually my mistake.

I have a local fix which I need to test a bit more, but makes your
script switch cleanly in both modes and the main submodule checkout
isn't touched.

Will send it in the next few days with you in Reported-by.

Thanks again,
Adrian
[PATCH 0/3] submodule: support linked worktrees in recursive checkout
#3303
Author: Adrian Ratiu
Date: Wed, 18 Nov 2026 16:32
31 lines
10287 bytes
Hello all,

This fixes a few submodule bugs reported by Andreas at:
https://lore.kernel.org/git/avuJBGlB3pRU3JU0@pampelmuse/

In short: when a submodule is a linked worktree of its repository
("git worktree add" inside the submodule), a recursive checkout in the
superproject either crashes with a BUG() or leaves the submodule broken.

The patches should be self-explanatory, many thanks to Andreas for
reporting and sharing a reproducer.

Regards,
Adrian

Adrian Ratiu (3):
  submodule: return an error instead of BUG() for an unexpected git dir
  submodule: only suggest submodulePathConfig when it is disabled
  submodule: accept a linked worktree as the submodule's git dir

 submodule.c                                | 72 +++++++++++++++++++---
 t/t2405-worktree-submodule.sh              | 40 ++++++++++++
 t/t7425-submodule-gitdir-path-extension.sh | 14 +++++
 t/t7450-bad-git-dotfiles.sh                | 31 ++++++++++
 4 files changed, 148 insertions(+), 9 deletions(-)


base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
-- 
2.56.1
[PATCH 1/3] submodule: return an error instead of BUG() for an unexpected git dir
#3306
Author: Adrian Ratiu
Date: Wed, 18 Nov 2026 16:32
82 lines
12725 bytes
During a recursive checkout, Git reads a submodule's .git file to find
its git dir and checks that the path ends in the submodule's name, e.g.
".git/modules/sub" for a submodule called "sub".

Without extensions.submodulePathConfig, a path that does not end that
way triggers a BUG() and aborts the command:

  BUG: submodule.c:2414: submodule name 'sub' not a suffix of git dir '...'

This path comes from a file on disk, not from Git's own code, so an
unexpected value is a problem with the user's setup, not a bug in Git.
It can happen when the git dir under .git/modules has been renamed or
the submodule is a linked worktree (handled in a later commit).

When the check was added in a8dee3ca61 (Disallow dubiously-nested
submodule git directories, 2019-10-01), its callers only passed git
dirs computed by Git from the submodule name, so BUG() was correct.

9cf8547320 (clone: prevent clashing git dirs when cloning submodule in
parallel, 2024-01-28) added a caller that passes a path read from the
gitfile, which made the BUG() reachable.

Return an error instead of BUG(), so the command dies with the same
"refusing to create/use ..." message as when the submodulePathConfig
extension is enabled.

Reported-by: Andreas Wendleder <gonsolo@gmail.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 submodule.c                 |  8 ++++++--
 t/t7450-bad-git-dotfiles.sh | 14 ++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/submodule.c b/submodule.c
index 1302c11ddf..d00d820d42 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2410,10 +2410,14 @@ static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodul
 		BUG("validate_submodule_git_dir() must be called with "
 		    "extensions.submodulePathConfig disabled.");
 
+	/*
+	 * The git dir may come from a gitfile on disk, so a mismatch is
+	 * not necessarily a bug in the caller.
+	 */
 	if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
 	    strcmp(p, submodule_name))
-		BUG("submodule name '%s' not a suffix of git dir '%s'",
-		    submodule_name, git_dir);
+		return error(_("submodule name '%s' not a suffix of git dir '%s'"),
+			     submodule_name, git_dir);
 
 	/*
 	 * We prevent the contents of sibling submodules' git directories to
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
index 72c7f6f73b..e17642fc16 100755
--- a/t/t7450-bad-git-dotfiles.sh
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -380,6 +380,20 @@ test_expect_success 'checkout -f --recurse-submodules must not use a nested gitd
 	test_path_is_missing nested_checkout/thing2/.git
 '
 
+test_expect_success 'checkout --recurse-submodules rejects gitfile not matching submodule name' '
+	git clone nested mismatched_gitdir &&
+	(
+		cd mismatched_gitdir &&
+		git submodule init thing1 &&
+		git submodule update thing1 &&
+		mv .git/modules/hippo .git/modules/elephant &&
+		echo "gitdir: ../.git/modules/elephant" >thing1/.git
+	) &&
+	test_must_fail git -C mismatched_gitdir checkout -f --recurse-submodules HEAD 2>err &&
+	test_grep "not a suffix of git dir" err &&
+	test_grep ! "BUG" err
+'
+
 test_expect_success SYMLINKS,!WINDOWS,!MINGW 'submodule must not checkout into different directory' '
 	test_when_finished "rm -rf sub repo bad-clone" &&
 
-- 
2.56.1
[PATCH 2/3] submodule: only suggest submodulePathConfig when it is disabled
#3305
Author: Adrian Ratiu
Date: Wed, 18 Nov 2026 16:32
79 lines
12522 bytes
When a recursive checkout refuses to use a submodule's git dir, the
error message ends with:

  Enabling extensions.submodulePathConfig should fix this.

This is printed even when the extension is already enabled, which is
confusing: the user is told to turn on something that is already on.

Move the suggestion into a separate hint, as submodule_name_to_gitdir()
already does, and only show it when the extension is disabled.

Reported-by: Andreas Wendleder <gonsolo@gmail.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 submodule.c                                | 10 ++++++----
 t/t7425-submodule-gitdir-path-extension.sh | 14 ++++++++++++++
 t/t7450-bad-git-dotfiles.sh                |  1 +
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/submodule.c b/submodule.c
index d00d820d42..64cd8005f7 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2171,11 +2171,13 @@ int submodule_move_head(const char *path, const char *super_prefix,
 
 				free(dotgit);
 				if (validate_submodule_git_dir(git_dir,
-							       sub->name) < 0)
+							       sub->name) < 0) {
+					if (!the_repository->repository_format_submodule_path_cfg)
+						advise(_("enabling extensions.submodulePathConfig "
+							 "might fix the following error."));
 					die(_("refusing to create/use '%s' in "
-					      "another submodule's git dir. "
-					      "Enabling extensions.submodulePathConfig "
-					      "should fix this."), git_dir);
+					      "another submodule's git dir."), git_dir);
+				}
 				free(git_dir);
 			}
 		} else {
diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh
index ea86ecf7ee..80438a7231 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -525,4 +525,18 @@ test_expect_success 'submodule gitdir conflicts with previously encoded name (lo
 	)
 '
 
+test_expect_success 'rejected submodule gitfile does not suggest enabling the extension' '
+	git clone -c extensions.submodulePathConfig=true --recurse-submodules nested clone_moved_gitdir &&
+	(
+		cd clone_moved_gitdir &&
+		mkdir .git/modules/zoo &&
+		mv .git/modules/hippo .git/modules/zoo/hippo &&
+		git config set -f .git/modules/zoo/hippo/config core.worktree ../../../../thing1 &&
+		echo "gitdir: ../.git/modules/zoo/hippo" >thing1/.git
+	) &&
+	test_must_fail git -C clone_moved_gitdir checkout -f --recurse-submodules HEAD 2>err &&
+	test_grep "refusing to create/use" err &&
+	test_grep ! "extensions.submodulePathConfig" err
+'
+
 test_done
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
index e17642fc16..a710c05a5d 100755
--- a/t/t7450-bad-git-dotfiles.sh
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -391,6 +391,7 @@ test_expect_success 'checkout --recurse-submodules rejects gitfile not matching
 	) &&
 	test_must_fail git -C mismatched_gitdir checkout -f --recurse-submodules HEAD 2>err &&
 	test_grep "not a suffix of git dir" err &&
+	test_grep "enabling extensions.submodulePathConfig" err &&
 	test_grep ! "BUG" err
 '
 
-- 
2.56.1
[PATCH 3/3] submodule: accept a linked worktree as the submodule's git dir
#3304
Author: Adrian Ratiu
Date: Wed, 18 Nov 2026 16:32
220 lines
18564 bytes
A submodule's working tree can be a linked worktree of the submodule's
repository, created e.g. with:

  git -C super/sub worktree add ../../wt/sub

Its .git file then points to "super/.git/modules/sub/worktrees/<id>"
instead of "super/.git/modules/sub". t2405 already creates such a
submodule, but it doesn't test a recursive checkout, and its worktree
id happens to match the submodule name.

A recursive checkout (--recurse-submodules or submodule.recurse=true)
has two problems with this setup:

 1. It checks that the git dir ends in the submodule's name. A
    worktree's git dir ends in the worktree id instead, so the check
    fails and the command dies. It only passes by chance, when the id
    is the same as the name.

 2. Even when the check passes, Git then rewrites the .git file to
    point to the git dir it would normally use for the submodule,
    "$GIT_DIR/modules/<name>" of the superproject. That is not the
    repository the worktree belongs to and it usually does not exist,
    so the submodule is left broken. Likewise, when the submodule is
    removed, Git tries to unset core.worktree in that git dir and
    prints an error and a warning.

    This happens on every recursive checkout, not only with --force,
    because unpack_trees() always passes SUBMODULE_MOVE_HEAD_FORCE to
    submodule_move_head().

Fix both: when the git dir is a linked worktree of a valid submodule
git dir, i.e. it is located at "<common-dir>/worktrees/<id>", check
the common dir against the submodule's name and leave the .git file
and core.worktree alone. Reject a git dir that has a commondir file but
is located anywhere else.

Reported-by: Andreas Wendleder <gonsolo@gmail.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 submodule.c                   | 56 ++++++++++++++++++++++++++++++++---
 t/t2405-worktree-submodule.sh | 40 +++++++++++++++++++++++++
 t/t7450-bad-git-dotfiles.sh   | 16 ++++++++++
 3 files changed, 108 insertions(+), 4 deletions(-)

diff --git a/submodule.c b/submodule.c
index 64cd8005f7..1bd7fb7e04 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2119,6 +2119,45 @@ static void submodule_reset_index(const char *path, const char *super_prefix)
 		die(_("could not reset submodule index"));
 }
 
+/*
+ * A submodule gitfile may point to a linked worktree of the submodule
+ * repository, i.e. "<common-dir>/worktrees/<id>", whose path does not end
+ * in the submodule name. Validate the common dir in that case, after making
+ * sure that the git dir is where "git worktree add" would have put it, and
+ * die if it is not.
+ *
+ * Returns:
+ *   1 if git_dir is a valid linked worktree.
+ *   0 if it is a valid submodule git dir.
+ *   a negative value otherwise.
+ */
+static int validate_submodule_gitfile_dir(const char *git_dir,
+					  const char *submodule_name)
+{
+	struct strbuf common_dir = STRBUF_INIT;
+	struct strbuf worktree_dir = STRBUF_INIT;
+	int is_worktree, ret;
+
+	is_worktree = get_common_dir_noenv(&common_dir, git_dir);
+	if (is_worktree) {
+		const char *id;
+
+		strbuf_realpath(&worktree_dir, git_dir, 1);
+		if (!skip_prefix(worktree_dir.buf, common_dir.buf, &id) ||
+		    !skip_prefix(id, "/worktrees/", &id) ||
+		    !*id || strchr(id, '/'))
+			die(_("submodule git dir '%s' is not a worktree of '%s'"),
+			    git_dir, common_dir.buf);
+	}
+
+	ret = validate_submodule_git_dir(common_dir.buf, submodule_name);
+	if (!ret)
+		ret = is_worktree;
+	strbuf_release(&common_dir);
+	strbuf_release(&worktree_dir);
+	return ret;
+}
+
 /**
  * Moves a submodule at a given path from a given head to another new head.
  * For edge cases (a submodule coming into existence or removing a submodule)
@@ -2132,6 +2171,7 @@ int submodule_move_head(const char *path, const char *super_prefix,
 	struct child_process cp = CHILD_PROCESS_INIT;
 	const struct submodule *sub;
 	int *error_code_ptr, error_code;
+	int is_worktree = 0;
 
 	if (!is_submodule_active(the_repository, path))
 		return 0;
@@ -2170,8 +2210,9 @@ int submodule_move_head(const char *path, const char *super_prefix,
 				char *git_dir = xstrdup(read_gitfile(dotgit));
 
 				free(dotgit);
-				if (validate_submodule_git_dir(git_dir,
-							       sub->name) < 0) {
+				is_worktree = validate_submodule_gitfile_dir(git_dir,
+									     sub->name);
+				if (is_worktree < 0) {
 					if (!the_repository->repository_format_submodule_path_cfg)
 						advise(_("enabling extensions.submodulePathConfig "
 							 "might fix the following error."));
@@ -2191,7 +2232,13 @@ int submodule_move_head(const char *path, const char *super_prefix,
 			submodule_reset_index(path, super_prefix);
 		}
 
-		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
+		/*
+		 * Do not reconnect a linked worktree to the submodule's
+		 * main git dir: that would take over the git dir of another
+		 * working tree.
+		 */
+		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE) &&
+		    !is_worktree) {
 			struct strbuf gitdir = STRBUF_INIT;
 			submodule_name_to_gitdir(&gitdir, the_repository,
 						 sub->name);
@@ -2256,7 +2303,8 @@ int submodule_move_head(const char *path, const char *super_prefix,
 			if (is_empty_dir(path))
 				rmdir_or_warn(path);
 
-			submodule_unset_core_worktree(sub);
+			if (!is_worktree)
+				submodule_unset_core_worktree(sub);
 		}
 	}
 out:
diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh
index f5c94d490b..fc5cb849de 100755
--- a/t/t2405-worktree-submodule.sh
+++ b/t/t2405-worktree-submodule.sh
@@ -59,6 +59,46 @@ test_expect_success 'submodule is checked out after manually adding submodule wo
 	test_grep "file1 updated" out
 '
 
+test_expect_success 'checkout --recurse-submodules accepts a submodule worktree' '
+	git -C main worktree add "$base_path/submodule-worktree" "$rev1_hash_main" &&
+	git -C main/sub worktree add "$base_path/submodule-worktree/sub" "$rev1_hash_sub" &&
+	cat submodule-worktree/sub/.git >expect-gitfile &&
+	# the worktree id must not end in the submodule name
+	test_grep "/worktrees/sub1$" expect-gitfile &&
+	git -C main/sub rev-parse HEAD >expect-head-main &&
+	git -C submodule-worktree checkout --recurse-submodules "$(git -C main rev-parse HEAD)" &&
+	git -C submodule-worktree rev-parse HEAD:sub >expect-head &&
+	git -C submodule-worktree/sub rev-parse HEAD >actual-head &&
+	test_cmp expect-head actual-head &&
+	cat submodule-worktree/sub/.git >actual-gitfile &&
+	test_cmp expect-gitfile actual-gitfile &&
+	git -C main/sub rev-parse HEAD >actual-head-main &&
+	test_cmp expect-head-main actual-head-main
+'
+
+test_expect_success 'checkout --recurse-submodules accepts a submodule worktree with submodulePathConfig' '
+	test_config -C main core.repositoryformatversion 1 &&
+	test_config -C main extensions.submodulePathConfig true &&
+	test_config -C main submodule.sub.gitdir .git/modules/sub &&
+	git -C submodule-worktree checkout --recurse-submodules "$rev1_hash_main" &&
+	git -C submodule-worktree/sub rev-parse --short HEAD >actual-head &&
+	echo "$rev1_hash_sub" >expect-head &&
+	test_cmp expect-head actual-head &&
+	cat submodule-worktree/sub/.git >actual-gitfile &&
+	test_cmp expect-gitfile actual-gitfile &&
+	git -C main/sub rev-parse HEAD >actual-head-main &&
+	test_cmp expect-head-main actual-head-main
+'
+
+test_expect_success 'checkout --recurse-submodules removes a submodule worktree' '
+	git -C main/sub config get core.worktree >expect-main &&
+	git -C submodule-worktree checkout --recurse-submodules first 2>err &&
+	test_grep ! "core.worktree" err &&
+	test_path_is_missing submodule-worktree/sub/.git &&
+	git -C main/sub config get core.worktree >actual-main &&
+	test_cmp expect-main actual-main
+'
+
 test_expect_success 'checkout --recurse-submodules uses $GIT_DIR for submodules in a linked worktree' '
 	git -C main worktree add "$base_path/checkout-recurse" --detach  &&
 	git -C checkout-recurse submodule update --init &&
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
index a710c05a5d..26d442c0ad 100755
--- a/t/t7450-bad-git-dotfiles.sh
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -395,6 +395,22 @@ test_expect_success 'checkout --recurse-submodules rejects gitfile not matching
 	test_grep ! "BUG" err
 '
 
+test_expect_success 'checkout --recurse-submodules rejects gitfile pointing to a forged worktree' '
+	git clone nested forged_worktree &&
+	(
+		cd forged_worktree &&
+		git submodule init thing1 &&
+		git submodule update thing1 &&
+		mkdir .git/modules/forged &&
+		cp .git/modules/hippo/HEAD .git/modules/forged/HEAD &&
+		echo "../hippo" >.git/modules/forged/commondir &&
+		echo "gitdir: ../.git/modules/forged" >thing1/.git
+	) &&
+	test_must_fail git -C forged_worktree checkout -f --recurse-submodules HEAD 2>err &&
+	test_grep "is not a worktree of" err &&
+	test_grep ! "extensions.submodulePathConfig" err
+'
+
 test_expect_success SYMLINKS,!WINDOWS,!MINGW 'submodule must not checkout into different directory' '
 	test_when_finished "rm -rf sub repo bad-clone" &&
 
-- 
2.56.1
Re: [PATCH 1/3] submodule: return an error instead of BUG() for an unexpected git dir
#3324
Author: Junio C Hamano
Date: Wed, 18 Nov 2026 19:15
53 lines
14773 bytes
Adrian Ratiu <adrian.ratiu@collabora.com> writes:

> 9cf8547320 (clone: prevent clashing git dirs when cloning submodule in
> parallel, 2024-01-28) added a caller that passes a path read from the
> gitfile, which made the BUG() reachable.
>
> Return an error instead of BUG(), so the command dies with the same
> "refusing to create/use ..." message as when the submodulePathConfig
> extension is enabled.

Makes sense.

The above archaeology was quite illuminating.  It seems that the
offending commit came without the usual on-list reviews as part of
the Git 2.39.4/2.45.1 updates.

> Reported-by: Andreas Wendleder <gonsolo@gmail.com>
> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
> ---
>  submodule.c                 |  8 ++++++--
>  t/t7450-bad-git-dotfiles.sh | 14 ++++++++++++++
>  2 files changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/submodule.c b/submodule.c
> index 1302c11ddf..d00d820d42 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -2410,10 +2410,14 @@ static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodul
>  		BUG("validate_submodule_git_dir() must be called with "
>  		    "extensions.submodulePathConfig disabled.");
>  
> +	/*
> +	 * The git dir may come from a gitfile on disk, so a mismatch is
> +	 * not necessarily a bug in the caller.
> +	 */

"is not necessarily a bug" is not incorrect per se, but it is only
relevant in the context of turning what was originally a BUG() into
a "return error()".  If this had been written to return an error
instead of calling BUG() from the beginning, you would not have
phrased the comment that way.

If this if() statement needs a comment, we should state why these
two are expected to match, or why it is an error if they do not
match.

>  	if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
>  	    strcmp(p, submodule_name))
> -		BUG("submodule name '%s' not a suffix of git dir '%s'",
> -		    submodule_name, git_dir);
> +		return error(_("submodule name '%s' not a suffix of git dir '%s'"),
> +			     submodule_name, git_dir);
Re: [PATCH 2/3] submodule: only suggest submodulePathConfig when it is disabled
#3325
Author: Junio C Hamano
Date: Wed, 18 Nov 2026 19:28
68 lines
15456 bytes
Adrian Ratiu <adrian.ratiu@collabora.com> writes:

> When a recursive checkout refuses to use a submodule's git dir, the
> error message ends with:
>
>   Enabling extensions.submodulePathConfig should fix this.
>
> This is printed even when the extension is already enabled, which is
> confusing: the user is told to turn on something that is already on.

The motivation sounds sensible.

> Move the suggestion into a separate hint, as submodule_name_to_gitdir()
> already does, and only show it when the extension is disabled.

Hmph, but submodule_name_to_gitdir() does

	/* validate because users might have modified the config */
	if (validate_submodule_git_dir(buf->buf, submodule_name)) {
		advise(_("enabling extensions.submodulePathConfig might fix the "
			 "following error, if it's not already enabled."));
		die(_("refusing to create/use '%s' in another submodule's "
		      " git dir."), buf->buf);
	}

which does not really check if it is enabled; it just hedges the
message by using "might fix ... if it's not" and lets the users do
the work themselves, which is not necessarily a bad approach to take
to simplify our job.

Similarly, builtin/submodule--helper.c:clone_submodule(), which is
another caller of validate_submodule_git_dir(), has an unconditional
"Enabling ... should fix this" message, which we may want to give a
similar treatment.

> Reported-by: Andreas Wendleder <gonsolo@gmail.com>
> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
> ---
>  submodule.c                                | 10 ++++++----
>  t/t7425-submodule-gitdir-path-extension.sh | 14 ++++++++++++++
>  t/t7450-bad-git-dotfiles.sh                |  1 +
>  3 files changed, 21 insertions(+), 4 deletions(-)
>
> diff --git a/submodule.c b/submodule.c
> index d00d820d42..64cd8005f7 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -2171,11 +2171,13 @@ int submodule_move_head(const char *path, const char *super_prefix,
>  
>  				free(dotgit);
>  				if (validate_submodule_git_dir(git_dir,
> -							       sub->name) < 0)
> +							       sub->name) < 0) {
> +					if (!the_repository->repository_format_submodule_path_cfg)
> +						advise(_("enabling extensions.submodulePathConfig "
> +							 "might fix the following error."));
>  					die(_("refusing to create/use '%s' in "
> -					      "another submodule's git dir. "
> -					      "Enabling extensions.submodulePathConfig "
> -					      "should fix this."), git_dir);
> +					      "another submodule's git dir."), git_dir);
> +				}

It is not exactly a new issue, but this makes an already too deeply
nested block even worse.  I wonder if the entire "if not dry-run, do
these" block should be moved to a private helper function to make it
a bit easier to read.
Re: [PATCH 1/3] submodule: return an error instead of BUG() for an unexpected git dir
#3327
Author: Adrian Ratiu
Date: Wed, 18 Nov 2026 19:33
55 lines
11424 bytes
On Wed, 18 Nov 2026, Junio C Hamano <gitster@pobox.com> wrote:
> Adrian Ratiu <adrian.ratiu@collabora.com> writes:
>
>> 9cf8547320 (clone: prevent clashing git dirs when cloning submodule in
>> parallel, 2024-01-28) added a caller that passes a path read from the
>> gitfile, which made the BUG() reachable.
>>
>> Return an error instead of BUG(), so the command dies with the same
>> "refusing to create/use ..." message as when the submodulePathConfig
>> extension is enabled.
>
> Makes sense.
>
> The above archaeology was quite illuminating.  It seems that the
> offending commit came without the usual on-list reviews as part of
> the Git 2.39.4/2.45.1 updates.

Yeah, I wasn't part of discussions on this, so I don't have the full
context either, but I do think switching BUG() -> error() makes sense.

>> Reported-by: Andreas Wendleder <gonsolo@gmail.com>
>> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
>> ---
>>  submodule.c                 |  8 ++++++--
>>  t/t7450-bad-git-dotfiles.sh | 14 ++++++++++++++
>>  2 files changed, 20 insertions(+), 2 deletions(-)
>>
>> diff --git a/submodule.c b/submodule.c
>> index 1302c11ddf..d00d820d42 100644
>> --- a/submodule.c
>> +++ b/submodule.c
>> @@ -2410,10 +2410,14 @@ static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodul
>>  		BUG("validate_submodule_git_dir() must be called with "
>>  		    "extensions.submodulePathConfig disabled.");
>>  
>> +	/*
>> +	 * The git dir may come from a gitfile on disk, so a mismatch is
>> +	 * not necessarily a bug in the caller.
>> +	 */
>
> "is not necessarily a bug" is not incorrect per se, but it is only
> relevant in the context of turning what was originally a BUG() into
> a "return error()".  If this had been written to return an error
> instead of calling BUG() from the beginning, you would not have
> phrased the comment that way.
>
> If this if() statement needs a comment, we should state why these
> two are expected to match, or why it is an error if they do not
> match.

Good point.

I'll drop the comment addition in v2 since we don't need to over-explain
the BUG() -> error() transition.
Re: [PATCH 3/3] submodule: accept a linked worktree as the submodule's git dir
#3328
Author: Junio C Hamano
Date: Wed, 18 Nov 2026 19:52
81 lines
15855 bytes
Adrian Ratiu <adrian.ratiu@collabora.com> writes:

> A submodule's working tree can be a linked worktree of the submodule's
> repository, created e.g. with:
>
>   git -C super/sub worktree add ../../wt/sub
>
> Its .git file then points to "super/.git/modules/sub/worktrees/<id>"
> instead of "super/.git/modules/sub". t2405 already creates such a
> submodule, but it doesn't test a recursive checkout, and its worktree
> id happens to match the submodule name.

It is tempting to say "don't do it then", but if we can cater to the
use case that is great ;-)

> @@ -2132,6 +2171,7 @@ int submodule_move_head(const char *path, const char *super_prefix,
>  	struct child_process cp = CHILD_PROCESS_INIT;
>  	const struct submodule *sub;
>  	int *error_code_ptr, error_code;
> +	int is_worktree = 0;
>  
>  	if (!is_submodule_active(the_repository, path))
>  		return 0;
> @@ -2170,8 +2210,9 @@ int submodule_move_head(const char *path, const char *super_prefix,
>  				char *git_dir = xstrdup(read_gitfile(dotgit));
>  
>  				free(dotgit);
> -				if (validate_submodule_git_dir(git_dir,
> -							       sub->name) < 0) {
> +				is_worktree = validate_submodule_gitfile_dir(git_dir,
> +									     sub->name);
> +				if (is_worktree < 0) {
>  					if (!the_repository->repository_format_submodule_path_cfg)
>  						advise(_("enabling extensions.submodulePathConfig "
>  							 "might fix the following error."));

Earlier near the beginning of the function we initialized
is_worktree to 0 and then only when submodule_uses_gitfile() is
true, we update it with validate_submodule_gitfile_dir().

> @@ -2191,7 +2232,13 @@ int submodule_move_head(const char *path, const char *super_prefix,
>  			submodule_reset_index(path, super_prefix);
>  		}
>  
> -		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
> +		/*
> +		 * Do not reconnect a linked worktree to the submodule's
> +		 * main git dir: that would take over the git dir of another
> +		 * working tree.
> +		 */
> +		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE) &&
> +		    !is_worktree) {

If a linked-worktree submodule 'sub' contains a nested submodule
'inner' whose .git is a directory, submodule_uses_gitfile("sub")
would have returned 0 (because the check is recursive), we ran
absorb_git_dir_into_superproject() (which is also recursive so it
would cause .git in 'inner' to be absorbed), and is_worktree is
still 0 at this point, which means if() condition here is satisfied
(if forced) and we will enter this block.  Would it cause an issue?

I am wondering if the check for is_worktree should be moved outside
the else clause, i.e. making the overall flow look more like

        if (old_head) {
                if (!submodule_uses_gitfile(path))
                        absorb_git_dir_into_superproject(path, super_prefix);

                /*
                 * Now path/.git is guaranteed to be a gitfile; 
                 * validate and detect worktrees.
                 */
                ...
                is_worktree = validate_submodule_gitfile_dir(git_dir, sub->name);
                ...
        }

Or am I grossly misreading the code flow?

Thanks.
Re: [PATCH 2/3] submodule: only suggest submodulePathConfig when it is disabled
#3385
Author: Adrian Ratiu
Date: Thu, 19 Nov 2026 11:46
77 lines
12521 bytes
On Wed, 18 Nov 2026, Junio C Hamano <gitster@pobox.com> wrote:
> Adrian Ratiu <adrian.ratiu@collabora.com> writes:
>
>> When a recursive checkout refuses to use a submodule's git dir, the
>> error message ends with:
>>
>>   Enabling extensions.submodulePathConfig should fix this.
>>
>> This is printed even when the extension is already enabled, which is
>> confusing: the user is told to turn on something that is already on.
>
> The motivation sounds sensible.
>
>> Move the suggestion into a separate hint, as submodule_name_to_gitdir()
>> already does, and only show it when the extension is disabled.
>
> Hmph, but submodule_name_to_gitdir() does
>
> 	/* validate because users might have modified the config */
> 	if (validate_submodule_git_dir(buf->buf, submodule_name)) {
> 		advise(_("enabling extensions.submodulePathConfig might fix the "
> 			 "following error, if it's not already enabled."));
> 		die(_("refusing to create/use '%s' in another submodule's "
> 		      " git dir."), buf->buf);
> 	}
>
> which does not really check if it is enabled; it just hedges the
> message by using "might fix ... if it's not" and lets the users do
> the work themselves, which is not necessarily a bad approach to take
> to simplify our job.
>
> Similarly, builtin/submodule--helper.c:clone_submodule(), which is
> another caller of validate_submodule_git_dir(), has an unconditional
> "Enabling ... should fix this" message, which we may want to give a
> similar treatment.

Ack, I mis-remembered that we already checked the extension.

Happy to reword as you suggested, but for v2 I'd also like to explore
dropping the "hedge" and actually check in all three places, it should
be a trivial helper which reads repository_format_submodule_path_cfg.

>> Reported-by: Andreas Wendleder <gonsolo@gmail.com>
>> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
>> ---
>>  submodule.c                                | 10 ++++++----
>>  t/t7425-submodule-gitdir-path-extension.sh | 14 ++++++++++++++
>>  t/t7450-bad-git-dotfiles.sh                |  1 +
>>  3 files changed, 21 insertions(+), 4 deletions(-)
>>
>> diff --git a/submodule.c b/submodule.c
>> index d00d820d42..64cd8005f7 100644
>> --- a/submodule.c
>> +++ b/submodule.c
>> @@ -2171,11 +2171,13 @@ int submodule_move_head(const char *path, const char *super_prefix,
>>  
>>  				free(dotgit);
>>  				if (validate_submodule_git_dir(git_dir,
>> -							       sub->name) < 0)
>> +							       sub->name) < 0) {
>> +					if (!the_repository->repository_format_submodule_path_cfg)
>> +						advise(_("enabling extensions.submodulePathConfig "
>> +							 "might fix the following error."));
>>  					die(_("refusing to create/use '%s' in "
>> -					      "another submodule's git dir. "
>> -					      "Enabling extensions.submodulePathConfig "
>> -					      "should fix this."), git_dir);
>> +					      "another submodule's git dir."), git_dir);
>> +				}
>
> It is not exactly a new issue, but this makes an already too deeply
> nested block even worse.  I wonder if the entire "if not dry-run, do
> these" block should be moved to a private helper function to make it
> a bit easier to read.

Sure, I can add a helper here as well in v2. Thanks!
Re: [PATCH 3/3] submodule: accept a linked worktree as the submodule's git dir
#3386
Author: Adrian Ratiu
Date: Thu, 19 Nov 2026 12:19
61 lines
11707 bytes
On Wed, 18 Nov 2026, Junio C Hamano <gitster@pobox.com> wrote:
>> @@ -2191,7 +2232,13 @@ int submodule_move_head(const char *path, const char *super_prefix,
>>  			submodule_reset_index(path, super_prefix);
>>  		}
>>  
>> -		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
>> +		/*
>> +		 * Do not reconnect a linked worktree to the submodule's
>> +		 * main git dir: that would take over the git dir of another
>> +		 * working tree.
>> +		 */
>> +		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE) &&
>> +		    !is_worktree) {
>
> If a linked-worktree submodule 'sub' contains a nested submodule
> 'inner' whose .git is a directory, submodule_uses_gitfile("sub")
> would have returned 0 (because the check is recursive), we ran
> absorb_git_dir_into_superproject() (which is also recursive so it
> would cause .git in 'inner' to be absorbed), and is_worktree is
> still 0 at this point, which means if() condition here is satisfied
> (if forced) and we will enter this block.  Would it cause an issue?

Yes, good catch. I reproduced it with a "sub" linked worktree and a
"sub/inner/.git" directory, a recursive checkout absorbs the "inner"
and then rewrites sub/.git to point to "$GIT_DIR/modules/sub" which
does not exist:

  fatal: gitfile does not point to a valid repository: .git
  error: Submodule 'sub' could not be updated.

It's basically the same breakage this patch is meant to fix, just
reached through the absorb path.

>
> I am wondering if the check for is_worktree should be moved outside
> the else clause, i.e. making the overall flow look more like
>
>         if (old_head) {
>                 if (!submodule_uses_gitfile(path))
>                         absorb_git_dir_into_superproject(path, super_prefix);
>
>                 /*
>                  * Now path/.git is guaranteed to be a gitfile; 
>                  * validate and detect worktrees.
>                  */
>                 ...
>                 is_worktree = validate_submodule_gitfile_dir(git_dir, sub->name);
>                 ...
>         }

Yes, that fixes it, the sub/.git is left alone and the submodule is
checked-out at the correct commit. The validation also runs after an
absorb, but absorb puts the gitdir where submodule_name_to_gitdir()
says it should be, so the check always passes.

> Or am I grossly misreading the code flow?

You understood it perfectly. :) I'll do this in v2 as well.

Many thanks!
Re: [PATCH 2/3] submodule: only suggest submodulePathConfig when it is disabled
#3422
Author: Junio C Hamano
Date: Thu, 19 Nov 2026 22:14
9 lines
12947 bytes
Adrian Ratiu <adrian.ratiu@collabora.com> writes:

> ... dropping the "hedge" and actually check in all three places, it should
> be a trivial helper which reads repository_format_submodule_path_cfg.

Sounds super sensible.

Thanks.
[PATCH v2 0/5] submodule: support linked worktrees in recursive checkout
#3628
Author: Adrian Ratiu
Date: Mon, 23 Nov 2026 15:44
420 lines
28821 bytes
Hello all,

This fixes a few submodule bugs reported by Andreas at:
https://lore.kernel.org/git/avuJBGlB3pRU3JU0@pampelmuse/

In short: when a submodule is a linked worktree of its repository
("git worktree add" inside the submodule), a recursive checkout in the
superproject either crashes with a BUG() or leaves the submodule broken.

Changes in v2:
 * 1/5: Dropped the added code comment. (Junio)
 * 2/5: Only suggest extensions.submodulePathConfig when it is disabled,
   via a helper used in all three places that refuse a git dir. (Junio)
 * 3/5: New: move the git dir setup out of submodule_move_head() to
   reduce nesting. (Junio)
 * 4/5: New: also validate the git dir after absorbing. (Junio)
 * 5/5: Simplified the worktree check and tests. Added a test for a
   submodule worktree with a nested git dir.

Regards,
Adrian

Adrian Ratiu (5):
  submodule: return an error instead of BUG() for an unexpected git dir
  submodule: only suggest submodulePathConfig when it is disabled
  submodule: move git dir setup out of submodule_move_head()
  submodule: validate the git dir after absorbing it
  submodule: accept a linked worktree as the submodule's git dir

 builtin/submodule--helper.c                |   4 +-
 submodule.c                                | 135 ++++++++++++++-------
 submodule.h                                |   7 ++
 t/t2405-worktree-submodule.sh              |  43 +++++++
 t/t7425-submodule-gitdir-path-extension.sh |  22 ++++
 t/t7450-bad-git-dotfiles.sh                |  49 ++++++++
 6 files changed, 211 insertions(+), 49 deletions(-)

Range-diff against v1:
1:  bddea5403d ! 1:  e3f5412bbd submodule: return an error instead of BUG() for an unexpected git dir
    @@ Commit message
     
      ## submodule.c ##
     @@ submodule.c: static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodul
    - 		BUG("validate_submodule_git_dir() must be called with "
    - 		    "extensions.submodulePathConfig disabled.");
      
    -+	/*
    -+	 * The git dir may come from a gitfile on disk, so a mismatch is
    -+	 * not necessarily a bug in the caller.
    -+	 */
      	if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
      	    strcmp(p, submodule_name))
     -		BUG("submodule name '%s' not a suffix of git dir '%s'",
2:  52e37fef11 ! 2:  fde0ceb72e submodule: only suggest submodulePathConfig when it is disabled
    @@ Metadata
      ## Commit message ##
         submodule: only suggest submodulePathConfig when it is disabled
     
    -    When a recursive checkout refuses to use a submodule's git dir, the
    -    error message ends with:
    +    When Git refuses to use a submodule's git dir, it suggests enabling
    +    extensions.submodulePathConfig, sometimes even when the extension is
    +    already enabled.
     
    -      Enabling extensions.submodulePathConfig should fix this.
    -
    -    This is printed even when the extension is already enabled, which is
    -    confusing: the user is told to turn on something that is already on.
    -
    -    Move the suggestion into a separate hint, as submodule_name_to_gitdir()
    -    already does, and only show it when the extension is disabled.
    +    We know whether it is enabled, so only suggest it when it is not. Do
    +    this in a common helper, so that all three places that refuse a git
    +    dir give the same hint and error message.
     
         Reported-by: Andreas Wendleder <gonsolo@gmail.com>
         Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
     
    + ## builtin/submodule--helper.c ##
    +@@ builtin/submodule--helper.c: static int clone_submodule(const struct module_clone_data *clone_data,
    + 		char *head = xstrfmt("%s/HEAD", sm_gitdir);
    + 		unlink(head);
    + 		free(head);
    +-		die(_("refusing to create/use '%s' in another submodule's git dir. "
    +-		      "Enabling extensions.submodulePathConfig should fix this."),
    +-		    sm_gitdir);
    ++		die_refusing_submodule_git_dir(the_repository, sm_gitdir);
    + 	}
    + 
    + 	connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
    +
      ## submodule.c ##
     @@ submodule.c: int submodule_move_head(const char *path, const char *super_prefix,
    - 
      				free(dotgit);
      				if (validate_submodule_git_dir(git_dir,
    --							       sub->name) < 0)
    -+							       sub->name) < 0) {
    -+					if (!the_repository->repository_format_submodule_path_cfg)
    -+						advise(_("enabling extensions.submodulePathConfig "
    -+							 "might fix the following error."));
    - 					die(_("refusing to create/use '%s' in "
    + 							       sub->name) < 0)
    +-					die(_("refusing to create/use '%s' in "
     -					      "another submodule's git dir. "
     -					      "Enabling extensions.submodulePathConfig "
     -					      "should fix this."), git_dir);
    -+					      "another submodule's git dir."), git_dir);
    -+				}
    ++					die_refusing_submodule_git_dir(the_repository,
    ++								       git_dir);
      				free(git_dir);
      			}
      		} else {
    +@@ submodule.c: int submodule_to_gitdir(struct repository *repo,
    + 	return ret;
    + }
    + 
    ++void die_refusing_submodule_git_dir(struct repository *r, const char *git_dir)
    ++{
    ++	if (!r->repository_format_submodule_path_cfg)
    ++		advise(_("enabling extensions.submodulePathConfig might fix the "
    ++			 "following error."));
    ++
    ++	die(_("refusing to create/use '%s' in another submodule's git dir."), git_dir);
    ++}
    ++
    + void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
    + 			      const char *submodule_name)
    + {
    +@@ submodule.c: void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
    + 	}
    + 
    + 	/* validate because users might have modified the config */
    +-	if (validate_submodule_git_dir(buf->buf, submodule_name)) {
    +-		advise(_("enabling extensions.submodulePathConfig might fix the "
    +-			 "following error, if it's not already enabled."));
    +-		die(_("refusing to create/use '%s' in another submodule's "
    +-		      " git dir."), buf->buf);
    +-	}
    ++	if (validate_submodule_git_dir(buf->buf, submodule_name))
    ++		die_refusing_submodule_git_dir(r, buf->buf);
    + }
    +
    + ## submodule.h ##
    +@@ submodule.h: void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
    +  */
    + int validate_submodule_git_dir(char *git_dir, const char *submodule_name);
    + 
    ++/*
    ++ * Die because "git_dir" failed validate_submodule_git_dir(), suggesting to
    ++ * enable extensions.submodulePathConfig if it is disabled in "r".
    ++ */
    ++NORETURN void die_refusing_submodule_git_dir(struct repository *r,
    ++					     const char *git_dir);
    ++
    + /*
    +  * Make sure that the given submodule path does not follow symlinks.
    +  */
     
      ## t/t7425-submodule-gitdir-path-extension.sh ##
     @@ t/t7425-submodule-gitdir-path-extension.sh: test_expect_success 'submodule gitdir conflicts with previously encoded name (lo
    @@ t/t7425-submodule-gitdir-path-extension.sh: test_expect_success 'submodule gitdi
     +	test_grep "refusing to create/use" err &&
     +	test_grep ! "extensions.submodulePathConfig" err
     +'
    ++
    ++test_expect_success 'rejected submodule gitdir config does not suggest enabling the extension' '
    ++	git clone -c extensions.submodulePathConfig=true nested clone_bad_gitdir_config &&
    ++	git -C clone_bad_gitdir_config config set submodule.hippo.gitdir .git/modules/zoo/hippo &&
    ++	test_must_fail git -C clone_bad_gitdir_config submodule--helper gitdir hippo 2>err &&
    ++	test_grep "refusing to create/use" err &&
    ++	test_grep ! "extensions.submodulePathConfig" err
    ++'
     +
      test_done
     
-:  ---------- > 3:  6ae934f8a1 submodule: move git dir setup out of submodule_move_head()
-:  ---------- > 4:  e272a947d3 submodule: validate the git dir after absorbing it
3:  1507fddb52 ! 5:  02adab3b90 submodule: accept a linked worktree as the submodule's git dir
    @@ Metadata
      ## Commit message ##
         submodule: accept a linked worktree as the submodule's git dir
     
    -    A submodule's working tree can be a linked worktree of the submodule's
    -    repository, created e.g. with:
    +    A submodule can be checked out as a linked worktree of its repository,
    +    created e.g. with:
     
           git -C super/sub worktree add ../../wt/sub
     
         Its .git file then points to "super/.git/modules/sub/worktrees/<id>"
    -    instead of "super/.git/modules/sub". t2405 already creates such a
    -    submodule, but it doesn't test a recursive checkout, and its worktree
    -    id happens to match the submodule name.
    +    instead of "super/.git/modules/sub".
    +
    +    t2405 already creates such a submodule, but it doesn't test a recursive
    +    checkout and its worktree id happens to match the submodule name.
     
         A recursive checkout (--recurse-submodules or submodule.recurse=true)
         has two problems with this setup:
    @@ submodule.c: static void submodule_reset_index(const char *path, const char *sup
      		die(_("could not reset submodule index"));
      }
      
    +-static void setup_submodule_git_dir(const struct submodule *sub,
    +-				    const char *path, const char *super_prefix,
    +-				    const char *old_head, unsigned flags)
     +/*
    -+ * A submodule gitfile may point to a linked worktree of the submodule
    -+ * repository, i.e. "<common-dir>/worktrees/<id>", whose path does not end
    -+ * in the submodule name. Validate the common dir in that case, after making
    -+ * sure that the git dir is where "git worktree add" would have put it, and
    -+ * die if it is not.
    -+ *
    -+ * Returns:
    -+ *   1 if git_dir is a valid linked worktree.
    -+ *   0 if it is a valid submodule git dir.
    -+ *   a negative value otherwise.
    ++ * A linked worktree's git dir is "<common-dir>/worktrees/<id>", which is
    ++ * where "git worktree add" puts it. Die if it is anywhere else.
     + */
    -+static int validate_submodule_gitfile_dir(const char *git_dir,
    -+					  const char *submodule_name)
    ++static void validate_submodule_worktree_dir(const char *git_dir,
    ++					    const char *common_dir)
     +{
    -+	struct strbuf common_dir = STRBUF_INIT;
     +	struct strbuf worktree_dir = STRBUF_INIT;
    -+	int is_worktree, ret;
    -+
    -+	is_worktree = get_common_dir_noenv(&common_dir, git_dir);
    -+	if (is_worktree) {
    -+		const char *id;
    ++	const char *id;
     +
    -+		strbuf_realpath(&worktree_dir, git_dir, 1);
    -+		if (!skip_prefix(worktree_dir.buf, common_dir.buf, &id) ||
    -+		    !skip_prefix(id, "/worktrees/", &id) ||
    -+		    !*id || strchr(id, '/'))
    -+			die(_("submodule git dir '%s' is not a worktree of '%s'"),
    -+			    git_dir, common_dir.buf);
    -+	}
    -+
    -+	ret = validate_submodule_git_dir(common_dir.buf, submodule_name);
    -+	if (!ret)
    -+		ret = is_worktree;
    -+	strbuf_release(&common_dir);
    ++	strbuf_realpath(&worktree_dir, git_dir, 1);
    ++	if (!skip_prefix(worktree_dir.buf, common_dir, &id) ||
    ++	    !skip_prefix(id, "/worktrees/", &id) ||
    ++	    !*id || strchr(id, '/'))
    ++		die(_("submodule git dir '%s' is not a worktree of '%s'"),
    ++		    git_dir, common_dir);
     +	strbuf_release(&worktree_dir);
    -+	return ret;
     +}
     +
    ++/* Returns 1 if the submodule at "path" is a linked worktree, 0 otherwise. */
    ++static int setup_submodule_git_dir(const struct submodule *sub,
    ++				   const char *path, const char *super_prefix,
    ++				   const char *old_head, unsigned flags)
    + {
    ++	int is_worktree = 0;
    ++
    + 	if (old_head) {
    ++		struct strbuf common_dir = STRBUF_INIT;
    + 		char *dotgit, *git_dir;
    + 
    + 		if (!submodule_uses_gitfile(path))
    +@@ submodule.c: static void setup_submodule_git_dir(const struct submodule *sub,
    + 		dotgit = xstrfmt("%s/.git", path);
    + 		git_dir = xstrdup(read_gitfile(dotgit));
    + 		free(dotgit);
    +-		if (validate_submodule_git_dir(git_dir, sub->name) < 0)
    ++
    ++		/* A linked worktree is validated through its common dir. */
    ++		is_worktree = get_common_dir_noenv(&common_dir, git_dir);
    ++		if (is_worktree)
    ++			validate_submodule_worktree_dir(git_dir, common_dir.buf);
    ++		if (validate_submodule_git_dir(common_dir.buf, sub->name) < 0)
    + 			die_refusing_submodule_git_dir(the_repository, git_dir);
    ++		strbuf_release(&common_dir);
    + 		free(git_dir);
    + 	} else {
    + 		struct strbuf gitdir = STRBUF_INIT;
    +@@ submodule.c: static void setup_submodule_git_dir(const struct submodule *sub,
    + 		submodule_reset_index(path, super_prefix);
    + 	}
    + 
    +-	if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
    ++	/*
    ++	 * Do not reconnect a linked worktree to the submodule's main git
    ++	 * dir: that would take over the git dir of another working tree.
    ++	 */
    ++	if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE) && !is_worktree) {
    + 		struct strbuf gitdir = STRBUF_INIT;
    + 		submodule_name_to_gitdir(&gitdir, the_repository, sub->name);
    + 		connect_work_tree_and_git_dir(path, gitdir.buf, 1);
    + 		strbuf_release(&gitdir);
    + 	}
    ++
    ++	return is_worktree;
    + }
    + 
      /**
    -  * Moves a submodule at a given path from a given head to another new head.
    -  * For edge cases (a submodule coming into existence or removing a submodule)
     @@ submodule.c: int submodule_move_head(const char *path, const char *super_prefix,
      	struct child_process cp = CHILD_PROCESS_INIT;
      	const struct submodule *sub;
    @@ submodule.c: int submodule_move_head(const char *path, const char *super_prefix,
      	if (!is_submodule_active(the_repository, path))
      		return 0;
     @@ submodule.c: int submodule_move_head(const char *path, const char *super_prefix,
    - 				char *git_dir = xstrdup(read_gitfile(dotgit));
    + 	}
      
    - 				free(dotgit);
    --				if (validate_submodule_git_dir(git_dir,
    --							       sub->name) < 0) {
    -+				is_worktree = validate_submodule_gitfile_dir(git_dir,
    -+									     sub->name);
    -+				if (is_worktree < 0) {
    - 					if (!the_repository->repository_format_submodule_path_cfg)
    - 						advise(_("enabling extensions.submodulePathConfig "
    - 							 "might fix the following error."));
    -@@ submodule.c: int submodule_move_head(const char *path, const char *super_prefix,
    - 			submodule_reset_index(path, super_prefix);
    - 		}
    + 	if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN))
    +-		setup_submodule_git_dir(sub, path, super_prefix, old_head, flags);
    ++		is_worktree = setup_submodule_git_dir(sub, path, super_prefix,
    ++						      old_head, flags);
    + 
    + 	prepare_submodule_repo_env(&cp.env);
      
    --		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
    -+		/*
    -+		 * Do not reconnect a linked worktree to the submodule's
    -+		 * main git dir: that would take over the git dir of another
    -+		 * working tree.
    -+		 */
    -+		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE) &&
    -+		    !is_worktree) {
    - 			struct strbuf gitdir = STRBUF_INIT;
    - 			submodule_name_to_gitdir(&gitdir, the_repository,
    - 						 sub->name);
     @@ submodule.c: int submodule_move_head(const char *path, const char *super_prefix,
      			if (is_empty_dir(path))
      				rmdir_or_warn(path);
    @@ t/t2405-worktree-submodule.sh: test_expect_success 'submodule is checked out aft
      	test_grep "file1 updated" out
      '
      
    ++# Helper to verify "sub" is still the same worktree, checked out at HEAD:sub.
    ++test_submodule_worktree_is_kept () {
    ++	test_cmp expect-gitfile submodule-worktree/sub/.git &&
    ++	git -C submodule-worktree rev-parse HEAD:sub >expect-head &&
    ++	git -C submodule-worktree/sub rev-parse HEAD >actual-head &&
    ++	test_cmp expect-head actual-head
    ++}
    ++
     +test_expect_success 'checkout --recurse-submodules accepts a submodule worktree' '
     +	git -C main worktree add "$base_path/submodule-worktree" "$rev1_hash_main" &&
     +	git -C main/sub worktree add "$base_path/submodule-worktree/sub" "$rev1_hash_sub" &&
     +	cat submodule-worktree/sub/.git >expect-gitfile &&
     +	# the worktree id must not end in the submodule name
     +	test_grep "/worktrees/sub1$" expect-gitfile &&
    -+	git -C main/sub rev-parse HEAD >expect-head-main &&
     +	git -C submodule-worktree checkout --recurse-submodules "$(git -C main rev-parse HEAD)" &&
    -+	git -C submodule-worktree rev-parse HEAD:sub >expect-head &&
    -+	git -C submodule-worktree/sub rev-parse HEAD >actual-head &&
    -+	test_cmp expect-head actual-head &&
    -+	cat submodule-worktree/sub/.git >actual-gitfile &&
    -+	test_cmp expect-gitfile actual-gitfile &&
    -+	git -C main/sub rev-parse HEAD >actual-head-main &&
    -+	test_cmp expect-head-main actual-head-main
    ++	test_submodule_worktree_is_kept
     +'
     +
     +test_expect_success 'checkout --recurse-submodules accepts a submodule worktree with submodulePathConfig' '
    @@ t/t2405-worktree-submodule.sh: test_expect_success 'submodule is checked out aft
     +	test_config -C main extensions.submodulePathConfig true &&
     +	test_config -C main submodule.sub.gitdir .git/modules/sub &&
     +	git -C submodule-worktree checkout --recurse-submodules "$rev1_hash_main" &&
    -+	git -C submodule-worktree/sub rev-parse --short HEAD >actual-head &&
    -+	echo "$rev1_hash_sub" >expect-head &&
    -+	test_cmp expect-head actual-head &&
    -+	cat submodule-worktree/sub/.git >actual-gitfile &&
    -+	test_cmp expect-gitfile actual-gitfile &&
    -+	git -C main/sub rev-parse HEAD >actual-head-main &&
    -+	test_cmp expect-head-main actual-head-main
    ++	test_submodule_worktree_is_kept
    ++'
    ++
    ++test_expect_success 'checkout --recurse-submodules accepts a submodule worktree with a nested git dir' '
    ++	# a nested .git directory makes the checkout absorb it first
    ++	git clone origin/sub submodule-worktree/sub/inner &&
    ++	git -C submodule-worktree/sub submodule add ./inner inner &&
    ++	git -C submodule-worktree checkout -f --recurse-submodules "$(git -C main rev-parse HEAD)" &&
    ++	test_submodule_worktree_is_kept
     +'
     +
     +test_expect_success 'checkout --recurse-submodules removes a submodule worktree' '
    @@ t/t2405-worktree-submodule.sh: test_expect_success 'submodule is checked out aft
     
      ## t/t7450-bad-git-dotfiles.sh ##
     @@ t/t7450-bad-git-dotfiles.sh: test_expect_success 'checkout --recurse-submodules rejects gitfile not matching
    - 	test_grep ! "BUG" err
    + 	test_cmp expect mismatched_absorbed/thing1/.git
      '
      
     +test_expect_success 'checkout --recurse-submodules rejects gitfile pointing to a forged worktree' '

base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
-- 
2.56.1
[PATCH v2 1/5] submodule: return an error instead of BUG() for an unexpected git dir
#3629
Author: Adrian Ratiu
Date: Mon, 23 Nov 2026 15:44
76 lines
12498 bytes
During a recursive checkout, Git reads a submodule's .git file to find
its git dir and checks that the path ends in the submodule's name, e.g.
".git/modules/sub" for a submodule called "sub".

Without extensions.submodulePathConfig, a path that does not end that
way triggers a BUG() and aborts the command:

  BUG: submodule.c:2414: submodule name 'sub' not a suffix of git dir '...'

This path comes from a file on disk, not from Git's own code, so an
unexpected value is a problem with the user's setup, not a bug in Git.
It can happen when the git dir under .git/modules has been renamed or
the submodule is a linked worktree (handled in a later commit).

When the check was added in a8dee3ca61 (Disallow dubiously-nested
submodule git directories, 2019-10-01), its callers only passed git
dirs computed by Git from the submodule name, so BUG() was correct.

9cf8547320 (clone: prevent clashing git dirs when cloning submodule in
parallel, 2024-01-28) added a caller that passes a path read from the
gitfile, which made the BUG() reachable.

Return an error instead of BUG(), so the command dies with the same
"refusing to create/use ..." message as when the submodulePathConfig
extension is enabled.

Reported-by: Andreas Wendleder <gonsolo@gmail.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 submodule.c                 |  4 ++--
 t/t7450-bad-git-dotfiles.sh | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/submodule.c b/submodule.c
index 1302c11ddf..6bdf1efd89 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2412,8 +2412,8 @@ static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodul
 
 	if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
 	    strcmp(p, submodule_name))
-		BUG("submodule name '%s' not a suffix of git dir '%s'",
-		    submodule_name, git_dir);
+		return error(_("submodule name '%s' not a suffix of git dir '%s'"),
+			     submodule_name, git_dir);
 
 	/*
 	 * We prevent the contents of sibling submodules' git directories to
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
index 72c7f6f73b..e17642fc16 100755
--- a/t/t7450-bad-git-dotfiles.sh
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -380,6 +380,20 @@ test_expect_success 'checkout -f --recurse-submodules must not use a nested gitd
 	test_path_is_missing nested_checkout/thing2/.git
 '
 
+test_expect_success 'checkout --recurse-submodules rejects gitfile not matching submodule name' '
+	git clone nested mismatched_gitdir &&
+	(
+		cd mismatched_gitdir &&
+		git submodule init thing1 &&
+		git submodule update thing1 &&
+		mv .git/modules/hippo .git/modules/elephant &&
+		echo "gitdir: ../.git/modules/elephant" >thing1/.git
+	) &&
+	test_must_fail git -C mismatched_gitdir checkout -f --recurse-submodules HEAD 2>err &&
+	test_grep "not a suffix of git dir" err &&
+	test_grep ! "BUG" err
+'
+
 test_expect_success SYMLINKS,!WINDOWS,!MINGW 'submodule must not checkout into different directory' '
 	test_when_finished "rm -rf sub repo bad-clone" &&
 
-- 
2.56.1
[PATCH v2 2/5] submodule: only suggest submodulePathConfig when it is disabled
#3630
Author: Adrian Ratiu
Date: Mon, 23 Nov 2026 15:44
143 lines
15228 bytes
When Git refuses to use a submodule's git dir, it suggests enabling
extensions.submodulePathConfig, sometimes even when the extension is
already enabled.

We know whether it is enabled, so only suggest it when it is not. Do
this in a common helper, so that all three places that refuse a git
dir give the same hint and error message.

Reported-by: Andreas Wendleder <gonsolo@gmail.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 builtin/submodule--helper.c                |  4 +---
 submodule.c                                | 23 ++++++++++++----------
 submodule.h                                |  7 +++++++
 t/t7425-submodule-gitdir-path-extension.sh | 22 +++++++++++++++++++++
 t/t7450-bad-git-dotfiles.sh                |  1 +
 5 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 461b682d0d..fa76d0a916 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -2011,9 +2011,7 @@ static int clone_submodule(const struct module_clone_data *clone_data,
 		char *head = xstrfmt("%s/HEAD", sm_gitdir);
 		unlink(head);
 		free(head);
-		die(_("refusing to create/use '%s' in another submodule's git dir. "
-		      "Enabling extensions.submodulePathConfig should fix this."),
-		    sm_gitdir);
+		die_refusing_submodule_git_dir(the_repository, sm_gitdir);
 	}
 
 	connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
diff --git a/submodule.c b/submodule.c
index 6bdf1efd89..c9dd7eae06 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2172,10 +2172,8 @@ int submodule_move_head(const char *path, const char *super_prefix,
 				free(dotgit);
 				if (validate_submodule_git_dir(git_dir,
 							       sub->name) < 0)
-					die(_("refusing to create/use '%s' in "
-					      "another submodule's git dir. "
-					      "Enabling extensions.submodulePathConfig "
-					      "should fix this."), git_dir);
+					die_refusing_submodule_git_dir(the_repository,
+								       git_dir);
 				free(git_dir);
 			}
 		} else {
@@ -2735,6 +2733,15 @@ int submodule_to_gitdir(struct repository *repo,
 	return ret;
 }
 
+void die_refusing_submodule_git_dir(struct repository *r, const char *git_dir)
+{
+	if (!r->repository_format_submodule_path_cfg)
+		advise(_("enabling extensions.submodulePathConfig might fix the "
+			 "following error."));
+
+	die(_("refusing to create/use '%s' in another submodule's git dir."), git_dir);
+}
+
 void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
 			      const char *submodule_name)
 {
@@ -2765,10 +2772,6 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
 	}
 
 	/* validate because users might have modified the config */
-	if (validate_submodule_git_dir(buf->buf, submodule_name)) {
-		advise(_("enabling extensions.submodulePathConfig might fix the "
-			 "following error, if it's not already enabled."));
-		die(_("refusing to create/use '%s' in another submodule's "
-		      " git dir."), buf->buf);
-	}
+	if (validate_submodule_git_dir(buf->buf, submodule_name))
+		die_refusing_submodule_git_dir(r, buf->buf);
 }
diff --git a/submodule.h b/submodule.h
index b10e16e6c0..3a5e4cd9ec 100644
--- a/submodule.h
+++ b/submodule.h
@@ -142,6 +142,13 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
  */
 int validate_submodule_git_dir(char *git_dir, const char *submodule_name);
 
+/*
+ * Die because "git_dir" failed validate_submodule_git_dir(), suggesting to
+ * enable extensions.submodulePathConfig if it is disabled in "r".
+ */
+NORETURN void die_refusing_submodule_git_dir(struct repository *r,
+					     const char *git_dir);
+
 /*
  * Make sure that the given submodule path does not follow symlinks.
  */
diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh
index ea86ecf7ee..1455dad017 100755
--- a/t/t7425-submodule-gitdir-path-extension.sh
+++ b/t/t7425-submodule-gitdir-path-extension.sh
@@ -525,4 +525,26 @@ test_expect_success 'submodule gitdir conflicts with previously encoded name (lo
 	)
 '
 
+test_expect_success 'rejected submodule gitfile does not suggest enabling the extension' '
+	git clone -c extensions.submodulePathConfig=true --recurse-submodules nested clone_moved_gitdir &&
+	(
+		cd clone_moved_gitdir &&
+		mkdir .git/modules/zoo &&
+		mv .git/modules/hippo .git/modules/zoo/hippo &&
+		git config set -f .git/modules/zoo/hippo/config core.worktree ../../../../thing1 &&
+		echo "gitdir: ../.git/modules/zoo/hippo" >thing1/.git
+	) &&
+	test_must_fail git -C clone_moved_gitdir checkout -f --recurse-submodules HEAD 2>err &&
+	test_grep "refusing to create/use" err &&
+	test_grep ! "extensions.submodulePathConfig" err
+'
+
+test_expect_success 'rejected submodule gitdir config does not suggest enabling the extension' '
+	git clone -c extensions.submodulePathConfig=true nested clone_bad_gitdir_config &&
+	git -C clone_bad_gitdir_config config set submodule.hippo.gitdir .git/modules/zoo/hippo &&
+	test_must_fail git -C clone_bad_gitdir_config submodule--helper gitdir hippo 2>err &&
+	test_grep "refusing to create/use" err &&
+	test_grep ! "extensions.submodulePathConfig" err
+'
+
 test_done
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
index e17642fc16..a710c05a5d 100755
--- a/t/t7450-bad-git-dotfiles.sh
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -391,6 +391,7 @@ test_expect_success 'checkout --recurse-submodules rejects gitfile not matching
 	) &&
 	test_must_fail git -C mismatched_gitdir checkout -f --recurse-submodules HEAD 2>err &&
 	test_grep "not a suffix of git dir" err &&
+	test_grep "enabling extensions.submodulePathConfig" err &&
 	test_grep ! "BUG" err
 '
 
-- 
2.56.1
[PATCH v2 3/5] submodule: move git dir setup out of submodule_move_head()
#3627
Author: Adrian Ratiu
Date: Mon, 23 Nov 2026 15:44
107 lines
12962 bytes
Before running "read-tree" in a submodule, submodule_move_head()
ensures that its .git is a gitfile pointing to the right git dir.

This code is nested four levels deep, which makes it hard to read
and to extend its logic. Move it to a separate helper.

This is just cosmetic cleanup. No functional change is intended.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 submodule.c | 72 +++++++++++++++++++++++++++--------------------------
 1 file changed, 37 insertions(+), 35 deletions(-)

diff --git a/submodule.c b/submodule.c
index c9dd7eae06..8aae789e98 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2119,6 +2119,41 @@ static void submodule_reset_index(const char *path, const char *super_prefix)
 		die(_("could not reset submodule index"));
 }
 
+static void setup_submodule_git_dir(const struct submodule *sub,
+				    const char *path, const char *super_prefix,
+				    const char *old_head, unsigned flags)
+{
+	if (old_head) {
+		if (!submodule_uses_gitfile(path))
+			absorb_git_dir_into_superproject(path, super_prefix);
+		else {
+			char *dotgit = xstrfmt("%s/.git", path);
+			char *git_dir = xstrdup(read_gitfile(dotgit));
+
+			free(dotgit);
+			if (validate_submodule_git_dir(git_dir, sub->name) < 0)
+				die_refusing_submodule_git_dir(the_repository,
+							       git_dir);
+			free(git_dir);
+		}
+	} else {
+		struct strbuf gitdir = STRBUF_INIT;
+		submodule_name_to_gitdir(&gitdir, the_repository, sub->name);
+		connect_work_tree_and_git_dir(path, gitdir.buf, 0);
+		strbuf_release(&gitdir);
+
+		/* make sure the index is clean as well */
+		submodule_reset_index(path, super_prefix);
+	}
+
+	if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
+		struct strbuf gitdir = STRBUF_INIT;
+		submodule_name_to_gitdir(&gitdir, the_repository, sub->name);
+		connect_work_tree_and_git_dir(path, gitdir.buf, 1);
+		strbuf_release(&gitdir);
+	}
+}
+
 /**
  * Moves a submodule at a given path from a given head to another new head.
  * For edge cases (a submodule coming into existence or removing a submodule)
@@ -2160,41 +2195,8 @@ int submodule_move_head(const char *path, const char *super_prefix,
 			return error(_("submodule '%s' has dirty index"), path);
 	}
 
-	if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
-		if (old_head) {
-			if (!submodule_uses_gitfile(path))
-				absorb_git_dir_into_superproject(path,
-								 super_prefix);
-			else {
-				char *dotgit = xstrfmt("%s/.git", path);
-				char *git_dir = xstrdup(read_gitfile(dotgit));
-
-				free(dotgit);
-				if (validate_submodule_git_dir(git_dir,
-							       sub->name) < 0)
-					die_refusing_submodule_git_dir(the_repository,
-								       git_dir);
-				free(git_dir);
-			}
-		} else {
-			struct strbuf gitdir = STRBUF_INIT;
-			submodule_name_to_gitdir(&gitdir, the_repository,
-						 sub->name);
-			connect_work_tree_and_git_dir(path, gitdir.buf, 0);
-			strbuf_release(&gitdir);
-
-			/* make sure the index is clean as well */
-			submodule_reset_index(path, super_prefix);
-		}
-
-		if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
-			struct strbuf gitdir = STRBUF_INIT;
-			submodule_name_to_gitdir(&gitdir, the_repository,
-						 sub->name);
-			connect_work_tree_and_git_dir(path, gitdir.buf, 1);
-			strbuf_release(&gitdir);
-		}
-	}
+	if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN))
+		setup_submodule_git_dir(sub, path, super_prefix, old_head, flags);
 
 	prepare_submodule_repo_env(&cp.env);
 
-- 
2.56.1
[PATCH v2 4/5] submodule: validate the git dir after absorbing it
#3632
Author: Adrian Ratiu
Date: Mon, 23 Nov 2026 15:44
82 lines
12471 bytes
When a submodule or one of its nested submodules has a .git directory,
submodule_move_head() absorbs it, then points the submodule .git file
to "$GIT_DIR/modules/<name>" without validating the git dir.

Absorbing only moves .git directories, so a submodule that already used
a gitfile keeps pointing to its old git dir, valid or not.

Validate it after absorbing too, so that Git refuses a bad one instead
of silently replacing it.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 submodule.c                 | 20 ++++++++++----------
 t/t7450-bad-git-dotfiles.sh | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/submodule.c b/submodule.c
index 8aae789e98..9eae297c8a 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2124,18 +2124,18 @@ static void setup_submodule_git_dir(const struct submodule *sub,
 				    const char *old_head, unsigned flags)
 {
 	if (old_head) {
+		char *dotgit, *git_dir;
+
 		if (!submodule_uses_gitfile(path))
 			absorb_git_dir_into_superproject(path, super_prefix);
-		else {
-			char *dotgit = xstrfmt("%s/.git", path);
-			char *git_dir = xstrdup(read_gitfile(dotgit));
-
-			free(dotgit);
-			if (validate_submodule_git_dir(git_dir, sub->name) < 0)
-				die_refusing_submodule_git_dir(the_repository,
-							       git_dir);
-			free(git_dir);
-		}
+
+		/* Now path/.git is a gitfile, so validate it. */
+		dotgit = xstrfmt("%s/.git", path);
+		git_dir = xstrdup(read_gitfile(dotgit));
+		free(dotgit);
+		if (validate_submodule_git_dir(git_dir, sub->name) < 0)
+			die_refusing_submodule_git_dir(the_repository, git_dir);
+		free(git_dir);
 	} else {
 		struct strbuf gitdir = STRBUF_INIT;
 		submodule_name_to_gitdir(&gitdir, the_repository, sub->name);
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
index a710c05a5d..72ef1af661 100755
--- a/t/t7450-bad-git-dotfiles.sh
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -395,6 +395,24 @@ test_expect_success 'checkout --recurse-submodules rejects gitfile not matching
 	test_grep ! "BUG" err
 '
 
+test_expect_success 'checkout --recurse-submodules rejects gitfile not matching submodule name after absorbing' '
+	git clone nested mismatched_absorbed &&
+	(
+		cd mismatched_absorbed &&
+		git submodule init thing1 &&
+		git submodule update thing1 &&
+		mv .git/modules/hippo .git/modules/elephant &&
+		echo "gitdir: ../.git/modules/elephant" >thing1/.git &&
+		# a nested .git directory makes the checkout absorb it first
+		git clone ../nested thing1/inner &&
+		git -C thing1 submodule add ./inner inner
+	) &&
+	echo "gitdir: ../.git/modules/elephant" >expect &&
+	test_must_fail git -C mismatched_absorbed checkout -f --recurse-submodules HEAD 2>err &&
+	test_grep "not a suffix of git dir" err &&
+	test_cmp expect mismatched_absorbed/thing1/.git
+'
+
 test_expect_success SYMLINKS,!WINDOWS,!MINGW 'submodule must not checkout into different directory' '
 	test_when_finished "rm -rf sub repo bad-clone" &&
 
-- 
2.56.1
[PATCH v2 5/5] submodule: accept a linked worktree as the submodule's git dir
#3631
Author: Adrian Ratiu
Date: Mon, 23 Nov 2026 15:45
235 lines
19039 bytes
A submodule can be checked out as a linked worktree of its repository,
created e.g. with:

  git -C super/sub worktree add ../../wt/sub

Its .git file then points to "super/.git/modules/sub/worktrees/<id>"
instead of "super/.git/modules/sub".

t2405 already creates such a submodule, but it doesn't test a recursive
checkout and its worktree id happens to match the submodule name.

A recursive checkout (--recurse-submodules or submodule.recurse=true)
has two problems with this setup:

 1. It checks that the git dir ends in the submodule's name. A
    worktree's git dir ends in the worktree id instead, so the check
    fails and the command dies. It only passes by chance, when the id
    is the same as the name.

 2. Even when the check passes, Git then rewrites the .git file to
    point to the git dir it would normally use for the submodule,
    "$GIT_DIR/modules/<name>" of the superproject. That is not the
    repository the worktree belongs to and it usually does not exist,
    so the submodule is left broken. Likewise, when the submodule is
    removed, Git tries to unset core.worktree in that git dir and
    prints an error and a warning.

    This happens on every recursive checkout, not only with --force,
    because unpack_trees() always passes SUBMODULE_MOVE_HEAD_FORCE to
    submodule_move_head().

Fix both: when the git dir is a linked worktree of a valid submodule
git dir, i.e. it is located at "<common-dir>/worktrees/<id>", check
the common dir against the submodule's name and leave the .git file
and core.worktree alone. Reject a git dir that has a commondir file but
is located anywhere else.

Reported-by: Andreas Wendleder <gonsolo@gmail.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 submodule.c                   | 52 ++++++++++++++++++++++++++++++-----
 t/t2405-worktree-submodule.sh | 43 +++++++++++++++++++++++++++++
 t/t7450-bad-git-dotfiles.sh   | 16 +++++++++++
 3 files changed, 104 insertions(+), 7 deletions(-)

diff --git a/submodule.c b/submodule.c
index 9eae297c8a..8ed79da14f 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2119,11 +2119,34 @@ static void submodule_reset_index(const char *path, const char *super_prefix)
 		die(_("could not reset submodule index"));
 }
 
-static void setup_submodule_git_dir(const struct submodule *sub,
-				    const char *path, const char *super_prefix,
-				    const char *old_head, unsigned flags)
+/*
+ * A linked worktree's git dir is "<common-dir>/worktrees/<id>", which is
+ * where "git worktree add" puts it. Die if it is anywhere else.
+ */
+static void validate_submodule_worktree_dir(const char *git_dir,
+					    const char *common_dir)
+{
+	struct strbuf worktree_dir = STRBUF_INIT;
+	const char *id;
+
+	strbuf_realpath(&worktree_dir, git_dir, 1);
+	if (!skip_prefix(worktree_dir.buf, common_dir, &id) ||
+	    !skip_prefix(id, "/worktrees/", &id) ||
+	    !*id || strchr(id, '/'))
+		die(_("submodule git dir '%s' is not a worktree of '%s'"),
+		    git_dir, common_dir);
+	strbuf_release(&worktree_dir);
+}
+
+/* Returns 1 if the submodule at "path" is a linked worktree, 0 otherwise. */
+static int setup_submodule_git_dir(const struct submodule *sub,
+				   const char *path, const char *super_prefix,
+				   const char *old_head, unsigned flags)
 {
+	int is_worktree = 0;
+
 	if (old_head) {
+		struct strbuf common_dir = STRBUF_INIT;
 		char *dotgit, *git_dir;
 
 		if (!submodule_uses_gitfile(path))
@@ -2133,8 +2156,14 @@ static void setup_submodule_git_dir(const struct submodule *sub,
 		dotgit = xstrfmt("%s/.git", path);
 		git_dir = xstrdup(read_gitfile(dotgit));
 		free(dotgit);
-		if (validate_submodule_git_dir(git_dir, sub->name) < 0)
+
+		/* A linked worktree is validated through its common dir. */
+		is_worktree = get_common_dir_noenv(&common_dir, git_dir);
+		if (is_worktree)
+			validate_submodule_worktree_dir(git_dir, common_dir.buf);
+		if (validate_submodule_git_dir(common_dir.buf, sub->name) < 0)
 			die_refusing_submodule_git_dir(the_repository, git_dir);
+		strbuf_release(&common_dir);
 		free(git_dir);
 	} else {
 		struct strbuf gitdir = STRBUF_INIT;
@@ -2146,12 +2175,18 @@ static void setup_submodule_git_dir(const struct submodule *sub,
 		submodule_reset_index(path, super_prefix);
 	}
 
-	if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
+	/*
+	 * Do not reconnect a linked worktree to the submodule's main git
+	 * dir: that would take over the git dir of another working tree.
+	 */
+	if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE) && !is_worktree) {
 		struct strbuf gitdir = STRBUF_INIT;
 		submodule_name_to_gitdir(&gitdir, the_repository, sub->name);
 		connect_work_tree_and_git_dir(path, gitdir.buf, 1);
 		strbuf_release(&gitdir);
 	}
+
+	return is_worktree;
 }
 
 /**
@@ -2167,6 +2202,7 @@ int submodule_move_head(const char *path, const char *super_prefix,
 	struct child_process cp = CHILD_PROCESS_INIT;
 	const struct submodule *sub;
 	int *error_code_ptr, error_code;
+	int is_worktree = 0;
 
 	if (!is_submodule_active(the_repository, path))
 		return 0;
@@ -2196,7 +2232,8 @@ int submodule_move_head(const char *path, const char *super_prefix,
 	}
 
 	if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN))
-		setup_submodule_git_dir(sub, path, super_prefix, old_head, flags);
+		is_worktree = setup_submodule_git_dir(sub, path, super_prefix,
+						      old_head, flags);
 
 	prepare_submodule_repo_env(&cp.env);
 
@@ -2254,7 +2291,8 @@ int submodule_move_head(const char *path, const char *super_prefix,
 			if (is_empty_dir(path))
 				rmdir_or_warn(path);
 
-			submodule_unset_core_worktree(sub);
+			if (!is_worktree)
+				submodule_unset_core_worktree(sub);
 		}
 	}
 out:
diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh
index f5c94d490b..f23c8c8abf 100755
--- a/t/t2405-worktree-submodule.sh
+++ b/t/t2405-worktree-submodule.sh
@@ -59,6 +59,49 @@ test_expect_success 'submodule is checked out after manually adding submodule wo
 	test_grep "file1 updated" out
 '
 
+# Helper to verify "sub" is still the same worktree, checked out at HEAD:sub.
+test_submodule_worktree_is_kept () {
+	test_cmp expect-gitfile submodule-worktree/sub/.git &&
+	git -C submodule-worktree rev-parse HEAD:sub >expect-head &&
+	git -C submodule-worktree/sub rev-parse HEAD >actual-head &&
+	test_cmp expect-head actual-head
+}
+
+test_expect_success 'checkout --recurse-submodules accepts a submodule worktree' '
+	git -C main worktree add "$base_path/submodule-worktree" "$rev1_hash_main" &&
+	git -C main/sub worktree add "$base_path/submodule-worktree/sub" "$rev1_hash_sub" &&
+	cat submodule-worktree/sub/.git >expect-gitfile &&
+	# the worktree id must not end in the submodule name
+	test_grep "/worktrees/sub1$" expect-gitfile &&
+	git -C submodule-worktree checkout --recurse-submodules "$(git -C main rev-parse HEAD)" &&
+	test_submodule_worktree_is_kept
+'
+
+test_expect_success 'checkout --recurse-submodules accepts a submodule worktree with submodulePathConfig' '
+	test_config -C main core.repositoryformatversion 1 &&
+	test_config -C main extensions.submodulePathConfig true &&
+	test_config -C main submodule.sub.gitdir .git/modules/sub &&
+	git -C submodule-worktree checkout --recurse-submodules "$rev1_hash_main" &&
+	test_submodule_worktree_is_kept
+'
+
+test_expect_success 'checkout --recurse-submodules accepts a submodule worktree with a nested git dir' '
+	# a nested .git directory makes the checkout absorb it first
+	git clone origin/sub submodule-worktree/sub/inner &&
+	git -C submodule-worktree/sub submodule add ./inner inner &&
+	git -C submodule-worktree checkout -f --recurse-submodules "$(git -C main rev-parse HEAD)" &&
+	test_submodule_worktree_is_kept
+'
+
+test_expect_success 'checkout --recurse-submodules removes a submodule worktree' '
+	git -C main/sub config get core.worktree >expect-main &&
+	git -C submodule-worktree checkout --recurse-submodules first 2>err &&
+	test_grep ! "core.worktree" err &&
+	test_path_is_missing submodule-worktree/sub/.git &&
+	git -C main/sub config get core.worktree >actual-main &&
+	test_cmp expect-main actual-main
+'
+
 test_expect_success 'checkout --recurse-submodules uses $GIT_DIR for submodules in a linked worktree' '
 	git -C main worktree add "$base_path/checkout-recurse" --detach  &&
 	git -C checkout-recurse submodule update --init &&
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
index 72ef1af661..717631a2ee 100755
--- a/t/t7450-bad-git-dotfiles.sh
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -413,6 +413,22 @@ test_expect_success 'checkout --recurse-submodules rejects gitfile not matching
 	test_cmp expect mismatched_absorbed/thing1/.git
 '
 
+test_expect_success 'checkout --recurse-submodules rejects gitfile pointing to a forged worktree' '
+	git clone nested forged_worktree &&
+	(
+		cd forged_worktree &&
+		git submodule init thing1 &&
+		git submodule update thing1 &&
+		mkdir .git/modules/forged &&
+		cp .git/modules/hippo/HEAD .git/modules/forged/HEAD &&
+		echo "../hippo" >.git/modules/forged/commondir &&
+		echo "gitdir: ../.git/modules/forged" >thing1/.git
+	) &&
+	test_must_fail git -C forged_worktree checkout -f --recurse-submodules HEAD 2>err &&
+	test_grep "is not a worktree of" err &&
+	test_grep ! "extensions.submodulePathConfig" err
+'
+
 test_expect_success SYMLINKS,!WINDOWS,!MINGW 'submodule must not checkout into different directory' '
 	test_when_finished "rm -rf sub repo bad-clone" &&
 
-- 
2.56.1
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads