@matheusbsbs / matheus-bagliano / commits / 6b2f2d9805
Fork 0

Add color.ui variable which globally enables colorization if set

Signed-off-by: Matthias Kestenholz <mk@spinlock.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthias Kestenholz committed Feb 18, 2008 at 08:26 UTC 6b2f2d9805dd22c6f74957e0d76a1d2921b40c16
10 files changed +70 -9
Documentation/config.txt
+7
@@ -489,6 +489,13 @@ color.status.<slot>::
489 commit.template::
490 Specify a file to use as the template for new commit messages.
491
492 +color.ui::
493 + When set to `always`, always use colors in all git commands which
494 + are capable of colored output. When false (or `never`), never. When
495 + set to `true` or `auto`, use colors only when the output is to the
496 + terminal. When more specific variables of color.* are set, they always
497 + take precedence over this setting. Defaults to false.
498 +
499 diff.autorefreshindex::
500 When using `git diff` to compare with work tree
501 files, do not consider stat-only change as changed.
builtin-branch.c
+7 -3
@@ -31,7 +31,7 @@ static unsigned char head_sha1[20];
31
32 static int branch_track = 1;
33
34 -static int branch_use_color;
34 +static int branch_use_color = -1;
35 static char branch_colors[][COLOR_MAXLEN] = {
36 "\033[m", /* reset */
37 "", /* PLAIN (normal) */
@@ -79,12 +79,12 @@ static int git_branch_config(const char *var, const char *value)
79 branch_track = git_config_bool(var, value);
80 return 0;
81 }
82 - return git_default_config(var, value);
82 + return git_color_default_config(var, value);
83 }
84
85 static const char *branch_get_color(enum color_branch ix)
86 {
87 - if (branch_use_color)
87 + if (branch_use_color > 0)
88 return branch_colors[ix];
89 return "";
90 }
@@ -588,6 +588,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
588 };
589
590 git_config(git_branch_config);
591 +
592 + if (branch_use_color == -1)
593 + branch_use_color = git_use_color_default;
594 +
595 track = branch_track;
596 argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
597 if (!!delete + !!rename + !!force_create > 1)
builtin-commit.c
+4
@@ -7,6 +7,7 @@
7
8 #include "cache.h"
9 #include "cache-tree.h"
10 +#include "color.h"
11 #include "dir.h"
12 #include "builtin.h"
13 #include "diff.h"
@@ -771,6 +772,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
772
773 git_config(git_status_config);
774
775 + if (wt_status_use_color == -1)
776 + wt_status_use_color = git_use_color_default;
777 +
778 argc = parse_and_validate_options(argc, argv, builtin_status_usage);
779
780 index_file = prepare_index(argc, argv, prefix);
builtin-diff.c
+5
@@ -4,6 +4,7 @@
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #include "cache.h"
7 +#include "color.h"
8 #include "commit.h"
9 #include "blob.h"
10 #include "tag.h"
@@ -229,6 +230,10 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
230
231 prefix = setup_git_directory_gently(&nongit);
232 git_config(git_diff_ui_config);
233 +
234 + if (diff_use_color_default == -1)
235 + diff_use_color_default = git_use_color_default;
236 +
237 init_revisions(&rev, prefix);
238 rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
239
builtin-log.c
+17
@@ -5,6 +5,7 @@
5 * 2006 Junio Hamano
6 */
7 #include "cache.h"
8 +#include "color.h"
9 #include "commit.h"
10 #include "diff.h"
11 #include "revision.h"
@@ -235,6 +236,10 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
236 struct rev_info rev;
237
238 git_config(git_log_config);
239 +
240 + if (diff_use_color_default == -1)
241 + diff_use_color_default = git_use_color_default;
242 +
243 init_revisions(&rev, prefix);
244 rev.diff = 1;
245 rev.simplify_history = 0;
@@ -307,6 +312,10 @@ int cmd_show(int argc, const char **argv, const char *prefix)
312 int i, count, ret = 0;
313
314 git_config(git_log_config);
315 +
316 + if (diff_use_color_default == -1)
317 + diff_use_color_default = git_use_color_default;
318 +
319 init_revisions(&rev, prefix);
320 rev.diff = 1;
321 rev.combine_merges = 1;
@@ -367,6 +376,10 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
376 struct rev_info rev;
377
378 git_config(git_log_config);
379 +
380 + if (diff_use_color_default == -1)
381 + diff_use_color_default = git_use_color_default;
382 +
383 init_revisions(&rev, prefix);
384 init_reflog_walk(&rev.reflog_info);
385 rev.abbrev_commit = 1;
@@ -395,6 +408,10 @@ int cmd_log(int argc, const char **argv, const char *prefix)
408 struct rev_info rev;
409
410 git_config(git_log_config);
411 +
412 + if (diff_use_color_default == -1)
413 + diff_use_color_default = git_use_color_default;
414 +
415 init_revisions(&rev, prefix);
416 rev.always_show_header = 1;
417 cmd_log_init(argc, argv, prefix, &rev);
color.c
+12
@@ -3,6 +3,8 @@
3
4 #define COLOR_RESET "\033[m"
5
6 +int git_use_color_default = 0;
7 +
8 static int parse_color(const char *name, int len)
9 {
10 static const char * const color_names[] = {
@@ -143,6 +145,16 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
145 return 0;
146 }
147
148 +int git_color_default_config(const char *var, const char *value)
149 +{
150 + if (!strcmp(var, "color.ui")) {
151 + git_use_color_default = git_config_colorbool(var, value, -1);
152 + return 0;
153 + }
154 +
155 + return git_default_config(var, value);
156 +}
157 +
158 static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
159 va_list args, const char *trail)
160 {
color.h
+11
@@ -4,6 +4,17 @@
4 /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
5 #define COLOR_MAXLEN 24
6
7 +/*
8 + * This variable stores the value of color.ui
9 + */
10 +extern int git_use_color_default;
11 +
12 +
13 +/*
14 + * Use this instead of git_default_config if you need the value of color.ui.
15 + */
16 +int git_color_default_config(const char *var, const char *value);
17 +
18 int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
19 void color_parse(const char *var, const char *value, char *dst);
20 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
diff.c
+3 -3
@@ -20,7 +20,7 @@
20
21 static int diff_detect_rename_default;
22 static int diff_rename_limit_default = 100;
23 -static int diff_use_color_default;
23 +int diff_use_color_default = -1;
24 static const char *external_diff_cmd_cfg;
25 int diff_auto_refresh_index = 1;
26
@@ -191,7 +191,7 @@ int git_diff_basic_config(const char *var, const char *value)
191 }
192 }
193
194 - return git_default_config(var, value);
194 + return git_color_default_config(var, value);
195 }
196
197 static char *quote_two(const char *one, const char *two)
@@ -2055,7 +2055,7 @@ void diff_setup(struct diff_options *options)
2055
2056 options->change = diff_change;
2057 options->add_remove = diff_addremove;
2058 - if (diff_use_color_default)
2058 + if (diff_use_color_default > 0)
2059 DIFF_OPT_SET(options, COLOR_DIFF);
2060 else
2061 DIFF_OPT_CLR(options, COLOR_DIFF);
diff.h
+1
@@ -174,6 +174,7 @@ extern void diff_unmerge(struct diff_options *,
174
175 extern int git_diff_basic_config(const char *var, const char *value);
176 extern int git_diff_ui_config(const char *var, const char *value);
177 +extern int diff_use_color_default;
178 extern void diff_setup(struct diff_options *);
179 extern int diff_opt_parse(struct diff_options *, const char **, int);
180 extern int diff_setup_done(struct diff_options *);
wt-status.c
+3 -3
@@ -9,7 +9,7 @@
9 #include "diffcore.h"
10
11 int wt_status_relative_paths = 1;
12 -int wt_status_use_color = 0;
12 +int wt_status_use_color = -1;
13 static char wt_status_colors[][COLOR_MAXLEN] = {
14 "", /* WT_STATUS_HEADER: normal */
15 "\033[32m", /* WT_STATUS_UPDATED: green */
@@ -40,7 +40,7 @@ static int parse_status_slot(const char *var, int offset)
40
41 static const char* color(int slot)
42 {
43 - return wt_status_use_color ? wt_status_colors[slot] : "";
43 + return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
44 }
45
46 void wt_status_prepare(struct wt_status *s)
@@ -401,5 +401,5 @@ int git_status_config(const char *k, const char *v)
401 wt_status_relative_paths = git_config_bool(k, v);
402 return 0;
403 }
404 - return git_default_config(k, v);
404 + return git_color_default_config(k, v);
405 }