New webgit_object_link implementation, obsoletes webgit_repo_link
authorChristian Thaeter <ct@pipapo.org>
Fri, 8 Feb 2008 06:47:23 +0000 (07:47 +0100)
committerChristian Thaeter <ct@pipapo.org>
Fri, 8 Feb 2008 06:47:23 +0000 (07:47 +0100)
webgit_object_link takes pairs of "param", value as va_args now,
this generalizes all kinds of links to object.

All existing links have to be rewritten to use repo/ref/commit/(path)
tuple to refer to objects when possible.

src/actions.c
src/branch.c
src/log.c
src/object.c
src/object.h
src/object_commit.c
src/object_tag.c
src/object_tree.c
src/repo.c
src/repo.h
src/tag.c

index 4271617..d97398b 100644 (file)
@@ -128,12 +128,10 @@ webgit_main_content_action (struct webgit_query* query)
                                          html(html_tag ("td"), n->description),
                                          html(html_tag ("td"), n->owner),
                                          html(html_tag ("td"),
-                                              webgit_object_link (query,
-                                                                  n->name, strlen(n->name),
-                                                                  n->last_commit, 40,
-                                                                  NULL,
-                                                                  NULL,
-                                                                  webgit_pretty_age (n->age))
+                                              webgit_object_link (query, webgit_pretty_age (n->age),
+                                                                  "repo", n->name,
+                                                                  "ref", n->last_head,
+                                                                  "commit", n->last_commit)
                                               ),
                                          html(html_tag ("td"),
                                               webgit_email_link (
@@ -160,12 +158,12 @@ webgit_main_content_action (struct webgit_query* query)
                                                    "Diff ",
                                                    //webgit_diff_link (query, n, n->last_head, html("Diff")),
                                                    //" ",
-                                                   webgit_object_link (query,
-                                                                       n->name, strlen(n->name),
-                                                                       n->last_tree, 40,
-                                                                       NULL,
-                                                                       NULL,
-                                                                       html("Tree"))
+                                                   webgit_object_link (query, html("Tree"),
+                                                                       "repo", n->name,
+                                                                       "ref", n->last_head,
+                                                                       "commit", n->last_commit,
+                                                                       "path", NULL
+                                                                       )
                                                    )
                                               )
                                          ),
@@ -242,12 +240,10 @@ webgit_summary_content_action (struct webgit_repo_info* repo)
                           html(
                                html_tag ("div"),
                                "Last change: ",
-                               webgit_object_link (repo->query,
-                                                   repo->name, strlen(repo->name),
-                                                   repo->last_commit, 40,
-                                                   NULL,
-                                                   NULL,
-                                                   webgit_pretty_age (repo->age)),
+                               webgit_object_link (repo->query, webgit_pretty_age (repo->age),
+                                                   "repo", repo->name,
+                                                   "ref", repo->last_head,
+                                                   "commit", repo->last_commit),
                                " in branch ",
                                 webgit_log_link (repo->query,
                                                  repo->name,
@@ -297,7 +293,6 @@ webgit_summary_content_action (struct webgit_repo_info* repo)
                           )
                     );
 
-
   return content;
 }
 
index 2a619bb..4debe02 100644 (file)
@@ -110,12 +110,10 @@ show_branch_ref (const char *refname, const unsigned char *sha1, int flags, void
                                 ),
                           html (
                                 html_tag ("td"),
-                                webgit_object_link (repo->query,
-                                                    repo->name, strlen(repo->name),
-                                                    sha1_to_hex (sha1), 40,
-                                                    NULL,
-                                                    NULL,
-                                                    webgit_pretty_age (repo->query->now - date))
+                                webgit_object_link (repo->query, webgit_pretty_age (repo->query->now - date),
+                                                    "repo", repo->query->repo,
+                                                    "ref", refname,
+                                                    "commit", sha1_to_hex (commit->object.sha1))
                                 ),
                           html (
                                 html_tag ("td"),
@@ -126,13 +124,11 @@ show_branch_ref (const char *refname, const unsigned char *sha1, int flags, void
                                 ),
                           html (
                                 html_tag ("td"),
-                                webgit_object_link (repo->query,
-                                                    repo->name, strlen(repo->name),
-                                                    webgit_object_commit_tree_parse (commit), 40,
-                                                    NULL,
-                                                    NULL,
-                                                    html ("Tree")
-                                                    ),
+                                webgit_object_link (repo->query, html ("Tree"),
+                                                    "repo", repo->query->repo,
+                                                    "ref", refname,
+                                                    "commit", sha1_to_hex (commit->object.sha1),
+                                                    "path", ""),
                                 html (" Snapshot")
                                 )
                           )
