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

[PATCH 2/3] commit: rename parse_commit_date()

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 8, 2013, 23:01 UTC
Message-ID
<1365462114-8630-3-git-send-email-gitster@pobox.com>
In-Reply-To
<1365462114-8630-1-git-send-email-gitster@pobox.com>

This function does a lot more than parsing the committer date out of a commit object buffer. After its sole caller parses one "tree", and 0 or more "parent", it makes sure the next one is "author" (and skips it), makes sure "committer" follows (and skips the committer identity), and parses the date field. Each of these fields must be on its own line (no header folding is allowed).

Rename it to parse_commit_standard_header(), and change the function signature to accept "struct commit *" to be updated as a parameter.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 commit.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
diff --git a/commit.c b/commit.c
index 9d7e81b..50a9827 100644
--- a/commit.c
+++ b/commit.c
@@ -78,31 +78,33 @@ struct commit *lookup_commit_reference_by_name(const char *name)
 	return commit;
 }
 
-static unsigned long parse_commit_date(const char *buf, const char *tail)
+static void parse_commit_standard_headers(const char *buf, const char *tail,
+					  struct commit *item)
 {
 	const char *dateptr;
 
+	item->date = 0;
 	if (buf + 6 >= tail)
-		return 0;
+		return;
 	if (memcmp(buf, "author", 6))
-		return 0;
+		return;
 	while (buf < tail && *buf++ != '\n')
-		/* nada */;
+		; /* skip to the end of the line */
 	if (buf + 9 >= tail)
-		return 0;
+		return;
 	if (memcmp(buf, "committer", 9))
-		return 0;
+		return;
 	while (buf < tail && *buf++ != '>')
-		/* nada */;
+		; /* skip to the end of the e-mail */
 	if (buf >= tail)
-		return 0;
+		return;
 	dateptr = buf;
 	while (buf < tail && *buf++ != '\n')
-		/* nada */;
+		; /* skip to the end of the line */
 	if (buf >= tail)
-		return 0;
+		return;
 	/* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
-	return strtoul(dateptr, NULL, 10);
+	item->date = strtoul(dateptr, NULL, 10);
 }
 
 static struct commit_graft **commit_graft;
@@ -262,6 +264,11 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
 	if (item->object.parsed)
 		return 0;
 	item->object.parsed = 1;
+
+	/*
+	 * tree, 0-or-more parents, author and committer are required
+	 * and must appear in this order; no line folding is allowed.
+	 */
 	tail += size;
 	if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
 		return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
@@ -301,8 +308,7 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
 			pptr = &commit_list_insert(new_parent, pptr)->next;
 		}
 	}
-	item->date = parse_commit_date(bufptr, tail);
-
+	parse_commit_standard_headers(bufptr, tail, item);
 	return 0;
 }
 
-- 
1.8.2.1-450-gd047976
Previous: Jeff KingNext: Junio C Hamano
Message 7 of 15 in “decorate: add "clear_decoration()"”
  1. 2/2 decorate: add "clear_decoration()"Junio C Hamano, Apr 8, 2013
  2. Jeff KingApr 8, 2013
  3. Junio C HamanoApr 8, 2013
  4. 0/3 Using a bit more decorationJunio C Hamano, Apr 8, 2013
  5. 1/3 commit: shrink "indegree" fieldJunio C Hamano, Apr 8, 2013
  6. Jeff KingApr 9, 2013
  7. 2/3 commit: rename parse_commit_date()Junio C Hamano, Apr 8, 2013
  8. 3/3 commit: add get_commit_encoding()Junio C Hamano, Apr 8, 2013
  9. Jeff KingApr 9, 2013
  10. Junio C HamanoApr 9, 2013
  11. Jeff KingApr 9, 2013
  12. Junio C HamanoApr 9, 2013
  13. Jeff KingApr 9, 2013
  14. Junio C HamanoApr 9, 2013
  15. Junio C HamanoApr 9, 2013

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.