Remove the old ad-hoc parser for commit objects
authorChristian Thaeter <ct@pipapo.org>
Thu, 7 Feb 2008 07:03:55 +0000 (08:03 +0100)
committerChristian Thaeter <ct@pipapo.org>
Thu, 7 Feb 2008 07:03:55 +0000 (08:03 +0100)
use the new 'struct commit' based parsers invented earlier now

src/object_commit.c

index cf82bef..c006827 100644 (file)
@@ -29,7 +29,7 @@ webgit_object_commit_tree_parse (struct commit* commit)
   char* tree = strstr (commit->buffer, "tree ");
   if (!tree)
     return NULL;
-  return tree + 5;
+  return strncpy (cwa_buffer_provide (41), tree+5, 40);
 }
 
 time_t
@@ -53,6 +53,35 @@ webgit_object_commit_author_date_parse (struct commit* commit, struct tm* tm)
   return mktime (tm);
 }
 
+
+Html
+webgit_object_commit_parents_parse (struct commit* commit, struct webgit_repo_info* repo)
+{
+  Html parents = html_list ();
+
+  struct commit* parent;
+  while ((parent = pop_commit(&commit->parents)))
+    {
+      char* sha1 = sha1_to_hex (parent->object.sha1);
+
+      html_list_append (parents,
+                        html (
+                            "Parent: ",
+                            webgit_object_link (repo->query,
+                                                repo->query->repo, strlen (repo->query->repo),
+                                                repo->query->head, strlen (repo->query->head),
+                                                sha1, 40,
+                                                NULL, 0,
+                                                NULL,
+                                                html_fmt ("%.40s", sha1))
+                            )
+                      );
+    }
+
+  return parents;
+}
+
+
 Html
 webgit_object_commit_author_name_parse (struct commit* commit)
 {
@@ -67,6 +96,7 @@ webgit_object_commit_author_name_parse (struct commit* commit)
   return html_fmt ("%.*s", end-author-8, author+7);
 }
 
+
 Html
 webgit_object_commit_author_email_parse (struct commit* commit)
 {
@@ -86,6 +116,7 @@ webgit_object_commit_author_email_parse (struct commit* commit)
   return html_fmt ("%.*s", end-beg-1, beg+1);
 }
 
+
 time_t
 webgit_object_commit_committer_date_parse (struct commit* commit, struct tm* tm)
 {
@@ -107,6 +138,7 @@ webgit_object_commit_committer_date_parse (struct commit* commit, struct tm* tm)
   return mktime (tm);
 }
 
+
 Html
 webgit_object_commit_committer_name_parse (struct commit* commit)
 {
@@ -121,6 +153,7 @@ webgit_object_commit_committer_name_parse (struct commit* commit)
   return html_fmt ("%.*s", end-committer-11, committer+10);
 }
 
+
 Html
 webgit_object_commit_committer_email_parse (struct commit* commit)
 {
@@ -140,6 +173,7 @@ webgit_object_commit_committer_email_parse (struct commit* commit)
   return html_fmt ("%.*s", end-beg-1, beg+1);
 }
 
+
 Html
 webgit_object_commit_header_parse (struct commit* commit)
 {
@@ -156,6 +190,7 @@ webgit_object_commit_header_parse (struct commit* commit)
   return html_fmt ("%.*s", end-header, header+2);
 }
 
