@matheusbsbs / matheus-bagliano / commits / a1158caead
Fork 0

Colourise git-branch output

I wanted to have a visual indication of which branches are local and which are remote in git-branch -a output; however Junio was concerned that someone might be using the output in a script. This patch addresses the problem by colouring the git-branch output - which in "auto" mode won't be activated. I've based it off the colouring code for builtin-diff.c; which means there is a branch color configuration variable that needs setting to something before the color will appear. The colour parameter is "color.branch" rather than "branch.color" to avoid clashing with the default namespace for default branch merge definitions. This patch chooses green for local, red for remote and bold green for current. Signed-off-by: Andy Parkins <andyparkins@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Andy Parkins committed Dec 12, 2006 at 06:41 UTC a1158caeadec4d48b185068f4120d85423de5970
1 file changed +87 -5
builtin-branch.c
+87 -5
@@ -5,6 +5,7 @@
5 * Based on git-branch.sh by Junio C Hamano.
6 */
7
8 +#include "color.h"
9 #include "cache.h"
10 #include "refs.h"
11 #include "commit.h"
@@ -17,6 +18,58 @@ static const char builtin_branch_usage[] =
18 static const char *head;
19 static unsigned char head_sha1[20];
20
21 +static int branch_use_color;
22 +static char branch_colors[][COLOR_MAXLEN] = {
23 + "\033[m", /* reset */
24 + "", /* PLAIN (normal) */
25 + "\033[31m", /* REMOTE (red) */
26 + "\033[32m", /* LOCAL (green) */
27 + "\033[1;32m", /* CURRENT (boldgreen) */
28 +};
29 +enum color_branch {
30 + COLOR_BRANCH_RESET = 0,
31 + COLOR_BRANCH_PLAIN = 1,
32 + COLOR_BRANCH_REMOTE = 2,
33 + COLOR_BRANCH_LOCAL = 3,
34 + COLOR_BRANCH_CURRENT = 4,
35 +};
36 +
37 +static int parse_branch_color_slot(const char *var, int ofs)
38 +{
39 + if (!strcasecmp(var+ofs, "plain"))
40 + return COLOR_BRANCH_PLAIN;
41 + if (!strcasecmp(var+ofs, "reset"))
42 + return COLOR_BRANCH_RESET;
43 + if (!strcasecmp(var+ofs, "remote"))
44 + return COLOR_BRANCH_REMOTE;
45 + if (!strcasecmp(var+ofs, "local"))
46 + return COLOR_BRANCH_LOCAL;
47 + if (!strcasecmp(var+ofs, "current"))
48 + return COLOR_BRANCH_CURRENT;
49 + die("bad config variable '%s'", var);
50 +}
51 +
52 +int git_branch_config(const char *var, const char *value)
53 +{
54 + if (!strcmp(var, "color.branch")) {
55 + branch_use_color = git_config_colorbool(var, value);
56 + return 0;
57 + }
58 + if (!strncmp(var, "color.branch.", 13)) {
59 + int slot = parse_branch_color_slot(var, 13);
60 + color_parse(value, var, branch_colors[slot]);
61 + return 0;
62 + }
63 + return git_default_config(var, value);
64 +}
65 +
66 +const char *branch_get_color(enum color_branch ix)
67 +{
68 + if (branch_use_color)
69 + return branch_colors[ix];
70 + return "";
71 +}
72 +
73 static int in_merge_bases(const unsigned char *sha1,
74 struct commit *rev1,
75 struct commit *rev2)
@@ -183,6 +236,7 @@ static void print_ref_list(int kinds, int verbose, int abbrev)
236 int i;
237 char c;
238 struct ref_list ref_list;
239 + int color;
240
241 memset(&ref_list, 0, sizeof(ref_list));
242 ref_list.kinds = kinds;
@@ -191,18 +245,38 @@ static void print_ref_list(int kinds, int verbose, int abbrev)
245 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
246
247 for (i = 0; i < ref_list.index; i++) {
248 + switch( ref_list.list[i].kind ) {
249 + case REF_LOCAL_BRANCH:
250 + color = COLOR_BRANCH_LOCAL;
251 + break;
252 + case REF_REMOTE_BRANCH:
253 + color = COLOR_BRANCH_REMOTE;
254 + break;
255 + default:
256 + color = COLOR_BRANCH_PLAIN;
257 + break;
258 + }
259 +
260 c = ' ';
261 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
196 - !strcmp(ref_list.list[i].name, head))
262 + !strcmp(ref_list.list[i].name, head)) {
263 c = '*';
264 + color = COLOR_BRANCH_CURRENT;
265 + }
266
267 if (verbose) {
200 - printf("%c %-*s", c, ref_list.maxwidth,
201 - ref_list.list[i].name);
268 + printf("%c %s%-*s%s", c,
269 + branch_get_color(color),
270 + ref_list.maxwidth,
271 + ref_list.list[i].name,
272 + branch_get_color(COLOR_BRANCH_RESET));
273 print_ref_info(ref_list.list[i].sha1, abbrev);
274 }
275 else
205 - printf("%c %s\n", c, ref_list.list[i].name);
276 + printf("%c %s%s%s\n", c,
277 + branch_get_color(color),
278 + ref_list.list[i].name,
279 + branch_get_color(COLOR_BRANCH_RESET));
280 }
281
282 free_ref_list(&ref_list);
@@ -253,7 +327,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
327 int kinds = REF_LOCAL_BRANCH;
328 int i;
329
256 - git_config(git_default_config);
330 + git_config(git_branch_config);
331
332 for (i = 1; i < argc; i++) {
333 const char *arg = argv[i];
@@ -297,6 +371,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
371 verbose = 1;
372 continue;
373 }
374 + if (!strcmp(arg, "--color")) {
375 + branch_use_color = 1;
376 + continue;
377 + }
378 + if (!strcmp(arg, "--no-color")) {
379 + branch_use_color = 0;
380 + continue;
381 + }
382 usage(builtin_branch_usage);
383 }
384