ident: add the ability to provide a "fallback identity"

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>

Johannes Schindelin committed Feb 25, 2019 at 23:16 UTC fd5a58477c62ddb6ec16cf7b5b2da7597f0fa398
2 files changed +27
cache.h
+5
@@ -1491,6 +1491,11 @@ extern const char *git_sequence_editor(void);
1491 extern const char *git_pager(int stdout_is_tty);
1492 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.
1497 + */
1498 +void prepare_fallback_ident(const char *name, const char *email);
1499 extern void reset_ident_date(void);
1500
1501 struct ident_split {
ident.c
+22
@@ -505,6 +505,28 @@ int git_ident_config(const char *var, const char *value, void *data)
505 return 0;
506 }
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))
513 + return; /* nothing to do */
514 + setenv(key, value, 0);
515 + *given |= bit;
516 +}
517 +
518 +void prepare_fallback_ident(const char *name, const char *email)
519 +{
520 + set_env_if("GIT_AUTHOR_NAME", name,
521 + &author_ident_explicitly_given, IDENT_NAME_GIVEN);
522 + set_env_if("GIT_AUTHOR_EMAIL", email,
523 + &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
524 + set_env_if("GIT_COMMITTER_NAME", name,
525 + &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
526 + set_env_if("GIT_COMMITTER_EMAIL", email,
527 + &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
528 +}
529 +
530 static int buf_cmp(const char *a_begin, const char *a_end,
531 const char *b_begin, const char *b_end)
532 {