{"thread":{"id":"43364","subject":"Re: [PATCH] Colourise git-branch output","startedAt":"2006-12-12T06:41:52Z","lastAt":"2006-12-12T18:43:42Z","messageCount":6,"participants":["Junio C Hamano","Johannes Sixt","Andy Parkins"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"295188","messageId":"200612120641.52556.andyparkins@gmail.com","threadId":"43364","inReplyTo":null,"subject":"[PATCH] Colourise git-branch output","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2006-12-12T06:41:52Z","receivedAt":"2006-12-12T06:41:52Z","isPatch":true,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"I wanted to have a visual indication of which branches are local and\nwhich are remote in git-branch -a output; however Junio was concerned\nthat someone might be using the output in a script.  This patch\naddresses the problem by colouring the git-branch output - which in\n\"auto\" mode won't be activated.\n\nI've based it off the colouring code for builtin-diff.c; which means\nthere is a branch color configuration variable that needs setting to\nsomething before the color will appear.\n\nThe colour parameter is \"color.branch\" rather than \"branch.color\" to\navoid clashing with the default namespace for default branch merge\ndefinitions.\n\nThis patch chooses green for local, red for remote and bold green for\ncurrent.\n\nSigned-off-by: Andy Parkins <andyparkins@gmail.com>\n---\n builtin-branch.c |   92 +++++++++++++++++++++++++++++++++++++++++++++++++++---\n 1 files changed, 87 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin-branch.c b/builtin-branch.c\nindex 3d5cb0e..7c87b8d 100644\n--- a/builtin-branch.c\n+++ b/builtin-branch.c\n@@ -5,6 +5,7 @@\n  * Based on git-branch.sh by Junio C Hamano.\n  */\n \n+#include \"color.h\"\n #include \"cache.h\"\n #include \"refs.h\"\n #include \"commit.h\"\n@@ -17,6 +18,58 @@ static const char builtin_branch_usage[] =\n static const char *head;\n static unsigned char head_sha1[20];\n \n+static int branch_use_color;\n+static char branch_colors[][COLOR_MAXLEN] = {\n+\t\"\\033[m\",\t/* reset */\n+\t\"\",\t\t/* PLAIN (normal) */\n+\t\"\\033[31m\",\t/* REMOTE (red) */\n+\t\"\\033[32m\",\t/* LOCAL (green) */\n+\t\"\\033[1;32m\",\t/* CURRENT (boldgreen) */\n+};\n+enum color_branch {\n+\tCOLOR_BRANCH_RESET = 0,\n+\tCOLOR_BRANCH_PLAIN = 1,\n+\tCOLOR_BRANCH_REMOTE = 2,\n+\tCOLOR_BRANCH_LOCAL = 3,\n+\tCOLOR_BRANCH_CURRENT = 4,\n+};\n+\n+static int parse_branch_color_slot(const char *var, int ofs)\n+{\n+\tif (!strcasecmp(var+ofs, \"plain\"))\n+\t\treturn COLOR_BRANCH_PLAIN;\n+\tif (!strcasecmp(var+ofs, \"reset\"))\n+\t\treturn COLOR_BRANCH_RESET;\n+\tif (!strcasecmp(var+ofs, \"remote\"))\n+\t\treturn COLOR_BRANCH_REMOTE;\n+\tif (!strcasecmp(var+ofs, \"local\"))\n+\t\treturn COLOR_BRANCH_LOCAL;\n+\tif (!strcasecmp(var+ofs, \"current\"))\n+\t\treturn COLOR_BRANCH_CURRENT;\n+\tdie(\"bad config variable '%s'\", var);\n+}\n+\n+int git_branch_config(const char *var, const char *value)\n+{\n+\tif (!strcmp(var, \"color.branch\")) {\n+\t\tbranch_use_color = git_config_colorbool(var, value);\n+\t\treturn 0;\n+\t}\n+\tif (!strncmp(var, \"color.branch.\", 13)) {\n+\t\tint slot = parse_branch_color_slot(var, 13);\n+\t\tcolor_parse(value, var, branch_colors[slot]);\n+\t\treturn 0;\n+\t}\n+\treturn git_default_config(var, value);\n+}\n+\n+const char *branch_get_color(enum color_branch ix)\n+{\n+\tif (branch_use_color)\n+\t\treturn branch_colors[ix];\n+\treturn \"\";\n+}\n+\n static int in_merge_bases(const unsigned char *sha1,\n \t\t\t  struct commit *rev1,\n \t\t\t  struct commit *rev2)\n@@ -183,6 +236,7 @@ static void print_ref_list(int kinds, int verbose, int abbrev)\n \tint i;\n \tchar c;\n \tstruct ref_list ref_list;\n+\tint color;\n \n \tmemset(&ref_list, 0, sizeof(ref_list));\n \tref_list.kinds = kinds;\n@@ -191,18 +245,38 @@ static void print_ref_list(int kinds, int verbose, int abbrev)\n \tqsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);\n \n \tfor (i = 0; i < ref_list.index; i++) {\n+\t\tswitch( ref_list.list[i].kind ) {\n+\t\t\tcase REF_LOCAL_BRANCH:\n+\t\t\t\tcolor = COLOR_BRANCH_LOCAL;\n+\t\t\t\tbreak;\n+\t\t\tcase REF_REMOTE_BRANCH:\n+\t\t\t\tcolor = COLOR_BRANCH_REMOTE;\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tcolor = COLOR_BRANCH_PLAIN;\n+\t\t\t\tbreak;\n+\t\t}\n+\n \t\tc = ' ';\n \t\tif (ref_list.list[i].kind == REF_LOCAL_BRANCH &&\n-\t\t\t\t!strcmp(ref_list.list[i].name, head))\n+\t\t\t\t!strcmp(ref_list.list[i].name, head)) {\n \t\t\tc = '*';\n+\t\t\tcolor = COLOR_BRANCH_CURRENT;\n+\t\t}\n \n \t\tif (verbose) {\n-\t\t\tprintf(\"%c %-*s\", c, ref_list.maxwidth,\n-\t\t\t       ref_list.list[i].name);\n+\t\t\tprintf(\"%c %s%-*s%s\", c,\n+\t\t\t\t\tbranch_get_color(color),\n+\t\t\t\t\tref_list.maxwidth,\n+\t\t\t\t\tref_list.list[i].name,\n+\t\t\t\t\tbranch_get_color(COLOR_BRANCH_RESET));\n \t\t\tprint_ref_info(ref_list.list[i].sha1, abbrev);\n \t\t}\n \t\telse\n-\t\t\tprintf(\"%c %s\\n\", c, ref_list.list[i].name);\n+\t\t\tprintf(\"%c %s%s%s\\n\", c,\n+\t\t\t\t\tbranch_get_color(color),\n+\t\t\t\t\tref_list.list[i].name,\n+\t\t\t\t\tbranch_get_color(COLOR_BRANCH_RESET));\n \t}\n \n \tfree_ref_list(&ref_list);\n@@ -253,7 +327,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)\n \tint kinds = REF_LOCAL_BRANCH;\n \tint i;\n \n-\tgit_config(git_default_config);\n+\tgit_config(git_branch_config);\n \n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n@@ -297,6 +371,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix)\n \t\t\tverbose = 1;\n \t\t\tcontinue;\n \t\t}\n+\t\tif (!strcmp(arg, \"--color\")) {\n+\t\t\tbranch_use_color = 1;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (!strcmp(arg, \"--no-color\")) {\n+\t\t\tbranch_use_color = 0;\n+\t\t\tcontinue;\n+\t\t}\n \t\tusage(builtin_branch_usage);\n \t}\n \n-- \n1.4.4.1.geeee8\n"},{"id":"294760","messageId":"457E6C65.4B6D131@eudaptics.com","threadId":"43364","inReplyTo":"200612120641.52556.andyparkins@gmail.com","subject":"Re: [PATCH] Colourise git-branch output","fromName":"Johannes Sixt","fromEmail":"j.sixt@eudaptics.com","sentAt":"2006-12-12T08:46:29Z","receivedAt":"2006-12-12T08:46:29Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Andy Parkins wrote:\n> This patch chooses green for local, red for remote and bold green for\n> current.\n\nSorry for chiming in so late, but red and green are usually poor choices\nsince red-green color-blindness is surprisingly frequent...\n\nMaybe its sufficient to have just the remote branches (dark-)red, and\nthe rest in the default color, with the current branch bold?\n\n-- Hannes\n"},{"id":"294506","messageId":"7vpsapwhl2.fsf@assigned-by-dhcp.cox.net","threadId":"43364","inReplyTo":"457E6C65.4B6D131@eudaptics.com","subject":"Re: [PATCH] Colourise git-branch output","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-12-12T10:10:01Z","receivedAt":"2006-12-12T10:10:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Sixt <J.Sixt@eudaptics.com> writes:\n\n> Andy Parkins wrote:\n>> This patch chooses green for local, red for remote and bold green for\n>> current.\n>\n> Sorry for chiming in so late, but red and green are usually poor choices\n> since red-green color-blindness is surprisingly frequent...\n>\n> Maybe its sufficient to have just the remote branches (dark-)red, and\n> the rest in the default color, with the current branch bold?\n\nEven without red-green blindness issue, I think that makes\nsense.  colored-diff uses green/red for added/deleted but that\nis shown against the context in plain.  A sane thing to do for\nbranch listing would be to show the usual case (i.e. local) in\nplain and show the remote ones differently.\n\nSomething like this on top of Andy's?\n\nIf we keep '*' prefix for the current one, I do not see a reason\nto show it in a different color from other local branches, by\nthe way, but I did not go that far in this patch.\n\n---\ndiff --git a/builtin-branch.c b/builtin-branch.c\nindex 7c87b8d..d1c243d 100644\n--- a/builtin-branch.c\n+++ b/builtin-branch.c\n@@ -23,8 +23,8 @@ static char branch_colors[][COLOR_MAXLEN] = {\n \t\"\\033[m\",\t/* reset */\n \t\"\",\t\t/* PLAIN (normal) */\n \t\"\\033[31m\",\t/* REMOTE (red) */\n-\t\"\\033[32m\",\t/* LOCAL (green) */\n-\t\"\\033[1;32m\",\t/* CURRENT (boldgreen) */\n+\t\"\",\t\t/* LOCAL (normal) */\n+\t\"\\033[32m\",\t/* CURRENT (green) */\n };\n enum color_branch {\n \tCOLOR_BRANCH_RESET = 0,\n"},{"id":"297712","messageId":"200612121103.31110.andyparkins@gmail.com","threadId":"43364","inReplyTo":"7vpsapwhl2.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] Colourise git-branch output","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2006-12-12T11:03:29Z","receivedAt":"2006-12-12T11:03:29Z","isPatch":true,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"On Tuesday 2006 December 12 10:10, Junio C Hamano wrote:\n\n> Even without red-green blindness issue, I think that makes\n> sense.  colored-diff uses green/red for added/deleted but that\n\nI certainly don't have any objection.  For me, the use of colour is not to \nmake things look pretty it's to give visual queues.\n\n> is shown against the context in plain.  A sane thing to do for\n> branch listing would be to show the usual case (i.e. local) in\n> plain and show the remote ones differently.\n\nThe only reason I picked red and green was to indicate \"can be checked out\" \nand \"cannot be checked out\".  However, when git eventually allows arbitrary \ncommits to be checked out that green/red, can/can't distinction will be \nmeaningless anyway.\n\n> +\t\"\",\t\t/* LOCAL (normal) */\n> +\t\"\\033[32m\",\t/* CURRENT (green) */\n\nIn keeping with the \"don't use green\" idea - can I suggest just bold normal \nfor the CURRENT?  That way there is the most minimal use of colour for the \ndefault git-branch output, but still retaining a visual indicator.\n\nAndy\n-- \nDr Andy Parkins, M Eng (hons), MIEE\n"},{"id":"297259","messageId":"7vpsapuf8i.fsf@assigned-by-dhcp.cox.net","threadId":"43364","inReplyTo":"200612120641.52556.andyparkins@gmail.com","subject":"Re: [PATCH] Colourise git-branch output","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-12-12T18:43:41Z","receivedAt":"2006-12-12T18:43:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Andy Parkins <andyparkins@gmail.com> writes:\n\n> The colour parameter is \"color.branch\" rather than \"branch.color\" to\n> avoid clashing with the default namespace for default branch merge\n> definitions.\n\nVery nice.\n\n>  \t\tc = ' ';\n>  \t\tif (ref_list.list[i].kind == REF_LOCAL_BRANCH &&\n> -\t\t\t\t!strcmp(ref_list.list[i].name, head))\n> +\t\t\t\t!strcmp(ref_list.list[i].name, head)) {\n>  \t\t\tc = '*';\n> +\t\t\tcolor = COLOR_BRANCH_CURRENT;\n> +\t\t}\n>  \n>  \t\tif (verbose) {\n> -\t\t\tprintf(\"%c %-*s\", c, ref_list.maxwidth,\n> -\t\t\t       ref_list.list[i].name);\n> +\t\t\tprintf(\"%c %s%-*s%s\", c,\n> +\t\t\t\t\tbranch_get_color(color),\n> +\t\t\t\t\tref_list.maxwidth,\n> +\t\t\t\t\tref_list.list[i].name,\n> +\t\t\t\t\tbranch_get_color(COLOR_BRANCH_RESET));\n>  \t\t\tprint_ref_info(ref_list.list[i].sha1, abbrev);\n>  \t\t}\n>  \t\telse\n> -\t\t\tprintf(\"%c %s\\n\", c, ref_list.list[i].name);\n> +\t\t\tprintf(\"%c %s%s%s\\n\", c,\n> +\t\t\t\t\tbranch_get_color(color),\n> +\t\t\t\t\tref_list.list[i].name,\n> +\t\t\t\t\tbranch_get_color(COLOR_BRANCH_RESET));\n>  \t}\n\nNow this makes me wonder if under output coloring we would still\nwant the two-space indent and '*' prefix.\n"},{"id":"298288","messageId":"7vk60xuf8h.fsf@assigned-by-dhcp.cox.net","threadId":"43364","inReplyTo":"200612121103.31110.andyparkins@gmail.com","subject":"Re: [PATCH] Colourise git-branch output","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-12-12T18:43:42Z","receivedAt":"2006-12-12T18:43:42Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Andy Parkins <andyparkins@gmail.com> writes:\n\n>> +\t\"\",\t\t/* LOCAL (normal) */\n>> +\t\"\\033[32m\",\t/* CURRENT (green) */\n>\n> In keeping with the \"don't use green\" idea - can I suggest just bold normal \n> for the CURRENT?  That way there is the most minimal use of colour for the \n> default git-branch output, but still retaining a visual indicator.\n\nI do not have strong preference either way.\n\nThe CURRENT is highlighted with '*' so I think we certainly can\nlose green and even go vanilla.  I only tried to avoid bold\nbecause no built-in default coloring currently use it, and in\nthe past I've worked with terminals that do not have enough\ncontrast between normal and bold and for some people that might\nbecome an issue.\n"}]}