git config --get-colorbool
This adds an option to help scripts find out color settings from the configuration file. git config --get-colorbool color.diff inspects color.diff variable, and exits with status 0 (i.e. success) if color is to be used. It exits with status 1 otherwise. If a script wants "true"/"false" answer to the standard output of the command, it can pass an additional boolean parameter to its command line, telling if its standard output is a terminal, like this: git config --get-colorbool color.diff true When called like this, the command outputs "true" to its standard output if color is to be used (i.e. "color.diff" says "always", "auto", or "true"), and "false" otherwise. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Dec 5, 2007 at 17:26 UTC
0f6f5a4022de5904926cd528c681468e3f635256
7 files changed
+58
-7
Documentation/git-config.txt
+10
@@ -21,6 +21,7 @@ SYNOPSIS
21
'git-config' [<file-option>] --remove-section name
22
'git-config' [<file-option>] [-z|--null] -l | --list
23
'git-config' [<file-option>] --get-color name [default]
24
+'git-config' [<file-option>] --get-colorbool name [stdout-is-tty]
25
26
DESCRIPTION
27
-----------
@@ -135,6 +136,15 @@ See also <<FILES>>.
136
output without getting confused e.g. by values that
137
contain line breaks.
138
139
+--get-colorbool name [stdout-is-tty]::
140
+
141
+ Find the color setting for `name` (e.g. `color.diff`) and output
142
+ "true" or "false". `stdout-is-tty` should be either "true" or
143
+ "false", and is taken into account when configuration says
144
+ "auto". If `stdout-is-tty` is missing, then checks the standard
145
+ output of the command itself, and exits with status 0 if color
146
+ is to be used, or exits with status 1 otherwise.
147
+
148
--get-color name default::
149
150
Find the color configured for `name` (e.g. `color.diff.new`) and
builtin-branch.c
+1
-1
@@ -65,7 +65,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
65
static int git_branch_config(const char *var, const char *value)
66
{
67
if (!strcmp(var, "color.branch")) {
68
- branch_use_color = git_config_colorbool(var, value);
68
+ branch_use_color = git_config_colorbool(var, value, -1);
69
return 0;
70
}
71
if (!prefixcmp(var, "color.branch.")) {
builtin-config.c
+40
-1
@@ -3,7 +3,7 @@
3
#include "color.h"
4
5
static const char git_config_set_usage[] =
6
-"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default]";
6
+"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default] | --get-colorbool name [stdout-is-tty]";
7
8
static char *key;
9
static regex_t *key_regexp;
@@ -208,6 +208,43 @@ static int get_color(int argc, const char **argv)
208
return 0;
209
}
210
211
+static int stdout_is_tty;
212
+static int get_colorbool_found;
213
+static int git_get_colorbool_config(const char *var, const char *value)
214
+{
215
+ if (!strcmp(var, get_color_slot))
216
+ get_colorbool_found =
217
+ git_config_colorbool(var, value, stdout_is_tty);
218
+ return 0;
219
+}
220
+
221
+static int get_colorbool(int argc, const char **argv)
222
+{
223
+ /*
224
+ * git config --get-colorbool <slot> [<stdout-is-tty>]
225
+ *
226
+ * returns "true" or "false" depending on how <slot>
227
+ * is configured.
228
+ */
229
+
230
+ if (argc == 2)
231
+ stdout_is_tty = git_config_bool("command line", argv[1]);
232
+ else if (argc == 1)
233
+ stdout_is_tty = isatty(1);
234
+ else
235
+ usage(git_config_set_usage);
236
+ get_colorbool_found = 0;
237
+ get_color_slot = argv[0];
238
+ git_config(git_get_colorbool_config);
239
+
240
+ if (argc == 1) {
241
+ return get_colorbool_found ? 0 : 1;
242
+ } else {
243
+ printf("%s\n", get_colorbool_found ? "true" : "false");
244
+ return 0;
245
+ }
246
+}
247
+
248
int cmd_config(int argc, const char **argv, const char *prefix)
249
{
250
int nongit = 0;
@@ -283,6 +320,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
320
return 0;
321
} else if (!strcmp(argv[1], "--get-color")) {
322
return get_color(argc-2, argv+2);
323
+ } else if (!strcmp(argv[1], "--get-colorbool")) {
324
+ return get_colorbool(argc-2, argv+2);
325
} else
326
break;
327
argc--;
color.c
+4
-2
@@ -116,7 +116,7 @@ bad:
116
die("bad config value '%s' for variable '%s'", value, var);
117
}
118
119
-int git_config_colorbool(const char *var, const char *value)
119
+int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
120
{
121
if (value) {
122
if (!strcasecmp(value, "never"))
@@ -133,7 +133,9 @@ int git_config_colorbool(const char *var, const char *value)
133
134
/* any normal truth value defaults to 'auto' */
135
auto_color:
136
- if (isatty(1) || (pager_in_use && pager_use_color)) {
136
+ if (stdout_is_tty < 0)
137
+ stdout_is_tty = isatty(1);
138
+ if (stdout_is_tty || (pager_in_use && pager_use_color)) {
139
char *term = getenv("TERM");
140
if (term && strcmp(term, "dumb"))
141
return 1;
color.h
+1
-1
@@ -4,7 +4,7 @@
4
/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
5
#define COLOR_MAXLEN 24
6
7
-int git_config_colorbool(const char *var, const char *value);
7
+int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
8
void color_parse(const char *var, const char *value, char *dst);
9
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
10
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
diff.c
+1
-1
@@ -146,7 +146,7 @@ int git_diff_ui_config(const char *var, const char *value)
146
return 0;
147
}
148
if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
149
- diff_use_color_default = git_config_colorbool(var, value);
149
+ diff_use_color_default = git_config_colorbool(var, value, -1);
150
return 0;
151
}
152
if (!strcmp(var, "diff.renames")) {
wt-status.c
+1
-1
@@ -391,7 +391,7 @@ void wt_status_print(struct wt_status *s)
391
int git_status_config(const char *k, const char *v)
392
{
393
if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
394
- wt_status_use_color = git_config_colorbool(k, v);
394
+ wt_status_use_color = git_config_colorbool(k, v, -1);
395
return 0;
396
}
397
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {