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
This commit is contained in:
commit
3022386fee
@ -68,7 +68,9 @@ if set:
|
|||||||
|
|
||||||
In case (some of) these environment variables are not set, the information
|
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
|
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
|
A commit comment is read from stdin. If a changelog
|
||||||
entry is not provided via "<" redirection, 'git commit-tree' will just wait
|
entry is not provided via "<" redirection, 'git commit-tree' will just wait
|
||||||
@ -90,6 +92,10 @@ Discussion
|
|||||||
|
|
||||||
include::i18n.txt[]
|
include::i18n.txt[]
|
||||||
|
|
||||||
|
FILES
|
||||||
|
-----
|
||||||
|
/etc/mailname
|
||||||
|
|
||||||
SEE ALSO
|
SEE ALSO
|
||||||
--------
|
--------
|
||||||
linkgit:git-write-tree[1]
|
linkgit:git-write-tree[1]
|
||||||
|
82
ident.c
82
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)
|
static void copy_email(const struct passwd *pw)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@ -61,35 +109,29 @@ static void copy_email(const struct passwd *pw)
|
|||||||
die("Your sysadmin must hate you!");
|
die("Your sysadmin must hate you!");
|
||||||
memcpy(git_default_email, pw->pw_name, len);
|
memcpy(git_default_email, pw->pw_name, len);
|
||||||
git_default_email[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);
|
if (!add_mailname_host(git_default_email + len,
|
||||||
git_default_email[len++] = '.';
|
sizeof(git_default_email) - len))
|
||||||
if (he && (domainname = strchr(he->h_name, '.')))
|
return; /* read from "/etc/mailname" (Debian) */
|
||||||
strlcpy(git_default_email + len, domainname + 1,
|
add_domainname(git_default_email + len,
|
||||||
sizeof(git_default_email) - len);
|
sizeof(git_default_email) - len);
|
||||||
else
|
|
||||||
strlcpy(git_default_email + len, "(none)",
|
|
||||||
sizeof(git_default_email) - len);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setup_ident(void)
|
static void setup_ident(const char **name, const char **emailp)
|
||||||
{
|
{
|
||||||
struct passwd *pw = NULL;
|
struct passwd *pw = NULL;
|
||||||
|
|
||||||
/* Get the name ("gecos") */
|
/* Get the name ("gecos") */
|
||||||
if (!git_default_name[0]) {
|
if (!*name && !git_default_name[0]) {
|
||||||
pw = getpwuid(getuid());
|
pw = getpwuid(getuid());
|
||||||
if (!pw)
|
if (!pw)
|
||||||
die("You don't exist. Go away!");
|
die("You don't exist. Go away!");
|
||||||
copy_gecos(pw, git_default_name, sizeof(git_default_name));
|
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");
|
const char *email = getenv("EMAIL");
|
||||||
|
|
||||||
if (email && email[0]) {
|
if (email && email[0]) {
|
||||||
@ -104,6 +146,8 @@ static void setup_ident(void)
|
|||||||
copy_email(pw);
|
copy_email(pw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!*emailp)
|
||||||
|
*emailp = git_default_email;
|
||||||
|
|
||||||
/* And set the default date */
|
/* And set the default date */
|
||||||
if (!git_default_date[0])
|
if (!git_default_date[0])
|
||||||
@ -199,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 warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
|
||||||
int name_addr_only = (flag & IDENT_NO_DATE);
|
int name_addr_only = (flag & IDENT_NO_DATE);
|
||||||
|
|
||||||
setup_ident();
|
setup_ident(&name, &email);
|
||||||
if (!name)
|
|
||||||
name = git_default_name;
|
|
||||||
if (!email)
|
|
||||||
email = git_default_email;
|
|
||||||
|
|
||||||
if (!*name) {
|
if (!*name) {
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
|
Loading…
Reference in New Issue
Block a user