Subject: Re: git-clone fails when current user is not in /etc/passwd

From: Junio C Hamano <[email-masked]>
Date: Thu, 10 Dec 2015 10:43:17 -0800

Duy Nguyen <[email-masked]> writes:

> Well.. reflog needs it. So either you disable reflog at clone time or
> define name/email via config file. I don't see anything wrong with
> this behavior.

Hmm, I am not quite sure about that.

In the codepath that computes ident_default_email(), which is one
half of what the "reflog" code you cite wants to do, copy_email()
calls copy_email() which in turn calls add_domainname().  If your
getpwuid() gave you some username, but your gethostname() gave you a
NULL, we do not barf but add "(none)" as and then issue a warning.

Perhaps we can do the same by returning a structure with members set
to a set of fake values.  Because we already have checks in places
that really matter to the recorded history (read: not reflog) in the
form of *_ident_sufficiently_given() functions, potential damage due
to having phoney names returned from here would not be too bad.

Totally untested...

 ident.c   | 13 ++++++++++---
 wrapper.c |  4 ----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/ident.c b/ident.c
index 4e7f99d..2ccae2c 100644
--- a/ident.c
+++ b/ident.c
@@ -31,7 +31,7 @@ static void copy_gecos(const struct passwd *w, struct strbuf *name)
 	 * with commas.  Also & stands for capitalized form of the login name.
 	 */
 
-	for (src = get_gecos(w); *src && *src != ','; src++) {
+	for (src = w ? get_gecos(w) : "&"; *src && *src != ','; src++) {
 		int ch = *src;
 		if (ch != '&')
 			strbuf_addch(name, ch);
@@ -117,7 +117,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
 	 * Make up a fake email address
 	 * (name + '@' + hostname [+ '.' + domainname])
 	 */
-	strbuf_addstr(email, pw->pw_name);
+	strbuf_addstr(email, pw ? pw->pw_name : "unknown");
 	strbuf_addch(email, '@');
 
 	if (!add_mailname_host(email))
@@ -332,8 +332,15 @@ const char *fmt_ident(const char *name, const char *email,
 				fputs(env_hint, stderr);
 			die("empty ident name (for <%s>) not allowed", email);
 		}
+		errno = 0;
 		pw = xgetpwuid_self();
-		name = pw->pw_name;
+		if (!pw) {
+			warning(_("unable to look up current user: %s"),
+				errno ? strerror(errno) : _("no such user"));
+			name = "unknown";
+		} else {
+			name = pw->pw_name;
+		}
 	}
 
 	if (strict && email == git_default_email.buf &&
diff --git a/wrapper.c b/wrapper.c
index 6fcaa4d..16ab45f 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -605,11 +605,7 @@ struct passwd *xgetpwuid_self(void)
 {
 	struct passwd *pw;
 
-	errno = 0;
 	pw = getpwuid(getuid());
-	if (!pw)
-		die(_("unable to look up current user in the passwd file: %s"),
-		    errno ? strerror(errno) : _("no such user"));
 	return pw;
 }