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

Re: Weird behavior of 'git log --before' or 'git log --date-order': Commits from 2011 are treated to be before 1980

From
Jeff King <peff@peff.net>
Date
Apr 15, 2023, 08:52 UTC
Message-ID
<20230415085207.GA656008@coredump.intra.peff.net>
In-Reply-To
<7728e059-d58d-cce7-c011-fbc16eb22fb9@cs.uni-saarland.de>
On Fri, Apr 14, 2023 at 01:37:57PM +0200, Thomas Bock wrote:
Show 6 quoted lines
> Example to reproduce:
> 
> git clone git@github.com:LibreOffice/core.git libreoffice
> cd libreoffice
> git log --no-merges --before="1980-01-01T00:00:00+0000"
> --format=%H,%ct,%ci,%ad

Thanks for providing a clear example and reproduction recipe. It's an interesting case. The commits _are_ malformed, but only slightly. For example:

  $ git cat-file commit d3b03514dde317473db0d247f21405b5db6a727e
  tree c7526375c71327a195714e9e325b66a9ad013d74
  parent 61099481271709723469421181f65e6219cbc271
  author Andre Fischer<andre.f.fischer <Andre Fischer<andre.f.fischer@oracle.com>> 1297247749 +0100
  committer Andre Fischer<andre.f.fischer <Andre Fischer<andre.f.fischer@oracle.com>> 1297247749 +0100

There's an extra weird set of angle brackets in both the author and committer lines.

But here's the really quirky part: there actually two parsers being used in Git here. One is in parse_commit_buffer(), which does the minimum to fill in the fields of a "struct commit": parents, tree, and committer timestamp. That parser calls parse_commit_date(), which finds the first ">" and then tries to parse "> 1297247749 +0100" as a timestamp, which fails. So it uses the sentinel value "0" (aka 1970-01-01). And that's what gets used for "--before" (and "--since", and things like queue order for showing commits).

But then when we actually _display_ the commit, we have a whole pretty-printing system. There we usually end up in split_ident_line(), which is a bit more forgiving, due to 03818a4a94 (split_ident: parse timestamp from end of line, 2013-10-14). It finds the trailing ">" from the right-hand side, which in this case yields the correct timestamp.

We could probably use the same trick in parse_commit_date(), something like:

diff --git a/commit.c b/commit.c
index 6d844da9a6..72d21a2bb8 100644
--- a/commit.c
+++ b/commit.c
@@ -95,6 +95,7 @@ struct commit *lookup_commit_reference_by_name(const char *name)
 static timestamp_t parse_commit_date(const char *buf, const char *tail)
 {
 	const char *dateptr;
+	const char *eol;
 
 	if (buf + 6 >= tail)
 		return 0;
@@ -106,16 +107,22 @@ static timestamp_t parse_commit_date(const char *buf, const char *tail)
 		return 0;
 	if (memcmp(buf, "committer", 9))
 		return 0;
-	while (buf < tail && *buf++ != '>')
+
+	/*
+	 * parse to end-of-line and then walk backwards, which
+	 * handles some malformed cases. Alternatively, once
+	 * we have eol, we could just call split_ident_line()
+	 */
+	eol = buf;
+	while (eol < tail && *eol++ != '\n')
 		/* nada */;
-	if (buf >= tail)
+	if (eol >= tail)
 		return 0;
-	dateptr = buf;
-	while (buf < tail && *buf++ != '\n')
+	for (dateptr = eol; dateptr > buf && dateptr[-1] != '>'; dateptr--)
 		/* nada */;
-	if (buf >= tail)
+	if (dateptr == buf || dateptr == eol)
 		return 0;
-	/* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
+	/* dateptr < eol && *eol == '\n', so parsing will stop at eol */
 	return parse_timestamp(dateptr, NULL, 10);
 }
 

which makes your case behave as expected. It may make sense to use
split_ident_line(), which I think has a few other rules (it actually
checks for something that looks like numbers, for instance).

