Support per-path identities by configuring Git like

    $ git config user.<pattern>.email <email address>

e.g.

    $ git config user.github.email sschuberth+git...@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 <sschube...@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
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to