Skip to content

Commit d3dc2ab

Browse files
committed
ident: add the ability to provide a "fallback identity"
In 3bc2111 (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 <[email protected]> [tg: add docs; make it a bug to call the function before other functions in the ident API] Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7cfbaa5 commit d3dc2ab

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

cache.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,11 @@ extern const char *git_sequence_editor(void);
15181518
extern const char *git_pager(int stdout_is_tty);
15191519
extern int is_terminal_dumb(void);
15201520
extern int git_ident_config(const char *, const char *, void *);
1521+
/*
1522+
* Prepare an ident to fall back on if the user didn't configure it.
1523+
* Must be called before any other function from the ident API.
1524+
*/
1525+
void prepare_fallback_ident(const char *name, const char *email);
15211526
extern void reset_ident_date(void);
15221527

15231528
struct ident_split {

ident.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,28 @@ int git_ident_config(const char *var, const char *value, void *data)
505505
return 0;
506506
}
507507

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+
508530
static int buf_cmp(const char *a_begin, const char *a_end,
509531
const char *b_begin, const char *b_end)
510532
{

0 commit comments

Comments
 (0)