Skip to content

Commit 59f9295

Browse files
peffgitster
authored andcommitted
fmt_ident: refactor strictness checks
This function has evolved quite a bit over time, and as a result, the logic for "is this an OK ident" has been sprinkled throughout. This ends up with a lot of redundant conditionals, like checking want_name repeatedly. Worse, we want to know in many cases whether we are using the "default" ident, and we do so by comparing directly to the global strbuf, which violates the abstraction of the ident_default_* functions. Let's reorganize the function into a hierarchy of conditionals to handle similar cases together. The only case that doesn't just work naturally for this is that of an empty name, where our advice is different based on whether we came from ident_default_name() or not. We can use a simple flag to cover this case. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7548842 commit 59f9295

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

ident.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -345,32 +345,34 @@ const char *fmt_ident(const char *name, const char *email,
345345
int want_date = !(flag & IDENT_NO_DATE);
346346
int want_name = !(flag & IDENT_NO_NAME);
347347

348-
if (want_name && !name)
349-
name = ident_default_name();
350-
if (!email)
351-
email = ident_default_email();
352-
353-
if (want_name && !*name) {
354-
struct passwd *pw;
355-
356-
if (strict) {
357-
if (name == git_default_name.buf)
348+
if (want_name) {
349+
int using_default = 0;
350+
if (!name) {
351+
name = ident_default_name();
352+
using_default = 1;
353+
if (strict && default_name_is_bogus) {
358354
fputs(env_hint, stderr);
359-
die("empty ident name (for <%s>) not allowed", email);
355+
die("unable to auto-detect name (got '%s')", name);
356+
}
357+
}
358+
if (!*name) {
359+
struct passwd *pw;
360+
if (strict) {
361+
if (using_default)
362+
fputs(env_hint, stderr);
363+
die("empty ident name (for <%s>) not allowed", email);
364+
}
365+
pw = xgetpwuid_self(NULL);
366+
name = pw->pw_name;
360367
}
361-
pw = xgetpwuid_self(NULL);
362-
name = pw->pw_name;
363-
}
364-
365-
if (want_name && strict &&
366-
name == git_default_name.buf && default_name_is_bogus) {
367-
fputs(env_hint, stderr);
368-
die("unable to auto-detect name (got '%s')", name);
369368
}
370369

371-
if (strict && email == git_default_email.buf && default_email_is_bogus) {
372-
fputs(env_hint, stderr);
373-
die("unable to auto-detect email address (got '%s')", email);
370+
if (!email) {
371+
email = ident_default_email();
372+
if (strict && default_email_is_bogus) {
373+
fputs(env_hint, stderr);
374+
die("unable to auto-detect email address (got '%s')", email);
375+
}
374376
}
375377

376378
strbuf_reset(&ident);

0 commit comments

Comments
 (0)