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

[PATCH v2 1/9] Generate unique ID for submodules created using "git submodule add"

From
Peter Collingbourne <peter@pcc.me.uk>
Date
Apr 10, 2010, 18:23 UTC
Message-ID
<1270923830-11830-2-git-send-email-peter@pcc.me.uk>
In-Reply-To
<1270923830-11830-1-git-send-email-peter@pcc.me.uk>

This patch causes "git submodule add" to generate a unique ID for the submodule which is used as its name. The ID is formed by concatenating the basename of the submodule path, a dash ("-") character and the 7-character truncated SHA1 hash of the pid, date and initial path.

The purpose of this patch is to avoid name conflicts which may arise due to the ability to rename submodules.

The justification for truncating the SHA1 to 7 characters is that a submodule naming clash is a highly infrequent event (as compared to a file naming clash) so we can minimise the ugliness of generated submodule names by using a smaller number of characters.

The justification for including the submodule path basename is that we should include some component of the submodule path in the submodule name to make it possible to identify a particular submodule by its name. At the same time we should be conscious of the fact that the submodule path may change. Including the entire submodule path in the submodule name is likely to confuse users if the path changes. The basename is the component of the path which is least likely to change, which is a factor in favour of its inclusion in the submodule name.

Signed-off-by: Peter Collingbourne <peter@pcc.me.uk>
---
 Documentation/git-submodule.txt |    8 +++++++-
 git-submodule.sh                |   31 +++++++++++++++++++++++++++++--
 t/t7403-submodule-sync.sh       |    2 +-
 t/t7406-submodule-update.sh     |    6 +++---
 t/t7407-submodule-foreach.sh    |   14 +++++++-------
 5 files changed, 47 insertions(+), 14 deletions(-)
diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt
index 2502531..1bf78b6 100644
--- a/Documentation/git-submodule.txt
+++ b/Documentation/git-submodule.txt
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git submodule' [--quiet] add [-b branch]
-	      [--reference <repository>] [--] <repository> [<path>]
+	      [--reference <repository>] [-n <name>] [--] <repository> [<path>]
 'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
 'git submodule' [--quiet] init [--] [<path>...]
 'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
@@ -199,6 +199,12 @@ OPTIONS
 	(the default). This limit only applies to modified submodules. The
 	size is always limited to 1 for added/deleted/typechanged submodules.
 
+-n <name>::
+--name <name>::
+	This option is only valid for the add command.
+	Name of the new submodule.  By default, a unique identifier
+	is generated.
+
 -N::
 --no-fetch::
 	This option is only valid for the update command.
diff --git a/git-submodule.sh b/git-submodule.sh
index 187461c..de29f3a 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -114,6 +114,23 @@ module_clone()
 }
 
 #
+# Generate a unique identifier.  Used to name a submodule.
+#
+gen_uid()
+{
+	path="$1"
+
+	pathbase=$(basename "$path")
+	echo -n "$pathbase"-
+
+	(
+	 echo "$path"
+	 echo $$
+	 date
+	) | git hash-object --stdin | cut -c1-7
+}
+
+#
 # Add a new submodule to the working tree, .gitmodules and the index
 #
 # $@ = repo path
@@ -131,6 +148,11 @@ cmd_add()
 			branch=$2
 			shift
 			;;
+		-n | --name)
+			case "$2" in '') usage ;; esac
+			name=$2
+			shift
+			;;
 		-q|--quiet)
 			GIT_QUIET=1
 			;;
@@ -235,8 +257,13 @@ cmd_add()
 	git add "$path" ||
 	die "Failed to add submodule '$path'"
 
-	git config -f .gitmodules submodule."$path".path "$path" &&
-	git config -f .gitmodules submodule."$path".url "$repo" &&
+	if test -z "$name"
+	then
+		name=$(gen_uid "$path")
+	fi
+
+	git config -f .gitmodules submodule."$name".path "$path" &&
+	git config -f .gitmodules submodule."$name".url "$repo" &&
 	git add .gitmodules ||
 	die "Failed to register submodule '$path'"
 }
