Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Jun 29, 2015
1 parent a5481a6 commit aa1462c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Documentation/rev-list-options.txt
Expand Up @@ -727,6 +727,11 @@ format, often found in email messages.
+
`--date=raw` shows the date in the internal raw Git format `%s %z` format.
+
`--date=format:...` feeds the format `...` to your system `strftime`.
Use `--date=format:%c` to show the date in your system locale's
preferred format. See the `strftime` manual for a complete list of
format placeholders.
+
`--date=default` shows timestamps in the original time zone
(either committer's or author's).

Expand Down
3 changes: 3 additions & 0 deletions builtin/blame.c
Expand Up @@ -2604,6 +2604,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
case DATE_NORMAL:
blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
break;
case DATE_STRFTIME:
blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
break;
}
blame_date_width -= 1; /* strip the null */

Expand Down
2 changes: 2 additions & 0 deletions cache.h
Expand Up @@ -1114,8 +1114,10 @@ struct date_mode {
DATE_ISO8601,
DATE_ISO8601_STRICT,
DATE_RFC2822,
DATE_STRFTIME,
DATE_RAW
} type;
const char *strftime_fmt;
};

/*
Expand Down
9 changes: 8 additions & 1 deletion date.c
Expand Up @@ -163,6 +163,8 @@ void show_date_relative(unsigned long time, int tz,
struct date_mode *date_mode_from_type(enum date_mode_type type)
{
static struct date_mode mode;
if (type == DATE_STRFTIME)
die("BUG: cannot create anonymous strftime date_mode struct");
mode.type = type;
return &mode;
}
Expand Down Expand Up @@ -221,6 +223,8 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
weekday_names[tm->tm_wday], tm->tm_mday,
month_names[tm->tm_mon], tm->tm_year + 1900,
tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
else if (mode->type == DATE_STRFTIME)
strbuf_addftime(&timebuf, mode->strftime_fmt, tm);
else
strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
weekday_names[tm->tm_wday],
Expand Down Expand Up @@ -787,7 +791,10 @@ void parse_date_format(const char *format, struct date_mode *mode)
mode->type = DATE_NORMAL;
else if (!strcmp(format, "raw"))
mode->type = DATE_RAW;
else
else if (skip_prefix(format, "format:", &format)) {
mode->type = DATE_STRFTIME;
mode->strftime_fmt = xstrdup(format);
} else
die("unknown date format %s", format);
}

Expand Down
1 change: 1 addition & 0 deletions gettext.c
Expand Up @@ -162,6 +162,7 @@ void git_setup_gettext(void)
podir = GIT_LOCALE_PATH;
bindtextdomain("git", podir);
setlocale(LC_MESSAGES, "");
setlocale(LC_TIME, "");
init_gettext_charset("git");
textdomain("git");
}
Expand Down
29 changes: 29 additions & 0 deletions strbuf.c
Expand Up @@ -709,3 +709,32 @@ char *xstrfmt(const char *fmt, ...)

return ret;
}

void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
{
size_t len;

/*
* strftime reports "0" if it could not fit the result in the buffer.
* Unfortunately, it also reports "0" if the requested time string
* takes 0 bytes. So if we were to probe and grow, we have to choose
* some arbitrary cap beyond which we guess that the format probably
* just results in a 0-length output. Since we have to choose some
* reasonable cap anyway, and since it is not that big, we may
* as well just grow to their in the first place.
*/
strbuf_grow(sb, 128);
len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);

if (!len) {
/*
* Either we failed, or the format actually produces a 0-length
* output. There's not much we can do, so we leave it blank.
* However, the output array is left in an undefined state, so
* we must re-assert our NUL terminator.
*/
sb->buf[sb->len] = '\0';
} else {
sb->len += len;
}
}
5 changes: 5 additions & 0 deletions strbuf.h
Expand Up @@ -344,6 +344,11 @@ extern void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
__attribute__((format (printf,2,0)))
extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);

/**
* Add the time specified by `tm`, as formatted by `strftime`.
*/
extern void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm);

/**
* Read a given size of data from a FILE* pointer to the buffer.
*
Expand Down
8 changes: 8 additions & 0 deletions t/t6300-for-each-ref.sh
Expand Up @@ -227,6 +227,14 @@ test_expect_success 'Check format "rfc2822" date fields output' '
test_cmp expected actual
'

test_expect_success 'Check format of strftime date fields' '
echo "my date is 2006-07-03" >expected &&
git for-each-ref \
--format="%(authordate:format:my date is %Y-%m-%d)" \
refs/heads >actual &&
test_cmp expected actual
'

cat >expected <<\EOF
refs/heads/master
refs/remotes/origin/master
Expand Down

0 comments on commit aa1462c

Please sign in to comment.