[PATCH] builtin-branch: highlight current remote branches with an asterisk

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux