All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jay Soffian <jaysoffian@gmail.com>
To: git@vger.kernel.org
Cc: Jay Soffian <jaysoffian@gmail.com>, gitster@pobox.com
Subject: [PATCH 2/3] builtin-remote: teach show to display remote HEAD
Date: Wed, 11 Feb 2009 01:01:22 -0500	[thread overview]
Message-ID: <1234332083-45147-3-git-send-email-jaysoffian@gmail.com> (raw)
In-Reply-To: <1234332083-45147-2-git-send-email-jaysoffian@gmail.com>

The remote HEAD cannot be determined unambiguosly, so use the same
heuristics as builtin-clone's locate_head().

Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
---
 builtin-remote.c |   39 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/builtin-remote.c b/builtin-remote.c
index 00e7ca5..0be8bfd 100644
--- a/builtin-remote.c
+++ b/builtin-remote.c
@@ -212,6 +212,7 @@ static void read_branches(void)
 struct ref_states {
 	struct remote *remote;
 	struct string_list new, stale, tracked;
+	char *head_name;
 };
 
 static int handle_one_branch(const char *refname,
@@ -271,6 +272,38 @@ static int get_ref_states(const struct ref *ref, struct ref_states *states)
 	return 0;
 }
 
+static char *get_head_name(const struct ref *ref)
+{
+	const struct ref *remote_head = NULL;
+	const struct ref *remote_master = NULL;
+	const struct ref *r;
+	for (r = ref; r; r = r->next) {
+		if (!strcmp(r->name, "HEAD"))
+			remote_head = r;
+		if (!strcmp(r->name, "refs/heads/master"))
+			remote_master = r;
+	}
+
+	/* If there's no HEAD value at all, never mind. */
+	if (!remote_head)
+		return NULL;
+
+	/* If refs/heads/master could be right, it is. */
+	if (remote_master && !hashcmp(remote_master->old_sha1,
+				      remote_head->old_sha1))
+		return xstrdup(abbrev_branch(remote_master->name));
+
+	/* Look for another ref that points there */
+	for (r = ref; r; r = r->next)
+		if (r != remote_head &&
+		    !hashcmp(r->old_sha1, remote_head->old_sha1) &&
+		    !prefixcmp(r->name, "refs/heads/"))
+			return xstrdup(abbrev_branch(r->name));
+
+	/* Nothing is the same */
+	return NULL;
+}
+
 struct known_remote {
 	struct known_remote *next;
 	struct remote *remote;
@@ -638,6 +671,8 @@ static void free_remote_ref_states(struct ref_states *states)
 	string_list_clear(&states->new, 0);
 	string_list_clear(&states->stale, 0);
 	string_list_clear(&states->tracked, 0);
+	if (states->head_name)
+		free(states->head_name);
 }
 
 static int get_remote_ref_states(const char *name,
@@ -659,6 +694,7 @@ static int get_remote_ref_states(const char *name,
 		ref = transport_get_remote_refs(transport);
 		transport_disconnect(transport);
 
+		states->head_name = get_head_name(ref);
 		get_ref_states(ref, states);
 	}
 
@@ -703,6 +739,9 @@ static int show(int argc, const char **argv)
 		printf("* remote %s\n  URL: %s\n", *argv,
 			states.remote->url_nr > 0 ?
 				states.remote->url[0] : "(no URL)");
+		if (!no_query)
+			printf("  HEAD: %s\n", states.head_name ?
+				states.head_name : "(unknown)");
 
 		for (i = 0; i < branch_list.nr; i++) {
 			struct string_list_item *branch = branch_list.items + i;
-- 
1.6.2.rc0.187.g9fcfb

  reply	other threads:[~2009-02-11  6:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-11  6:01 [PATCH 0/3] remote HEAD improvements Jay Soffian
2009-02-11  6:01 ` [PATCH 1/3] builtin-remote: move duplicated cleanup code its own function Jay Soffian
2009-02-11  6:01   ` Jay Soffian [this message]
2009-02-11  6:01     ` [PATCH 3/3] builtin-remote: add sethead verb Jay Soffian
2009-02-12  0:26     ` [PATCH 2/3] builtin-remote: teach show to display remote HEAD Jeff King
2009-02-12  1:48       ` Jay Soffian
2009-02-12  1:56         ` Jeff King
2009-02-12 20:27         ` Daniel Barkalow
2009-02-12 21:24           ` Junio C Hamano
2009-02-12 21:34             ` Daniel Barkalow
2009-02-12 21:37           ` Jay Soffian
2009-02-12 22:55             ` Daniel Barkalow
2009-02-12  0:18   ` [PATCH 1/3] builtin-remote: move duplicated cleanup code its own function Jeff King
2009-02-12  1:44     ` Jay Soffian
2009-02-12  1:50       ` Jeff King
2009-02-12 20:13       ` Daniel Barkalow
2009-02-12  0:17 ` [PATCH 0/3] remote HEAD improvements Jeff King

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=1234332083-45147-3-git-send-email-jaysoffian@gmail.com \
    --to=jaysoffian@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.