ident: add the ability to provide a "fallback identity"
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 25 Feb 2019 23:16:08 +0000 (23:16 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Feb 2019 23:03:46 +0000 (08:03 +0900)
In 3bc2111fc2e9 (stash: tolerate missing user identity, 2018-11-18),
`git stash` learned to provide a fallback identity for the case that no
proper name/email was given (and `git stash` does not really care about
a correct identity anyway, but it does want to create a commit object).

In preparation for the same functionality in the upcoming built-in
version of `git stash`, let's offer the same functionality as an API
function.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
[tg: add docs; make it a bug to call the function before other
functions in the ident API]
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
ident.c
diff --git a/cache.h b/cache.h
index 7b6b89fc4c0c285efb29265c5ae10a01d80ffcbf..611e554dea0c2065d4f376f6710e71f586aead28 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1491,6 +1491,11 @@ extern const char *git_sequence_editor(void);
 extern const char *git_pager(int stdout_is_tty);
 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);
 
 struct ident_split {
diff --git a/ident.c b/ident.c
index 33bcf40644cdf23434a7cb622b6be71bb7cd867c..f30bd623f0afd1c373d47bf035b767f1cd0a8056 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -505,6 +505,28 @@ int git_ident_config(const char *var, const char *value, void *data)
        return 0;
 }
 
+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))
+               return; /* nothing to do */
+       setenv(key, value, 0);
+       *given |= bit;
+}
+
+void prepare_fallback_ident(const char *name, const char *email)
+{
+       set_env_if("GIT_AUTHOR_NAME", name,
+                  &author_ident_explicitly_given, IDENT_NAME_GIVEN);
+       set_env_if("GIT_AUTHOR_EMAIL", email,
+                  &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
+       set_env_if("GIT_COMMITTER_NAME", name,
+                  &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
+       set_env_if("GIT_COMMITTER_EMAIL", email,
+                  &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
+}
+
 static int buf_cmp(const char *a_begin, const char *a_end,
                   const char *b_begin, const char *b_end)
 {