Skip to content

Commit

Permalink
ident: do not retrieve default ident when unnecessary
Browse files Browse the repository at this point in the history
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 <j6t@kdgb.org>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jonathan Nieder authored and Junio C Hamano committed Oct 6, 2011
1 parent 8a55caa commit d855e4d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ident.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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])
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d855e4d

Please sign in to comment.