threads / rfc / 39828

[RFC] ident: support per-path configs by matching the path against a pattern

Subject: [RFC] ident: support per-path configs by matching the path against a pattern

## tl;dr

7 messages between Jul 10, 2015 and Jul 10, 2015.

replies: 6people: 3as markdown or json

Sebastian Schuberth· Jul 10, 2015, 09:36 UTC · lore
Support per-path identities by configuring Git like
    $ git config user.<pattern>.email <email address>
e.g.
    $ git config user.github.email sschuberth+github@gmail.com

In this example, the middle "github" pattern is searched for case-insensitively in the absolute path name to the current git work tree. If there is a match the configured email address is used instead of what otherwise would be the default email address.

Note that repository-local identities still take precedence over global / system ones even if the pattern configured in the global / system identity matches the path to the local work tree.

This change avoids the need to use external tools like [1].
TODO: Once the community agrees that this is a feature worth having, add
tests and adjust the docs.
[1] https://github.com/prydonius/karn
Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
---
 ident.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/ident.c b/ident.c
index 5ff1aad..2429ed8 100644
--- a/ident.c
+++ b/ident.c
@@ -390,7 +390,21 @@ int author_ident_sufficiently_given(void)
 
 int git_ident_config(const char *var, const char *value, void *data)
 {
-	if (!strcmp(var, "user.name")) {
+	/* the caller has already checked that var starts with "user." */
+
+	const char *first_period = strchr(var, '.');
+	const char *last_period = strrchr(var, '.');
+
+	if (first_period < last_period) {
+		++first_period;
+		char *pattern = xstrndup(first_period, last_period - first_period);
+		const char *match = strcasestr(get_git_work_tree(), pattern);
+		free(pattern);
+		if (!match)
+			return 0;
+	}
+
+	if (ends_with(var, ".name")) {
 		if (!value)
 			return config_error_nonbool(var);
 		strbuf_reset(&git_default_name);
@@ -400,7 +414,7 @@ int git_ident_config(const char *var, const char *value, void *data)
 		return 0;
 	}
 
-	if (!strcmp(var, "user.email")) {
+	if (ends_with(var, ".email")) {
 		if (!value)
 			return config_error_nonbool(var);
 		strbuf_reset(&git_default_email);

---
https://github.com/git/git/pull/161
Junio C Hamano· Jul 10, 2015, 15:10 UTC · re: Sebastian Schuberth · lore

Re: [RFC] ident: support per-path configs by matching the path against a pattern

Sebastian Schuberth <sschuberth@gmail.com> writes:
Show 10 quoted lines
> Support per-path identities by configuring Git like
>
>     $ git config user.<pattern>.email <email address>
>
> e.g.
>
>     $ git config user.github.email sschuberth+github@gmail.com
>
> In this example, the middle "github" pattern is searched for
> case-insensitively in the absolute path name to the current git work tree.

I do agree it is a good feature to have to allow you to keep a centralized registry of possible configuration in a single place, e.g. $HOME/.gitconfig, and selectively apply pieces for multiple places.

Having said that, a few comments.
 - It feels very hacky to only do this for the ident.  You would
   want to have, (conceptually, not necessarily at the syntax level)
   something more along the lines of:
   	if path matches this pattern
        	[user]
                	email = address
                        name = name
	end
   to allow any configuration to be covered by this new "selectively
   use from the centralized registry" feature.
 - It is iffy to match the pattern with "working tree".  There are
   pros and cons but a viable alternative would be to match with the
   location of the $GIT_DIR.  Given that we now are slowly moving to
   a world where a single $GIT_DIR can have multiple working trees,
   and that user.name ultimately matters per-project you are
   contributing to, it is more likely that you would want to tie it
   to a local repository, i.e. $GIT_DIR, no matter where working
   trees you have a checkout out of that repository live.  They
   would want to share the same setting.
   On the other hand, some things may be truly per working tree even
   if it is tied to the same local repository.
   I am not saying it is better to tie this to $GIT_DIR not
   $GIT_WORK_TREE.  I am not necessarily saying we should support
   both.  I am only saying that we need to consider if tying only to
   $GIT_WORK_TREE makes sense (and if so, clearly communicate that
   to the end users).  I couldn't read from your patch [*1*] what
   your stance on this point.
 - The pattern defined to be case-insensitive substring would not be
   a sane general design.  I would have expected it to be fnmatch
   pattern that may match case insensitively only on case insensitive
   filesystems.
[Footnote]

*1* this is one of the reasons why I usually ignore "I'll do the doc and test if people agree it is a good idea." Often, we cannot judge if it is a good idea without these things.

Jeff King· Jul 10, 2015, 15:43 UTC · re: Junio C Hamano · lore

Re: [RFC] ident: support per-path configs by matching the path against a pattern

On Fri, Jul 10, 2015 at 08:10:54AM -0700, Junio C Hamano wrote:
Show 19 quoted lines
> I do agree it is a good feature to have to allow you to keep a
> centralized registry of possible configuration in a single place,
> e.g. $HOME/.gitconfig, and selectively apply pieces for multiple
> places.
> 
> Having said that, a few comments.
> 
>  - It feels very hacky to only do this for the ident.  You would
>    want to have, (conceptually, not necessarily at the syntax level)
>    something more along the lines of:
> 
>    	if path matches this pattern
>         	[user]
>                 	email = address
>                         name = name
> 	end
> 
>    to allow any configuration to be covered by this new "selectively
>    use from the centralized registry" feature.

Yeah, I agree it would be nice to cover all config keys. Since it's syntactically difficult to add conditionals to the existing config format, I tried to leave open a space for this in the "include" design. That is, right now:

  [include]
  path = foo
will unconditionally include "foo". But something like:
  [include "gitdir:bar"]
  path = foo

could do so only when the "gitdir:bar" conditional is satisfied (where that is just a syntax I made up to mean fnmatch("bar", $GIT_DIR)). So like user.<pattern>.*, we still put our section-specific hack into one special section, but that one place is capable of chaining to multiple other config keys. :)

My only request is that any conditional we add have some prefix (like "gitdir:") so that we have space to add more types later if we choose.

There's discussion on this topic somewhere on the list, but I didn't bother to dig it up. I don't think it adds anything over what I summarized above.

-Peff
Jeff King· Jul 10, 2015, 16:46 UTC · re: Jeff King · lore

Re: [RFC] ident: support per-path configs by matching the path against a pattern

On Fri, Jul 10, 2015 at 11:43:08AM -0400, Jeff King wrote:
Show 10 quoted lines
> But something like:
> 
>   [include "gitdir:bar"]
>   path = foo
> 
> could do so only when the "gitdir:bar" conditional is satisfied (where
> that is just a syntax I made up to mean fnmatch("bar", $GIT_DIR)). So
> like user.<pattern>.*, we still put our section-specific hack into one
> special section, but that one place is capable of chaining to multiple
> other config keys. :)

Here's a sketch if anybody is inclined to pick it up and run with it. Note that I did not think too hard about little things like the de-anchoring.

---
diff --git a/config.c b/config.c
index 29fa012..47b01f0 100644
--- a/config.c
+++ b/config.c
@@ -139,9 +139,45 @@ static int handle_path_include(const char *path, struct config_include_data *inc
 	return ret;
 }
 
+static int include_condition_is_true(const char *cond, int cond_len)
+{
+	const char *value;
+
+	/* no condition (i.e., "include.path") is always true */
+	if (!cond)
+		return 1;
+
+	/*
+	 * It's OK to run over cond_len in our checks here, as that just pushes
+	 * us past the final ".", which cannot match any of our prefixes.
+	 */
+	if (skip_prefix(cond, "gitdir:", &value)) {
+		struct strbuf text = STRBUF_INIT;
+		struct strbuf pattern = STRBUF_INIT;
+		int ret;
+
+		strbuf_add_absolute_path(&text, get_git_dir());
+
+		/* de-anchor match for convenience */
+		strbuf_addstr(&pattern, "**");
+		strbuf_add(&pattern, value, cond_len - (value - cond));
+		strbuf_addstr(&pattern, "**");
+
+		ret = !wildmatch(pattern.buf, text.buf, 0, NULL);
+		strbuf_release(&pattern);
+		strbuf_release(&text);
+		return ret;
+	}
+
+	/* unknown conditionals are always false */
+	return 0;
+}
+
 int git_config_include(const char *var, const char *value, void *data)
 {
 	struct config_include_data *inc = data;
+	const char *cond, *key;
+	int cond_len;
 	int ret;
 
 	/*
@@ -152,8 +188,12 @@ int git_config_include(const char *var, const char *value, void *data)
 	if (ret < 0)
 		return ret;
 
-	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(value, inc);
+	if (!parse_config_key(var, "include", &cond, &cond_len, &key) &&
+	    include_condition_is_true(cond, cond_len)) {
+		if (!strcmp(key, "path"))
+			ret = handle_path_include(value, inc);
+		/* else we do not know about this type of include; ignore */
+	}
 	return ret;
 }
 
Junio C Hamano· Jul 10, 2015, 20:23 UTC · re: Jeff King · lore

Re: [RFC] ident: support per-path configs by matching the path against a pattern

Jeff King <peff@peff.net> writes:
Show 20 quoted lines
> Here's a sketch if anybody is inclined to pick it up and run with it.
> Note that I did not think too hard about little things like the
> de-anchoring.
> ...
>  int git_config_include(const char *var, const char *value, void *data)
>  {
>  	struct config_include_data *inc = data;
> +	const char *cond, *key;
> +	int cond_len;
>  	int ret;
>  
>  	/*
> @@ -152,8 +188,12 @@ int git_config_include(const char *var, const char *value, void *data)
>  	if (ret < 0)
>  		return ret;
>  
> -	if (!strcmp(var, "include.path"))
> -		ret = handle_path_include(value, inc);
> +	if (!parse_config_key(var, "include", &cond, &cond_len, &key) &&
> +	    include_condition_is_true(cond, cond_len)) {
I really like these two lines.

Whoever designed that parse_config_key() interface either is a genious or had a foresight with a crystal ball, or perhaps both.

Show 7 quoted lines
> +		if (!strcmp(key, "path"))
> +			ret = handle_path_include(value, inc);
> +		/* else we do not know about this type of include; ignore */
> +	}
>  	return ret;
>  }
>  
Jeff King· Jul 10, 2015, 20:58 UTC · re: Junio C Hamano · lore

