git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] submodule: return an error instead of BUG() for an unexpected git dir

From
Adrian Ratiu <adrian.ratiu@collabora.com>
Date
Nov 18, 2026, 16:32 UTC
Message-ID
<20261118163249.1693361-2-adrian.ratiu@collabora.com>
In-Reply-To
<20261118163249.1693361-1-adrian.ratiu@collabora.com>

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
Previous: Adrian RatiuNext: Junio C Hamano
Message 4 of 13 in “submodule: linked-worktree submodule hits BUG() in validate_submodule_legacy_git_dir()”
  1. Andreas WendlederNov 16, 2026
  2. Adrian RatiuNov 17, 2026
  3. 0/3 submodule: support linked worktrees in recursive checkoutAdrian Ratiu, Nov 18, 2026
  4. 1/3 submodule: return an error instead of BUG() for an unexpected git dirAdrian Ratiu, Nov 18, 2026
  5. Junio C HamanoNov 18, 2026
  6. Adrian RatiuNov 18, 2026
  7. 2/3 submodule: only suggest submodulePathConfig when it is disabledAdrian Ratiu, Nov 18, 2026
  8. Junio C HamanoNov 18, 2026
  9. Adrian RatiuNov 19, 2026
  10. Junio C HamanoNov 19, 2026
  11. 3/3 submodule: accept a linked worktree as the submodule's git dirAdrian Ratiu, Nov 18, 2026
  12. Junio C HamanoNov 18, 2026
  13. Adrian RatiuNov 19, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.