+
 Html
 webgit_object_commit_message_parse (struct commit* commit)
 {
@@ -176,14 +211,11 @@ webgit_object_commit_message_parse (struct commit* commit)
 /*
   Display commits
 */
-
 static Html
-webgit_object_commit_menu_action (struct webgit_repo_info* repo, unsigned char* sha1, void* buf, unsigned long size)
+webgit_object_commit_menu_action (struct webgit_repo_info* repo, unsigned char* sha1, struct commit* commit)
 {
   (void) repo;
   (void) sha1;
-  (void) buf;
-  (void) size;
   return html (html_tag ("div"),
                webgit_repo_logo (repo), "<br />",
                // TODO: "diff against.. <br />",
@@ -196,182 +228,52 @@ webgit_object_commit_menu_action (struct webgit_repo_info* repo, unsigned char*
                );
 }
 
+
 static Html
-webgit_object_commit_content_action (struct webgit_repo_info* repo, unsigned char* sha1, void* buf, unsigned long size)
+webgit_object_commit_content_action (struct webgit_repo_info* repo, unsigned char* sha1, struct commit* commit)
 {
-/*
-  TODO pass commit objects instead buf/size, use parsers from above
-*/
-  (void) sha1;
-
-  Html tree = html_list ();
-  Html parents = html_list ();
-  Html author_text = html_list ();
-  Html author = html_list ();
-  Html committer = html_list ();
-
-  const char* author_beg = NULL;
-  const char* author_end = NULL;
-
-  const char* i = buf;
-  while (i && (void*)i < buf+size)
-    {
-      if (*i == '\n')
-        {
-          while (*i == '\n')
-            ++i;
-          break; /* message */
-        }
-      if (!strncmp (i, "tree ", 5))
-        {
-          html_list_append (tree,
-                            "Tree: ",
-                            webgit_object_link (repo->query,
-                                                repo->name, strlen (repo->name),
-                                                i+5, 40,
-                                                NULL,
-                                                NULL,
-                                                html_fmt ("%.40s", i+5))
-                            );
-          if ((i = strchr (i, '\n')))
-            ++i;
-          continue;
-        }
-      else if (!strncmp (i, "parent ", 7))
-        {
-          html_list_append (parents, html (
-                                           "Parent: ",
-                                           webgit_object_link (repo->query,
-                                                               repo->name, strlen (repo->name),
-                                                               i+7, 40,
-                                                               NULL,
-                                                               NULL,
-                                                               html_fmt ("%.40s", i+7))
-                                           )
-                            );
-          if ((i = strchr (i, '\n')))
-            ++i;
-          continue;
-        }
-      else if (!strncmp (i, "author ", 7))
-        {
-          char* email_beg = strchr (i, '<');
-          char* email_end = strchr (email_beg, '>');
-
-          author_beg = i+7;
-          author_end = strchr (author_beg, '\n');
-
-          Html name = html_strndup (i+7, email_beg - i - 8);
-          Html email = html_strndup (email_beg + 1, email_end - email_beg - 1);
-
-          struct tm tm;
-          strptime (email_end + 2, "%s %Z", &tm);
-          char pretty_date[256];
-          strftime (pretty_date, 255, "%c", &tm);
-
-          html_list_append (author_text, "Author");
-
-          html_list_append (author, html (
-                                          html ( author_text ), /*BUG: libcwa bug, must be wraped in html()*/
-                                          webgit_email_link (name, email),
-                                          html_strndup (pretty_date, 255)
-                                          )
-                            );
-          if ((i = strchr (i, '\n')))
-            ++i;
-          continue;
-        }
-      else if (!strncmp (i, "committer ", 10))
-        {
-          char* email_beg = strchr (i, '<');
-          char* email_end = strchr (email_beg, '>');
-
-          if (author_beg && author_end && strncmp (i + 10, author_beg, author_end - author_beg))
-            {
-              Html name = html_strndup (i+10, email_beg - i - 11);
-              Html email = html_strndup (email_beg + 1, email_end - email_beg - 1);
-
-              struct tm tm;
-              strptime (email_end + 2, "%s %Z", &tm);
-              char pretty_date[256];
-              strftime (pretty_date, 255, "%c", &tm);
-
-              html_list_append (committer, html ("Committer: ",
-                                                 webgit_email_link (name, email),
-                                                 html_strndup (pretty_date, 255)
-                                                 )
-                                );
-            }
-          else
-            html_list_append (author_text, " and Committer");
-
-          if ((i = strchr (i, '\n')))
-            ++i;
-          continue;
-        }
-      ++i;
-    }
-
-  html_list_append (author_text, ": ");
-
-  Html headline = html_list ();
-  Html message = html_list ();
-
-  if (i)
-    {
-      const char* message_beg = strchr (i, '\n');
-
-      if (message_beg)
-        {
-          html_list_append (headline,
-                            html_strndup (i, message_beg - i)
-                            );
-
-          while (*message_beg == '\n')
-            ++message_beg;
-
-          html_list_append (message,
-                            html_strndup (message_beg, SIZE_MAX)
-                            );
-        }
-      else
-        {
-          html_list_append (headline,
-                            html_strndup (i, SIZE_MAX)
-                            );
-          html_list_append (message,
-                            html_strndup (i, SIZE_MAX)
-                            );
-        }
-    }
-
-  /* TODO: diffstat */
-
+  char* tree = webgit_object_commit_tree_parse (commit);
 
   return html (
-               html (html_tag ("h3"), headline),
-               html (html_tag ("div"), tree), html_nl (),
-               html (html_tag ("div"), parents), html_nl (),
-               html (html_tag ("div"), author), html_nl (),
-               html (html_tag ("div"), committer), html_nl (),
-               html (html_tag ("pre"), message)
+               html (html_tag ("h3"), webgit_object_commit_header_parse (commit)),
+               html (html_tag ("div"),
+                     "Tree: ",
+                     webgit_object_link (repo->query,
+                                         repo->query->repo, strlen (repo->query->repo),
+                                         repo->query->head, strlen (repo->query->head),
+                                         tree, 40,
+                                         "", 0,
+                                         NULL,
+                                         html_strndup (tree, 40)
+                                         )
+                     ), html_nl (),
+               html (html_tag ("div"), webgit_object_commit_parents_parse (commit, repo)), html_nl (),
+               html (html_tag ("div"),
+                     webgit_object_commit_author_name_parse (commit), " ",
+                     webgit_object_commit_author_email_parse (commit), " ",
+                     webgit_pretty_date (webgit_object_commit_author_date_parse (commit, NULL))
+                     ), html_nl (),
+               html (html_tag ("div"),
+                     webgit_object_commit_committer_name_parse (commit), " ",
+                     webgit_object_commit_committer_email_parse (commit), " ",
+                     webgit_pretty_date (webgit_object_commit_committer_date_parse (commit, NULL))
+                     ), html_nl (),
+               html (html_tag ("pre"), webgit_object_commit_message_parse (commit))
                );
 }
 
 Html
 webgit_object_commit_action (struct webgit_repo_info* repo, unsigned char* sha1)
 {
-  void* buf;
-  unsigned long size;
-
-  buf = read_object_with_reference (sha1, "commit", &size, NULL);
+  struct commit* commit = lookup_commit_reference (sha1);
+  parse_commit (commit);
 
   return html(
               html (html_tag("div", html_attr ("id", "sub-menu")),
-                    webgit_object_commit_menu_action (repo, sha1, buf, size)
+                    webgit_object_commit_menu_action (repo, sha1, commit)
                     ), html_nl (),
               html (html_tag("div", html_attr ("id", "object-commit")),
-                    webgit_object_commit_content_action (repo, sha1, buf, size)
+                    webgit_object_commit_content_action (repo, sha1, commit)
                     ), html_nl ()
               );
 }