convert "enum date_mode" into a struct
In preparation for adding date modes that may carry extra
information beyond the mode itself, this patch converts the
date_mode enum into a struct.
Most of the conversion is fairly straightforward; we pass
the struct as a pointer and dereference the type field where
necessary. Locations that declare a date_mode can use a "{}"
constructor. However, the tricky case is where we use the
enum labels as constants, like:
show_date(t, tz, DATE_NORMAL);
Ideally we could say:
show_date(t, tz, &{ DATE_NORMAL });
but of course C does not allow that. Likewise, we cannot
cast the constant to a struct, because we need to pass an
actual address. Our options are basically:
1. Manually add a "struct date_mode d = { DATE_NORMAL }"
definition to each caller, and pass "&d". This makes
the callers uglier, because they sometimes do not even
have their own scope (e.g., they are inside a switch
statement).
2. Provide a pre-made global "date_normal" struct that can
be passed by address. We'd also need "date_rfc2822",
"date_iso8601", and so forth. But at least the ugliness
is defined in one place.
3. Provide a wrapper that generates the correct struct on
the fly. The big downside is that we end up pointing to
a single global, which makes our wrapper non-reentrant.
But show_date is already not reentrant, so it does not
matter.
This patch implements 3, along with a minor macro to keep
the size of the callers sane.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
b7c1e11dc4
commit
a5481a6c94
29
pretty.c
29
pretty.c
@ -399,7 +399,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
|
||||
}
|
||||
|
||||
const char *show_ident_date(const struct ident_split *ident,
|
||||
enum date_mode mode)
|
||||
const struct date_mode *mode)
|
||||
{
|
||||
unsigned long date = 0;
|
||||
long tz = 0;
|
||||
@ -489,15 +489,15 @@ void pp_user_info(struct pretty_print_context *pp,
|
||||
switch (pp->fmt) {
|
||||
case CMIT_FMT_MEDIUM:
|
||||
strbuf_addf(sb, "Date: %s\n",
|
||||
show_ident_date(&ident, pp->date_mode));
|
||||
show_ident_date(&ident, &pp->date_mode));
|
||||
break;
|
||||
case CMIT_FMT_EMAIL:
|
||||
strbuf_addf(sb, "Date: %s\n",
|
||||
show_ident_date(&ident, DATE_RFC2822));
|
||||
show_ident_date(&ident, DATE_MODE(RFC2822)));
|
||||
break;
|
||||
case CMIT_FMT_FULLER:
|
||||
strbuf_addf(sb, "%sDate: %s\n", what,
|
||||
show_ident_date(&ident, pp->date_mode));
|
||||
show_ident_date(&ident, &pp->date_mode));
|
||||
break;
|
||||
default:
|
||||
/* notin' */
|
||||
@ -671,7 +671,8 @@ static int mailmap_name(const char **email, size_t *email_len,
|
||||
}
|
||||
|
||||
static size_t format_person_part(struct strbuf *sb, char part,
|
||||
const char *msg, int len, enum date_mode dmode)
|
||||
const char *msg, int len,
|
||||
const struct date_mode *dmode)
|
||||
{
|
||||
/* currently all placeholders have same length */
|
||||
const int placeholder_len = 2;
|
||||
@ -711,16 +712,16 @@ static size_t format_person_part(struct strbuf *sb, char part,
|
||||
strbuf_addstr(sb, show_ident_date(&s, dmode));
|
||||
return placeholder_len;
|
||||
case 'D': /* date, RFC2822 style */
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_RFC2822));
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822)));
|
||||
return placeholder_len;
|
||||
case 'r': /* date, relative */
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_RELATIVE));
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE)));
|
||||
return placeholder_len;
|
||||
case 'i': /* date, ISO 8601-like */
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601));
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601)));
|
||||
return placeholder_len;
|
||||
case 'I': /* date, ISO 8601 strict */
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601_STRICT));
|
||||
strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
|
||||
return placeholder_len;
|
||||
}
|
||||
|
||||
@ -933,7 +934,7 @@ static void rewrap_message_tail(struct strbuf *sb,
|
||||
static int format_reflog_person(struct strbuf *sb,
|
||||
char part,
|
||||
struct reflog_walk_info *log,
|
||||
enum date_mode dmode)
|
||||
const struct date_mode *dmode)
|
||||
{
|
||||
const char *ident;
|
||||
|
||||
@ -1185,7 +1186,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
|
||||
if (c->pretty_ctx->reflog_info)
|
||||
get_reflog_selector(sb,
|
||||
c->pretty_ctx->reflog_info,
|
||||
c->pretty_ctx->date_mode,
|
||||
&c->pretty_ctx->date_mode,
|
||||
c->pretty_ctx->date_mode_explicit,
|
||||
(placeholder[1] == 'd'));
|
||||
return 2;
|
||||
@ -1200,7 +1201,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
|
||||
return format_reflog_person(sb,
|
||||
placeholder[1],
|
||||
c->pretty_ctx->reflog_info,
|
||||
c->pretty_ctx->date_mode);
|
||||
&c->pretty_ctx->date_mode);
|
||||
}
|
||||
return 0; /* unknown %g placeholder */
|
||||
case 'N':
|
||||
@ -1251,11 +1252,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
|
||||
case 'a': /* author ... */
|
||||
return format_person_part(sb, placeholder[1],
|
||||
msg + c->author.off, c->author.len,
|
||||
c->pretty_ctx->date_mode);
|
||||
&c->pretty_ctx->date_mode);
|
||||
case 'c': /* committer ... */
|
||||
return format_person_part(sb, placeholder[1],
|
||||
msg + c->committer.off, c->committer.len,
|
||||
c->pretty_ctx->date_mode);
|
||||
&c->pretty_ctx->date_mode);
|
||||
case 'e': /* encoding */
|
||||
if (c->commit_encoding)
|
||||
strbuf_addstr(sb, c->commit_encoding);
|
||||
|
||||
Reference in New Issue
Block a user