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

Re: Local clone checks out wrong branch based on remote HEAD

From
Jeff King <peff@peff.net>
Date
Mar 18, 2009, 00:54 UTC
Message-ID
<20090318005413.GC25454@coredump.intra.peff.net>
In-Reply-To
<b97024a40903171219k8841508p774d9dc4295a09bc@mail.gmail.com>
On Tue, Mar 17, 2009 at 12:19:35PM -0700, Tom Preston-Werner wrote:
Show 8 quoted lines
> After cloning this with a standard `git clone`, the refs are:
> 
> [11:48][tom@solid:~/dev/sandbox/site(release)]$ git branch -r -v
>   origin/HEAD    a52528a Fixed some routing problems
>   origin/release a52528a Fixed some routing problems
>   origin/trunk   a52528a Fixed some routing problems
> 
> And the checked out branch is 'release' instead of 'trunk' as I would expect:

As others have explained, this is because the information is lacking at the client and we are forced to make a guess. There is a heuristic in the guess to prefer "master" if it is an option. I suppose we could make a similar exception for "trunk", which might make sense to people working with SVN repositories.

OTOH, I am not sure I want to open the can of worms that is writing an exhaustive list of heuristics that will work for everybody. Fixing the protocol itself would probably be easier. :)

Here is what such a heuristic would look like, though (on top of next and totally untested):

---
diff --git a/remote.c b/remote.c
index 76b1bbd..99d2281 100644
--- a/remote.c
+++ b/remote.c
@@ -1529,11 +1529,18 @@ struct ref *guess_remote_head(const struct ref *head,
 	if (head->symref)
 		return copy_ref(find_ref_by_name(refs, head->symref));
 
-	/* If refs/heads/master could be right, it is. */
+	/* We heuristically prefer certain names */
 	if (!all) {
-		r = find_ref_by_name(refs, "refs/heads/master");
-		if (r && !hashcmp(r->old_sha1, head->old_sha1))
-			return copy_ref(r);
+		const char *rules[] = {
+			"refs/heads/master",
+			"refs/heads/trunk",
+		};
+		int i;
+		for (i = 0; i < ARRAY_SIZE(rules); i++) {
+			r = find_ref_by_name(refs, rules[i]);
+			if (r && !hashcmp(r->old_sha1, head->old_sha1))
+				return copy_ref(r);
+		}
 	}
 
 	/* Look for another ref that points there */
Previous: Nanako ShiraishiNext: Michael J Gruber
Message 6 of 10 in “Local clone checks out wrong branch based on remote HEAD”
  1. Tom Preston-WernerMar 17, 2009
  2. Daniel BarkalowMar 17, 2009
  3. Junio C HamanoMar 17, 2009
  4. Jeff KingMar 18, 2009
  5. Nanako ShiraishiMar 17, 2009
  6. Jeff KingMar 18, 2009
  7. Michael J GruberMar 18, 2009
  8. Jay SoffianMar 18, 2009
  9. Jeff KingMar 19, 2009
  10. Jeff KingMar 19, 2009

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.