Skip to content

Commit 92bcbb9

Browse files
peffgitster
authored andcommitted
ident: loosen getpwuid error in non-strict mode
If the user has not specified an identity and we have to turn to getpwuid() to find the username or gecos field, we die immediately when getpwuid fails (e.g., because the user does not exist). This is OK for making a commit, where we have set IDENT_STRICT and would want to bail on bogus input. But for something like a reflog, where the ident is "best effort", it can be pain. For instance, even running "git clone" with a UID that is not in /etc/passwd will result in git barfing, just because we can't find an ident to put in the reflog. Instead of dying in xgetpwuid_self, we can instead return a fallback value, and set a "bogus" flag. For the username in an email, we already have a "default_email_is_bogus" flag. For the name field, we introduce (and check) a matching "default_name_is_bogus" flag. As a bonus, this means you now get the usual "tell me who you are" advice instead of just a "no such user" error. No tests, as this is dependent on configuration outside of git's control. However, I did confirm that it behaves sensibly when I delete myself from the local /etc/passwd (reflogs get written, and commits complain). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 19ce497 commit 92bcbb9

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

ident.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ static struct strbuf git_default_name = STRBUF_INIT;
1111
static struct strbuf git_default_email = STRBUF_INIT;
1212
static struct strbuf git_default_date = STRBUF_INIT;
1313
static int default_email_is_bogus;
14+
static int default_name_is_bogus;
1415

1516
#define IDENT_NAME_GIVEN 01
1617
#define IDENT_MAIL_GIVEN 02
@@ -24,15 +25,22 @@ static int author_ident_explicitly_given;
2425
#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
2526
#endif
2627

27-
static struct passwd *xgetpwuid_self(void)
28+
static struct passwd *xgetpwuid_self(int *is_bogus)
2829
{
2930
struct passwd *pw;
3031

3132
errno = 0;
3233
pw = getpwuid(getuid());
33-
if (!pw)
34-
die(_("unable to look up current user in the passwd file: %s"),
35-
errno ? strerror(errno) : _("no such user"));
34+
if (!pw) {
35+
static struct passwd fallback;
36+
fallback.pw_name = "unknown";
37+
#ifndef NO_GECOS_IN_PWENT
38+
fallback.pw_gecos = "Unknown";
39+
#endif
40+
pw = &fallback;
41+
if (is_bogus)
42+
*is_bogus = 1;
43+
}
3644
return pw;
3745
}
3846

@@ -122,7 +130,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email,
122130
const char *ident_default_name(void)
123131
{
124132
if (!git_default_name.len) {
125-
copy_gecos(xgetpwuid_self(), &git_default_name);
133+
copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
126134
strbuf_trim(&git_default_name);
127135
}
128136
return git_default_name.buf;
@@ -138,8 +146,8 @@ const char *ident_default_email(void)
138146
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
139147
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
140148
} else
141-
copy_email(xgetpwuid_self(), &git_default_email,
142-
&default_email_is_bogus);
149+
copy_email(xgetpwuid_self(&default_email_is_bogus),
150+
&git_default_email, &default_email_is_bogus);
143151
strbuf_trim(&git_default_email);
144152
}
145153
return git_default_email.buf;
@@ -327,10 +335,16 @@ const char *fmt_ident(const char *name, const char *email,
327335
fputs(env_hint, stderr);
328336
die("empty ident name (for <%s>) not allowed", email);
329337
}
330-
pw = xgetpwuid_self();
338+
pw = xgetpwuid_self(NULL);
331339
name = pw->pw_name;
332340
}
333341

342+
if (want_name && strict &&
343+
name == git_default_name.buf && default_name_is_bogus) {
344+
fputs(env_hint, stderr);
345+
die("unable to auto-detect name (got '%s')", name);
346+
}
347+
334348
if (strict && email == git_default_email.buf && default_email_is_bogus) {
335349
fputs(env_hint, stderr);
336350
die("unable to auto-detect email address (got '%s')", email);

0 commit comments

Comments
 (0)