summaryrefslogtreecommitdiff
path: root/ident.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2012-05-21 23:10:20 (GMT)
committerJunio C Hamano <gitster@pobox.com>2012-05-22 16:08:20 (GMT)
commit2f70587502b6b5a8cfda5a3eff54392f48e2db8d (patch)
tree444e6b44d4b083958ba642b9217dacecb32ef382 /ident.c
parent8587ead78ad3d2af760270b21035ffe48fde3e7b (diff)
downloadgit-2f70587502b6b5a8cfda5a3eff54392f48e2db8d.zip
git-2f70587502b6b5a8cfda5a3eff54392f48e2db8d.tar.gz
git-2f70587502b6b5a8cfda5a3eff54392f48e2db8d.tar.bz2
ident: report passwd errors with a more friendly message
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'ident.c')
-rw-r--r--ident.c20
1 files changed, 5 insertions, 15 deletions
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;
}