Re: [PATCH v2] config: add '--sources' option to print the source of a config value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Feb 10, 2016 at 11:13:18AM +0100, larsxschneider@xxxxxxxxx wrote:
> 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@xxxxxxxx>
> Signed-off-by: Lars Schneider <larsxschneider@xxxxxxxxx>
> ---
> 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

> +
> +	if (fp)
> +		fputc(term, fp);
> +	else
> +		strbuf_addch(buf, term);

Why not combine this 'if' with the preceding one?

> +
> +	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);
        ...
    }
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]