Re: [RFC] ident: support per-path configs by matching the path against a pattern

On Fri, Jul 10, 2015 at 01:23:07PM -0700, Junio C Hamano wrote:
Show 9 quoted lines
> > -	if (!strcmp(var, "include.path"))
> > -		ret = handle_path_include(value, inc);
> > +	if (!parse_config_key(var, "include", &cond, &cond_len, &key) &&
> > +	    include_condition_is_true(cond, cond_len)) {
> 
> I really like these two lines.
> 
> Whoever designed that parse_config_key() interface either is a
> genious or had a foresight with a crystal ball, or perhaps both.

Heh. I peeked at the archive, and I think it was actually designed by committee. Perhaps we are greater than the sum of our parts? ;)

-Peff
Junio C Hamano· Jul 10, 2015, 21:07 UTC · re: Jeff King · lore

Re: [RFC] ident: support per-path configs by matching the path against a pattern

Jeff King <peff@peff.net> writes:
Show 14 quoted lines
> On Fri, Jul 10, 2015 at 01:23:07PM -0700, Junio C Hamano wrote:
>
>> > -	if (!strcmp(var, "include.path"))
>> > -		ret = handle_path_include(value, inc);
>> > +	if (!parse_config_key(var, "include", &cond, &cond_len, &key) &&
>> > +	    include_condition_is_true(cond, cond_len)) {
>> 
>> I really like these two lines.
>> 
>> Whoever designed that parse_config_key() interface either is a
>> genious or had a foresight with a crystal ball, or perhaps both.
>
> Heh. I peeked at the archive, and I think it was actually designed by
> committee. Perhaps we are greater than the sum of our parts? ;)
It does not look like a camel, though ;-).
Thanks.  Let's see if Sebastian finds the direction satisfactory.

← back to recent threads