From 8a55caa8a3d9169ba14861b8917e739a50a322fa Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Mon, 3 Oct 2011 01:16:33 -0500 Subject: ident: check /etc/mailname if email is unknown Before falling back to gethostname(), check /etc/mailname if GIT_AUTHOR_EMAIL is not set in the environment or through config files. Only fall back if /etc/mailname cannot be opened or read. The /etc/mailname convention comes from Debian policy section 11.6 ("mail transport, delivery and user agents"), though maybe it could be useful sometimes on other machines, too. The lack of this support was noticed by various people in different ways: - Ian observed that git was choosing the address 'ian@anarres.relativity.greenend.org.uk' rather than 'ian@davenant.greenend.org.uk' as it should have done. - Jonathan noticed that operations like "git commit" were needlessly slow when using a resolver that was slow to handle reverse DNS lookups. Alas, after this patch, if /etc/mailname is set up and the [user] name and email configuration aren't, the committer email will not provide a charming reminder of which machine commits were made on any more. But I think it's worth it. Mechanics: the functionality of reading mailname goes in its own function, so people who care about other distros can easily add an implementation to a similar location without making copy_email() too long and losing clarity. While at it, we split out the fallback default logic that does gethostname(), too (rearranging it a little and adding a check for errors from gethostname while at it). Based on a patch by Gerrit Pape . Requested-by: Ian Jackson Signed-off-by: Jonathan Nieder Improved-by: Junio C Hamano Signed-off-by: Junio C Hamano diff --git a/Documentation/git-commit-tree.txt b/Documentation/git-commit-tree.txt index f524d76..9ac30a3 100644 --- a/Documentation/git-commit-tree.txt +++ b/Documentation/git-commit-tree.txt @@ -67,7 +67,9 @@ if set: In case (some of) these environment variables are not set, the information is taken from the configuration items user.name and user.email, or, if not -present, system user name and fully qualified hostname. +present, system user name and the hostname used for outgoing mail (taken +from `/etc/mailname` and falling back to the fully qualified hostname when +that file does not exist). A commit comment is read from stdin. If a changelog entry is not provided via "<" redirection, 'git commit-tree' will just wait @@ -89,6 +91,10 @@ Discussion include::i18n.txt[] +FILES +----- +/etc/mailname + SEE ALSO -------- linkgit:git-write-tree[1] diff --git a/ident.c b/ident.c index 35a6f26..edb4314 100644 --- a/ident.c +++ b/ident.c @@ -50,6 +50,54 @@ static void copy_gecos(const struct passwd *w, char *name, size_t sz) } +static int add_mailname_host(char *buf, size_t len) +{ + FILE *mailname; + + mailname = fopen("/etc/mailname", "r"); + if (!mailname) { + if (errno != ENOENT) + warning("cannot open /etc/mailname: %s", + strerror(errno)); + return -1; + } + if (!fgets(buf, len, mailname)) { + if (ferror(mailname)) + warning("cannot read /etc/mailname: %s", + strerror(errno)); + fclose(mailname); + return -1; + } + /* success! */ + fclose(mailname); + return 0; +} + +static void add_domainname(char *buf, size_t len) +{ + struct hostent *he; + size_t namelen; + const char *domainname; + + if (gethostname(buf, len)) { + warning("cannot get host name: %s", strerror(errno)); + strlcpy(buf, "(none)", len); + return; + } + namelen = strlen(buf); + if (memchr(buf, '.', namelen)) + return; + + he = gethostbyname(buf); + buf[namelen++] = '.'; + buf += namelen; + len -= namelen; + if (he && (domainname = strchr(he->h_name, '.'))) + strlcpy(buf, domainname + 1, len); + else + strlcpy(buf, "(none)", len); +} + static void copy_email(const struct passwd *pw) { /* @@ -61,20 +109,12 @@ static void copy_email(const struct passwd *pw) die("Your sysadmin must hate you!"); memcpy(git_default_email, pw->pw_name, len); git_default_email[len++] = '@'; - gethostname(git_default_email + len, sizeof(git_default_email) - len); - if (!strchr(git_default_email+len, '.')) { - struct hostent *he = gethostbyname(git_default_email + len); - char *domainname; - - len = strlen(git_default_email); - git_default_email[len++] = '.'; - if (he && (domainname = strchr(he->h_name, '.'))) - strlcpy(git_default_email + len, domainname + 1, - sizeof(git_default_email) - len); - else - strlcpy(git_default_email + len, "(none)", - sizeof(git_default_email) - len); - } + + if (!add_mailname_host(git_default_email + len, + sizeof(git_default_email) - len)) + return; /* read from "/etc/mailname" (Debian) */ + add_domainname(git_default_email + len, + sizeof(git_default_email) - len); } static void setup_ident(void) -- cgit v0.10.2-6-g49f6 From d855e4d35de214e651a62909de96d54915273665 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Thu, 6 Oct 2011 12:17:19 -0500 Subject: ident: do not retrieve default ident when unnecessary Avoid a getpwuid() call (which contacts the network if the password database is not local), read of /etc/mailname, gethostname() call, and reverse DNS lookup if the user has already chosen a name and email through configuration, the environment, or the command line. This should slightly speed up commands like "git commit". More importantly, it improves error reporting when computation of the default ident string does not go smoothly. For example, after detecting a problem (e.g., "warning: cannot open /etc/mailname: Permission denied") in retrieving the default committer identity: touch /etc/mailname; # as root chmod -r /etc/mailname; # as root git commit -m 'test commit' you can squelch the warning while waiting for your sysadmin to fix the permissions problem. echo '[user] email = me@example.com' >>~/.gitconfig Inspired-by: Johannes Sixt Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano diff --git a/ident.c b/ident.c index edb4314..f619619 100644 --- a/ident.c +++ b/ident.c @@ -117,19 +117,21 @@ static void copy_email(const struct passwd *pw) sizeof(git_default_email) - len); } -static void setup_ident(void) +static void setup_ident(const char **name, const char **emailp) { struct passwd *pw = NULL; /* Get the name ("gecos") */ - if (!git_default_name[0]) { + if (!*name && !git_default_name[0]) { pw = getpwuid(getuid()); if (!pw) die("You don't exist. Go away!"); copy_gecos(pw, git_default_name, sizeof(git_default_name)); } + if (!*name) + *name = git_default_name; - if (!git_default_email[0]) { + if (!*emailp && !git_default_email[0]) { const char *email = getenv("EMAIL"); if (email && email[0]) { @@ -144,6 +146,8 @@ static void setup_ident(void) copy_email(pw); } } + if (!*emailp) + *emailp = git_default_email; /* And set the default date */ if (!git_default_date[0]) @@ -239,11 +243,7 @@ const char *fmt_ident(const char *name, const char *email, int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME); int name_addr_only = (flag & IDENT_NO_DATE); - setup_ident(); - if (!name) - name = git_default_name; - if (!email) - email = git_default_email; + setup_ident(&name, &email); if (!*name) { struct passwd *pw; -- cgit v0.10.2-6-g49f6