index 5d7f938..c41033a 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -44,7 +44,7 @@ webgit_log_link (struct webgit_query* query,
                                                  html_fmt ("%s?repo=%s&action=log",
                                                            query->request->script_name,
                                                            repo),
-                                                 head ? html_fmt("&head=%s", head)
+                                                 head ? html_fmt("&ref=%s", head)
                                                  : html (),
                                                  object ? html_fmt("&object=%.*s",
                                                                    object_len, object)
@@ -79,6 +79,8 @@ webgit_log_table (struct webgit_query* query, const char* object, int count, int
   const char* argv[] = {NULL, object, NULL};
   int argc = 2;
 
+  webgit_object_deduce (query);
+
   init_revisions (&rev, NULL);
   rev.verbose_header = 1;
   rev.topo_order = 1;
@@ -91,6 +93,8 @@ webgit_log_table (struct webgit_query* query, const char* object, int count, int
   Html cont = html_list ();
   int row = 0;
 
+  // TODO "Prev..." to go back
+
   struct commit *commit;
   while ((commit = get_revision (&rev)))
     {
@@ -137,15 +141,10 @@ webgit_log_table (struct webgit_query* query, const char* object, int count, int
                               html(html_tag ("div",
                                              html_attr ("class", "test1")
                                              ),
-                                   webgit_object_link (query,
-                                                       query->repo,
-                                                       strlen (query->repo),
-                                                       sha1_to_hex (commit->object.sha1),
-                                                       40,
-                                                       NULL,
-                                                       NULL,
-                                                       webgit_object_commit_header_parse (commit)
-                                                       )
+                                   webgit_object_link (query, webgit_object_commit_header_parse (commit),
+                                                       "repo", query->repo,
+                                                       "ref", query->head,
+                                                       "commit", sha1_to_hex (commit->object.sha1))
                                    ),
                               // TODO please refactor this if it's really silly
                               (verbosity > 0) ?
@@ -164,15 +163,11 @@ webgit_log_table (struct webgit_query* query, const char* object, int count, int
                                           html(
                                                "Diff ",
                                                " ",
-                                               webgit_object_link (query,
-                                                                   query->repo,
-                                                                   strlen (query->repo),
-                                                                   webgit_object_commit_tree_parse (commit),
-                                                                   40,
-                                                                   NULL,
-                                                                   NULL,
-                                                                   html ("Tree")
-                                                                   )
+                                               webgit_object_link (query, html ("Tree"),
+                                                                   "repo", query->repo,
+                                                                   "ref", query->head,
+                                                                   "commit", webgit_object_commit_tree_parse (commit),
+                                                                   "path", "")
                                                )
                                           )
                                     )
index ac4eae2..28acab1 100644 (file)
 
 #include "object.h"
 
+#define SHA1_HEADER <openssl/sha.h>
+#include "git/cache.h"
+#include "git/refs.h"
+
+#include <stdarg.h>
+
+
 Html
-webgit_object_link (struct webgit_query* query,
-                    const char* repo,
-                    int repo_len,
-                    const char* object,
-                    int object_len,
-                    const char* path,
-                    const char* action,
-                    Html text)
+webgit_object_link_ (struct webgit_query* query, Html text, ...)
 {
+  Html params = html_list ();
+  va_list args;
+
+  const char* p;
+  const char* v;
+  char delim = '?';
+
+  va_start (args, text);
+
+  while ((p = va_arg (args, const char*)))
+    {
+      v = va_arg (args, const char*);
+
+      if (v)
+        {
+          html_list_append (params, html_fmt ("%c%s=%s", delim, p, v));
+          delim = '&';
+        }
+    }
+
+  va_end (args);
+
   return html (
                html_tag ("a",
-                         html_attr ("href", html (
-                                                  html_fmt ("%s?repo=%.*s&object=%.*s",
-                                                            query->request->script_name,
-                                                            repo_len, repo,
-                                                            object_len, object),
-                                                  path ? html_fmt ("&path=%s", path) : html (),
-                                                  action ? html_fmt ("&action=%s", action) : html ()
-                                                  )
+                         html_attr ("href",
+                                    html (
+                                          query->request->script_name, params)
                                     )
                          ),
                text
index 5f682c9..41a1d45 100644 (file)
 #include "git/commit.h"
 
 Html
-webgit_object_link (struct webgit_query* query,
-                   const char* repo,
-                   int repo_len,
-                   const char* object,
-                   int object_len,
-                   const char* path,
-                   const char* action,
-                   Html text);
+webgit_object_link_ (struct webgit_query* query, Html text, ...);
+#define webgit_object_link(...) webgit_object_link_ CWA_OPT (__VA_ARGS__)
 
 char*
 webgit_object_commit_tree_parse (struct commit* commit);
index c006827..5f826c1 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "object.h"
 #include "repo.h"
+#include "date.h"
 #include "actions.h"
 #include "summary.h"
 
@@ -67,13 +68,10 @@ webgit_object_commit_parents_parse (struct commit* commit, struct webgit_repo_in
       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))
+                            webgit_object_link (repo->query, html_fmt ("%.40s", sha1),
+                                                "repo", repo->query->repo,
+                                                "ref", repo->query->head,
+                                                "commit", sha1)
                             )
                       );
     }
@@ -238,14 +236,11 @@ webgit_object_commit_content_action (struct webgit_repo_info* repo, unsigned cha
                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)
-                                         )
+                     webgit_object_link (repo->query, html_strndup (tree, 40),
+                                         "repo", repo->query->repo,
+                                         "ref", repo->query->head,
+                                         "commit", sha1_to_hex (commit->object.sha1),
+                                         "path", "")
                      ), html_nl (),
                html (html_tag ("div"), webgit_object_commit_parents_parse (commit, repo)), html_nl (),
                html (html_tag ("div"),
index 514b76c..bb73a49 100644 (file)
@@ -196,21 +196,18 @@ webgit_object_tag_content_action (struct webgit_repo_info* repo, struct tag* tag
                    webgit_object_tag_header_parse (buf, size)
                    ),
              html_fmt ("Type '%s': ", type),
-             webgit_object_link (repo->query,
-                                 repo->name, strlen(repo->name),
-                                 sha1hex, 40,
-                                 NULL,
-                                 NULL,
-                                 html (sha1hex)),
+             webgit_object_link (repo->query, html (sha1hex),
+                                 "repo", repo->query->repo,
+                                 "ref", repo->query->head,
+                                 "object", sha1hex),
              !strcmp (type, "commit") ?
              html (
                    " ",
-                   webgit_object_link (repo->query,
-                                       repo->name, strlen(repo->name),
-                                       sha1hex, 40,
-                                       NULL,
-                                       "tree",
-                                       html ("Tree"))
+                   webgit_object_link (repo->query, html ("Tree"),
+                                       "repo", repo->query->repo,
+                                       "ref", repo->query->head,
+                                       "commit", sha1hex,
+                                       "path", "")
                    ) : html (),
              html (html_tag ("br")),
              "Tagger: ",
index 9a791e5..d84bb4d 100644 (file)
@@ -79,72 +79,71 @@ webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
 
   if (S_ISGITLINK(mode))
     {
-      html_list_append (tree_in_flight, html (
-                                              html (
-                                                    html_tag ("tr"),
-                                                    html (html_tag ("td"), pretty_mode (mode)),
-                                                    html (html_tag ("td"),
-                                                          webgit_repo_link (query_in_flight,
-                                                                            query_in_flight->repo,
-                                                                            strlen (query_in_flight->repo),
-                                                                            name, strlen (pathname),
-                                                                            sha1_to_hex (sha1), 40,
-                                                                            "tree",
-                                                                            html_strndup (pathname, SIZE_MAX))
-                                                          ),
-                                                    html (html_tag ("td"), "history summary")
-                                                    ),
-                                              html_nl ()
-                                              )
+      char* repo = cwa_malloc (strlen (query_in_flight->repo) + strlen (name) + 2);
+      sprintf (repo, "%s/%s", query_in_flight->repo, name);
+
+      html_list_append (tree_in_flight,
+                        html (
+                              html (
+                                    html_tag ("tr"),
+                                    html (html_tag ("td"), pretty_mode (mode)),
+                                    html (html_tag ("td"),
+                                          webgit_object_link (query_in_flight, html_strndup (pathname, SIZE_MAX),
+                                                              "repo", repo,
+                                                              "ref","",
+                                                              "commit", sha1_to_hex (sha1),
+                                                              "path", "")
+                                          ),
+                                    html (html_tag ("td"), "history summary")
+                                    ),
+                              html_nl ()
+                              )
                         );
     }
   else if (S_ISDIR(mode))
     {
-      html_list_append (tree_in_flight, html (
-                                              html (html_tag ("tr"),
-                                                    html (html_tag ("td"), pretty_mode (mode)),
-                                                    html (html_tag ("td"),
-                                                          webgit_object_link (query_in_flight,
-                                                                              query_in_flight->repo,
-                                                                              strlen (query_in_flight->repo),
-                                                                              sha1_to_hex (sha1), 40,
-                                                                              pathname,
-                                                                              NULL,
-                                                                              html_strndup (name, SIZE_MAX))
-                                                          ),
-                                                  html (html_tag ("td"), "history snap")
-                                                  ),
-                                             html_nl ()
-                                             )
+      html_list_append (tree_in_flight,
+                        html (
+                              html (html_tag ("tr"),
+                                    html (html_tag ("td"), pretty_mode (mode)),
+                                    html (html_tag ("td"),
+                                          webgit_object_link (query_in_flight, html_strndup (name, SIZE_MAX),
+                                                              "repo", query_in_flight->repo,
+                                                              "ref", query_in_flight->head,
+                                                              "commit", query_in_flight->commit,
+                                                              "path", pathname)
+                                          ),
+                                    html (html_tag ("td"), "history snap")
+                                    ),
+                              html_nl ()
+                              )
                         );
     }
   else
     {
-      html_list_append (tree_in_flight, html (
-                                              html (html_tag ("tr"),
-                                                    html (html_tag ("td"), pretty_mode (mode)),
-                                                    html (html_tag ("td"),
-                                                          webgit_object_link (query_in_flight,
-                                                                              query_in_flight->repo,
-                                                                              strlen (query_in_flight->repo),
-                                                                              sha1_to_hex (sha1), 40,
-                                                                              pathname,
-                                                                              NULL,
-                                                                              html_strndup (name, SIZE_MAX))
-                                                          ),
-                                                    html (html_tag ("td"),
-                                                          "history ",
-                                                          webgit_object_link (query_in_flight,
-                                                                              query_in_flight->repo,
-                                                                              strlen (query_in_flight->repo),
-                                                                              sha1_to_hex (sha1), 40,
-                                                                              pathname,
-                                                                              "raw",
-                                                                              html ("raw")),
-                                                          " edit")
-                                                    ),
-                                              html_nl()
-                                              )
+      html_list_append (tree_in_flight,
+                        html (
+                              html (html_tag ("tr"),
+                                    html (html_tag ("td"), pretty_mode (mode)),
+                                    html (html_tag ("td"),
+                                          webgit_object_link (query_in_flight, html_strndup (name, SIZE_MAX),
+                                                              "repo", query_in_flight->repo,
+                                                              "ref", query_in_flight->head,
+                                                              "commit", query_in_flight->commit,
+                                                              "path", pathname)
+                                          ),
+                                    html (html_tag ("td"),
+                                          "history ",
+                                          webgit_object_link (query_in_flight, html ("raw"),
+                                                              "repo", query_in_flight->repo,
+                                                              "ref", query_in_flight->head,
+                                                              "commit", query_in_flight->commit,
+                                                              "path", pathname,
+                                                              "action", "raw"),
+                                          " edit")
+                                    ),
+                              html_nl()
+                              )
                         );
     }
 
@@ -175,8 +174,14 @@ webgit_object_tree_action (struct webgit_repo_info* repo, unsigned char* sha1)
     die("not a tree object");
 
   return html(
-              html(html_tag("div", html_attr("id", "sub-menu")), webgit_object_tree_menu_action (repo, sha1, tree)), html_nl (),
-              html(html_tag("div", html_attr("id", "content")), webgit_object_tree_content_action (repo, sha1, tree)), html_nl ()
+              html (html_tag ("div",
+                              html_attr("id", "sub-menu")),
+                    webgit_object_tree_menu_action (repo, sha1, tree)),
+              html_nl (),
+              html (html_tag("div",
+                             html_attr("id", "content")),
+                    webgit_object_tree_content_action (repo, sha1, tree)),
+              html_nl ()
               );
 }
 
index e255e98..ed7467f 100644 (file)
 #include <stdint.h>
 #include <time.h>
 
-
-Html
-webgit_repo_link (struct webgit_query* query,
-                  const char* repo_prefix,
-                  int repo_prefix_len,
-                  const char* repo,
-                  int repo_len,
-                  const char* object,
-                  int object_len,
-                  const char* action,
-                  Html text)
-{
-  return html (
-               html_tag ("a",
-                         html_attr ("href", html (
-                                                  html_fmt ("%s?repo=%.*s%s%.*s",
-                                                            query->request->script_name,
-                                                            repo_prefix_len, repo_prefix,
-                                                            repo_prefix && repo ? "/" : "",
-                                                            repo_len, repo,
-                                                            object_len, object),
-                                                  action ?
-                                                  html_fmt ("&action=%s", action) : html (),
-                                                  object ?
-                                                  html_fmt ("&object=%.*s", object_len, object) : html ()
-                                                  )
-                                    )
-                         ),
-               text
-               );
-}
-
-
 Html
 webgit_repo_logo (struct webgit_repo_info* repo)
 {
index 8513cff..250c4fd 100644 (file)
 
 #define WEBGIT_CONF_PREFIX "web."
 
-
-Html
-webgit_repo_link (struct webgit_query* query,
-                  const char* repo_prefix,
-                  int repo_prefix_len,
-                  const char* repo,
-                  int repo_len,
-                  const char* object,
-                  int object_len,
-                  const char* action,
-                  Html text);
-
-
 Html
 webgit_repo_logo (struct webgit_repo_info* repo);
 
index 61188e7..c619b49 100644 (file)
--- a/src/tag.c
+++ b/src/tag.c
@@ -117,20 +117,17 @@ show_tag_ref (const char *refname, const unsigned char *sha1, int flags, void *d
                           html (
                                 html_tag ("td"),
                                 webgit_object_link (repo->query,
-                                                    repo->name, strlen(repo->name),
-                                                    sha1_to_hex (sha1), 40,
-                                                    NULL,
-                                                    NULL,
-                                                    html_strndup (refname, SIZE_MAX))
+                                                    html_strndup (refname, SIZE_MAX),
+                                                    "repo", repo->name,
+                                                    "ref", tagref,
+                                                    "object", sha1_to_hex (sha1))
                                 ),
                           html (
                                 html_tag ("td"),
-                                webgit_object_link (repo->query,
-                                                    repo->name, strlen(repo->name),
-                                                    sha1_to_hex (derefed->sha1), 40,
-                                                    NULL,
-                                                    NULL,
-                                                    webgit_pretty_age (repo->query->now - date))
+                                webgit_object_link (repo->query, webgit_pretty_age (repo->query->now - date),
+                                                    "repo", repo->name,
+                                                    "ref", tagref,
+                                                    "object", sha1_to_hex (derefed->sha1))
                                 ),
                           html (
                                 html_tag ("td"),
@@ -151,12 +148,11 @@ show_tag_ref (const char *refname, const unsigned char *sha1, int flags, void *d
                                                  NULL, 0,
                                                  repo->query->count,
                                                  html ("Log")),
-                                webgit_object_link (repo->query,
-                                                    repo->name, strlen(repo->name),
-                                                    sha1_to_hex (derefed->sha1), 40,
-                                                    NULL,
-                                                    "tree",
-                                                    html ("Tree")),
+                                webgit_object_link (repo->query, html ("Tree"),
+                                                    "repo", repo->name,
+                                                    "ref", tagref,
+                                                    "commit", sha1_to_hex (derefed->sha1),
+                                                    "path", ""),
                                 " Snapshot"
                                 )
                           )