There may be also cases where the two diverge. Obviously having two
parsers isn't ideal. I think sharing the code may involve a lot of work,
though. The pretty-print parser is interested in pulling out more
information, and is less focused on performance. Parsing commits is
traditionally a hot path, as we historically had to parse every commit,
even if we weren't showing it (including non-log operations like
computing merge bases, reachability, and so forth).

But that may not matter so much. One, we already inflate the whole
commit object, not just the header. So even if we spend a few extra
instructions on parsing, it may not be noticeable. And two, these days
we often cache commit metadata in the commit-graph files, which avoids
loading the commit message entirely (and thus this parsing) for most
operations.

Of course it may also be reasonable to consider this mystery solved and
leave the code as-is. These objects _are_ malformed. I note they're all
at least a decade old, and most of them are from a handful of committers
(who presumably had misconfigured idents for a while). These days I
think Git wouldn't allow them (I don't think it rejects the commit, but
it does screen out the syntactically bogus characters).

-Peff
Previous: Thomas BockNext: Jeff King
Message 2 of 46 in “Weird behavior of 'git log --before' or 'git log --date-order': Commits from 2011 are treated to be before 1980”
  1. Thomas BockApr 14, 2023
  2. Jeff KingApr 15, 2023
  3. Jeff KingApr 15, 2023
  4. Kristoffer HaugsbakkApr 15, 2023
  5. Jeff KingApr 17, 2023
  6. Kristoffer HaugsbakkApr 17, 2023
  7. Jeff KingApr 17, 2023
  8. Kristoffer HaugsbakkApr 27, 2023
  9. Junio C HamanoApr 17, 2023
  10. Jeff KingApr 18, 2023
  11. Derrick StoleeApr 18, 2023
  12. Thomas BockApr 21, 2023
  13. 0/3 fixing some parse_commit() timestamp corner casesJeff King, Apr 22, 2023
  14. 1/3 t4212: avoid putting git on left-hand side of pipeJeff King, Apr 22, 2023
  15. 2/3 parse_commit(): parse timestamp from end of lineJeff King, Apr 22, 2023
  16. Junio C HamanoApr 24, 2023
  17. Jeff KingApr 25, 2023
  18. Junio C HamanoApr 24, 2023
  19. 0/3 fixing some parse_commit() timestamp corner casesJeff King, Apr 25, 2023
  20. Jeff KingApr 25, 2023
  21. 1/4 t4212: avoid putting git on left-hand side of pipeJeff King, Apr 25, 2023
  22. 2/4 parse_commit(): parse timestamp from end of lineJeff King, Apr 25, 2023
  23. 3/4 parse_commit(): handle broken whitespace-only timestampJeff King, Apr 25, 2023
  24. Phillip WoodApr 25, 2023
  25. Junio C HamanoApr 25, 2023
  26. Jeff KingApr 26, 2023
  27. Junio C HamanoApr 26, 2023
  28. 0/4 fixing some parse_commit() timestamp corner casesJeff King, Apr 27, 2023
  29. 1/4 t4212: avoid putting git on left-hand side of pipeJeff King, Apr 27, 2023
  30. 2/4 parse_commit(): parse timestamp from end of lineJeff King, Apr 27, 2023
  31. 3/4 parse_commit(): handle broken whitespace-only timestampJeff King, Apr 27, 2023
  32. Phillip WoodApr 27, 2023
  33. Phillip WoodApr 27, 2023
  34. Jeff KingApr 27, 2023
  35. Junio C HamanoApr 27, 2023
  36. Jeff KingApr 27, 2023
  37. Junio C HamanoApr 27, 2023
  38. Jeff KingApr 27, 2023
  39. 4/4 parse_commit(): describe more date-parsing failure modesJeff King, Apr 27, 2023
  40. Jeff KingApr 27, 2023
  41. Junio C HamanoApr 27, 2023
  42. Phillip WoodApr 26, 2023
  43. Andreas SchwabApr 26, 2023
  44. Phillip WoodApr 26, 2023
  45. 4/4 parse_commit(): describe more date-parsing failure modesJeff King, Apr 25, 2023
  46. Jeff KingApr 22, 2023

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.