Skip to content

Commit a5481a6

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7c1e11 commit a5481a6

23 files changed

+97
-77
lines changed

archive.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void format_subst(const struct commit *commit,
3333
char *to_free = NULL;
3434
struct strbuf fmt = STRBUF_INIT;
3535
struct pretty_print_context ctx = {0};
36-
ctx.date_mode = DATE_NORMAL;
36+
ctx.date_mode.type = DATE_NORMAL;
3737
ctx.abbrev = DEFAULT_ABBREV;
3838

3939
if (src == buf->buf)

builtin/blame.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static int xdl_opts;
5050
static int abbrev = -1;
5151
static int no_whole_file_rename;
5252

53-
static enum date_mode blame_date_mode = DATE_ISO8601;
53+
static struct date_mode blame_date_mode = { DATE_ISO8601 };
5454
static size_t blame_date_width;
5555

5656
static struct string_list mailmap;
@@ -1827,7 +1827,7 @@ static const char *format_time(unsigned long time, const char *tz_str,
18271827
size_t time_width;
18281828
int tz;
18291829
tz = atoi(tz_str);
1830-
time_str = show_date(time, tz, blame_date_mode);
1830+
time_str = show_date(time, tz, &blame_date_mode);
18311831
strbuf_addstr(&time_buf, time_str);
18321832
/*
18331833
* Add space paddings to time_buf to display a fixed width
@@ -2187,7 +2187,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
21872187
if (!strcmp(var, "blame.date")) {
21882188
if (!value)
21892189
return config_error_nonbool(var);
2190-
blame_date_mode = parse_date_format(value);
2190+
parse_date_format(value, &blame_date_mode);
21912191
return 0;
21922192
}
21932193

@@ -2569,13 +2569,13 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
25692569

25702570
if (cmd_is_annotate) {
25712571
output_option |= OUTPUT_ANNOTATE_COMPAT;
2572-
blame_date_mode = DATE_ISO8601;
2572+
blame_date_mode.type = DATE_ISO8601;
25732573
} else {
25742574
blame_date_mode = revs.date_mode;
25752575
}
25762576

25772577
/* The maximum width used to show the dates */
2578-
switch (blame_date_mode) {
2578+
switch (blame_date_mode.type) {
25792579
case DATE_RFC2822:
25802580
blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
25812581
break;

builtin/commit.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
856856
_("%s"
857857
"Date: %s"),
858858
ident_shown++ ? "" : "\n",
859-
show_ident_date(&ai, DATE_NORMAL));
859+
show_ident_date(&ai, DATE_MODE(NORMAL)));
860860

861861
if (!committer_ident_sufficiently_given())
862862
status_printf_ln(s, GIT_COLOR_NORMAL,
@@ -1046,7 +1046,7 @@ static const char *find_author_by_nickname(const char *name)
10461046
commit = get_revision(&revs);
10471047
if (commit) {
10481048
struct pretty_print_context ctx = {0};
1049-
ctx.date_mode = DATE_NORMAL;
1049+
ctx.date_mode.type = DATE_NORMAL;
10501050
strbuf_release(&buf);
10511051
format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
10521052
clear_mailmap(&mailmap);

builtin/for-each-ref.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
389389
char *zone;
390390
unsigned long timestamp;
391391
long tz;
392-
enum date_mode date_mode = DATE_NORMAL;
392+
struct date_mode date_mode = { DATE_NORMAL };
393393
const char *formatp;
394394

395395
/*
@@ -401,7 +401,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
401401
formatp = strchr(atomname, ':');
402402
if (formatp != NULL) {
403403
formatp++;
404-
date_mode = parse_date_format(formatp);
404+
parse_date_format(formatp, &date_mode);
405405
}
406406

407407
if (!eoemail)
@@ -412,7 +412,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
412412
tz = strtol(zone, NULL, 10);
413413
if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
414414
goto bad;
415-
v->s = xstrdup(show_date(timestamp, tz, date_mode));
415+
v->s = xstrdup(show_date(timestamp, tz, &date_mode));
416416
v->ul = timestamp;
417417
return;
418418
bad:

builtin/log.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static void cmd_log_init_defaults(struct rev_info *rev)
112112
DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
113113

114114
if (default_date_mode)
115-
rev->date_mode = parse_date_format(default_date_mode);
115+
parse_date_format(default_date_mode, &rev->date_mode);
116116
rev->diffopt.touched_flags = 0;
117117
}
118118

@@ -939,7 +939,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
939939

940940
msg = body;
941941
pp.fmt = CMIT_FMT_EMAIL;
942-
pp.date_mode = DATE_RFC2822;
942+
pp.date_mode.type = DATE_RFC2822;
943943
pp_user_info(&pp, NULL, &sb, committer, encoding);
944944
pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
945945
pp_remainder(&pp, &msg, &sb, 0);

builtin/shortlog.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
138138
ctx.abbrev = log->abbrev;
139139
ctx.subject = "";
140140
ctx.after_subject = "";
141-
ctx.date_mode = DATE_NORMAL;
141+
ctx.date_mode.type = DATE_NORMAL;
142142
ctx.output_encoding = get_log_output_encoding();
143143
pretty_print_commit(&ctx, commit, &ufbuf);
144144
buffer = ufbuf.buf;

builtin/show-branch.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
784784
else
785785
msg++;
786786
reflog_msg[i] = xstrfmt("(%s) %s",
787-
show_date(timestamp, tz, DATE_RELATIVE),
787+
show_date(timestamp, tz,
788+
DATE_MODE(RELATIVE)),
788789
msg);
789790
free(logmsg);
790791
sprintf(nth_desc, "%s@{%d}", *av, base+i);

cache.h

+23-12
Original file line numberDiff line numberDiff line change
@@ -1105,18 +1105,28 @@ extern void *read_object_with_reference(const unsigned char *sha1,
11051105
extern struct object *peel_to_type(const char *name, int namelen,
11061106
struct object *o, enum object_type);
11071107

1108-
enum date_mode {
1109-
DATE_NORMAL = 0,
1110-
DATE_RELATIVE,
1111-
DATE_SHORT,
1112-
DATE_LOCAL,
1113-
DATE_ISO8601,
1114-
DATE_ISO8601_STRICT,
1115-
DATE_RFC2822,
1116-
DATE_RAW
1108+
struct date_mode {
1109+
enum date_mode_type {
1110+
DATE_NORMAL = 0,
1111+
DATE_RELATIVE,
1112+
DATE_SHORT,
1113+
DATE_LOCAL,
1114+
DATE_ISO8601,
1115+
DATE_ISO8601_STRICT,
1116+
DATE_RFC2822,
1117+
DATE_RAW
1118+
} type;
11171119
};
11181120

1119-
const char *show_date(unsigned long time, int timezone, enum date_mode mode);
1121+
/*
1122+
* Convenience helper for passing a constant type, like:
1123+
*
1124+
* show_date(t, tz, DATE_MODE(NORMAL));
1125+
*/
1126+
#define DATE_MODE(t) date_mode_from_type(DATE_##t)
1127+
struct date_mode *date_mode_from_type(enum date_mode_type type);
1128+
1129+
const char *show_date(unsigned long time, int timezone, const struct date_mode *mode);
11201130
void show_date_relative(unsigned long time, int tz, const struct timeval *now,
11211131
struct strbuf *timebuf);
11221132
int parse_date(const char *date, struct strbuf *out);
@@ -1126,7 +1136,7 @@ void datestamp(struct strbuf *out);
11261136
#define approxidate(s) approxidate_careful((s), NULL)
11271137
unsigned long approxidate_careful(const char *, int *);
11281138
unsigned long approxidate_relative(const char *date, const struct timeval *now);
1129-
enum date_mode parse_date_format(const char *format);
1139+
void parse_date_format(const char *format, struct date_mode *mode);
11301140
int date_overflows(unsigned long date);
11311141

11321142
#define IDENT_STRICT 1
@@ -1163,7 +1173,8 @@ extern int split_ident_line(struct ident_split *, const char *, int);
11631173
* the ident_split. It will also sanity-check the values and produce
11641174
* a well-known sentinel date if they appear bogus.
11651175
*/
1166-
const char *show_ident_date(const struct ident_split *id, enum date_mode mode);
1176+
const char *show_ident_date(const struct ident_split *id,
1177+
const struct date_mode *mode);
11671178

11681179
/*
11691180
* Compare split idents for equality or strict ordering. Note that we

commit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ struct pretty_print_context {
145145
const char *subject;
146146
const char *after_subject;
147147
int preserve_subject;
148-
enum date_mode date_mode;
148+
struct date_mode date_mode;
149149
unsigned date_mode_explicit:1;
150150
int need_8bit_cte;
151151
char *notes_message;

date.c

+25-18
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,25 @@ void show_date_relative(unsigned long time, int tz,
160160
(diff + 183) / 365);
161161
}
162162

163-
const char *show_date(unsigned long time, int tz, enum date_mode mode)
163+
struct date_mode *date_mode_from_type(enum date_mode_type type)
164+
{
165+
static struct date_mode mode;
166+
mode.type = type;
167+
return &mode;
168+
}
169+
170+
const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
164171
{
165172
struct tm *tm;
166173
static struct strbuf timebuf = STRBUF_INIT;
167174

168-
if (mode == DATE_RAW) {
175+
if (mode->type == DATE_RAW) {
169176
strbuf_reset(&timebuf);
170177
strbuf_addf(&timebuf, "%lu %+05d", time, tz);
171178
return timebuf.buf;
172179
}
173180

174-
if (mode == DATE_RELATIVE) {
181+
if (mode->type == DATE_RELATIVE) {
175182
struct timeval now;
176183

177184
strbuf_reset(&timebuf);
@@ -180,7 +187,7 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
180187
return timebuf.buf;
181188
}
182189

183-
if (mode == DATE_LOCAL)
190+
if (mode->type == DATE_LOCAL)
184191
tz = local_tzoffset(time);
185192

186193
tm = time_to_tm(time, tz);
@@ -190,17 +197,17 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
190197
}
191198

192199
strbuf_reset(&timebuf);
193-
if (mode == DATE_SHORT)
200+
if (mode->type == DATE_SHORT)
194201
strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
195202
tm->tm_mon + 1, tm->tm_mday);
196-
else if (mode == DATE_ISO8601)
203+
else if (mode->type == DATE_ISO8601)
197204
strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
198205
tm->tm_year + 1900,
199206
tm->tm_mon + 1,
200207
tm->tm_mday,
201208
tm->tm_hour, tm->tm_min, tm->tm_sec,
202209
tz);
203-
else if (mode == DATE_ISO8601_STRICT) {
210+
else if (mode->type == DATE_ISO8601_STRICT) {
204211
char sign = (tz >= 0) ? '+' : '-';
205212
tz = abs(tz);
206213
strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
@@ -209,7 +216,7 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
209216
tm->tm_mday,
210217
tm->tm_hour, tm->tm_min, tm->tm_sec,
211218
sign, tz / 100, tz % 100);
212-
} else if (mode == DATE_RFC2822)
219+
} else if (mode->type == DATE_RFC2822)
213220
strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
214221
weekday_names[tm->tm_wday], tm->tm_mday,
215222
month_names[tm->tm_mon], tm->tm_year + 1900,
@@ -221,7 +228,7 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
221228
tm->tm_mday,
222229
tm->tm_hour, tm->tm_min, tm->tm_sec,
223230
tm->tm_year + 1900,
224-
(mode == DATE_LOCAL) ? 0 : ' ',
231+
(mode->type == DATE_LOCAL) ? 0 : ' ',
225232
tz);
226233
return timebuf.buf;
227234
}
@@ -759,27 +766,27 @@ int parse_date(const char *date, struct strbuf *result)
759766
return 0;
760767
}
761768

762-
enum date_mode parse_date_format(const char *format)
769+
void parse_date_format(const char *format, struct date_mode *mode)
763770
{
764771
if (!strcmp(format, "relative"))
765-
return DATE_RELATIVE;
772+
mode->type = DATE_RELATIVE;
766773
else if (!strcmp(format, "iso8601") ||
767774
!strcmp(format, "iso"))
768-
return DATE_ISO8601;
775+
mode->type = DATE_ISO8601;
769776
else if (!strcmp(format, "iso8601-strict") ||
770777
!strcmp(format, "iso-strict"))
771-
return DATE_ISO8601_STRICT;
778+
mode->type = DATE_ISO8601_STRICT;
772779
else if (!strcmp(format, "rfc2822") ||
773780
!strcmp(format, "rfc"))
774-
return DATE_RFC2822;
781+
mode->type = DATE_RFC2822;
775782
else if (!strcmp(format, "short"))
776-
return DATE_SHORT;
783+
mode->type = DATE_SHORT;
777784
else if (!strcmp(format, "local"))
778-
return DATE_LOCAL;
785+
mode->type = DATE_LOCAL;
779786
else if (!strcmp(format, "default"))
780-
return DATE_NORMAL;
787+
mode->type = DATE_NORMAL;
781788
else if (!strcmp(format, "raw"))
782-
return DATE_RAW;
789+
mode->type = DATE_RAW;
783790
else
784791
die("unknown date format %s", format);
785792
}

fast-import.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static void write_crash_report(const char *err)
421421
fprintf(rpt, "fast-import crash report:\n");
422422
fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid());
423423
fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid());
424-
fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
424+
fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_MODE(LOCAL)));
425425
fputc('\n', rpt);
426426

427427
fputs("fatal: ", rpt);

http-backend.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static void hdr_int(const char *name, uintmax_t value)
9292

9393
static void hdr_date(const char *name, unsigned long when)
9494
{
95-
const char *value = show_date(when, 0, DATE_RFC2822);
95+
const char *value = show_date(when, 0, DATE_MODE(RFC2822));
9696
hdr_str(name, value);
9797
}
9898

log-tree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ void show_log(struct rev_info *opt)
639639
*/
640640
show_reflog_message(opt->reflog_info,
641641
opt->commit_format == CMIT_FMT_ONELINE,
642-
opt->date_mode,
642+
&opt->date_mode,
643643
opt->date_mode_explicit);
644644
if (opt->commit_format == CMIT_FMT_ONELINE)
645645
return;

0 commit comments

Comments
 (0)