Skip to content

Commit 862e80a

Browse files
peffgitster
authored andcommitted
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 59f9295 (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 734c778 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent afb6c30 commit 862e80a

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

ident.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ const char *fmt_ident(const char *name, const char *email,
351351
int want_date = !(flag & IDENT_NO_DATE);
352352
int want_name = !(flag & IDENT_NO_NAME);
353353

354+
if (!email) {
355+
if (strict && ident_use_config_only
356+
&& !(ident_config_given & IDENT_MAIL_GIVEN)) {
357+
fputs(_(env_hint), stderr);
358+
die(_("no email was given and auto-detection is disabled"));
359+
}
360+
email = ident_default_email();
361+
if (strict && default_email_is_bogus) {
362+
fputs(_(env_hint), stderr);
363+
die(_("unable to auto-detect email address (got '%s')"), email);
364+
}
365+
}
366+
354367
if (want_name) {
355368
int using_default = 0;
356369
if (!name) {
@@ -378,19 +391,6 @@ const char *fmt_ident(const char *name, const char *email,
378391
}
379392
}
380393

381-
if (!email) {
382-
if (strict && ident_use_config_only
383-
&& !(ident_config_given & IDENT_MAIL_GIVEN)) {
384-
fputs(_(env_hint), stderr);
385-
die(_("no email was given and auto-detection is disabled"));
386-
}
387-
email = ident_default_email();
388-
if (strict && default_email_is_bogus) {
389-
fputs(_(env_hint), stderr);
390-
die(_("unable to auto-detect email address (got '%s')"), email);
391-
}
392-
}
393-
394394
strbuf_reset(&ident);
395395
if (want_name) {
396396
strbuf_addstr_without_crud(&ident, name);

t/t7518-ident-corner-cases.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
test_description='corner cases in ident strings'
4+
. ./test-lib.sh
5+
6+
# confirm that we do not segfault _and_ that we do not say "(null)", as
7+
# glibc systems will quietly handle our NULL pointer
8+
#
9+
# Note also that we can't use "env" here because we need to unset a variable,
10+
# and "-u" is not portable.
11+
test_expect_success 'empty name and missing email' '
12+
(
13+
sane_unset GIT_AUTHOR_EMAIL &&
14+
GIT_AUTHOR_NAME= &&
15+
test_must_fail git commit --allow-empty -m foo 2>err &&
16+
test_i18ngrep ! null err
17+
)
18+
'
19+
20+
test_done

0 commit comments

Comments
 (0)