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

Re: git clone -b

From
Jeff King <peff@peff.net>
Date
Jul 3, 2013, 10:01 UTC
Message-ID
<20130703100118.GA21130@sigill.intra.peff.net>
In-Reply-To
<CABURp0oQ9MNGZecD4mkniXMEUJ5L7Xn+JFQMEgc97Mfe26JYvg@mail.gmail.com>
On Mon, Jul 01, 2013 at 01:49:37PM -0400, Phil Hord wrote:
Show 8 quoted lines
> It would be nice to support more generic specs for the --branch
> switch. But it is complicated because the refs have not been fetched
> yet during the clone, and so normal refs operations -- which expect to
> work on a local repository -- do not work.  So, the ref is looked up
> locally from a list in expected locations after fetching the remote
> refs but before the clone occurs.  The remote refs which are fetched
> is not configurable during clone, and so only 'refs/heads/*' is
> fetched for non-mirrors.
I think there are two problems:
  1. Our find_remote_branch function implements only half of the regular
     ref_rev_parse_rules (heads and tags). Fixing that to make "-b
     refs/heads/master" work is pretty easy. Patch is below.
  2. When we give a ref that is not going to be fetched, we should
     fetch it explicitly. It looks like --single-branch tries to do
     this, but only handles tags. I am not sure what a non-single-branch
     would want to do (since you are effectively overriding the
     default refspecs).
     So even with the patch, doing "clone -b refs/foo/bar" does not
     quite work.
diff --git a/builtin/clone.c b/builtin/clone.c
index 14b1323..5984303 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -433,24 +433,24 @@ static struct ref *find_remote_branch(const struct ref *refs, const char *branch
 	raise(signo);
 }
 
-static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
+static struct ref *find_remote_branch(const struct ref *refs, const char *name)
 {
-	struct ref *ref;
-	struct strbuf head = STRBUF_INIT;
-	strbuf_addstr(&head, "refs/heads/");
-	strbuf_addstr(&head, branch);
-	ref = find_ref_by_name(refs, head.buf);
-	strbuf_release(&head);
-
-	if (ref)
-		return ref;
-
-	strbuf_addstr(&head, "refs/tags/");
-	strbuf_addstr(&head, branch);
-	ref = find_ref_by_name(refs, head.buf);
-	strbuf_release(&head);
-
-	return ref;
+	int len = strlen(name);
+	const char **p;
+
+	for (p = ref_rev_parse_rules; *p; p++) {
+		struct ref *ref;
+
+		ref = find_ref_by_name(refs, mkpath(*p, len, name));
+		if (ref)
+			/*
+			 * optionally check for and complain about ambiguity
+			 * here, like dwim_ref does
+			 */
+			return ref;
+	}
+
+	return NULL;
 }
 
 static struct ref *wanted_peer_refs(const struct ref *refs,
Previous: Phil Hord
Message 10 of 10 in “git clone -b”
  1. Stefan NäweJun 28, 2013
  2. Stefan NäweJun 28, 2013
  3. Fredrik GustafssonJun 28, 2013
  4. Stefan NäweJun 28, 2013
  5. Stefan NäweJul 1, 2013
  6. Junio C HamanoJul 1, 2013
  7. Stefan NäweJul 2, 2013
  8. Junio C HamanoJul 2, 2013
  9. Phil HordJul 1, 2013
  10. Jeff KingJul 3, 2013

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.