diff --git a/t/t7403-submodule-sync.sh b/t/t7403-submodule-sync.sh
index 7538756..f2c66f8 100755
--- a/t/t7403-submodule-sync.sh
+++ b/t/t7403-submodule-sync.sh
@@ -18,7 +18,7 @@ test_expect_success setup '
 	git clone . super &&
 	git clone super submodule &&
 	(cd super &&
-	 git submodule add ../submodule submodule &&
+	 git submodule add -n submodule ../submodule submodule &&
 	 test_tick &&
 	 git commit -m "submodule"
 	) &&
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index 1382a8e..5bcac8f 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -31,7 +31,7 @@ test_expect_success 'setup a submodule tree' '
 	git clone super rebasing &&
 	git clone super merging &&
 	(cd super &&
-	 git submodule add ../submodule submodule &&
+	 git submodule add -n submodule ../submodule submodule &&
 	 test_tick &&
 	 git commit -m "submodule" &&
 	 git submodule init submodule
@@ -49,12 +49,12 @@ test_expect_success 'setup a submodule tree' '
 	 git commit -m "submodule update"
 	) &&
 	(cd super &&
-	 git submodule add ../rebasing rebasing &&
+	 git submodule add -n rebasing ../rebasing rebasing &&
 	 test_tick &&
 	 git commit -m "rebasing"
 	) &&
 	(cd super &&
-	 git submodule add ../merging merging &&
+	 git submodule add -n merging ../merging merging &&
 	 test_tick &&
 	 git commit -m "rebasing"
 	)
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 2a52775..a0390dd 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -21,9 +21,9 @@ test_expect_success 'setup a submodule tree' '
 	git clone super submodule &&
 	(
 		cd super &&
-		git submodule add ../submodule sub1 &&
-		git submodule add ../submodule sub2 &&
-		git submodule add ../submodule sub3 &&
+		git submodule add -n sub1 ../submodule sub1 &&
+		git submodule add -n sub2 ../submodule sub2 &&
+		git submodule add -n sub3 ../submodule sub3 &&
 		git config -f .gitmodules --rename-section \
 			submodule.sub1 submodule.foo1 &&
 		git config -f .gitmodules --rename-section \
@@ -82,28 +82,28 @@ test_expect_success 'setup nested submodules' '
 	git clone submodule nested3 &&
 	(
 		cd nested3 &&
-		git submodule add ../submodule submodule &&
+		git submodule add -n submodule ../submodule submodule &&
 		test_tick &&
 		git commit -m "submodule" &&
 		git submodule init submodule
 	) &&
 	(
 		cd nested2 &&
-		git submodule add ../nested3 nested3 &&
+		git submodule add -n nested3 ../nested3 nested3 &&
 		test_tick &&
 		git commit -m "nested3" &&
 		git submodule init nested3
 	) &&
 	(
 		cd nested1 &&
-		git submodule add ../nested2 nested2 &&
+		git submodule add -n nested2 ../nested2 nested2 &&
 		test_tick &&
 		git commit -m "nested2" &&
 		git submodule init nested2
 	) &&
 	(
 		cd super &&
-		git submodule add ../nested1 nested1 &&
+		git submodule add -n nested1 ../nested1 nested1 &&
 		test_tick &&
 		git commit -m "nested1" &&
 		git submodule init nested1
-- 
1.6.5
Previous: Peter CollingbourneNext: Sverre Rabbelier
Message 2 of 14 in “Improve handling of moving and removing submodules”
  1. 0/9 Improve handling of moving and removing submodulesPeter Collingbourne, Apr 10, 2010
  2. 1/9 Generate unique ID for submodules created using "git submodule add"Peter Collingbourne, Apr 10, 2010
  3. Sverre RabbelierApr 10, 2010
  4. 2/9 Implement "git mv" for submodulesPeter Collingbourne, Apr 10, 2010
  5. Junio C HamanoApr 11, 2010
  6. 3/9 git rm: test failure behaviour for multiple removalsPeter Collingbourne, Apr 10, 2010
  7. 4/9 git rm: display a warning for every unremovable filePeter Collingbourne, Apr 10, 2010
  8. 5/9 git rm: collect file modesPeter Collingbourne, Apr 10, 2010
  9. 6/9 Add a mode parameter to the remove_path functionPeter Collingbourne, Apr 10, 2010
  10. Junio C HamanoApr 11, 2010
  11. 7/9 git rm: do not abort due to an initialised submodulePeter Collingbourne, Apr 10, 2010
  12. Junio C HamanoApr 11, 2010
  13. 8/9 git submodule: infrastructure for reading .gitmodules files in arbitrary locationsPeter Collingbourne, Apr 10, 2010
  14. 9/9 git rm: remove submodule entries from .gitmodulesPeter Collingbourne, Apr 10, 2010

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.