Thread (2 messages) flat view 2 messages, 2 authors, 2016-06-15
DORMANTno replies

[PATCH] Try harder to find a remote when on a detached HEAD or non-tracking branch.

From: <hidden>
Date: 2016-06-15 22:54:07
Subsystem: the rest · Maintainer: Linus Torvalds

From: Marc Branchaud <redacted>

get_default_remote() tries to use the checked-out branch's 'remote' config
value to figure out the remote's name.  This fails if there is no currently
checked-out branch (i.e. HEAD is detached) or if the checked-out branch
doesn't track a remote.  In these cases and the function would just fall
back to "origin".

Instead, let's use the first remote listed in the configuration, and fall
back to "origin" only if we don't find any configured remotes.

Prior to this change, git would fail to initialize a relative-path
submodule if the super-repo was on a detached HEAD and it had no remote
named "origin".

Signed-off-by: Marc Branchaud <redacted>
---

Our build system likes to use detached HEADs, so we got tripped up when we
started using relative submodule URLs.

(I'm not sure about the portability of my change, or if I should wrap it
to 80 columns...)

 git-parse-remote.sh        |  1 +
 t/t7400-submodule-basic.sh | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
diff --git a/git-parse-remote.sh b/git-parse-remote.sh
index 484b2e6..225ad94 100644
--- a/git-parse-remote.sh
+++ b/git-parse-remote.sh
@@ -8,6 +8,7 @@ get_default_remote () {
 	curr_branch=$(git symbolic-ref -q HEAD)
 	curr_branch="${curr_branch#refs/heads/}"
 	origin=$(git config --get "branch.$curr_branch.remote")
+	test -z "$origin" && origin=$(git config --list | grep '^remote\.' | head -1 | awk -F . '{print $2}')
 	echo ${origin:-origin}
 }
 
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index 81827e6..8f1ff4f 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -507,6 +507,28 @@ test_expect_success 'relative path works with user@host:path' '
 	)
 '
 
+test_expect_success 'relative path works on detached HEAD and remote is not named "origin"' '
+	mkdir detachtest &&
+	(
+		cd detachtest &&
+		git init &&
+		mkdir sub &&
+		(
+			cd sub &&
+			git init &&
+			test_commit foo
+		) &&
+		git add sub &&
+		git commit -m "added sub" &&
+		git checkout HEAD@{0} &&
+		git config -f .gitmodules submodule.sub.path sub &&
+		git config -f .gitmodules submodule.sub.url ../subrepo &&
+		git remote add awkward ssh://awkward/repo &&
+		git submodule init sub &&
+		test "$(git config submodule.sub.url)" = ssh://awkward/subrepo
+	)
+'
+
 test_expect_success 'moving the superproject does not break submodules' '
 	(
 		cd addtest &&
-- 
1.7.11.dirty
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help