1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-01 14:46:08 +02:00

ident: handle NULL email when complaining of empty name

If we see an empty name, we complain about and mention the
matching email in the error message (to give it some
context). However, the "email" pointer may be NULL here if
we were planning to fill it in later from ident_default_email().

This was broken by 59f929596 (fmt_ident: refactor strictness
checks, 2016-02-04). Prior to that commit, we would look up
the default name and email before doing any other actions.
So one solution would be to go back to that.

However, we can't just do so blindly. The logic for handling
the "!email" condition has grown since then. In particular,
looking up the default email can die if getpwuid() fails,
but there are other errors that should take precedence.
Commit 734c7789a (ident: check for useConfigOnly before
auto-detection of name/email, 2016-03-30) reordered the
checks so that we prefer the error message for
useConfigOnly.

Instead, we can observe that while the name-handling depends
on "email" being set, the reverse is not true. So we can
simply set up the email variable first.

This does mean that if both are bogus, we'll complain about
the email before the name. But between the two, there is no
reason to prefer one over the other.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2017-02-23 03:13:53 -05:00 committed by Junio C Hamano
parent afb6c30b27
commit 862e80a413
2 changed files with 33 additions and 13 deletions

26
ident.c
View File

@ -351,6 +351,19 @@ const char *fmt_ident(const char *name, const char *email,
int want_date = !(flag & IDENT_NO_DATE);
int want_name = !(flag & IDENT_NO_NAME);
if (!email) {
if (strict && ident_use_config_only
&& !(ident_config_given & IDENT_MAIL_GIVEN)) {
fputs(_(env_hint), stderr);
die(_("no email was given and auto-detection is disabled"));
}
email = ident_default_email();
if (strict && default_email_is_bogus) {
fputs(_(env_hint), stderr);
die(_("unable to auto-detect email address (got '%s')"), email);
}
}
if (want_name) {
int using_default = 0;
if (!name) {
@ -378,19 +391,6 @@ const char *fmt_ident(const char *name, const char *email,
}
}
if (!email) {
if (strict && ident_use_config_only
&& !(ident_config_given & IDENT_MAIL_GIVEN)) {
fputs(_(env_hint), stderr);
die(_("no email was given and auto-detection is disabled"));
}
email = ident_default_email();
if (strict && default_email_is_bogus) {
fputs(_(env_hint), stderr);
die(_("unable to auto-detect email address (got '%s')"), email);
}
}
strbuf_reset(&ident);
if (want_name) {
strbuf_addstr_without_crud(&ident, name);

20
t/t7518-ident-corner-cases.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/sh
test_description='corner cases in ident strings'
. ./test-lib.sh
# confirm that we do not segfault _and_ that we do not say "(null)", as
# glibc systems will quietly handle our NULL pointer
#
# Note also that we can't use "env" here because we need to unset a variable,
# and "-u" is not portable.
test_expect_success 'empty name and missing email' '
(
sane_unset GIT_AUTHOR_EMAIL &&
GIT_AUTHOR_NAME= &&
test_must_fail git commit --allow-empty -m foo 2>err &&
test_i18ngrep ! null err
)
'
test_done