threads / patch / 66954

patch, 2 partssubmodule: fix handling of unusual submodule names

Subject: [PATCH 0/2] submodule: fix handling of unusual submodule names

## tl;dr

One patch stops deinit from leaving config sections behind for names containing regexp characters, the other rejects "." as a name. Read the story.

replies: 3people: 2as markdown or json

Yoichi NAKAYAMA via GitGitGadget· Nov 29, 2026, 22:02 UTC · lore
This series fixes issues with unusual submodule names.

The first patch fixes 'git submodule deinit' failing to remove the configuration section for submodules whose names contain regexp metacharacters.

The second patch rejects "." as a submodule name in 'git submodule add'. This avoids a confusing error and prevents an invalid submodule...gitdir entry from being written to the configuration when extensions.submodulePathConfig is enabled.

 * [1/2] submodule deinit: handle regexp metacharacters in names
 * [2/2] submodule add: reject "." as submodule name
Yoichi NAKAYAMA (2):
  submodule deinit: handle regexp metacharacters in names
  submodule add: reject "." as submodule name
 builtin/submodule--helper.c | 26 +++++++++++++++++++++++---
 submodule-config.c          |  4 ++--
 t/t7400-submodule-basic.sh  | 28 ++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 5 deletions(-)
base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2340%2Fyoichi%2Fhandle-unusual-submodule-name-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2340/yoichi/handle-unusual-submodule-name-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2340
-- 
gitgitgadget
Yoichi NAKAYAMA via GitGitGadget· Nov 29, 2026, 22:02 UTC · re: Yoichi NAKAYAMA via GitGitGadget · lore

[PATCH 1/2] submodule deinit: handle regexp metacharacters in names

From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>

'git submodule deinit' internally calls 'git config --get-regexp' to determine whether the configuration section for the submodule should be removed. Since the submodule name is included directly in the regular expression, names containing regexp metacharacters may cause the `submodule.<name>` section to remain in the configuration after 'git submodule deinit'.

Change the arguments passed to the internal 'git config' call to retrieve matching configuration keys without including the submodule name in the regular expression.

Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
 builtin/submodule--helper.c | 26 +++++++++++++++++++++++---
 t/t7400-submodule-basic.sh  | 16 ++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)
Show changes to 2 files +39 −3

