All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] Identify where a Git config is defined
@ 2016-02-02  9:27 Lars Schneider
  2016-02-02 10:15 ` Jeff King
  0 siblings, 1 reply; 3+ messages in thread
From: Lars Schneider @ 2016-02-02  9:27 UTC (permalink / raw)
  To: Git Users

Hi,

Using "git config --list" shows me all configs but sometimes I have a hard time to figure out where a certain config is defined. This is especially true on Windows as I found the system config in various places. I wonder if other people would find it useful to enable something like "git config --list --print-source" where every config value is printed with the file where it originates from.

If I read the source correctly this would mean I would need to change "config_fn_t" to pass not only key and value but also the config source file in addition. Since "config_fn_t" is used in many places this would be a big change that probably is not worth the effort?!

Alternatively I was thinking about "git config --print-source-files" to print all config files that Git would parse. This would already help to find the configs and would probably be a smaller change.

Thoughts?

Thanks,
Lars

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC] Identify where a Git config is defined
  2016-02-02  9:27 [RFC] Identify where a Git config is defined Lars Schneider
@ 2016-02-02 10:15 ` Jeff King
  2016-02-02 10:32   ` Jeff King
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff King @ 2016-02-02 10:15 UTC (permalink / raw)
  To: Lars Schneider; +Cc: Git Users

On Tue, Feb 02, 2016 at 10:27:06AM +0100, Lars Schneider wrote:

> Using "git config --list" shows me all configs but sometimes I have a
> hard time to figure out where a certain config is defined. This is
> especially true on Windows as I found the system config in various
> places. I wonder if other people would find it useful to enable
> something like "git config --list --print-source" where every config
> value is printed with the file where it originates from.

We discussed this exact thing a while ago, and it looks like I started
on a patch, but didn't pursue it much further. There's some discussion
in there if you are interested in designing such a feature:

  http://thread.gmane.org/gmane.comp.version-control.git/190027/focus=190267

It looks like I tweaked it at some point, and I've been carrying this in
my tree (rebasing forward and using it in my normal build):

  git fetch git://github.com/peff/git jk/config-sources

Feel free to use it as a starting point if that's helpful. I don't
recall offhand how close it is to ready.

> If I read the source correctly this would mean I would need to change
> "config_fn_t" to pass not only key and value but also the config
> source file in addition. Since "config_fn_t" is used in many places
> this would be a big change that probably is not worth the effort?!

There's a global struct for the current config file.  In the patches
above, I just added an accessor function. Hooray for single-threaded
programs.

> Alternatively I was thinking about "git config --print-source-files"
> to print all config files that Git would parse. This would already
> help to find the configs and would probably be a smaller change.

I think the "--sources" option is more useful, because it can show
included files, too.

-Peff

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC] Identify where a Git config is defined
  2016-02-02 10:15 ` Jeff King
@ 2016-02-02 10:32   ` Jeff King
  0 siblings, 0 replies; 3+ messages in thread
From: Jeff King @ 2016-02-02 10:32 UTC (permalink / raw)
  To: Lars Schneider; +Cc: Git Users

On Tue, Feb 02, 2016 at 05:15:51AM -0500, Jeff King wrote:

> It looks like I tweaked it at some point, and I've been carrying this in
> my tree (rebasing forward and using it in my normal build):
> 
>   git fetch git://github.com/peff/git jk/config-sources
> 
> Feel free to use it as a starting point if that's helpful. I don't
> recall offhand how close it is to ready.

Just to leave a record in the list archive, here's the patch itself:

-- >8 --
Subject: [PATCH] show sources of config options (WIP)

 - needs tests
 - probably should complain when not LIST or getting a value

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/config.c | 32 ++++++++++++++++++++++++++++++++
 cache.h          |  1 +
 config.c         |  7 +++++++
 3 files changed, 40 insertions(+)

diff --git a/builtin/config.c b/builtin/config.c
index adc7727..b8caf18 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -3,6 +3,7 @@
 #include "color.h"
 #include "parse-options.h"
 #include "urlmatch.h"
+#include "quote.h"
 
 static const char *const builtin_config_usage[] = {
 	N_("git config [<options>]"),
@@ -27,6 +28,7 @@ static int actions, types;
 static const char *get_color_slot, *get_colorbool_slot;
 static int end_null;
 static int respect_includes = -1;
+static int show_sources;
 
 #define ACTION_GET (1<<0)
 #define ACTION_GET_ALL (1<<1)
@@ -81,6 +83,7 @@ static struct option builtin_config_options[] = {
 	OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
 	OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
 	OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
+	OPT_BOOL(0, "sources", &show_sources, N_("show source filenames of config")),
 	OPT_END(),
 };
 
@@ -91,8 +94,35 @@ static void check_argc(int argc, int min, int max) {
 	usage_with_options(builtin_config_usage, builtin_config_options);
 }
 
+/* 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';
+	const char *fn = current_config_filename();
+
+	if (!fn)
+		fn = "";
+
+	if (!end_null)
+		quote_c_style(fn, buf, fp, 0);
+	else {
+		term = '\0';
+		if (fp)
+			fprintf(fp, "%s", fn);
+		else
+			strbuf_addstr(buf, fn);
+	}
+
+	if (fp)
+		fputc(term, fp);
+	else
+		strbuf_addch(buf, term);
+}
+
 static int show_all_config(const char *key_, const char *value_, void *cb)
 {
+	if (show_sources)
+		show_config_source(NULL, stdout);
 	if (!omit_values && value_)
 		printf("%s%c%s%c", key_, delim, value_, term);
 	else
@@ -108,6 +138,8 @@ struct strbuf_list {
 
 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 {
+	if (show_sources)
+		show_config_source(buf, NULL);
 	if (show_keys)
 		strbuf_addstr(buf, key_);
 	if (!omit_values) {
diff --git a/cache.h b/cache.h
index dfc459c..95a7f65 100644
--- a/cache.h
+++ b/cache.h
@@ -1528,6 +1528,7 @@ extern const char *get_log_output_encoding(void);
 extern const char *get_commit_output_encoding(void);
 
 extern int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
+extern const char *current_config_filename(void);
 
 struct config_include_data {
 	int depth;
diff --git a/config.c b/config.c
index 86a5eb2..b437002 100644
--- a/config.c
+++ b/config.c
@@ -2385,3 +2385,10 @@ int parse_config_key(const char *var,
 
 	return 0;
 }
+
+const char *current_config_filename(void)
+{
+	if (cf && cf->name)
+		return cf->name;
+	return NULL;
+}
-- 
2.7.0.489.g6faad84

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-02-02 10:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-02  9:27 [RFC] Identify where a Git config is defined Lars Schneider
2016-02-02 10:15 ` Jeff King
2016-02-02 10:32   ` Jeff King

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.