git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFC] ident: support per-path configs by matching the path against a pattern
@ 2015-07-10  9:36 Sebastian Schuberth
  2015-07-10 15:10 ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Schuberth @ 2015-07-10  9:36 UTC (permalink / raw)
  To: git

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

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

* Re: [RFC] ident: support per-path configs by matching the path against a pattern
  2015-07-10  9:36 [RFC] ident: support per-path configs by matching the path against a pattern Sebastian Schuberth
@ 2015-07-10 15:10 ` Junio C Hamano
  2015-07-10 15:43   ` Jeff King
  0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2015-07-10 15:10 UTC (permalink / raw)
  To: Sebastian Schuberth; +Cc: git

Sebastian Schuberth <sschuberth@gmail.com> writes:

> 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.

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

* Re: [RFC] ident: support per-path configs by matching the path against a pattern
  2015-07-10 15:10 ` Junio C Hamano
@ 2015-07-10 15:43   ` Jeff King
  2015-07-10 16:46     ` Jeff King
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff King @ 2015-07-10 15:43 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Sebastian Schuberth, git

On Fri, Jul 10, 2015 at 08:10:54AM -0700, Junio C Hamano wrote:

> 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

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

* Re: [RFC] ident: support per-path configs by matching the path against a pattern
  2015-07-10 15:43   ` Jeff King
@ 2015-07-10 16:46     ` Jeff King
  2015-07-10 20:23       ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff King @ 2015-07-10 16:46 UTC (permalink / raw)
  To: Sebastian Schuberth; +Cc: Junio C Hamano, git

On Fri, Jul 10, 2015 at 11:43:08AM -0400, Jeff King wrote:

> 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;
 }
 

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

* Re: [RFC] ident: support per-path configs by matching the path against a pattern
  2015-07-10 16:46     ` Jeff King
@ 2015-07-10 20:23       ` Junio C Hamano
  2015-07-10 20:58         ` Jeff King
  0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2015-07-10 20:23 UTC (permalink / raw)
  To: Jeff King; +Cc: Sebastian Schuberth, git

Jeff King <peff@peff.net> writes:

> 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.

> +		if (!strcmp(key, "path"))
> +			ret = handle_path_include(value, inc);
> +		/* else we do not know about this type of include; ignore */
> +	}
>  	return ret;
>  }
>  

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

* Re: [RFC] ident: support per-path configs by matching the path against a pattern
  2015-07-10 20:23       ` Junio C Hamano
@ 2015-07-10 20:58         ` Jeff King
  2015-07-10 21:07           ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff King @ 2015-07-10 20:58 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Sebastian Schuberth, git

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? ;)

-Peff

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

* Re: [RFC] ident: support per-path configs by matching the path against a pattern
  2015-07-10 20:58         ` Jeff King
@ 2015-07-10 21:07           ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2015-07-10 21:07 UTC (permalink / raw)
  To: Jeff King; +Cc: Sebastian Schuberth, git

Jeff King <peff@peff.net> writes:

> 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.

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

end of thread, other threads:[~2015-07-10 21:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-10  9:36 [RFC] ident: support per-path configs by matching the path against a pattern Sebastian Schuberth
2015-07-10 15:10 ` Junio C Hamano
2015-07-10 15:43   ` Jeff King
2015-07-10 16:46     ` Jeff King
2015-07-10 20:23       ` Junio C Hamano
2015-07-10 20:58         ` Jeff King
2015-07-10 21:07           ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).