{"thread":{"id":"12168","subject":"[PATCH] Add color.ui variable which globally enables colorization if set","startedAt":"2008-02-18T07:26:03Z","lastAt":"2008-02-18T11:17:30Z","messageCount":3,"participants":["Matthias Kestenholz","Junio C Hamano","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"69096","messageId":"1203319563.6721.7.camel@futex","threadId":"12168","inReplyTo":null,"subject":"[PATCH] Add color.ui variable which globally enables colorization if set","fromName":"Matthias Kestenholz","fromEmail":"mk@spinlock.ch","sentAt":"2008-02-18T07:26:03Z","receivedAt":"2008-02-18T07:26:03Z","isPatch":true,"sender":{"key":"matthias@spinlock.ch","avatar":"https://gravatar.com/avatar/bc18f396e70163d09ab458a341b1decb7e8b6ee3aa2c0c954ec20162e67c4d46?d=mp&s=160"},"body":"Signed-off-by: Matthias Kestenholz <mk@spinlock.ch>\n---\n\nRebased on 'master' and tested. Thanks for all previous comments!\n\n\n Documentation/config.txt |    7 +++++++\n builtin-branch.c         |   10 +++++++---\n builtin-commit.c         |    4 ++++\n builtin-diff.c           |    5 +++++\n builtin-log.c            |   17 +++++++++++++++++\n color.c                  |   12 ++++++++++++\n color.h                  |   11 +++++++++++\n diff.c                   |    6 +++---\n diff.h                   |    1 +\n wt-status.c              |    6 +++---\n 10 files changed, 70 insertions(+), 9 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex f2f6a77..7b67671 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -489,6 +489,13 @@ color.status.<slot>::\n commit.template::\n \tSpecify a file to use as the template for new commit messages.\n \n+color.ui::\n+\tWhen set to `always`, always use colors in all git commands which\n+\tare capable of colored output. When false (or `never`), never. When\n+\tset to `true` or `auto`, use colors only when the output is to the\n+\tterminal. When more specific variables of color.* are set, they always\n+\ttake precedence over this setting. Defaults to false.\n+\n diff.autorefreshindex::\n \tWhen using `git diff` to compare with work tree\n \tfiles, do not consider stat-only change as changed.\ndiff --git a/builtin-branch.c b/builtin-branch.c\nindex e414c88..9edf2eb 100644\n--- a/builtin-branch.c\n+++ b/builtin-branch.c\n@@ -31,7 +31,7 @@ static unsigned char head_sha1[20];\n \n static int branch_track = 1;\n \n-static int branch_use_color;\n+static int branch_use_color = -1;\n static char branch_colors[][COLOR_MAXLEN] = {\n \t\"\\033[m\",\t/* reset */\n \t\"\",\t\t/* PLAIN (normal) */\n@@ -79,12 +79,12 @@ static int git_branch_config(const char *var, const char *value)\n \t\tbranch_track = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n-\treturn git_default_config(var, value);\n+\treturn git_color_default_config(var, value);\n }\n \n static const char *branch_get_color(enum color_branch ix)\n {\n-\tif (branch_use_color)\n+\tif (branch_use_color > 0)\n \t\treturn branch_colors[ix];\n \treturn \"\";\n }\n@@ -588,6 +588,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)\n \t};\n \n \tgit_config(git_branch_config);\n+\n+\tif (branch_use_color == -1)\n+\t\tbranch_use_color = git_use_color_default;\n+\n \ttrack = branch_track;\n \targc = parse_options(argc, argv, options, builtin_branch_usage, 0);\n \tif (!!delete + !!rename + !!force_create > 1)\ndiff --git a/builtin-commit.c b/builtin-commit.c\nindex 6612b4f..065e1f7 100644\n--- a/builtin-commit.c\n+++ b/builtin-commit.c\n@@ -7,6 +7,7 @@\n \n #include \"cache.h\"\n #include \"cache-tree.h\"\n+#include \"color.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n #include \"diff.h\"\n@@ -771,6 +772,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \n \tgit_config(git_status_config);\n \n+\tif (wt_status_use_color == -1)\n+\t\twt_status_use_color = git_use_color_default;\n+\n \targc = parse_and_validate_options(argc, argv, builtin_status_usage);\n \n \tindex_file = prepare_index(argc, argv, prefix);\ndiff --git a/builtin-diff.c b/builtin-diff.c\nindex 8d7a569..8f53f52 100644\n--- a/builtin-diff.c\n+++ b/builtin-diff.c\n@@ -4,6 +4,7 @@\n  * Copyright (c) 2006 Junio C Hamano\n  */\n #include \"cache.h\"\n+#include \"color.h\"\n #include \"commit.h\"\n #include \"blob.h\"\n #include \"tag.h\"\n@@ -229,6 +230,10 @@ int cmd_diff(int argc, const char **argv, const char *prefix)\n \n \tprefix = setup_git_directory_gently(&nongit);\n \tgit_config(git_diff_ui_config);\n+\n+\tif (diff_use_color_default == -1)\n+\t\tdiff_use_color_default = git_use_color_default;\n+\n \tinit_revisions(&rev, prefix);\n \trev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;\n \ndiff --git a/builtin-log.c b/builtin-log.c\nindex 99d69f0..f2216d3 100644\n--- a/builtin-log.c\n+++ b/builtin-log.c\n@@ -5,6 +5,7 @@\n  *\t\t 2006 Junio Hamano\n  */\n #include \"cache.h\"\n+#include \"color.h\"\n #include \"commit.h\"\n #include \"diff.h\"\n #include \"revision.h\"\n@@ -235,6 +236,10 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)\n \tstruct rev_info rev;\n \n \tgit_config(git_log_config);\n+\n+\tif (diff_use_color_default == -1)\n+\t\tdiff_use_color_default = git_use_color_default;\n+\n \tinit_revisions(&rev, prefix);\n \trev.diff = 1;\n \trev.simplify_history = 0;\n@@ -307,6 +312,10 @@ int cmd_show(int argc, const char **argv, const char *prefix)\n \tint i, count, ret = 0;\n \n \tgit_config(git_log_config);\n+\n+\tif (diff_use_color_default == -1)\n+\t\tdiff_use_color_default = git_use_color_default;\n+\n \tinit_revisions(&rev, prefix);\n \trev.diff = 1;\n \trev.combine_merges = 1;\n@@ -367,6 +376,10 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)\n \tstruct rev_info rev;\n \n \tgit_config(git_log_config);\n+\n+\tif (diff_use_color_default == -1)\n+\t\tdiff_use_color_default = git_use_color_default;\n+\n \tinit_revisions(&rev, prefix);\n \tinit_reflog_walk(&rev.reflog_info);\n \trev.abbrev_commit = 1;\n@@ -395,6 +408,10 @@ int cmd_log(int argc, const char **argv, const char *prefix)\n \tstruct rev_info rev;\n \n \tgit_config(git_log_config);\n+\n+\tif (diff_use_color_default == -1)\n+\t\tdiff_use_color_default = git_use_color_default;\n+\n \tinit_revisions(&rev, prefix);\n \trev.always_show_header = 1;\n \tcmd_log_init(argc, argv, prefix, &rev);\ndiff --git a/color.c b/color.c\nindex cb70340..12a6453 100644\n--- a/color.c\n+++ b/color.c\n@@ -3,6 +3,8 @@\n \n #define COLOR_RESET \"\\033[m\"\n \n+int git_use_color_default = 0;\n+\n static int parse_color(const char *name, int len)\n {\n \tstatic const char * const color_names[] = {\n@@ -143,6 +145,16 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)\n \treturn 0;\n }\n \n+int git_color_default_config(const char *var, const char *value)\n+{\n+\tif (!strcmp(var, \"color.ui\")) {\n+\t\tgit_use_color_default = git_config_colorbool(var, value, -1);\n+\t\treturn 0;\n+\t}\n+\n+\treturn git_default_config(var, value);\n+}\n+\n static int color_vfprintf(FILE *fp, const char *color, const char *fmt,\n \t\tva_list args, const char *trail)\n {\ndiff --git a/color.h b/color.h\nindex ff63513..ecda556 100644\n--- a/color.h\n+++ b/color.h\n@@ -4,6 +4,17 @@\n /* \"\\033[1;38;5;2xx;48;5;2xxm\\0\" is 23 bytes */\n #define COLOR_MAXLEN 24\n \n+/*\n+ * This variable stores the value of color.ui\n+ */\n+extern int git_use_color_default;\n+\n+\n+/*\n+ * Use this instead of git_default_config if you need the value of color.ui.\n+ */\n+int git_color_default_config(const char *var, const char *value);\n+\n int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);\n void color_parse(const char *var, const char *value, char *dst);\n int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);\ndiff --git a/diff.c b/diff.c\nindex 58fe775..c30c252 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -20,7 +20,7 @@\n \n static int diff_detect_rename_default;\n static int diff_rename_limit_default = 100;\n-static int diff_use_color_default;\n+int diff_use_color_default = -1;\n static const char *external_diff_cmd_cfg;\n int diff_auto_refresh_index = 1;\n \n@@ -191,7 +191,7 @@ int git_diff_basic_config(const char *var, const char *value)\n \t\t}\n \t}\n \n-\treturn git_default_config(var, value);\n+\treturn git_color_default_config(var, value);\n }\n \n static char *quote_two(const char *one, const char *two)\n@@ -2055,7 +2055,7 @@ void diff_setup(struct diff_options *options)\n \n \toptions->change = diff_change;\n \toptions->add_remove = diff_addremove;\n-\tif (diff_use_color_default)\n+\tif (diff_use_color_default > 0)\n \t\tDIFF_OPT_SET(options, COLOR_DIFF);\n \telse\n \t\tDIFF_OPT_CLR(options, COLOR_DIFF);\ndiff --git a/diff.h b/diff.h\nindex 073d5cb..8e73f07 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -174,6 +174,7 @@ extern void diff_unmerge(struct diff_options *,\n \n extern int git_diff_basic_config(const char *var, const char *value);\n extern int git_diff_ui_config(const char *var, const char *value);\n+extern int diff_use_color_default;\n extern void diff_setup(struct diff_options *);\n extern int diff_opt_parse(struct diff_options *, const char **, int);\n extern int diff_setup_done(struct diff_options *);\ndiff --git a/wt-status.c b/wt-status.c\nindex 0b06093..32d780a 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -9,7 +9,7 @@\n #include \"diffcore.h\"\n \n int wt_status_relative_paths = 1;\n-int wt_status_use_color = 0;\n+int wt_status_use_color = -1;\n static char wt_status_colors[][COLOR_MAXLEN] = {\n \t\"\",         /* WT_STATUS_HEADER: normal */\n \t\"\\033[32m\", /* WT_STATUS_UPDATED: green */\n@@ -40,7 +40,7 @@ static int parse_status_slot(const char *var, int offset)\n \n static const char* color(int slot)\n {\n-\treturn wt_status_use_color ? wt_status_colors[slot] : \"\";\n+\treturn wt_status_use_color > 0 ? wt_status_colors[slot] : \"\";\n }\n \n void wt_status_prepare(struct wt_status *s)\n@@ -401,5 +401,5 @@ int git_status_config(const char *k, const char *v)\n \t\twt_status_relative_paths = git_config_bool(k, v);\n \t\treturn 0;\n \t}\n-\treturn git_default_config(k, v);\n+\treturn git_color_default_config(k, v);\n }\n-- \n1.5.4.2.124.gc25103\n"},{"id":"69105","messageId":"7vfxvqr71o.fsf@gitster.siamese.dyndns.org","threadId":"12168","inReplyTo":"1203319563.6721.7.camel@futex","subject":"Re: [PATCH] Add color.ui variable which globally enables colorization if set","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-02-18T08:40:51Z","receivedAt":"2008-02-18T08:40:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Matthias Kestenholz <mk@spinlock.ch> writes:\n\n> Signed-off-by: Matthias Kestenholz <mk@spinlock.ch>\n> ---\n>\n> Rebased on 'master' and tested. Thanks for all previous comments!\n\nNicely done.  Will queue.  Thanks.\n"},{"id":"69121","messageId":"20080218111730.GB7672@sigill.intra.peff.net","threadId":"12168","inReplyTo":"7vfxvqr71o.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] Add color.ui variable which globally enables colorization if set","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-02-18T11:17:30Z","receivedAt":"2008-02-18T11:17:30Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Feb 18, 2008 at 12:40:51AM -0800, Junio C Hamano wrote:\n\n> Matthias Kestenholz <mk@spinlock.ch> writes:\n> \n> > Signed-off-by: Matthias Kestenholz <mk@spinlock.ch>\n> > ---\n> >\n> > Rebased on 'master' and tested. Thanks for all previous comments!\n> \n> Nicely done.  Will queue.  Thanks.\n\nLooks good to me, as well.\n\n-Peff\n"}]}