branch: colour upstream branches

Otherwise when using 'git branch -vv' it's hard to see them among so much output. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Apr 14, 2013 at 21:37 UTC dbda21fa87c1c5495c2f8ced4c40d7dbfa1cdcc2
2 files changed +30 -9
Documentation/config.txt
+2 -1
@@ -807,7 +807,8 @@ color.branch::
807 color.branch.<slot>::
808 Use customized color for branch coloration. `<slot>` is one of
809 `current` (the current branch), `local` (a local branch),
810 - `remote` (a remote-tracking branch in refs/remotes/), `plain` (other
810 + `remote` (a remote-tracking branch in refs/remotes/),
811 + `upstream` (upstream tracking branch), `plain` (other
812 refs).
813 +
814 The value for these configuration variables is a list of colors (at most
builtin/branch.c
+28 -8
@@ -41,13 +41,15 @@ static char branch_colors[][COLOR_MAXLEN] = {
41 GIT_COLOR_RED, /* REMOTE */
42 GIT_COLOR_NORMAL, /* LOCAL */
43 GIT_COLOR_GREEN, /* CURRENT */
44 + GIT_COLOR_BLUE, /* UPSTREAM */
45 };
46 enum color_branch {
47 BRANCH_COLOR_RESET = 0,
48 BRANCH_COLOR_PLAIN = 1,
49 BRANCH_COLOR_REMOTE = 2,
50 BRANCH_COLOR_LOCAL = 3,
50 - BRANCH_COLOR_CURRENT = 4
51 + BRANCH_COLOR_CURRENT = 4,
52 + BRANCH_COLOR_UPSTREAM = 5
53 };
54
55 static enum merge_filter {
@@ -72,6 +74,8 @@ static int parse_branch_color_slot(const char *var, int ofs)
74 return BRANCH_COLOR_LOCAL;
75 if (!strcasecmp(var+ofs, "current"))
76 return BRANCH_COLOR_CURRENT;
77 + if (!strcasecmp(var+ofs, "upstream"))
78 + return BRANCH_COLOR_UPSTREAM;
79 return -1;
80 }
81
@@ -418,36 +422,52 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
422 int ours, theirs;
423 char *ref = NULL;
424 struct branch *branch = branch_get(branch_name);
425 + struct strbuf fancy = STRBUF_INIT;
426
427 if (!stat_tracking_info(branch, &ours, &theirs)) {
428 if (branch && branch->merge && branch->merge[0]->dst &&
424 - show_upstream_ref)
425 - strbuf_addf(stat, "[%s] ",
426 - shorten_unambiguous_ref(branch->merge[0]->dst, 0));
429 + show_upstream_ref) {
430 + ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
431 + if (want_color(branch_use_color))
432 + strbuf_addf(stat, "[%s%s%s] ",
433 + branch_get_color(BRANCH_COLOR_UPSTREAM),
434 + ref, branch_get_color(BRANCH_COLOR_RESET));
435 + else
436 + strbuf_addf(stat, "[%s] ", ref);
437 + }
438 return;
439 }
440
430 - if (show_upstream_ref)
441 + if (show_upstream_ref) {
442 ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
443 + if (want_color(branch_use_color))
444 + strbuf_addf(&fancy, "%s%s%s",
445 + branch_get_color(BRANCH_COLOR_UPSTREAM),
446 + ref, branch_get_color(BRANCH_COLOR_RESET));
447 + else
448 + strbuf_addstr(&fancy, ref);
449 + }
450 +
451 if (!ours) {
452 if (ref)
434 - strbuf_addf(stat, _("[%s: behind %d]"), ref, theirs);
453 + strbuf_addf(stat, _("[%s: behind %d]"), fancy.buf, theirs);
454 else
455 strbuf_addf(stat, _("[behind %d]"), theirs);
456
457 } else if (!theirs) {
458 if (ref)
440 - strbuf_addf(stat, _("[%s: ahead %d]"), ref, ours);
459 + strbuf_addf(stat, _("[%s: ahead %d]"), fancy.buf, ours);
460 else
461 strbuf_addf(stat, _("[ahead %d]"), ours);
462 } else {
463 if (ref)
464 strbuf_addf(stat, _("[%s: ahead %d, behind %d]"),
446 - ref, ours, theirs);
465 + fancy.buf, ours, theirs);
466 else
467 strbuf_addf(stat, _("[ahead %d, behind %d]"),
468 ours, theirs);
469 }
470 + strbuf_release(&fancy);
471 strbuf_addch(stat, ' ');
472 free(ref);
473 }