As of this commit, the canonical way to retreive an ANSI-compatible
color escape sequence from a configuration file is with the
`--get-color` action.

This is to allow Git to "fall back" on a default value for the color
should the given section not exist in the specified configuration(s).

With the addition of `--default`, this is no longer needed since:

  $ git config --default red --color core.section

will be have exactly as:

  $ git config --get-color core.section red

For consistency, let's introduce `--color` and encourage `--color`,
`--default` together over `--get-color` alone.

Signed-off-by: Taylor Blau <m...@ttaylorr.com>
---
 Documentation/git-config.txt |  7 +++++++
 builtin/config.c             | 13 +++++++++++++
 t/t1300-repo-config.sh       | 24 ++++++++++++++++++++++++
 t/t1310-config-default.sh    |  6 ++++++
 4 files changed, 50 insertions(+)

diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index d9d45aca3..e70c0a855 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -56,6 +56,9 @@ conversion to canonical form.  Currently supported type 
specifiers are:
 `expiry-date`::
     The value is taken as a timestamp.
 
+`color`::
+    The value is taken as an ANSI color escape sequence.
+
 For more information about the above type specifiers, see <<OPTIONS>> below.
 
 When reading, the values are read from the system, global and
@@ -198,6 +201,10 @@ See also <<FILES>>.
        a fixed or relative date-string to a timestamp. This option
        has no effect when setting the value.
 
+--color::
+  `git config` will ensure that the output is converted to an
+  ANSI color escape sequence.
+
 -z::
 --null::
        For all options that output values and/or keys, always
diff --git a/builtin/config.c b/builtin/config.c
index 1410be850..e8661bc12 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -61,6 +61,7 @@ static int show_origin;
 #define TYPE_BOOL_OR_INT (1<<2)
 #define TYPE_PATH (1<<3)
 #define TYPE_EXPIRY_DATE (1<<4)
+#define TYPE_COLOR (1<<5)
 
 static struct option builtin_config_options[] = {
        OPT_GROUP(N_("Config file location")),
@@ -90,6 +91,7 @@ static struct option builtin_config_options[] = {
        OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), 
TYPE_BOOL_OR_INT),
        OPT_BIT(0, "path", &types, N_("value is a path (file or directory 
name)"), TYPE_PATH),
        OPT_BIT(0, "expiry-date", &types, N_("value is an expiry date"), 
TYPE_EXPIRY_DATE),
+       OPT_BIT(0, "color", &types, N_("value is a color"), TYPE_COLOR),
        OPT_GROUP(N_("Other")),
        OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
        OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
@@ -175,6 +177,11 @@ static int format_config(struct strbuf *buf, const char 
*key_, const char *value
                        if (git_config_expiry_date(&t, key_, value_) < 0)
                                return -1;
                        strbuf_addf(buf, "%"PRItime, t);
+               } else if (types == TYPE_COLOR) {
+                       char v[COLOR_MAXLEN];
+                       if (git_config_color(v, key_, value_) < 0)
+                               return -1;
+                       strbuf_addstr(buf, v);
                } else if (value_) {
                        strbuf_addstr(buf, value_);
                } else {
@@ -320,6 +327,12 @@ static char *normalize_value(const char *key, const char 
*value)
                else
                        return xstrdup(v ? "true" : "false");
        }
+       if (types == TYPE_COLOR) {
+               char v[COLOR_MAXLEN];
+               if (!git_config_color(v, key, value))
+                       return xstrdup(value);
+               die("cannot parse color '%s'", value);
+       }
 
        die("BUG: cannot normalize type %d", types);
 }
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 4f8e6f5fd..dab94d0c4 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -931,6 +931,30 @@ test_expect_success 'get --expiry-date' '
        test_must_fail git config --expiry-date date.invalid1
 '
 
+test_expect_success 'get --color' '
+       rm .git/config &&
+       git config foo.color "red" &&
+       git config --get --color foo.color | test_decode_color >actual &&
+       echo "<RED>" >expect &&
+       test_cmp expect actual
+'
+
+cat >expect << EOF
+[foo]
+       color = red
+EOF
+
+test_expect_success 'set --color' '
+       rm .git/config &&
+       git config --color foo.color "red" &&
+       test_cmp expect .git/config
+'
+
+test_expect_success 'get --color barfs on non-color' '
+       echo "[foo]bar=not-a-color" >.git/config &&
+       test_must_fail git config --get --color foo.bar
+'
+
 cat > expect << EOF
 [quote]
        leading = " test"
diff --git a/t/t1310-config-default.sh b/t/t1310-config-default.sh
index 0e464c206..0ebece7d2 100755
--- a/t/t1310-config-default.sh
+++ b/t/t1310-config-default.sh
@@ -62,6 +62,12 @@ test_expect_success 'marshal default value as expiry-date' '
        test_cmp expect actual
 '
 
+test_expect_success 'marshal default value as color' '
+       echo "\033[31m" >expect &&
+       git config --default red --color core.foo >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'does not allow --default with --get-all' '
        test_must_fail git config --default quux --get-all a >output 2>&1 &&
        test_i18ngrep "\-\-default is only applicable to" output
-- 
2.16.2.440.gc6284da4f

Reply via email to