const char *ident_default_name(void) { if (!git_default_name.len) { copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); strbuf_trim(&git_default_name); } return git_default_name.buf; }
const char *ident_default_email(void) { if (!git_default_email.len) { const char *email = getenv("EMAIL"); if (email && email[0]) { strbuf_addstr(&git_default_email, email); committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; author_ident_explicitly_given |= IDENT_MAIL_GIVEN; } else copy_email(xgetpwuid_self(&default_email_is_bogus), &git_default_email, &default_email_is_bogus); strbuf_trim(&git_default_email); } return git_default_email.buf; }
const char *fmt_ident(const char *name, const char *email, const char *date_str, int flag) { static struct strbuf ident = STRBUF_INIT; int strict = (flag & IDENT_STRICT); int want_date = !(flag & IDENT_NO_DATE); int want_name = !(flag & IDENT_NO_NAME); if (want_name) { int using_default = 0; if (!name) { if (strict && ident_use_config_only && !(ident_config_given & IDENT_NAME_GIVEN)) { fputs(env_hint, stderr); die("no name was given and auto-detection is disabled"); } name = ident_default_name(); using_default = 1; if (strict && default_name_is_bogus) { fputs(env_hint, stderr); die("unable to auto-detect name (got '%s')", name); } } if (!*name) { struct passwd *pw; if (strict) { if (using_default) fputs(env_hint, stderr); die("empty ident name (for <%s>) not allowed", email); } pw = xgetpwuid_self(NULL); name = pw->pw_name; } } if (!email) { if (strict && ident_use_config_only && !(ident_config_given & IDENT_MAIL_GIVEN)) { fputs(env_hint, stderr); die("no email was given and auto-detection is disabled"); } email = ident_default_email(); if (strict && default_email_is_bogus) { fputs(env_hint, stderr); die("unable to auto-detect email address (got '%s')", email); } } strbuf_reset(&ident); if (want_name) { strbuf_addstr_without_crud(&ident, name); strbuf_addstr(&ident, " <"); } strbuf_addstr_without_crud(&ident, email); if (want_name) strbuf_addch(&ident, '>'); if (want_date) { strbuf_addch(&ident, ' '); if (date_str && date_str[0]) { if (parse_date(date_str, &ident) < 0) die("invalid date format: %s", date_str); } else strbuf_addstr(&ident, ident_default_date()); } return ident.buf; }