Skip to contentSkip to conversation

config: add string mapping for enum config_scope

PATCH In review Emily Shaffer 3 participants 7 messages Dec 11, 2019 – Dec 12, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 config: add string mapping for enum config_scope

If a user is interacting with their config files primarily by the 'git config' command, using the location flags (--global, --system, etc) then they may be more interested to see the scope of the config file they are editing, rather than the filepath.

---

Since v1, the only change is removing locale markers around the strings returned from the helper.

As mentioned in lore.kernel.org/git/[email protected] I'm still not sure whether it's better to return "local" for CONFIG_SCOPE_REPO. Since that's the scope returned for both local and worktree (.git/config, .git/config.worktree) configs, I'm happy to leave it the way it is to indicate "one of the configs in the repo".

- Emily

 config.c | 17 +++++++++++++++++
 config.h |  1 +
 2 files changed, 18 insertions(+)
config.c +17 -0
index e7052b3977..baab4a916e 100644
@@ -3312,6 +3312,23 @@ enum config_scope current_config_scope(void)
 		return current_parsing_scope;
 }
 
+const char *config_scope_to_string(enum config_scope scope)
+{
+	switch (scope) {
+	case CONFIG_SCOPE_SYSTEM:
+		return "system";
+	case CONFIG_SCOPE_GLOBAL:
+		return "global";
+	case CONFIG_SCOPE_REPO:
+		return "repo";
+	case CONFIG_SCOPE_CMDLINE:
+		return "cmdline";
+	case CONFIG_SCOPE_UNKNOWN:
+	default:
+		return "unknown";
+	}
+}
+
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
 	int i;
config.h +1 -0
index 91fd4c5e96..c8bf296dcc 100644
@@ -303,6 +303,7 @@ enum config_scope {
 };
 
 enum config_scope current_config_scope(void);
+const char *config_scope_to_string(enum config_scope);
 const char *current_config_origin_type(void);
 const char *current_config_name(void);
 
Signature · 1 line
2.24.0.525.g8f36a354ae-goog
#1 lore raw
Quoted 65 lines · Emily Shaffer

If a user is interacting with their config files primarily by the 'git config' command, using the location flags (--global, --system, etc) then they may be more interested to see the scope of the config file they are editing, rather than the filepath.

---

Since v1, the only change is removing locale markers around the strings returned from the helper.

As mentioned in lore.kernel.org/git/[email protected] I'm still not sure whether it's better to return "local" for CONFIG_SCOPE_REPO. Since that's the scope returned for both local and worktree (.git/config, .git/config.worktree) configs, I'm happy to leave it the way it is to indicate "one of the configs in the repo".

- Emily

 config.c | 17 +++++++++++++++++
 config.h |  1 +
 2 files changed, 18 insertions(+)
config.c +17 -0
index e7052b3977..baab4a916e 100644
@@ -3312,6 +3312,23 @@ enum config_scope current_config_scope(void)
                return current_parsing_scope;
 }
 
+const char *config_scope_to_string(enum config_scope scope)
+{
+       switch (scope) {
+       case CONFIG_SCOPE_SYSTEM:
+               return "system";
+       case CONFIG_SCOPE_GLOBAL:
+               return "global";
+       case CONFIG_SCOPE_REPO:
+               return "repo";
+       case CONFIG_SCOPE_CMDLINE:
+               return "cmdline";
+       case CONFIG_SCOPE_UNKNOWN:
+       default:
+               return "unknown";
+       }
+}
+
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
        int i;
config.h +1 -1
index 91fd4c5e96..c8bf296dcc 100644
@@ -303,6 +303,7 @@ enum config_scope {
 };
 
 enum config_scope current_config_scope(void);
+const char *config_scope_to_string(enum config_scope);
 const char *current_config_origin_type(void);
 const char *current_config_name(void);
 
--
Quoted 2 lines

