Browse Source

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>
maint
Jeff King 13 years ago committed by Junio C Hamano
parent
commit
2f70587502
  1. 5
      Documentation/git-commit-tree.txt
  2. 5
      Documentation/git-var.txt
  3. 3
      git-compat-util.h
  4. 20
      ident.c
  5. 12
      wrapper.c

5
Documentation/git-commit-tree.txt

@ -88,11 +88,6 @@ for one to be entered and terminated with ^D. @@ -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
----------


5
Documentation/git-var.txt

@ -59,11 +59,6 @@ ifdef::git-default-pager[] @@ -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]

3
git-compat-util.h

@ -595,4 +595,7 @@ int rmdir_or_warn(const char *path); @@ -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

20
ident.c

@ -100,12 +100,8 @@ static void copy_email(const struct passwd *pw, struct strbuf *email) @@ -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) @@ -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, @@ -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;
}


12
wrapper.c

@ -402,3 +402,15 @@ int remove_or_warn(unsigned int mode, const char *file) @@ -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;
}

Loading…
Cancel
Save