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

[PATCH v3 2/4] submodules: always use a relative path from gitdir to work tree

From
Jens Lehmann <jens.lehmann@web.de>
Date
Mar 4, 2012, 21:15 UTC
Message-ID
<4F53DB5C.2060104@web.de>
In-Reply-To
<4F53DA95.2020402@web.de>

Since recently a submodule with name <name> has its git directory in the .git/modules/<name> directory of the superproject while the work tree contains a gitfile pointing there. To make that work the git directory has the core.worktree configuration set in its config file to point back to the work tree.

That core.worktree is an absolute path set by the initial clone of the submodule. A relative path is preferable here because it allows the superproject to be moved around without invalidating that setting, so compute and set that relative path after cloning or reactivating the submodule.

This also fixes a bug when moving a submodule around inside the superproject, as the current code forgot to update the setting to the new submodule work tree location.

Enhance t7400 to ensure that future versions won't re-add absolute paths by accident and that moving a superproject won't break submodules.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 git-submodule.sh           |   18 ++++++++++++++++++
 t/t7400-submodule-basic.sh |   20 ++++++++++++++++++++
 2 files changed, 38 insertions(+)
diff --git a/git-submodule.sh b/git-submodule.sh
index 2a93c61..c405caa 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -169,6 +169,24 @@ module_clone()
 	fi

 	echo "gitdir: $rel_gitdir" >"$path/.git"
+
+	a=$(cd "$gitdir" && pwd)/
+	b=$(cd "$path" && pwd)/
+	# Remove all common leading directories after a sanity check
+	if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
+		die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
+	fi
+	while test "${a%%/*}" = "${b%%/*}"
+	do
+		a=${a#*/}
+		b=${b#*/}
+	done
+	# Now chop off the trailing '/'s that were added in the beginning
+	a=${a%/}
+	b=${b%/}
+
+	rel=$(echo $a | sed -e 's|[^/]*|..|g')
+	(clear_local_git_env; cd "$path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
 }

 #
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 2b70b22..b377a7a 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -81,6 +81,13 @@ test_expect_success 'submodule add' '
 		test ! -s actual &&
 		echo "gitdir: ../.git/modules/submod" >expect &&
 		test_cmp expect submod/.git &&
+		(
+			cd submod &&
+			git config core.worktree >actual &&
+			echo "../../../submod" >expect &&
+			test_cmp expect actual &&
+			rm -f actual expect
+		) &&
 		git submodule init
 	) &&

@@ -500,4 +507,17 @@ test_expect_success 'relative path works with user@host:path' '
 	)
 '

+test_expect_success 'moving the superproject does not break submodules' '
+	(
+		cd addtest &&
+		git submodule status >expect
+	)
+	mv addtest addtest2 &&
+	(
+		cd addtest2 &&
+		git submodule status >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
1.7.9.2.362.g684a8
Previous: Jens LehmannNext: Jens Lehmann
Message 3 of 7 in “submodules: Use relative paths to gitdir and work tree”
  1. 0/4 submodules: Use relative paths to gitdir and work treeJens Lehmann, Mar 4, 2012
  2. 1/4 submodules: always use a relative path to gitdirJens Lehmann, Mar 4, 2012
  3. 2/4 submodules: always use a relative path from gitdir to work treeJens Lehmann, Mar 4, 2012
  4. 3/4 submodules: refactor computation of relative gitdir pathJens Lehmann, Mar 4, 2012
  5. 4/4 submodules: fix ambiguous absolute paths under WindowsJens Lehmann, Mar 4, 2012
  6. Junio C HamanoMar 7, 2012
  7. Johannes SixtMar 7, 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.