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 <gits...@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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to