ident: let callers omit name with fmt_indent

Most callers want to see all of "$name <$email> $date", but
a few want only limited parts, omitting the date, or even
the name. We already have IDENT_NO_DATE to handle the date
part, but there's not a good option for getting just the
email. Callers have to done one of:

  1. Call ident_default_email; this does not respect
     environment variables, nor does it promise to trim
     whitespace or other crud from the result.

  2. Call git_{committer,author}_info; this returns the name
     and email, leaving the caller to parse out the wanted
     bits.

This patch adds IDENT_NO_NAME; it stops short of adding
IDENT_NO_EMAIL, as no callers want it (nor are likely to),
and it complicates the error handling of the function.

When no name is requested, the angle brackets (<>) around
the email address are also omitted.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2012-05-24 19:27:24 -04:00 committed by Junio C Hamano
parent 359b27add3
commit c15e1987ae
2 changed files with 10 additions and 5 deletions

View File

@ -889,6 +889,7 @@ enum date_mode parse_date_format(const char *format);
#define IDENT_ERROR_ON_NO_NAME 1
#define IDENT_NO_DATE 2
#define IDENT_NO_NAME 4
extern const char *git_author_info(int);
extern const char *git_committer_info(int);
extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);

14
ident.c
View File

@ -269,13 +269,14 @@ const char *fmt_ident(const char *name, const char *email,
char date[50];
int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
int want_date = !(flag & IDENT_NO_DATE);
int want_name = !(flag & IDENT_NO_NAME);
if (!name)
if (want_name && !name)
name = ident_default_name();
if (!email)
email = ident_default_email();
if (!*name) {
if (want_name && !*name) {
struct passwd *pw;
if (error_on_no_name) {
@ -297,10 +298,13 @@ const char *fmt_ident(const char *name, const char *email,
}
strbuf_reset(&ident);
strbuf_addstr_without_crud(&ident, name);
strbuf_addstr(&ident, " <");
if (want_name) {
strbuf_addstr_without_crud(&ident, name);
strbuf_addstr(&ident, " <");
}
strbuf_addstr_without_crud(&ident, email);
strbuf_addch(&ident, '>');
if (want_name)
strbuf_addch(&ident, '>');
if (want_date) {
strbuf_addch(&ident, ' ');
strbuf_addstr_without_crud(&ident, date);