ident: keep separate "explicit" flags for author and committer

We keep track of whether the user ident was given to us explicitly, or if we guessed at it from system parameters like username and hostname. However, we kept only a single variable. This covers the common cases (because the author and committer will usually come from the same explicit source), but can miss two cases: 1. GIT_COMMITTER_* is set explicitly, but we fallback for GIT_AUTHOR. We claim the ident is explicit, even though the author is not. 2. GIT_AUTHOR_* is set and we ask for author ident, but not committer ident. We will claim the ident is implicit, even though it is explicit. This patch uses two variables instead of one, updates both when we set the "fallback" values, and updates them individually when we read from the environment. Rather than keep user_ident_sufficiently_given as a compatibility wrapper, we update the only two callers to check the committer_ident, which matches their intent and what was happening already. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 14, 2012 at 16:34 UTC d6991ceedc6a6fbcc2184f8328eb1b4c88b55267
3 files changed +29 -10
builtin/commit.c
+2 -2
@@ -755,7 +755,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
755 ident_shown++ ? "" : "\n",
756 author_ident->buf);
757
758 - if (!user_ident_sufficiently_given())
758 + if (!committer_ident_sufficiently_given())
759 status_printf_ln(s, GIT_COLOR_NORMAL,
760 _("%s"
761 "Committer: %s"),
@@ -1265,7 +1265,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
1265 strbuf_addstr(&format, "\n Author: ");
1266 strbuf_addbuf_percentquote(&format, &author_ident);
1267 }
1268 - if (!user_ident_sufficiently_given()) {
1268 + if (!committer_ident_sufficiently_given()) {
1269 strbuf_addstr(&format, "\n Committer: ");
1270 strbuf_addbuf_percentquote(&format, &committer_ident);
1271 if (advice_implicit_identity) {
cache.h
+2 -1
@@ -1149,7 +1149,8 @@ struct config_include_data {
1149 #define CONFIG_INCLUDE_INIT { 0 }
1150 extern int git_config_include(const char *name, const char *value, void *data);
1151
1152 -extern int user_ident_sufficiently_given(void);
1152 +extern int committer_ident_sufficiently_given(void);
1153 +extern int author_ident_sufficiently_given(void);
1154
1155 extern const char *git_commit_encoding;
1156 extern const char *git_log_output_encoding;
ident.c
+25 -7
@@ -14,7 +14,8 @@ static char git_default_date[50];
14 #define IDENT_NAME_GIVEN 01
15 #define IDENT_MAIL_GIVEN 02
16 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
17 -static int user_ident_explicitly_given;
17 +static int committer_ident_explicitly_given;
18 +static int author_ident_explicitly_given;
19
20 #ifdef NO_GECOS_IN_PWENT
21 #define get_gecos(ignored) "&"
@@ -113,7 +114,8 @@ const char *ident_default_email(void)
114
115 if (email && email[0]) {
116 strbuf_addstr(&git_default_email, email);
116 - user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
117 + committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
118 + author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
119 } else
120 copy_email(xgetpwuid_self(), &git_default_email);
121 strbuf_trim(&git_default_email);
@@ -331,6 +333,10 @@ const char *fmt_name(const char *name, const char *email)
333
334 const char *git_author_info(int flag)
335 {
336 + if (getenv("GIT_AUTHOR_NAME"))
337 + author_ident_explicitly_given |= IDENT_NAME_GIVEN;
338 + if (getenv("GIT_AUTHOR_EMAIL"))
339 + author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
340 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
341 getenv("GIT_AUTHOR_EMAIL"),
342 getenv("GIT_AUTHOR_DATE"),
@@ -340,16 +346,16 @@ const char *git_author_info(int flag)
346 const char *git_committer_info(int flag)
347 {
348 if (getenv("GIT_COMMITTER_NAME"))
343 - user_ident_explicitly_given |= IDENT_NAME_GIVEN;
349 + committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
350 if (getenv("GIT_COMMITTER_EMAIL"))
345 - user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
351 + committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
352 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
353 getenv("GIT_COMMITTER_EMAIL"),
354 getenv("GIT_COMMITTER_DATE"),
355 flag);
356 }
357
352 -int user_ident_sufficiently_given(void)
358 +static int ident_is_sufficient(int user_ident_explicitly_given)
359 {
360 #ifndef WINDOWS
361 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
@@ -358,6 +364,16 @@ int user_ident_sufficiently_given(void)
364 #endif
365 }
366
367 +int committer_ident_sufficiently_given(void)
368 +{
369 + return ident_is_sufficient(committer_ident_explicitly_given);
370 +}
371 +
372 +int author_ident_sufficiently_given(void)
373 +{
374 + return ident_is_sufficient(author_ident_explicitly_given);
375 +}
376 +
377 int git_ident_config(const char *var, const char *value, void *data)
378 {
379 if (!strcmp(var, "user.name")) {
@@ -365,7 +381,8 @@ int git_ident_config(const char *var, const char *value, void *data)
381 return config_error_nonbool(var);
382 strbuf_reset(&git_default_name);
383 strbuf_addstr(&git_default_name, value);
368 - user_ident_explicitly_given |= IDENT_NAME_GIVEN;
384 + committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
385 + author_ident_explicitly_given |= IDENT_NAME_GIVEN;
386 return 0;
387 }
388
@@ -374,7 +391,8 @@ int git_ident_config(const char *var, const char *value, void *data)
391 return config_error_nonbool(var);
392 strbuf_reset(&git_default_email);
393 strbuf_addstr(&git_default_email, value);
377 - user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
394 + committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
395 + author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
396 return 0;
397 }
398