ident: don't require calling prepare_fallback_ident first
authorThomas Gummerer <t.gummerer@gmail.com>
Wed, 6 Mar 2019 22:09:11 +0000 (22:09 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 7 Mar 2019 00:41:40 +0000 (09:41 +0900)
In fd5a58477c ("ident: add the ability to provide a "fallback
identity"", 2019-02-25) I made it a requirement to call
prepare_fallback_ident as the first function in the ident API.
However in stash we didn't actually end up following that.

This leads to a BUG if user.email and user.name are set. It was not
caught in the test suite because we only rely on environment variables
for setting the user name and email instead of the config.

Instead of making it a bug to call other functions in the ident API
first, just return silently if the identity of a user was already set
up.

Reported-by: Denton Liu <liu.denton@gmail.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
ident.c
t/t3903-stash.sh
diff --git a/cache.h b/cache.h
index 611e554dea0c2065d4f376f6710e71f586aead28..67e74b7f75d8794df995ef66f3c174875e3e676e 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1493,7 +1493,6 @@ extern int is_terminal_dumb(void);
 extern int git_ident_config(const char *, const char *, void *);
 /*
  * Prepare an ident to fall back on if the user didn't configure it.
- * Must be called before any other function from the ident API.
  */
 void prepare_fallback_ident(const char *name, const char *email);
 extern void reset_ident_date(void);
diff --git a/ident.c b/ident.c
index f30bd623f0afd1c373d47bf035b767f1cd0a8056..bce20e8652ee4959cfd1c576a90a322dfd6ef44d 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -507,9 +507,7 @@ int git_ident_config(const char *var, const char *value, void *data)
 
 static void set_env_if(const char *key, const char *value, int *given, int bit)
 {
-       if (*given & bit)
-               BUG("%s was checked before prepare_fallback got called", key);
-       if (getenv(key))
+       if ((*given & bit) || getenv(key))
                return; /* nothing to do */
        setenv(key, value, 0);
        *given |= bit;
index 5f8272b6f94b513b250575c6e99e4f76d6b08e75..2115e8767e2b4017b42409835c2193d60c2737e1 100755 (executable)
@@ -1096,6 +1096,12 @@ test_expect_success 'stash -- <subdir> works with binary files' '
        test_path_is_file subdir/untracked
 '
 
+test_expect_success 'stash with user.name and user.email set works' '
+       test_config user.name "A U Thor" &&
+       test_config user.email "a.u@thor" &&
+       git stash
+'
+
 test_expect_success 'stash works when user.name and user.email are not set' '
        git reset &&
        >1 &&