Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 14ac286

Browse files
peffgitster
authored andcommitted
commit: accept more date formats for "--date"
Right now we pass off the string found by "--date" straight to the fmt_ident function, which will use our strict parse_date to normalize it. However, this means obvious things like "--date=now" or "--date=2.days.ago" will not work. Instead, let's fallback to the approxidate function to handle this for us. Note that we must try parse_date ourselves first, even though approxidate will try strict parsing itself. The reason is that approxidate throws away any timezone information it sees from the strict parsing, and we want to preserve it. So asking for: git commit --date="@1234567890 -0700" continues to set the date in -0700, regardless of what the local timezone is. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7242b8 commit 14ac286

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

builtin/commit.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,29 @@ static int sane_ident_split(struct ident_split *person)
526526
return 1;
527527
}
528528

529+
static int parse_force_date(const char *in, char *out, int len)
530+
{
531+
if (len < 1)
532+
return -1;
533+
*out++ = '@';
534+
len--;
535+
536+
if (parse_date(in, out, len) < 0) {
537+
int errors = 0;
538+
unsigned long t = approxidate_careful(in, &errors);
539+
if (errors)
540+
return -1;
541+
snprintf(out, len, "%lu", t);
542+
}
543+
544+
return 0;
545+
}
546+
529547
static void determine_author_info(struct strbuf *author_ident)
530548
{
531549
char *name, *email, *date;
532550
struct ident_split author;
551+
char date_buf[64];
533552

534553
name = getenv("GIT_AUTHOR_NAME");
535554
email = getenv("GIT_AUTHOR_EMAIL");
@@ -574,8 +593,12 @@ static void determine_author_info(struct strbuf *author_ident)
574593
email = xstrndup(lb + 2, rb - (lb + 2));
575594
}
576595

577-
if (force_date)
578-
date = force_date;
596+
if (force_date) {
597+
if (parse_force_date(force_date, date_buf, sizeof(date_buf)))
598+
die(_("invalid date format: %s"), force_date);
599+
date = date_buf;
600+
}
601+
579602
strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
580603
if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
581604
sane_ident_split(&author)) {

t/t7501-commit.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,16 @@ test_expect_success 'commit mentions forced date in output' '
351351
grep "Date: *Sat Jan 2 03:04:05 2010" output
352352
'
353353

354-
test_expect_success 'commit complains about bogus date' '
355-
test_must_fail git commit --amend --date=10.11.2010
354+
test_expect_success 'commit complains about completely bogus dates' '
355+
test_must_fail git commit --amend --date=seventeen
356+
'
357+
358+
test_expect_success 'commit --date allows approxidate' '
359+
git commit --amend \
360+
--date="midnight the 12th of october, anno domini 1979" &&
361+
echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
362+
git log -1 --format=%ad >actual &&
363+
test_cmp expect actual
356364
'
357365

358366
test_expect_success 'sign off (1)' '

0 commit comments

Comments
 (0)