ident: don't require calling prepare_fallback_ident first

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>

Thomas Gummerer committed Mar 6, 2019 at 22:09 UTC 0640897dc5ff1f75308d3ebb45e152aed40bb9a3
3 files changed +7 -4
cache.h
-1
@@ -1493,7 +1493,6 @@ extern int is_terminal_dumb(void);
1493 extern int git_ident_config(const char *, const char *, void *);
1494 /*
1495 * Prepare an ident to fall back on if the user didn't configure it.
1496 - * Must be called before any other function from the ident API.
1496 */
1497 void prepare_fallback_ident(const char *name, const char *email);
1498 extern void reset_ident_date(void);
ident.c
+1 -3
@@ -507,9 +507,7 @@ int git_ident_config(const char *var, const char *value, void *data)
507
508 static void set_env_if(const char *key, const char *value, int *given, int bit)
509 {
510 - if (*given & bit)
511 - BUG("%s was checked before prepare_fallback got called", key);
512 - if (getenv(key))
510 + if ((*given & bit) || getenv(key))
511 return; /* nothing to do */
512 setenv(key, value, 0);
513 *given |= bit;
t/t3903-stash.sh
+6
@@ -1096,6 +1096,12 @@ test_expect_success 'stash -- <subdir> works with binary files' '
1096 test_path_is_file subdir/untracked
1097 '
1098
1099 +test_expect_success 'stash with user.name and user.email set works' '
1100 + test_config user.name "A U Thor" &&
1101 + test_config user.email "a.u@thor" &&
1102 + git stash
1103 +'
1104 +
1105 test_expect_success 'stash works when user.name and user.email are not set' '
1106 git reset &&
1107 >1 &&