Skip to content

Commit 9fead7a

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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac3b0de commit 9fead7a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,7 @@ extern const char *git_sequence_editor(void);
14911491
extern const char *git_pager(int stdout_is_tty);
14921492
extern int is_terminal_dumb(void);
14931493
extern int git_ident_config(const char *, const char *, void *);
1494+
void prepare_fallback_ident(const char *name, const char *email);
14941495
extern void reset_ident_date(void);
14951496

14961497
struct ident_split {

ident.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,26 @@ 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) || getenv(key))
511+
return; /* nothing to do */
512+
setenv(key, value, 0);
513+
*given |= bit;
514+
}
515+
516+
void prepare_fallback_ident(const char *name, const char *email)
517+
{
518+
set_env_if("GIT_AUTHOR_NAME", name,
519+
&author_ident_explicitly_given, IDENT_NAME_GIVEN);
520+
set_env_if("GIT_AUTHOR_EMAIL", email,
521+
&author_ident_explicitly_given, IDENT_MAIL_GIVEN);
522+
set_env_if("GIT_COMMITTER_NAME", name,
523+
&committer_ident_explicitly_given, IDENT_NAME_GIVEN);
524+
set_env_if("GIT_COMMITTER_EMAIL", email,
525+
&committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
526+
}
527+
508528
static int buf_cmp(const char *a_begin, const char *a_end,
509529
const char *b_begin, const char *b_end)
510530
{

0 commit comments

Comments
 (0)