git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: marcnarc@xiplink.com
To: git@vger.kernel.org
Cc: Jens Lehmann <Jens.Lehmann@web.de>,
	Marc Branchaud <marcnarc@xiplink.com>
Subject: [PATCH] Try harder to find a remote when on a detached HEAD or non-tracking branch.
Date: Mon, 18 Jun 2012 13:01:06 -0400	[thread overview]
Message-ID: <1340038866-24552-1-git-send-email-marcnarc@xiplink.com> (raw)

From: Marc Branchaud <marcnarc@xiplink.com>

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 <marcnarc@xiplink.com>
---

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

             reply	other threads:[~2012-06-18 17:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-18 17:01 marcnarc [this message]
2012-06-18 17:33 ` [PATCH] Try harder to find a remote when on a detached HEAD or non-tracking branch Junio C Hamano
2012-06-18 21:40   ` Marc Branchaud
2012-06-18 22:12     ` Junio C Hamano
2012-06-19 14:07       ` Marc Branchaud
2012-06-19 17:55         ` Junio C Hamano
2012-06-19 19:31           ` Heiko Voigt
2012-06-19 21:42             ` Marc Branchaud
2012-06-20 17:42               ` Heiko Voigt
2012-06-19 20:12           ` Jeff King
2012-06-19 20:31             ` Junio C Hamano
2012-06-19 21:43             ` Marc Branchaud
2012-06-19 21:46               ` Jeff King
2012-06-19 21:58                 ` Junio C Hamano
2012-06-19 22:00                   ` Jeff King
2012-06-19 22:26                     ` Junio C Hamano
2012-06-19 19:41         ` Jens Lehmann
2012-06-19 21:43           ` Marc Branchaud
2012-06-20  2:49             ` Phil Hord
2012-06-18 17:53 ` Arnaud Lacombe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1340038866-24552-1-git-send-email-marcnarc@xiplink.com \
    --to=marcnarc@xiplink.com \
    --cc=Jens.Lehmann@web.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).