From f5116f43f69720059375059311485d99c462551b Mon Sep 17 00:00:00 2001 From: Paul-Sebastian Ungureanu Date: Mon, 25 Feb 2019 23:16:05 +0000 Subject: sha1-name.c: add `get_oidf()` which acts like `get_oid()` Compared to `get_oid()`, `get_oidf()` has as parameters a pointer to `object_id`, a printf format string and additional arguments. This will help simplify the code in subsequent commits. Original-idea-by: Johannes Schindelin Signed-off-by: Paul-Sebastian Ungureanu Signed-off-by: Junio C Hamano --- cache.h | 1 + 1 file changed, 1 insertion(+) (limited to 'cache.h') diff --git a/cache.h b/cache.h index ca36b44ee0..7b6b89fc4c 100644 --- a/cache.h +++ b/cache.h @@ -1333,6 +1333,7 @@ struct object_context { GET_OID_BLOB) extern int get_oid(const char *str, struct object_id *oid); +extern int get_oidf(struct object_id *oid, const char *fmt, ...); extern int get_oid_commit(const char *str, struct object_id *oid); extern int get_oid_committish(const char *str, struct object_id *oid); extern int get_oid_tree(const char *str, struct object_id *oid); -- cgit v1.3 From fd5a58477c62ddb6ec16cf7b5b2da7597f0fa398 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 25 Feb 2019 23:16:08 +0000 Subject: 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 [tg: add docs; make it a bug to call the function before other functions in the ident API] Signed-off-by: Thomas Gummerer Signed-off-by: Junio C Hamano --- cache.h | 5 +++++ ident.c | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'cache.h') diff --git a/cache.h b/cache.h index 7b6b89fc4c..611e554dea 100644 --- 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 33bcf40644..f30bd623f0 100644 --- 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) { -- cgit v1.3 From 0640897dc5ff1f75308d3ebb45e152aed40bb9a3 Mon Sep 17 00:00:00 2001 From: Thomas Gummerer Date: Wed, 6 Mar 2019 22:09:11 +0000 Subject: 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 Helped-by: Jeff King Signed-off-by: Thomas Gummerer Signed-off-by: Junio C Hamano --- cache.h | 1 - ident.c | 4 +--- t/t3903-stash.sh | 6 ++++++ 3 files changed, 7 insertions(+), 4 deletions(-) (limited to 'cache.h') diff --git a/cache.h b/cache.h index 611e554dea..67e74b7f75 100644 --- a/cache.h +++ b/cache.h @@ -1493,7 +1493,6 @@ 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); diff --git a/ident.c b/ident.c index f30bd623f0..bce20e8652 100644 --- a/ident.c +++ b/ident.c @@ -507,9 +507,7 @@ int git_ident_config(const char *var, const char *value, void *data) 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)) + if ((*given & bit) || getenv(key)) return; /* nothing to do */ setenv(key, value, 0); *given |= bit; diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 5f8272b6f9..2115e8767e 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1096,6 +1096,12 @@ test_expect_success 'stash -- works with binary files' ' test_path_is_file subdir/untracked ' +test_expect_success 'stash with user.name and user.email set works' ' + test_config user.name "A U Thor" && + test_config user.email "a.u@thor" && + git stash +' + test_expect_success 'stash works when user.name and user.email are not set' ' git reset && >1 && -- cgit v1.3