{"thread":{"id":"11564","subject":"[PATCH/resend] Add color.ui variable which globally enables colorization if set","startedAt":"2008-01-10T09:03:16Z","lastAt":"2008-01-10T09:03:16Z","messageCount":1,"participants":["Matthias Kestenholz"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"64914","messageId":"1199955796.6831.5.camel@futex","threadId":"11564","inReplyTo":null,"subject":"[PATCH/resend] Add color.ui variable which globally enables colorization if set","fromName":"Matthias Kestenholz","fromEmail":"mk@spinlock.ch","sentAt":"2008-01-10T09:03:16Z","receivedAt":"2008-01-10T09:03:16Z","isPatch":true,"sender":{"key":"matthias@spinlock.ch","avatar":"https://gravatar.com/avatar/bc18f396e70163d09ab458a341b1decb7e8b6ee3aa2c0c954ec20162e67c4d46?d=mp&s=160"},"body":"This patch introduces the color.ui variable which globally enables colorization.\nMore specific colorization variables like color.branch, color.diff etc. take\nprecedence.\n\ncolor.ui is not respected by git add --interactive and git svn yet.\n\nSigned-off-by: Matthias Kestenholz <mk@spinlock.ch>\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 1b6d6d6..b55f3b4 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -439,6 +439,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 089cae5..9a1eb21 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@@ -76,12 +76,12 @@ static int git_branch_config(const char *var, const char *value)\n \tif (!strcmp(var, \"branch.autosetupmerge\"))\n \t\t\tbranch_track = git_config_bool(var, value);\n \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@@ -585,6 +585,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 73f1e35..ba60cfa 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@@ -640,6 +641,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 29365a0..77a9c9a 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 dcc9f81..880da94 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 7f66c29..09b81fe 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 b18c140..d6f23c7 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@@ -189,7 +189,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@@ -2048,7 +2048,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 c0c2472..0dfc909 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@@ -409,5 +409,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.rc2.68.ge708a-dirty\n"}]}