On Fri, Mar 23, 2018 at 08:55:56PM -0400, Taylor Blau wrote:

> 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.

s/retreive/retrieve/

> 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.

I don't think we'll ever get rid of --get-color (at the very least, we'd
need a deprecation period). But it's probably worth adding a note under
the --get-color description to mention that it's a historical synonym,
and that using "--default" should be preferred.

> @@ -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")),

I just had a funny thought. Normally in Git the "--color" option means
"colorize the output". And we are diverging from that here. I wonder if
anybody would be confused by that, or if we would ever want to later add
an option to colorize git-config output. Would we regret squatting on
--color?

I'm not sure what else to name it. Anything _except_ "--color" would
diverge from the existing scheme of "--<type>".

If we were designing from scratch, I'd consider:

  git config --type=int ...
  git config --type=color ...

etc. I'm not sure if it's worth trying to switch now (on the other hand,
it resolves the documentation issue I mentioned earlier, since that
would naturally group all of the types ;) ).

It would be pretty easy to declare "--type" as the Right Way, and list
"--int" as a historical synonym for "--type=int".

> @@ -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);

Looks good.

> @@ -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);
> +     }

Interesting. This doesn't actually normalize anything, since we always
pass back the original value (or die). I think that's the right thing to
do, since otherwise you'd end up with ANSI codes in your config file.

I wondered at first if this should go in the "noop" normalization that
TYPE_PATH undergoes. But I like that it actually sanity-checks the
value. We should probably have a comment here explaining that yes, we
parse and then throw away the value (similar to the one near TYPE_PATH).

I suspect that TYPE_EXPIRY_DATE should do the same thing (parse and
complain if you fed nonsense, but always keep the original value).

> +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
> +'

We should probably write this as:

  git config --get --color foo.color >actual.raw &&
  test_decode_color <actual.raw >actual

to catch failures from git-config itself (there's a lot of old tests
which pipe, but we've been trying to convert them to be more careful).

> +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
> +'

After reading the normalize bits above, I think there's one more case to
cover:

  test_must_fail git config --color foo.color not-a-color

> 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
> +'

I don't offhand recall whether octal escapes with "echo" are portable.
It _is_ in POSIX, but only for XSI. I think using "printf" would be
portable.

-Peff

Reply via email to