Skip to content

Commit aa1462c

Browse files
peffgitster
authored andcommitted
introduce "format" date-mode
This feeds the format directly to strftime. Besides being a little more flexible, the main advantage is that your system strftime may know more about your locale's preferred format (e.g., how to spell the days of the week). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5481a6 commit aa1462c

File tree

8 files changed

+61
-1
lines changed

8 files changed

+61
-1
lines changed

Documentation/rev-list-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,11 @@ format, often found in email messages.
727727
+
728728
`--date=raw` shows the date in the internal raw Git format `%s %z` format.
729729
+
730+
`--date=format:...` feeds the format `...` to your system `strftime`.
731+
Use `--date=format:%c` to show the date in your system locale's
732+
preferred format. See the `strftime` manual for a complete list of
733+
format placeholders.
734+
+
730735
`--date=default` shows timestamps in the original time zone
731736
(either committer's or author's).
732737

builtin/blame.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
26042604
case DATE_NORMAL:
26052605
blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
26062606
break;
2607+
case DATE_STRFTIME:
2608+
blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
2609+
break;
26072610
}
26082611
blame_date_width -= 1; /* strip the null */
26092612

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,10 @@ struct date_mode {
11141114
DATE_ISO8601,
11151115
DATE_ISO8601_STRICT,
11161116
DATE_RFC2822,
1117+
DATE_STRFTIME,
11171118
DATE_RAW
11181119
} type;
1120+
const char *strftime_fmt;
11191121
};
11201122

11211123
/*

date.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ void show_date_relative(unsigned long time, int tz,
163163
struct date_mode *date_mode_from_type(enum date_mode_type type)
164164
{
165165
static struct date_mode mode;
166+
if (type == DATE_STRFTIME)
167+
die("BUG: cannot create anonymous strftime date_mode struct");
166168
mode.type = type;
167169
return &mode;
168170
}
@@ -221,6 +223,8 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
221223
weekday_names[tm->tm_wday], tm->tm_mday,
222224
month_names[tm->tm_mon], tm->tm_year + 1900,
223225
tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
226+
else if (mode->type == DATE_STRFTIME)
227+
strbuf_addftime(&timebuf, mode->strftime_fmt, tm);
224228
else
225229
strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
226230
weekday_names[tm->tm_wday],
@@ -787,7 +791,10 @@ void parse_date_format(const char *format, struct date_mode *mode)
787791
mode->type = DATE_NORMAL;
788792
else if (!strcmp(format, "raw"))
789793
mode->type = DATE_RAW;
790-
else
794+
else if (skip_prefix(format, "format:", &format)) {
795+
mode->type = DATE_STRFTIME;
796+
mode->strftime_fmt = xstrdup(format);
797+
} else
791798
die("unknown date format %s", format);
792799
}
793800

gettext.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ void git_setup_gettext(void)
162162
podir = GIT_LOCALE_PATH;
163163
bindtextdomain("git", podir);
164164
setlocale(LC_MESSAGES, "");
165+
setlocale(LC_TIME, "");
165166
init_gettext_charset("git");
166167
textdomain("git");
167168
}

strbuf.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,32 @@ char *xstrfmt(const char *fmt, ...)
709709

710710
return ret;
711711
}
712+
713+
void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
714+
{
715+
size_t len;
716+
717+
/*
718+
* strftime reports "0" if it could not fit the result in the buffer.
719+
* Unfortunately, it also reports "0" if the requested time string
720+
* takes 0 bytes. So if we were to probe and grow, we have to choose
721+
* some arbitrary cap beyond which we guess that the format probably
722+
* just results in a 0-length output. Since we have to choose some
723+
* reasonable cap anyway, and since it is not that big, we may
724+
* as well just grow to their in the first place.
725+
*/
726+
strbuf_grow(sb, 128);
727+
len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
728+
729+
if (!len) {
730+
/*
731+
* Either we failed, or the format actually produces a 0-length
732+
* output. There's not much we can do, so we leave it blank.
733+
* However, the output array is left in an undefined state, so
734+
* we must re-assert our NUL terminator.
735+
*/
736+
sb->buf[sb->len] = '\0';
737+
} else {
738+
sb->len += len;
739+
}
740+
}

strbuf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ extern void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
344344
__attribute__((format (printf,2,0)))
345345
extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
346346

347+
/**
348+
* Add the time specified by `tm`, as formatted by `strftime`.
349+
*/
350+
extern void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm);
351+
347352
/**
348353
* Read a given size of data from a FILE* pointer to the buffer.
349354
*

t/t6300-for-each-ref.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ test_expect_success 'Check format "rfc2822" date fields output' '
227227
test_cmp expected actual
228228
'
229229

230+
test_expect_success 'Check format of strftime date fields' '
231+
echo "my date is 2006-07-03" >expected &&
232+
git for-each-ref \
233+
--format="%(authordate:format:my date is %Y-%m-%d)" \
234+
refs/heads >actual &&
235+
test_cmp expected actual
236+
'
237+
230238
cat >expected <<\EOF
231239
refs/heads/master
232240
refs/remotes/origin/master

0 commit comments

Comments
 (0)