git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 10/13] ident: report passwd errors with a more friendly message

From
Jeff King <peff@peff.net>
Date
May 18, 2012, 23:21 UTC
Message-ID
<20120518232106.GJ30031@sigill.intra.peff.net>
In-Reply-To
<20120518230528.GA30510@sigill.intra.peff.net>

When getpwuid fails, we give a cute but cryptic message. While it makes sense if you know that getpwuid or identity functions are being called, this code is triggered behind the scenes by quite a few git commands these days (e.g., receive-pack on a remote server might use it for a reflog; the current message is hard to distinguish from an authentication error). Let's switch to something that gives a little more context.

While we're at it, we can factor out all of the cut-and-pastes of the "you don't exist" message into a wrapper function. Rather than provide xgetpwuid, let's make it even more specific to just getting the passwd entry for the current uid. That's the only way we use getpwuid anyway, and it lets us make an even more specific error message.

The current message also fails to mention errno. While the usual cause for getpwuid failing is that the user does not exist, mentioning errno makes it easier to diagnose these problems. Note that POSIX specifies that errno remain untouched if the passwd entry does not exist (but will be set on actual errors), whereas some systems will return ENOENT or similar for a missing entry. We handle both cases in our wrapper.

Signed-off-by: Jeff King <peff@peff.net>
---
 Documentation/git-commit-tree.txt |  5 -----
 Documentation/git-var.txt         |  5 -----
 git-compat-util.h                 |  3 +++
 ident.c                           | 20 +++++---------------
 wrapper.c                         | 12 ++++++++++++
 5 files changed, 20 insertions(+), 25 deletions(-)
diff --git a/Documentation/git-commit-tree.txt b/Documentation/git-commit-tree.txt
index eb12b2d..eb8ee99 100644
--- a/Documentation/git-commit-tree.txt
+++ b/Documentation/git-commit-tree.txt
@@ -88,11 +88,6 @@ for one to be entered and terminated with ^D.
 
 include::date-formats.txt[]
 
-Diagnostics
------------
-You don't exist. Go away!::
-    The passwd(5) gecos field couldn't be read
-
 Discussion
 ----------
 
diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt
index 3f703e3..67edf58 100644
--- a/Documentation/git-var.txt
+++ b/Documentation/git-var.txt
@@ -59,11 +59,6 @@ ifdef::git-default-pager[]
     The build you are using chose '{git-default-pager}' as the default.
 endif::git-default-pager[]
 
-Diagnostics
------------
-You don't exist. Go away!::
-    The passwd(5) gecos field couldn't be read
-
 SEE ALSO
 --------
 linkgit:git-commit-tree[1]
diff --git a/git-compat-util.h b/git-compat-util.h
index ed11ad8..5bd9ad7 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -595,4 +595,7 @@ int rmdir_or_warn(const char *path);
  */
 int remove_or_warn(unsigned int mode, const char *path);
 
+/* Get the passwd entry for the UID of the current process. */
+struct passwd *xgetpwuid_self(void);
+
 #endif
diff --git a/ident.c b/ident.c
index 73a06a1..5aec073 100644
--- a/ident.c
+++ b/ident.c
@@ -100,12 +100,8 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
 
 const char *ident_default_name(void)
 {
-	if (!git_default_name.len) {
-		struct passwd *pw = getpwuid(getuid());
-		if (!pw)
-			die("You don't exist. Go away!");
-		copy_gecos(pw, &git_default_name);
-	}
+	if (!git_default_name.len)
+		copy_gecos(xgetpwuid_self(), &git_default_name);
 	return git_default_name.buf;
 }
 
@@ -117,12 +113,8 @@ const char *ident_default_email(void)
 		if (email && email[0]) {
 			strbuf_addstr(&git_default_email, email);
 			user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
-		} else {
-			struct passwd *pw = getpwuid(getuid());
-			if (!pw)
-				die("You don't exist. Go away!");
-			copy_email(pw, &git_default_email);
-		}
+		} else
+			copy_email(xgetpwuid_self(), &git_default_email);
 	}
 	return git_default_email.buf;
 }
@@ -303,9 +295,7 @@ const char *fmt_ident(const char *name, const char *email,
 				fputs(env_hint, stderr);
 			die("empty ident %s <%s> not allowed", name, email);
 		}
