Skip to content

Commit 19ce497

Browse files
peffgitster
authored andcommitted
ident: keep a flag for bogus default_email
If we have to deduce the user's email address and can't come up with something plausible for the hostname, we simply write "(none)" or ".(none)" in the hostname. Later, our strict-check is forced to use strstr to look for this magic string. This is probably not a problem in practice, but it's rather ugly. Let's keep an extra flag that tells us the email is bogus, and check that instead. We could get away with simply setting the global in add_domainname(); it only gets called to write into git_default_email. However, let's make the code a little more obvious to future readers by actually passing a pointer to our "bogus" flag down the call-chain. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e850194 commit 19ce497

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

ident.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
static struct strbuf git_default_name = STRBUF_INIT;
1111
static struct strbuf git_default_email = STRBUF_INIT;
1212
static struct strbuf git_default_date = STRBUF_INIT;
13+
static int default_email_is_bogus;
1314

1415
#define IDENT_NAME_GIVEN 01
1516
#define IDENT_MAIL_GIVEN 02
@@ -82,25 +83,29 @@ static int add_mailname_host(struct strbuf *buf)
8283
return 0;
8384
}
8485

85-
static void add_domainname(struct strbuf *out)
86+
static void add_domainname(struct strbuf *out, int *is_bogus)
8687
{
8788
char buf[1024];
8889
struct hostent *he;
8990

9091
if (gethostname(buf, sizeof(buf))) {
9192
warning("cannot get host name: %s", strerror(errno));
9293
strbuf_addstr(out, "(none)");
94+
*is_bogus = 1;
9395
return;
9496
}
9597
if (strchr(buf, '.'))
9698
strbuf_addstr(out, buf);
9799
else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
98100
strbuf_addstr(out, he->h_name);
99-
else
101+
else {
100102
strbuf_addf(out, "%s.(none)", buf);
103+
*is_bogus = 1;
104+
}
101105
}
102106

103-
static void copy_email(const struct passwd *pw, struct strbuf *email)
107+
static void copy_email(const struct passwd *pw, struct strbuf *email,
108+
int *is_bogus)
104109
{
105110
/*
106111
* Make up a fake email address
@@ -111,7 +116,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
111116

112117
if (!add_mailname_host(email))
113118
return; /* read from "/etc/mailname" (Debian) */
114-
add_domainname(email);
119+
add_domainname(email, is_bogus);
115120
}
116121

117122
const char *ident_default_name(void)
@@ -133,7 +138,8 @@ const char *ident_default_email(void)
133138
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
134139
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
135140
} else
136-
copy_email(xgetpwuid_self(), &git_default_email);
141+
copy_email(xgetpwuid_self(), &git_default_email,
142+
&default_email_is_bogus);
137143
strbuf_trim(&git_default_email);
138144
}
139145
return git_default_email.buf;
@@ -325,8 +331,7 @@ const char *fmt_ident(const char *name, const char *email,
325331
name = pw->pw_name;
326332
}
327333

328-
if (strict && email == git_default_email.buf &&
329-
strstr(email, "(none)")) {
334+
if (strict && email == git_default_email.buf && default_email_is_bogus) {
330335
fputs(env_hint, stderr);
331336
die("unable to auto-detect email address (got '%s')", email);
332337
}

0 commit comments

Comments
 (0)