git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] fast-import: be strict about formatting of raw dates

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Apr 24, 2010, 20:50 UTC
Message-ID
<20100424205036.GB24948@progeny.tock>
In-Reply-To
<20100424203827.GA24948@progeny.tock>

git proper never zero- or space-pads its dates and its time zones are always 4 digits long. Require fast-import front-ends to behave likewise to avoid generating puzzling objects.

Since this makes the input format more strict, some front-ends may not be happy. But they were warned:

   This is the Git native format and is <time> SP <offutc>. It is also
   fast-import’s default format, if --date-format was not specified.
   Unlike the rfc2822 format, this format is very strict. Any variation
   in formatting will cause fast-import to reject the value.

Aside from ensuring the format is predictable so tools like git can handle it, making the date format this strict ensures that there is only one valid representation for a given date and time zone, which would be useful for round-trip conversion of objects to and from other formats (for storage by other version control systems, for example).

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Is -0000 the same time zone as +0000?  I wasn’t sure so I erred on the
side of not worrying about it.
 fast-import.c          |    9 +++++++--
 t/t9300-fast-import.sh |   30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/fast-import.c b/fast-import.c
index 74f08bd..1701cf1 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1906,6 +1906,8 @@ static int validate_raw_date(const char *src, char *result, int maxlen)
 
 	errno = 0;
 
+	if ((*src == '0' && isdigit(src[1])) || !isdigit(*src))
+		return -1;
 	num = strtoul(src, &endp, 10);
 	/* NEEDSWORK: perhaps check for reasonable values? */
 	if (errno || endp == src || *endp != ' ')
@@ -1915,8 +1917,11 @@ static int validate_raw_date(const char *src, char *result, int maxlen)
 	if (*src != '-' && *src != '+')
 		return -1;
 
-	num = strtoul(src + 1, &endp, 10);
-	if (errno || endp == src + 1 || *endp || (endp - orig_src) >= maxlen ||
+	src++;
+	if (*src != '0' && *src != '1')
+		return -1;
+	num = strtoul(src, &endp, 10);
+	if (errno || endp != src + 4 || *endp || (endp - orig_src) >= maxlen ||
 	    1400 < num)
 		return -1;
 
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 131f032..ed653a7 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -348,6 +348,36 @@ test_expect_success \
 
 cat >input <<INPUT_END
 commit refs/heads/branch
+author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 1170783301 -  0500
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 117078330 -0500
+data <<COMMIT
+Malformed time zone
+COMMIT
+
+from refs/heads/branch^0
+
+INPUT_END
+test_expect_success 'E: blanks in raw time zone' '
+    test_must_fail git fast-import --date-format=raw <input
+'
+
+cat >input <<INPUT_END
+commit refs/heads/branch
+author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 01170783301 -0500
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 117078330 -0500
+data <<COMMIT
+Malformed date
+COMMIT
+
+from refs/heads/branch^0
+
+INPUT_END
+test_expect_success 'E: leading zero in raw date' '
+    test_must_fail git fast-import --date-format=raw <input
+'
+
+cat >input <<INPUT_END
+commit refs/heads/branch
 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> Tue Feb 6 11:22:18 2007 -0500
 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> Tue Feb 6 12:35:02 2007 -0500
 data <<COMMIT
-- 
1.7.1.rc1
Previous: Jonathan NiederNext: Jonathan Nieder
Message 6 of 13 in “fast-import docs: LT is valid in email, GT is not”
  1. fast-import docs: LT is valid in email, GT is notMark Lodato, Apr 24, 2010
  2. fsck: check ident lines in commit objectsJonathan Nieder, Apr 24, 2010
  3. Jonathan NiederApr 24, 2010
  4. Shawn O. PearceApr 24, 2010
  5. 0/2 fast-import: tighten up parsing ident lineJonathan Nieder, Apr 24, 2010
  6. 1/2 fast-import: be strict about formatting of raw datesJonathan Nieder, Apr 24, 2010
  7. 2/2 fast-import: validate entire ident stringJonathan Nieder, Apr 24, 2010
  8. Shawn O. PearceApr 26, 2010
  9. Jonathan NiederApr 26, 2010
  10. Jonathan NiederApr 26, 2010
  11. Junio C HamanoMay 4, 2010
  12. Jonathan NiederApr 24, 2010
  13. Mark LodatoApr 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.