-		pw = getpwuid(getuid());
-		if (!pw)
-			die("You don't exist. Go away!");
+		pw = xgetpwuid_self();
 		name = pw->pw_name;
 	}
 
diff --git a/wrapper.c b/wrapper.c
index 6ccd059..b5e33e4 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -402,3 +402,15 @@ int remove_or_warn(unsigned int mode, const char *file)
 {
 	return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
 }
+
+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;
+}
-- 
1.7.10.1.16.g53a707b
Previous: Jeff KingNext: Jeff King
Message 42 of 45 in “ident cleanups and bugfixes”
  1. 0/13 ident cleanups and bugfixesJeff King, May 18, 2012
  2. 01/13 ident: split setup_ident into separate functionsJeff King, May 18, 2012
  3. 02/13 http-push: do not access git_default_email directlyJeff King, May 18, 2012
  4. 03/13 fmt-merge-msg: don't use static buffer in record_personJeff King, May 18, 2012
  5. 04/13 move identity config parsing to ident.cJeff King, May 18, 2012
  6. 05/13 move git_default_* variables to ident.cJeff King, May 18, 2012
  7. Junio C HamanoMay 21, 2012
  8. Jeff KingMay 21, 2012
  9. Jeff KingMay 21, 2012
  10. 06/13 format-patch: use default email for generating message idsJeff King, May 18, 2012
  11. Junio C HamanoMay 21, 2012
  12. Jeff KingMay 21, 2012
  13. 07/13 fmt_ident: drop IDENT_WARN_ON_NO_NAME codeJeff King, May 18, 2012
  14. 08/13 ident: don't write fallback username into git_default_nameJeff King, May 18, 2012
  15. Junio C HamanoMay 21, 2012
  16. Jeff KingMay 21, 2012
  17. Junio C HamanoMay 21, 2012
  18. 0/15 ident cleanups git_default_nameJeff King, May 21, 2012
  19. 01/15 ident: split setup_ident into separate functionsJeff King, May 21, 2012
  20. 02/15 http-push: do not access git_default_email directlyJeff King, May 21, 2012
  21. 03/15 fmt-merge-msg: don't use static buffer in record_personJeff King, May 21, 2012
  22. 04/15 move identity config parsing to ident.cJeff King, May 21, 2012
  23. 05/15 move git_default_* variables to ident.cJeff King, May 21, 2012
  24. 06/15 ident: trim trailing newline from /etc/mailnameJeff King, May 21, 2012
  25. 07/15 format-patch: use default email for generating message idsJeff King, May 21, 2012
  26. 08/15 fmt_ident: drop IDENT_WARN_ON_NO_NAME codeJeff King, May 21, 2012
  27. 09/15 ident: don't write fallback username into git_default_nameJeff King, May 21, 2012
  28. 10/15 drop length limitations on gecos-derived names and emailsJeff King, May 21, 2012
  29. [regression] Re: [PATCHv2 10/15] drop length limitations on gecos-derived names and emailsJonathan Nieder, Jan 24, 2013
  30. Jeff KingJan 25, 2013
  31. Junio C HamanoJan 25, 2013
  32. Jeff KingJan 25, 2013
  33. 11/15 ident: report passwd errors with a more friendly messageJeff King, May 21, 2012
  34. 12/15 ident: use full dns names to generate email addressesJeff King, May 21, 2012
  35. 13/15 ident: use a dynamic strbuf in fmt_identJeff King, May 21, 2012
  36. 14/15 ident: trim whitespace from default name/emailJeff King, May 21, 2012
  37. Junio C HamanoMay 22, 2012
  38. Jeff KingMay 22, 2012
  39. Junio C HamanoMay 22, 2012
  40. 15/15 format-patch: refactor get_patch_filenameJeff King, May 21, 2012
  41. 09/13 drop length limitations on gecos-derived names and emailsJeff King, May 18, 2012
  42. 10/13 ident: report passwd errors with a more friendly messageJeff King, May 18, 2012
  43. 11/13 ident: use full dns names to generate email addressesJeff King, May 18, 2012
  44. 12/13 ident: use a dynamic strbuf in fmt_identJeff King, May 18, 2012
  45. 13/13 format-patch: refactor get_patch_filenameJeff King, May 18, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.