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 <j6t@kdgb.org> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Oct 6, 2011 at 12:17 UTC d855e4d35de214e651a62909de96d54915273665
1 file changed +8 -8
ident.c
+8 -8
@@ -117,19 +117,21 @@ static void copy_email(const struct passwd *pw)
117 sizeof(git_default_email) - len);
118 }
119
120 -static void setup_ident(void)
120 +static void setup_ident(const char **name, const char **emailp)
121 {
122 struct passwd *pw = NULL;
123
124 /* Get the name ("gecos") */
125 - if (!git_default_name[0]) {
125 + if (!*name && !git_default_name[0]) {
126 pw = getpwuid(getuid());
127 if (!pw)
128 die("You don't exist. Go away!");
129 copy_gecos(pw, git_default_name, sizeof(git_default_name));
130 }
131 + if (!*name)
132 + *name = git_default_name;
133
132 - if (!git_default_email[0]) {
134 + if (!*emailp && !git_default_email[0]) {
135 const char *email = getenv("EMAIL");
136
137 if (email && email[0]) {
@@ -144,6 +146,8 @@ static void setup_ident(void)
146 copy_email(pw);
147 }
148 }
149 + if (!*emailp)
150 + *emailp = git_default_email;
151
152 /* And set the default date */
153 if (!git_default_date[0])
@@ -239,11 +243,7 @@ const char *fmt_ident(const char *name, const char *email,
243 int warn_on_no_name = (flag & IDENT_WARN_ON_NO_NAME);
244 int name_addr_only = (flag & IDENT_NO_DATE);
245
242 - setup_ident();
243 - if (!name)
244 - name = git_default_name;
245 - if (!email)
246 - email = git_default_email;
246 + setup_ident(&name, &email);
247
248 if (!*name) {
249 struct passwd *pw;