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

[PATCH 1/2] submodules: always use a relative path to gitdir

From
Jens Lehmann <jens.lehmann@web.de>
Date
Feb 8, 2012, 22:11 UTC
Message-ID
<4F32F2F6.6040006@web.de>
In-Reply-To
<4F32F252.7050105@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. When the submodule git directory needs to be cloned because it is not found in .git/modules/<name> the clone command will write an absolute path into the gitfile. When no clone is necessary the git directory will be reactivated by the git-submodule.sh script by writing a relative path into the gitfile.

This is inconsistent, as the behavior depends on the submodule having been cloned before into the .git/modules of the superproject. A relative path is preferable here because it allows the superproject to be moved around without invalidating the gitfile. We do that by always writing the relative path into the gitfile, which overwrites the absolute path the clone command may have written there.

This is only the first step to make superprojects movable again like they were before the separate-git-dir approach was introduced. The second step is to use a relative path in core.worktree too.

Enhance t7400 to ensure that future versions won't re-add absolute paths by accident.

While at it also replace an if/else construct evaluating the presence of the 'reference' option with a single line of bash code.

Reported-by: Antony Male <antony.male@gmail.com>
Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 git-submodule.sh           |   11 ++++-------
 t/t7400-submodule-basic.sh |    2 ++
 2 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index 9bb2e13..2a93c61 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -160,18 +160,15 @@ module_clone()
     if test -d "$gitdir"
     then
         mkdir -p "$path"
-        echo "gitdir: $rel_gitdir" >"$path/.git"
         rm -f "$gitdir/index"
     else
         mkdir -p "$gitdir_base"
-        if test -n "$reference"
-        then
-            git-clone $quiet "$reference" -n "$url" "$path" --separate-git-dir "$gitdir"
-        else
-            git-clone $quiet -n "$url" "$path" --separate-git-dir "$gitdir"
-        fi ||
+        git clone $quiet -n ${reference:+"$reference"} \
+            --separate-git-dir "$gitdir" "$url" "$path" ||
         die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
     fi
+
+    echo "gitdir: $rel_gitdir" >"$path/.git"
 }
 
 #
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 695f7af..2b70b22 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -79,6 +79,8 @@ test_expect_success 'submodule add' '
         cd addtest &&
         git submodule add -q "$submodurl" submod >actual &&
         test ! -s actual &&
+        echo "gitdir: ../.git/modules/submod" >expect &&
+        test_cmp expect submod/.git &&
         git submodule init
     ) &&
 
-- 
1.7.9.190.gb17a42.dirty
Previous: Jens LehmannNext: Jens Lehmann
Message 2 of 21 in “submodules: Use relative paths to gitdir and work tree”
  1. 0/2 submodules: Use relative paths to gitdir and work treeJens Lehmann, Feb 8, 2012
  2. 1/2 submodules: always use a relative path to gitdirJens Lehmann, Feb 8, 2012
  3. 1/2 submodules: always use a relative path to gitdirJens Lehmann, Feb 9, 2012
  4. Junio C HamanoFeb 9, 2012
  5. Junio C HamanoFeb 9, 2012
  6. Jens LehmannFeb 9, 2012
  7. 2/2 submodules: always use a relative path from gitdir to work treeJens Lehmann, Feb 8, 2012
  8. 2/2 submodules: always use a relative path from gitdir to work treeJens Lehmann, Feb 9, 2012
  9. Junio C HamanoFeb 13, 2012
  10. Jens LehmannFeb 14, 2012
  11. Junio C HamanoFeb 14, 2012
  12. Junio C HamanoFeb 14, 2012
  13. Jens LehmannFeb 15, 2012
  14. Johannes SixtFeb 26, 2012
  15. Jens LehmannFeb 26, 2012
  16. Johannes SixtFeb 27, 2012
  17. Johannes SchindelinFeb 27, 2012
  18. Johannes SixtFeb 28, 2012
  19. Junio C HamanoFeb 28, 2012
  20. Jens LehmannFeb 28, 2012
  21. Jens LehmannFeb 28, 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.