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] builtin-branch: highlight current remote branches with an asterisk
Date: Mon,  9 Feb 2009 18:32:06 -0500	[thread overview]
Message-ID: <1234222326-55818-1-git-send-email-jaysoffian@gmail.com> (raw)

Teach git branch -{r,a} how to interpret remote HEADs and highlight the
corresponding remote branch with an asterisk, instead of showing literal
"<remote_name>/HEAD".

Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
---
git branch -r before patch:
  origin/HEAD
  origin/html
  origin/maint
  origin/man
  origin/master
  origin/next
  origin/pu
  origin/todo

git branch -r after patch:
  origin/html
  origin/maint
  origin/man
* origin/master
  origin/next
  origin/pu
  origin/todo

The coloring for the current remote branch remains red, not green like
the current local branch.

I think it's an improvement. :)

 builtin-branch.c |   41 ++++++++++++++++++++++++++++++-----------
 1 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/builtin-branch.c b/builtin-branch.c
index 56a1971..62558a7 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -15,6 +15,7 @@
 #include "branch.h"
 #include "diff.h"
 #include "revision.h"
+#include "string-list.h"
 
 static const char * const builtin_branch_usage[] = {
 	"git branch [options] [-r | -a] [--merged | --no-merged]",
@@ -190,9 +191,19 @@ struct ref_list {
 	int index, alloc, maxwidth;
 	struct ref_item *list;
 	struct commit_list *with_commit;
+	struct string_list *remote_heads;
 	int kinds;
 };
 
+static void add_to_remote_heads(struct string_list *remote_heads, const char *head) {
+	unsigned char sha1[20];
+	int flag;
+	const char *refname = resolve_ref(head, sha1, 0, &flag);
+	if (refname && (flag & REF_ISSYMREF) &&
+	    !prefixcmp(refname, "refs/remotes/"))
+		string_list_insert(refname + 13, remote_heads);
+}
+
 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 {
 	struct ref_list *ref_list = (struct ref_list*)(cb_data);
@@ -223,6 +234,13 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	if ((kind & ref_list->kinds) == 0)
 		return 0;
 
+	/* Handle remote HEAD */
+	if (kind == REF_REMOTE_BRANCH && ((len = strlen(refname)) > 5) &&
+	    !strcmp(refname + len - 5, "/HEAD")) {
+		add_to_remote_heads(ref_list->remote_heads, refname - 13);
+		return 0;
+	}
+
 	if (merge_filter != NO_FILTER)
 		add_pending_object(&ref_list->revs,
 				   (struct object *)commit, refname);
@@ -294,8 +312,8 @@ static int matches_merge_filter(struct commit *commit)
 static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 			   int abbrev, int current)
 {
-	char c;
-	int color;
+	char c = ' ';
+	int color = COLOR_BRANCH_PLAIN, current_color = COLOR_BRANCH_CURRENT;
 	struct commit *commit = item->commit;
 
 	if (!matches_merge_filter(commit))
@@ -306,17 +324,13 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 		color = COLOR_BRANCH_LOCAL;
 		break;
 	case REF_REMOTE_BRANCH:
-		color = COLOR_BRANCH_REMOTE;
-		break;
-	default:
-		color = COLOR_BRANCH_PLAIN;
+		color = current_color = COLOR_BRANCH_REMOTE;
 		break;
 	}
 
-	c = ' ';
 	if (current) {
 		c = '*';
-		color = COLOR_BRANCH_CURRENT;
+		color = current_color;
 	}
 
 	if (verbose) {
@@ -364,10 +378,12 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 	int i;
 	struct ref_list ref_list;
 	struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
+	struct string_list remote_heads = { NULL, 0, 0, 1};
 
 	memset(&ref_list, 0, sizeof(ref_list));
 	ref_list.kinds = kinds;
 	ref_list.with_commit = with_commit;
+	ref_list.remote_heads = &remote_heads;
 	if (merge_filter != NO_FILTER)
 		init_revisions(&ref_list.revs, NULL);
 	for_each_ref(append_ref, &ref_list);
@@ -399,13 +415,16 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 	}
 
 	for (i = 0; i < ref_list.index; i++) {
-		int current = !detached &&
+		int current = (!detached &&
 			(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
-			!strcmp(ref_list.list[i].name, head);
+			!strcmp(ref_list.list[i].name, head)) ||
+			(ref_list.list[i].kind == REF_REMOTE_BRANCH &&
+			string_list_has_string(&remote_heads,
+			                       ref_list.list[i].name));
 		print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
 			       abbrev, current);
 	}
-
+	string_list_clear(&remote_heads, 0);
 	free_ref_list(&ref_list);
 }
 
-- 
1.6.1.2.354.ge44a2

             reply	other threads:[~2009-02-09 23:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-09 23:32 Jay Soffian [this message]
2009-02-09 23:49 ` [PATCH] builtin-branch: highlight current remote branches with an asterisk Johannes Schindelin
2009-02-10  0:03   ` Mark Burton
2009-02-10  0:22     ` Jay Soffian
2009-02-10 11:05       ` Mark Burton
2009-02-10  0:24     ` Junio C Hamano
2009-02-10  7:59       ` Santi Béjar
2009-02-10  0:10   ` Jay Soffian
2009-02-10  7:52 ` Björn Steinbrink
2009-02-10  8:02   ` Santi Béjar
2009-02-10  8:24     ` Björn Steinbrink
2009-02-10 11:19   ` Jeff King
2009-02-10 11:50     ` Björn Steinbrink
2009-02-10 11:59       ` Jeff King
2009-02-10 12:23         ` Björn Steinbrink
2009-02-10 13:04           ` 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=1234222326-55818-1-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.