Skip to content

Commit bb622de

Browse files
committed
Merge branch 'jk/ident-empty' into maint
user.email that consists of only cruft chars should consistently error out, but didn't. * jk/ident-empty: ident: do not ignore empty config name/email ident: reject all-crud ident name ident: handle NULL email when complaining of empty name ident: mark error messages for translation
2 parents 252ef8f + 9442555 commit bb622de

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

ident.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email,
153153

154154
const char *ident_default_name(void)
155155
{
156-
if (!git_default_name.len) {
156+
if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
157157
copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
158158
strbuf_trim(&git_default_name);
159159
}
@@ -162,7 +162,7 @@ const char *ident_default_name(void)
162162

163163
const char *ident_default_email(void)
164164
{
165-
if (!git_default_email.len) {
165+
if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
166166
const char *email = getenv("EMAIL");
167167

168168
if (email && email[0]) {
@@ -203,6 +203,15 @@ static int crud(unsigned char c)
203203
c == '\'';
204204
}
205205

206+
static int has_non_crud(const char *str)
207+
{
208+
for (; *str; str++) {
209+
if (!crud(*str))
210+
return 1;
211+
}
212+
return 0;
213+
}
214+
206215
/*
207216
* Copy over a string to the destination, but avoid special
208217
* characters ('\n', '<' and '>') and remove crud at the end
@@ -351,44 +360,46 @@ const char *fmt_ident(const char *name, const char *email,
351360
int want_date = !(flag & IDENT_NO_DATE);
352361
int want_name = !(flag & IDENT_NO_NAME);
353362

363+
if (!email) {
364+
if (strict && ident_use_config_only
365+
&& !(ident_config_given & IDENT_MAIL_GIVEN)) {
366+
fputs(_(env_hint), stderr);
367+
die(_("no email was given and auto-detection is disabled"));
368+
}
369+
email = ident_default_email();
370+
if (strict && default_email_is_bogus) {
371+
fputs(_(env_hint), stderr);
372+
die(_("unable to auto-detect email address (got '%s')"), email);
373+
}
374+
}
375+
354376
if (want_name) {
355377
int using_default = 0;
356378
if (!name) {
357379
if (strict && ident_use_config_only
358380
&& !(ident_config_given & IDENT_NAME_GIVEN)) {
359381
fputs(_(env_hint), stderr);
360-
die("no name was given and auto-detection is disabled");
382+
die(_("no name was given and auto-detection is disabled"));
361383
}
362384
name = ident_default_name();
363385
using_default = 1;
364386
if (strict && default_name_is_bogus) {
365387
fputs(_(env_hint), stderr);
366-
die("unable to auto-detect name (got '%s')", name);
388+
die(_("unable to auto-detect name (got '%s')"), name);
367389
}
368390
}
369391
if (!*name) {
370392
struct passwd *pw;
371393
if (strict) {
372394
if (using_default)
373395
fputs(_(env_hint), stderr);
374-
die("empty ident name (for <%s>) not allowed", email);
396+
die(_("empty ident name (for <%s>) not allowed"), email);
375397
}
376398
pw = xgetpwuid_self(NULL);
377399
name = pw->pw_name;
378400
}
379-
}
380-
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-
}
401+
if (strict && !has_non_crud(name))
402+
die(_("name consists only of disallowed characters: %s"), name);
392403
}
393404

394405
strbuf_reset(&ident);
@@ -403,7 +414,7 @@ const char *fmt_ident(const char *name, const char *email,
403414
strbuf_addch(&ident, ' ');
404415
if (date_str && date_str[0]) {
405416
if (parse_date(date_str, &ident) < 0)
406-
die("invalid date format: %s", date_str);
417+
die(_("invalid date format: %s"), date_str);
407418
}
408419
else
409420
strbuf_addstr(&ident, ident_default_date());

t/t7518-ident-corner-cases.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_expect_success 'commit rejects all-crud name' '
21+
test_must_fail env GIT_AUTHOR_NAME=" .;<>" \
22+
git commit --allow-empty -m foo
23+
'
24+
25+
# We must test the actual error message here, as an unwanted
26+
# auto-detection could fail for other reasons.
27+
test_expect_success 'empty configured name does not auto-detect' '
28+
(
29+
sane_unset GIT_AUTHOR_NAME &&
30+
test_must_fail \
31+
git -c user.name= commit --allow-empty -m foo 2>err &&
32+
test_i18ngrep "empty ident name" err
33+
)
34+
'
35+
36+
test_done

0 commit comments

Comments
 (0)