Re: [PATCH v2] config: add '--sources' option to print the source of a config value
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Feb 10, 2016, 19:03 UTC
- Message-ID
- <20160210190328.GA2376@flurp.local>
- In-Reply-To
- <1455099198-11515-1-git-send-email-larsxschneider@gmail.com>
On Wed, Feb 10, 2016 at 11:13:18AM +0100, larsxschneider@gmail.com wrote:
Show 39 quoted lines
> If config values are queried using 'git config' (e.g. via '--list' flag
> or the '--get*' flags) then it is sometimes hard to find the
> configuration file where the values were defined.
>
> Teach 'git config' the '--sources' option to print the source
> configuration file for every printed value.
>
> Based-on-patch-by: Jeff King <peff@peff.net>
> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
> ---
> diff --git a/builtin/config.c b/builtin/config.c
> @@ -91,8 +94,58 @@ static void check_argc(int argc, int min, int max) {
> +/* output to either fp or buf; only one should be non-NULL */
> +static void show_config_source(struct strbuf *buf, FILE *fp)
> +{
> + char term = '\t';
> + char *prefix;
> + const char *fn = current_config_filename();
> +
> + if (end_null)
> + term = '\0';
> +
> + if (fn) {
> + if (given_config_source.use_stdin)
> + prefix = "stdin";
> + else if (given_config_source.blob)
> + prefix = "blob";
> + else
> + prefix = "file";
> + } else {
> + fn = "";
> + prefix = "cmd";
> + }
> +
> + if (fp)
> + fprintf(fp, "%s", prefix);
> + else {
> + strbuf_addstr(buf, prefix);
> + }Style: drop unnecessary braces
Show 5 quoted lines
> + > + if (fp) > + fputc(term, fp); > + else > + strbuf_addch(buf, term);
Why not combine this 'if' with the preceding one?
Show 15 quoted lines
> +
> + if (!end_null)
> + quote_c_style(fn, buf, fp, 0);
> + else {
> + if (fp)
> + fprintf(fp, "%s", fn);
> + else
> + strbuf_addstr(buf, fn);
> + }
> +
> + if (fp)
> + fputc(term, fp);
> + else
> + strbuf_addch(buf, term);
> +}Overall, due to its duality (outputting to FILE* or strbuf), this function is more difficult to read than it probably ought to be. Have you considered simplifying it by making it single-purpose. Something like this, for instance:
static void get_config_source(const char **name, const char **prefix)
{
*name = current_config_filename();
if (!*name) {
*name = "";
*prefix = "cmd";
} else if (given_config_source.use_stdin)
*prefix = "stdin";
else if (given_config_source.blob)
*prefix = "blob";
else
*prefix = "file";
} static void show_config_source(struct strbuf *buf)
{
char term = end_null ? '\0' : '\t';
char *prefix;
const char *name; get_config_source(&name, &prefix);
strbuf_addstr(buf, prefix);
strbuf_addch(buf, term);
if (end_null)
strbuf_addstr(buf, name);
else
quote_c_style(name, buf, NULL, 0);
strbuf_addch(buf, term);
} static int show_all_config(...)
{
if (show_sources) {
struct strbuf buf = STRBUF_INIT;
show_config_source(&buf);
fputs(buf.buf, stdout);
strbuf_release(&buf);
}
...
} static int format_config(...)
{
if (show_sources)
show_config_source(buf);
...
}