git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Git Mailing List <git@vger.kernel.org>
Subject: [PATCH 4/4] commit: accept more date formats for "--date"
Date: Thu, 1 May 2014 21:12:42 -0400	[thread overview]
Message-ID: <20140502011242.GD25413@sigill.intra.peff.net> (raw)
In-Reply-To: <20140502010328.GA30556@sigill.intra.peff.net>

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 <peff@peff.net>
---
 builtin/commit.c  | 27 +++++++++++++++++++++++++--
 t/t7501-commit.sh | 12 ++++++++++--
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index a25661f..d1c90db 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -526,10 +526,29 @@ static int sane_ident_split(struct ident_split *person)
 	return 1;
 }
 
+static int parse_force_date(const char *in, char *out, int len)
+{
+	if (len < 1)
+		return -1;
+	*out++ = '@';
+	len--;
+
+	if (parse_date(in, out, len) < 0) {
+		int errors = 0;
+		unsigned long t = approxidate_careful(in, &errors);
+		if (errors)
+			return -1;
+		snprintf(out, len, "%lu", t);
+	}
+
+	return 0;
+}
+
 static void determine_author_info(struct strbuf *author_ident)
 {
 	char *name, *email, *date;
 	struct ident_split author;
+	char date_buf[64];
 
 	name = getenv("GIT_AUTHOR_NAME");
 	email = getenv("GIT_AUTHOR_EMAIL");
@@ -574,8 +593,12 @@ static void determine_author_info(struct strbuf *author_ident)
 		email = xstrndup(lb + 2, rb - (lb + 2));
 	}
 
-	if (force_date)
-		date = force_date;
+	if (force_date) {
+		if (parse_force_date(force_date, date_buf, sizeof(date_buf)))
+			die(_("invalid date format: %s"), force_date);
+		date = date_buf;
+	}
+
 	strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
 	if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
 	    sane_ident_split(&author)) {
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index 5a76823..63e0427 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -351,8 +351,16 @@ test_expect_success 'commit mentions forced date in output' '
 	grep "Date: *Sat Jan 2 03:04:05 2010" output
 '
 
-test_expect_success 'commit complains about bogus date' '
-	test_must_fail git commit --amend --date=10.11.2010
+test_expect_success 'commit complains about completely bogus dates' '
+	test_must_fail git commit --amend --date=seventeen
+'
+
+test_expect_success 'commit --date allows approxidate' '
+	git commit --amend \
+		--date="midnight the 12th of october, anno domini 1979" &&
+	echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
+	git log -1 --format=%ad >actual &&
+	test_cmp expect actual
 '
 
 test_expect_success 'sign off (1)' '
-- 
1.9.1.656.ge8a0637

  parent reply	other threads:[~2014-05-02  1:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-30 21:09 [RFD] use approxidate for "git commit --date=xyz"? Linus Torvalds
2014-04-30 21:34 ` Junio C Hamano
2014-05-01 22:06   ` Jeff King
2014-05-02  1:03     ` [PATCH 0/4] approxidate for "git commit --date=foo" Jeff King
2014-05-02  1:06       ` [PATCH 1/4] commit: use split_ident_line to compare author/committer Jeff King
2014-05-02  1:07       ` [PATCH 2/4] pretty: make show_ident_date public Jeff King
2014-05-02  1:10       ` [PATCH 3/4] commit: print "Date" line when the user has set date Jeff King
2014-05-02  1:12       ` Jeff King [this message]
2014-05-07  7:22   ` [RFD] use approxidate for "git commit --date=xyz"? Peter Krefting

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140502011242.GD25413@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).