git-config --get-color: get configured color
This new option allows scripts to grab color setting from the user configuration, translated to ANSI color escape sequence. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Nov 27, 2007 at 22:41 UTC
9ce0352258a421b654b5db145a42d07cbaef416c
2 files changed
+68
-3
Documentation/git-config.txt
+16
@@ -20,6 +20,7 @@ SYNOPSIS
20
'git-config' [<file-option>] --rename-section old_name new_name
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
25
DESCRIPTION
26
-----------
@@ -134,6 +135,12 @@ See also <<FILES>>.
135
output without getting confused e.g. by values that
136
contain line breaks.
137
138
+--get-color name default::
139
+
140
+ Find the color configured for `name` (e.g. `color.diff.new`) and
141
+ output it as the ANSI color escape sequence to the standard
142
+ output. The optional `default` parameter is used instead, if
143
+ there is no color configured for `name`.
144
145
[[FILES]]
146
FILES
@@ -292,6 +299,15 @@ To add a new proxy, without altering any of the existing ones, use
299
% git config core.gitproxy '"proxy-command" for example.com'
300
------------
301
302
+An example to use customized color from the configuration in your
303
+script:
304
+
305
+------------
306
+#!/bin/sh
307
+WS=$(git config --get-color color.diff.whitespace "blue reverse")
308
+RESET=$(git config --get-color "" "reset")
309
+echo "${WS}your whitespace color or blue reverse${RESET}"
310
+------------
311
312
include::config.txt[]
313
builtin-config.c
+52
-3
@@ -1,8 +1,9 @@
1
#include "builtin.h"
2
#include "cache.h"
3
+#include "color.h"
4
5
static const char git_config_set_usage[] =
5
-"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";
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]";
7
8
static char *key;
9
static regex_t *key_regexp;
@@ -161,6 +162,53 @@ char *normalize_value(const char *key, const char *value)
162
return normalized;
163
}
164
165
+static int get_color_found;
166
+static const char *get_color_slot;
167
+static char parsed_color[COLOR_MAXLEN];
168
+
169
+static int git_get_color_config(const char *var, const char *value)
170
+{
171
+ if (!strcmp(var, get_color_slot)) {
172
+ color_parse(value, var, parsed_color);
173
+ get_color_found = 1;
174
+ }
175
+ return 0;
176
+}
177
+
178
+static int get_color(int argc, const char **argv)
179
+{
180
+ /*
181
+ * grab the color setting for the given slot from the configuration,
182
+ * or parse the default value if missing, and return ANSI color
183
+ * escape sequence.
184
+ *
185
+ * e.g.
186
+ * git config --get-color color.diff.whitespace "blue reverse"
187
+ */
188
+ const char *def_color = NULL;
189
+
190
+ switch (argc) {
191
+ default:
192
+ usage(git_config_set_usage);
193
+ case 2:
194
+ def_color = argv[1];
195
+ /* fallthru */
196
+ case 1:
197
+ get_color_slot = argv[0];
198
+ break;
199
+ }
200
+
201
+ get_color_found = 0;
202
+ parsed_color[0] = '\0';
203
+ git_config(git_get_color_config);
204
+
205
+ if (!get_color_found && def_color)
206
+ color_parse(def_color, "command line", parsed_color);
207
+
208
+ fputs(parsed_color, stdout);
209
+ return 0;
210
+}
211
+
212
int cmd_config(int argc, const char **argv, const char *prefix)
213
{
214
int nongit = 0;
@@ -234,8 +282,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
282
return 1;
283
}
284
return 0;
237
- }
238
- else
285
+ } else if (!strcmp(argv[1], "--get-color")) {
286
+ return get_color(argc-2, argv+2);
287
+ } else
288
break;
289
argc--;
290
argv++;