builtin/submodule--helper.c, t/t7400-submodule-basic.sh

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 461b682d0d..a09afd6630 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1589,6 +1589,8 @@ static void deinit_submodule(const char *path, const char *prefix,
 	struct child_process cp_config = CHILD_PROCESS_INIT;
 	struct strbuf sb_config = STRBUF_INIT;
 	char *sub_git_dir = xstrfmt("%s/.git", path);
+	struct strbuf **names, **ptr;
+	bool found = false;
 
 	if (validate_submodule_path(path) < 0)
 		die(NULL);
@@ -1649,11 +1651,29 @@ static void deinit_submodule(const char *path, const char *prefix,
 		      displaypath);
 
 	cp_config.git_cmd = 1;
-	strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
-	strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
+	strvec_pushl(&cp_config.args, "config", "get", "--all", "--show-names",
+		     "--name-only", "-z", "--regexp", "submodule\\.", NULL);
 
 	/* remove the .git/config entries (unless the user already did it) */
-	if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
+	if (capture_command(&cp_config, &sb_config, 0) || !sb_config.len)
+		goto cleanup;
+
+	names = strbuf_split_buf(sb_config.buf, sb_config.len - 1, '\0', 0);
+	for (ptr = names; *ptr; ptr++) {
+		const char *subsection, *key;
+		size_t subsection_len;
+
+		if (!parse_config_key((*ptr)->buf, "submodule",
+				      &subsection, &subsection_len, &key) &&
+		    subsection_len == strlen(sub->name) &&
+		    !strncmp(subsection, sub->name, subsection_len)) {
+			found = true;
+			break;
+		}
+	}
+	strbuf_list_free(names);
+
+	if (found) {
 		char *sub_key = xstrfmt("submodule.%s", sub->name);
 
 		/*
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 345c257829..2b3aff4cb1 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -1283,6 +1283,22 @@ test_expect_success 'submodule deinit absorbs .git directory if .git is a direct
 	test -z "$(git config --get-regexp "submodule\.example\.")"
 '
 
+test_expect_success 'deinit with regexp metacharacters in submodule name' '
+	test_when_finished "rm -rf submodule-name-with-metachar" &&
+	git init submodule-name-with-metachar &&
+	(
+		cd submodule-name-with-metachar &&
+		git submodule add --name "(" "$submodurl" paren &&
+		git submodule add --name "[" "$submodurl" bra &&
+		git submodule add --name "^" "$submodurl" hat &&
+		git submodule add --name "$" "$submodurl" dollar &&
+		git commit -m "add submodules" &&
+		git submodule deinit --all &&
+		(git config --get-regexp "submodule\." || true) >config &&
+		test_must_be_empty config
+	)
+'
+
 test_expect_success 'submodule with UTF-8 name' '
 	svname=$(printf "\303\245 \303\244\303\266") &&
 	mkdir "$svname" &&
-- 
gitgitgadget
Yoichi NAKAYAMA via GitGitGadget· Nov 29, 2026, 22:02 UTC · re: Yoichi NAKAYAMA via GitGitGadget · lore

[PATCH 2/2] submodule add: reject "." as submodule name

From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Specifying "." as the submodule name for 'git submodule add' causes
the command to fail with a confusing error:
    fatal: not a git repository: '.git/modules/.'

Additionally, when `extensions.submodulePathConfig` is set to true, a `submodule...gitdir` entry is written to the configuration.

Reject "." as a submodule name early in 'git submodule add' to avoid these problems.

Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
 submodule-config.c         |  4 ++--
 t/t7400-submodule-basic.sh | 12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)
Show changes to 2 files +14 −2

submodule-config.c, t/t7400-submodule-basic.sh

diff --git a/submodule-config.c b/submodule-config.c
index 37c3be377b..2f4cff0a78 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -214,8 +214,8 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
 
 int check_submodule_name(const char *name)
 {
-	/* Disallow empty names */
-	if (!*name)
+	/* Disallow empty names and "." */
+	if (!*name || !strcmp(name, "."))
 		return -1;
 
 	/*
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 2b3aff4cb1..de474d94b8 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -1122,6 +1122,18 @@ test_expect_success 'submodule add with an existing name fails unless forced' '
 	)
 '
 
+test_expect_success 'submodule add with invalid name fails' '
+	test_when_finished "rm -rf invalid-submodule-name" &&
+	git init invalid-submodule-name &&
+	(
+		cd invalid-submodule-name &&
+		test_must_fail git submodule add --name "." "$submodurl" dot 2>err &&
+		test_grep "not a valid submodule name" err &&
+		(git config --get-regexp "submodule\." || true) >config &&
+		test_must_be_empty config
+	)
+'
+
 test_expect_success 'set up a second submodule' '
 	git submodule add ./init2 example2 &&
 	git commit -m "submodule example2 added"
-- 
gitgitgadget
Ben Knoble· Nov 30, 2026, 13:42 UTC · re: Yoichi NAKAYAMA via GitGitGadget · lore

Re: [PATCH 0/2] submodule: fix handling of unusual submodule names

Show 24 quoted lines
> Le 29 nov. 2026 à 17:02, Yoichi NAKAYAMA via GitGitGadget <gitgitgadget@gmail.com> a écrit :
> 
> This series fixes issues with unusual submodule names.
> 
> The first patch fixes 'git submodule deinit' failing to remove the
> configuration section for submodules whose names contain regexp
> metacharacters.
> 
> The second patch rejects "." as a submodule name in 'git submodule add'.
> This avoids a confusing error and prevents an invalid submodule...gitdir
> entry from being written to the configuration when
> extensions.submodulePathConfig is enabled.
> 
> * [1/2] submodule deinit: handle regexp metacharacters in names
> * [2/2] submodule add: reject "." as submodule name
> 
> Yoichi NAKAYAMA (2):
>  submodule deinit: handle regexp metacharacters in names
>  submodule add: reject "." as submodule name
> 
> builtin/submodule--helper.c | 26 +++++++++++++++++++++++---
> submodule-config.c          |  4 ++--
> t/t7400-submodule-basic.sh  | 28 ++++++++++++++++++++++++++++
> 3 files changed, 53 insertions(+), 5 deletions(-)

I gave this series a quick read, and the comments + code seem like the right direction to me. A more detailed review is probably warranted, but I figured a positive ack was better than nothing.

← back to recent threads