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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 4, 2016 at 11:12 UTC 59f929596b611d88270ac0fd3ea132b32d66d460
1 file changed +24 -22
ident.c
+24 -22
@@ -345,32 +345,34 @@ const char *fmt_ident(const char *name, const char *email,
345 int want_date = !(flag & IDENT_NO_DATE);
346 int want_name = !(flag & IDENT_NO_NAME);
347
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) {
354 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;
367 }
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);
368 }
369
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 + }
376 }
377
378 strbuf_reset(&ident);