2.24.0.525.g8f36a354ae-goog

Okay Good to know, I'm using gmail right now, so the default is to start at the top

I wouldn't really consider it a bug so much as just something that hasn't been an issue before. The way config.c is set up kind of makes it hard to pass in the information that it uses the --local option, so that may need to be refactored, since it just passes in the appropriate file based on the --local, etc. flags to builtin\config.c which has never really needed that information before now. Anyways, I don't think it needs to be addressed right now, and I'm working on something that would address it anyways... I just need to find some more time to work on it.

Signature · 1 line
Matthew Rogers
#2 lore raw
In reply to Emily Shaffer
Quoted 4 lines · Emily Shaffer

If a user is interacting with their config files primarily by the 'git config' command, using the location flags (--global, --system, etc) then they may be more interested to see the scope of the config file they are editing, rather than the filepath.

I don't mind adding this in, but I think we did discuss this same concept when we did "config --show-origin", and ended up showing the whole path, to help with a few cases:

- you know you're getting a value from the "system" config, but you don't know where that is (e.g., because the baked-in sysconfdir path is something you didn't expect)

- you're in a scope like "global", but the value actually comes from an included file

Of course there's a flip-side, which is that showing "/etc/gitconfig" doesn't tell you that this is the "--system" file; the user has to infer that themselves.

There are no callers added here, so I'm not sure exactly how the new function is meant to be used. But I'd caution that it might be worth showing the scope _and_ the path, instead of one or the other.

-Peff

#3 lore raw

From: Jeff King <[email protected]>
Sent: Wednesday, December 11, 2019 10:10 PM
To: Emily Shaffer <[email protected]>
Cc: [email protected]; Matthew Rogers <[email protected]>; Philip Oakley <[email protected]>
Subject: Re: [PATCH v2] config: add string mapping for enum config_scope

Quoted 4 lines · Emily Shaffer

If a user is interacting with their config files primarily by the 'git config' command, using the location flags (--global, --system, etc) then they may be more interested to see the scope of the config file they are editing, rather than the filepath.

I don't mind adding this in, but I think we did discuss this same concept when we did "config --show-origin", and ended up showing the whole path, to help with a few cases:

- you know you're getting a value from the "system" config, but you don't know where that is (e.g., because the baked-in sysconfdir path is something you didn't expect)

- you're in a scope like "global", but the value actually comes from an included file

Of course there's a flip-side, which is that showing "/etc/gitconfig" doesn't tell you that this is the "--system" file; the user has to infer that themselves.

There are no callers added here, so I'm not sure exactly how the new function is meant to be used. But I'd caution that it might be worth showing the scope _and_ the path, instead of one or the other.

-Peff

Just to mention, that I am working on submitting a feature to expand config with a --show-scope option via gitgitgadget. I still have some kinks to iron out before it's ready for submission, but maybe it would make sense to wait for that? Or to wait for the rest of the patches this was taken from to actually materialize?

#4 lore raw
Quoted 2 lines · On Wed, Dec 11, 2019 at 10:40:37PM -0500, [email protected] wrote:

Just to mention, that I am working on submitting a feature to expand config with a --show-scope option via gitgitgadget.

That makes sense (hopefully it can be combined with --show-origin, too, to get all of the information).

Quoted 3 lines

I still have some kinks to iron out before it's ready for submission, but maybe it would make sense to wait for that? Or to wait for the rest of the patches this was taken from to actually materialize?

In general, yes, I think it makes sense for a patch like this to come as part of a larger topic, that all gets merged at once. That way we don't end up with unused cruft.

It sounds like the issue is that there are two topics that use this. The simplest thing may be to have a common topic branch that both build on, but with a note never to merge it directly (that last part would be done by Junio, but the submitter would want to give that guidance along with the patch).

I _think_ that would be easy to handle with the way Junio handles the topics, but he may correct me.

-Peff

#5 lore raw
In reply to Jeff King
Quoted 18 lines · Jeff King
Quoted 4 lines · Emily Shaffer

If a user is interacting with their config files primarily by the 'git config' command, using the location flags (--global, --system, etc) then they may be more interested to see the scope of the config file they are editing, rather than the filepath.

I don't mind adding this in, but I think we did discuss this same concept when we did "config --show-origin", and ended up showing the whole path, to help with a few cases:

- you know you're getting a value from the "system" config, but you don't know where that is (e.g., because the baked-in sysconfdir path is something you didn't expect)

- you're in a scope like "global", but the value actually comes from an included file

Quoted 7 lines

Of course there's a flip-side, which is that showing "/etc/gitconfig" doesn't tell you that this is the "--system" file; the user has to infer that themselves.

There are no callers added here, so I'm not sure exactly how the new function is meant to be used. But I'd caution that it might be worth showing the scope _and_ the path, instead of one or the other.

Yeah, I hear you - I had added this originally to the config-based hooks topic to get an output like this:

$ git hook --list pre-commit
001	global	~/foo.sh
002	local	~/bar.sh

That's a scenario where it might be handy to add the path, especially if it's coming in via an import, sure. (For brevity I think I'd want to turn it on via an argument.)

As I was working through the comments on v3 of git-bugreport, though, I saw a request to add the origin of the configs - and that's a case where I don't necessarily want someone to see, say:

[Selected Configs]
user.name (/home/emily/robot-revolution/stairclimber/.git/config) : Emily Shaffer

when I mail that bugreport to the Git list.

So, I think I hear what you're saying - use wisely - but I think it's OK for a user to say:

printf("%s (%s): %s = %s\n",

         current_config_name(),
	 config_scope_to_string(current_config_scope()),
	 var,
	 value);

That is, I don't think the right solution is to make current_config_name() provide a stringification of current_config_scope() as well.

Or, I guess we can decide that the bugreport scenario is different enough that this helper should exist only there, and everybody else should use current_config_name().

- Emily

#6 lore raw
Quoted 7 lines · Emily Shaffer
Quoted 3 lines

There are no callers added here, so I'm not sure exactly how the new function is meant to be used. But I'd caution that it might be worth showing the scope _and_ the path, instead of one or the other.

Yeah, I hear you - I had added this originally to the config-based hooks topic to get an output like this:

Quoted 7 lines
$ git hook --list pre-commit
001	global	~/foo.sh
002	local	~/bar.sh

That's a scenario where it might be handy to add the path, especially if it's coming in via an import, sure. (For brevity I think I'd want to turn it on via an argument.)

Yeah, there I think showing the file path would be helpful.

Quoted 8 lines

As I was working through the comments on v3 of git-bugreport, though, I saw a request to add the origin of the configs - and that's a case where I don't necessarily want someone to see, say:

[Selected Configs]
user.name (/home/emily/robot-revolution/stairclimber/.git/config) : Emily Shaffer

when I mail that bugreport to the Git list.

Yeah, agreed. You'd want less information there, because we should be sensitive to sharing user filesystem paths.

Quoted 4 lines

So, I think I hear what you're saying - use wisely - but I think it's OK for a user to say:

printf("%s (%s): %s = %s\n",

Quoted 8 lines
         current_config_name(),
	 config_scope_to_string(current_config_scope()),
	 var,
	 value);

That is, I don't think the right solution is to make current_config_name() provide a stringification of current_config_scope() as well.

Yes, I think the way you're thinking about it all makes sense. We definitely can't just change current_config_name(); its output ends up in --show-origin, which is plumbing.

Quoted 3 lines

Or, I guess we can decide that the bugreport scenario is different enough that this helper should exist only there, and everybody else should use current_config_name().

No, I think this helper makes sense in config.c. And then callers can choose how much (and in what format) to show the various bits as appropriate.

So this seems like the right direction.

-Peff