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

[PATCH 1/2] submodule add: extend force flag to add existing repos

From
Stefan Beller <sbeller@google.com>
Date
Oct 6, 2016, 19:37 UTC
Message-ID
<20161006193725.31553-2-sbeller@google.com>
In-Reply-To
<20161006193725.31553-1-sbeller@google.com>

Currently the force flag in `git submodule add` takes care of possibly ignored files or when a name collision occurs.

However there is another situation where submodule add comes in handy: When you already have a gitlink recorded, but no configuration was done (i.e. no .gitmodules file nor any entry in .git/config) and you want to generate these config entries. For this situation allow `git submodule add` to proceed if there is already a submodule at the given path in the index.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 git-submodule.sh           | 10 ++++++++--
 t/t7400-submodule-basic.sh | 14 ++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index a024a13..3762616 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -207,8 +207,14 @@ cmd_add()
 			tstart
 			s|/*$||
 		')
-	git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
-	die "$(eval_gettext "'\$sm_path' already exists in the index")"
+	if test -z "$force"
+	then
+		git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
+		die "$(eval_gettext "'\$sm_path' already exists in the index")"
+	else
+		git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
+		die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
+	fi
 
 	if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
 	then
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index b77cce8..c09ce0d 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -152,6 +152,20 @@ test_expect_success 'submodule add to .gitignored path with --force' '
 	)
 '
 
+test_expect_success 'submodule add to reconfigure existing submodule with --force' '
+	(
+		cd addtest-ignore &&
+		git submodule add --force bogus-url submod &&
+		git submodule add -b initial "$submodurl" submod-branch &&
+		test "bogus-url" = "$(git config -f .gitmodules submodule.submod.url)" &&
+		test "bogus-url" = "$(git config submodule.submod.url)" &&
+		# Restore the url
+		git submodule add --force "$submodurl" submod
+		test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
+		test "$submodurl" = "$(git config submodule.submod.url)"
+	)
+'
+
 test_expect_success 'submodule add --branch' '
 	echo "refs/heads/initial" >expect-head &&
 	cat <<-\EOF >expect-heads &&
-- 
2.10.1.353.g1629400
Previous: Stefan BellerNext: Junio C Hamano
Message 2 of 10 in “submodule pushes be extra careful”
  1. 0/2 submodule pushes be extra carefulStefan Beller, Oct 6, 2016
  2. 1/2 submodule add: extend force flag to add existing reposStefan Beller, Oct 6, 2016
  3. Junio C HamanoOct 6, 2016
  4. Heiko VoigtOct 7, 2016
  5. Stefan BellerOct 7, 2016
  6. Heiko VoigtOct 11, 2016
  7. 2/2 push: change submodule default to check when submodules existStefan Beller, Oct 6, 2016
  8. Junio C HamanoOct 6, 2016
  9. Stefan BellerOct 6, 2016
  10. [PATCHv5] push: change submodule default to check when submodules existStefan Beller, Oct 6, 2016

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.