Skip to content

Commit 3022386

Browse files
committed
Merge branch 'jn/ident-from-etc-mailname'
* jn/ident-from-etc-mailname: ident: do not retrieve default ident when unnecessary ident: check /etc/mailname if email is unknown
2 parents 1810abb + d855e4d commit 3022386

2 files changed

Lines changed: 69 additions & 23 deletions

File tree

Documentation/git-commit-tree.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ if set:
6868

6969
In case (some of) these environment variables are not set, the information
7070
is taken from the configuration items user.name and user.email, or, if not
71-
present, system user name and fully qualified hostname.
71+
present, system user name and the hostname used for outgoing mail (taken
72+
from `/etc/mailname` and falling back to the fully qualified hostname when
73+
that file does not exist).
7274

7375
A commit comment is read from stdin. If a changelog
7476
entry is not provided via "<" redirection, 'git commit-tree' will just wait
@@ -90,6 +92,10 @@ Discussion
9092

9193
include::i18n.txt[]
9294

95+
FILES
96+
-----
97+
/etc/mailname
98+
9399
SEE ALSO
94100
--------
95101
linkgit:git-write-tree[1]

ident.c

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,54 @@ static void copy_gecos(const struct passwd *w, char *name, size_t sz)
5050

5151
}
5252

53+
static int add_mailname_host(char *buf, size_t len)
54+
{
55+
FILE *mailname;
56+
57+
mailname = fopen("/etc/mailname", "r");
58+
if (!mailname) {
59+
if (errno != ENOENT)
60+
warning("cannot open /etc/mailname: %s",
61+
strerror(errno));
62+
return -1;
63+
}
64+
if (!fgets(buf, len, mailname)) {
65+
if (ferror(mailname))
66+
warning("cannot read /etc/mailname: %s",
67+
strerror(errno));
68+
fclose(mailname);
69+
return -1;
70+
}
71+
/* success! */
72+
fclose(mailname);
73+
return 0;
74+
}
75+
76+
static void add_domainname(char *buf, size_t len)
77+
{
78+
struct hostent *he;
79+
size_t namelen;
80+
const char *domainname;
81+
82+
if (gethostname(buf, len)) {
83+
warning("cannot get host name: %s", strerror(errno));
84+
strlcpy(buf, "(none)", len);
85+
return;
86+
}
87+
namelen = strlen(buf);
88+
if (memchr(buf, '.', namelen))
89+
return;
90+
91+
he = gethostbyname(buf);
92+
buf[namelen++] = '.';
93+
buf += namelen;
94+
len -= namelen;
95+
if (he && (domainname = strchr(he->h_name, '.')))
96+
strlcpy(buf, domainname + 1, len);
97+
else
98+
strlcpy(buf, "(none)", len);
99+
}
100+
53101
static void copy_email(const struct passwd *pw)
54102
{
55103
/*
@@ -61,35 +109,29 @@ static void copy_email(const struct passwd *pw)
61109
die("Your sysadmin must hate you!");
62110
memcpy(git_default_email, pw->pw_name, len);
63111
git_default_email[len++] = '@';
64-
gethostname(git_default_email + len, sizeof(git_default_email) - len);
65-
if (!strchr(git_default_email+len, '.')) {
66-
struct hostent *he = gethostbyname(git_default_email + len);
67-
char *domainname;
68-
69-
len = strlen(git_default_email);
70-
git_default_email[len++] = '.';
71-
if (he && (domainname = strchr(he->h_name, '.')))
72-
strlcpy(git_default_email + len, domainname + 1,
73-
sizeof(git_default_email) - len);
74-
else
75-
strlcpy(git_default_email + len, "(none)",
76-
sizeof(git_default_email) - len);
77-
}
112+
113+
if (!add_mailname_host(git_default_email + len,
114+
sizeof(git_default_email) - len))
115+
return; /* read from "/etc/mailname" (Debian) */
116+
add_domainname(git_default_email + len,
117+
sizeof(git_default_email) - len);
78118
}
79119

80-
static void setup_ident(void)
120+
static void setup_ident(const char **name, const char **emailp)
81121
{
82122
struct passwd *pw = NULL;
83123

84124
/* Get the name ("gecos") */
85-
if (!git_default_name[0]) {
125+
if (!*name && !git_default_name[0]) {
86126
pw = getpwuid(getuid());
87127
if (!pw)
88128
die("You don't exist. Go away!");
89129
copy_gecos(pw, git_default_name, sizeof(git_default_name));
90130
}
131+
if (!*name)
132+
*name = git_default_name;
91133

92-
if (!git_default_email[0]) {
134+
if (!*emailp && !git_default_email[0]) {
93135
const char *email = getenv("EMAIL");
94136

95137
if (email && email[0]) {
@@ -104,6 +146,8 @@ static void setup_ident(void)
104146
copy_email(pw);
105147
}
106148
}
149+
if (!*emailp)
150+
*emailp = git_default_email;
107151

108152
/* And set the default date */
109153
if (!git_default_date[0])
@@ -199,11 +243,7 @@ const char *fmt_ident(const char *name, const char *email,
199243
int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
200244
int name_addr_only = (flag & IDENT_NO_DATE);
201245

202-
setup_ident();
203-
if (!name)
204-
name = git_default_name;
205-
if (!email)
206-
email = git_default_email;
246+
setup_ident(&name, &email);
207247

208248
if (!*name) {
209249
struct passwd *pw;

0 commit comments

Comments
 (0)