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

[PATCH 2/2] submodule add: Fail when .git/modules/<name> already exists

From
Jens Lehmann <jens.lehmann@web.de>
Date
Sep 29, 2012, 23:07 UTC
Message-ID
<50677F2A.6060807@web.de>
In-Reply-To
<50677E76.1050204@web.de>

When adding a new submodule it can happen that .git/modules/<name> already contains a submodule repo, e.g. when a submodule is removed from the work tree and another submodule is added at the same path. But then the work tree of the submodule will be populated using the existing repository and not the one the user provided.

Error out in that case and tell the user she should use a different name for the submodule with the "--name" option to avoid this problem. In one test in t7406 the --name option had to be added to "git submodule add", as that test re-adds a formerly removed submodule.

Reported-by: Jonathan Johnson <me@jondavidjohn.com>
Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 git-submodule.sh            |  3 ++-
 t/t7400-submodule-basic.sh  | 18 ++++++++++++++++++
 t/t7406-submodule-update.sh |  2 +-
 3 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index 22febb1..58cd053 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -359,7 +359,8 @@ Use -f if you really want to add it." >&2
 		fi

 	else
-
+		test ! -d ".git/modules/$sm_name" ||
+		die "$(eval_gettext "Submodule name '\$sm_name' is already used. Please choose another name with the '--name' option.")"
 		module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
 		(
 			clear_local_git_env
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 78bf739..a031a27 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -726,4 +726,22 @@ test_expect_success 'submodule add --name allows to replace a submodule with ano
 	)
 '

+test_expect_success 'submodule add with an existing name fails' '
+	(
+		cd addtest2 &&
+		rm -rf repo &&
+		test_must_fail git submodule add -q --name repo_new "$submodurl/bare.git" repo &&
+		test ! -d repo &&
+		echo "repo" >expect &&
+		git config -f .gitmodules submodule.repo_new.path >actual &&
+		test_cmp expect actual&&
+		echo "$submodurl/bare.git" >expect &&
+		git config -f .gitmodules submodule.repo_new.url >actual &&
+		test_cmp expect actual &&
+		echo "$submodurl/bare.git" >expect &&
+		git config submodule.repo_new.url >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index 1542653..2d44c51 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -627,7 +627,7 @@ test_expect_success 'submodule add properly re-creates deeper level submodules'
 	(cd super &&
 	 git reset --hard master &&
 	 rm -rf deeper/ &&
-	 git submodule add ../submodule deeper/submodule
+	 git submodule add --name deeper/submodule2 ../submodule deeper/submodule
 	)
 '
-- 
1.7.12.1.430.g4fd6dc4
Previous: Jens LehmannNext: Junio C Hamano
Message 5 of 9 in “Bug in Submodule add”
  1. Jonathan JohnsonSep 26, 2012
  2. Jens LehmannSep 26, 2012
  3. 0/2 Let "git submodule add" fail when .git/modules/<name> already existsJens Lehmann, Sep 29, 2012
  4. 1/2 Teach "git submodule add" the --name optionJens Lehmann, Sep 29, 2012
  5. 2/2 submodule add: Fail when .git/modules/<name> already existsJens Lehmann, Sep 29, 2012
  6. Junio C HamanoSep 30, 2012
  7. Jens LehmannSep 30, 2012
  8. 2/2 submodule add: Fail when .git/modules/<name> already exists unless forcedJens Lehmann, Sep 30, 2012
  9. Junio C HamanoOct 1, 2012

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.