diff options
author | Jeff King <peff@peff.net> | 2014-08-27 11:57:08 +0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-08-27 21:32:56 +0400 |
commit | c33ddc2e33d51da9391a81206a1d9e4a92d97d10 (patch) | |
tree | eb4a28190a98e436d07875d0785b21a27103f827 /builtin/commit.c | |
parent | ea5517f04b08bdb40eca72888220bd6a90d3cf17 (diff) |
date: use strbufs in date-formatting functions
Many of the date functions write into fixed-size buffers.
This is a minor pain, as we have to take special
precautions, and frequently end up copying the result into a
strbuf or heap-allocated buffer anyway (for which we
sometimes use strcpy!).
Let's instead teach parse_date, datestamp, etc to write to a
strbuf. The obvious downside is that we might need to
perform a heap allocation where we otherwise would not need
to. However, it turns out that the only two new allocations
required are:
1. In test-date.c, where we don't care about efficiency.
2. In determine_author_info, which is not performance
critical (and where the use of a strbuf will help later
refactoring).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/commit.c')
-rw-r--r-- | builtin/commit.c | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/builtin/commit.c b/builtin/commit.c index 5ed60364ce..8da0a9f3e3 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -520,19 +520,16 @@ static int sane_ident_split(struct ident_split *person) return 1; } -static int parse_force_date(const char *in, char *out, int len) +static int parse_force_date(const char *in, struct strbuf *out) { - if (len < 1) - return -1; - *out++ = '@'; - len--; + strbuf_addch(out, '@'); - if (parse_date(in, out, len) < 0) { + if (parse_date(in, out) < 0) { int errors = 0; unsigned long t = approxidate_careful(in, &errors); if (errors) return -1; - snprintf(out, len, "%lu", t); + strbuf_addf(out, "%lu", t); } return 0; @@ -542,7 +539,7 @@ static void determine_author_info(struct strbuf *author_ident) { char *name, *email, *date; struct ident_split author; - char date_buf[64]; + struct strbuf date_buf = STRBUF_INIT; name = getenv("GIT_AUTHOR_NAME"); email = getenv("GIT_AUTHOR_EMAIL"); @@ -588,9 +585,10 @@ static void determine_author_info(struct strbuf *author_ident) } if (force_date) { - if (parse_force_date(force_date, date_buf, sizeof(date_buf))) + strbuf_reset(&date_buf); + if (parse_force_date(force_date, &date_buf)) die(_("invalid date format: %s"), force_date); - date = date_buf; + date = date_buf.buf; } strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT)); @@ -600,6 +598,8 @@ static void determine_author_info(struct strbuf *author_ident) export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0); export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@'); } + + strbuf_release(&date_buf); } static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf) |