let summary headers link to to the respective subpages
[webgit] / src / actions.c
index 13c27aa..4242ecb 100644 (file)
     cehtehs git web frontend
 
   Copyright (C)
-    2007,               Christian Thaeter <ct@pipapo.org>
+    2007, 2008,         Christian Thaeter <ct@pipapo.org>
 
-  This program is free software; you can redistribute it and/or
-  modify it under the terms of the GNU General Public License as
-  published by the Free Software Foundation; either version 2 of the
-  License, or (at your option) any later version.
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU Affero General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
+  GNU Affero General Public License for more details.
 
-  You should have received a copy of the GNU General Public License
-  along with this program; if not, write to the Free Software
-  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+  You should have received a copy of the GNU Affero General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "webgit.h"
 #include "actions.h"
+#include "summary.h"
+#include "object.h"
+#include "log.h"
+#include "repo.h"
+#include "age.h"
+#include "branch.h"
+#include "tag.h"
 
-#include "cwa.h"
+#include "llist.h"
+#include <cwa.h>
 
-Html
-ctgit_summary_link (struct ctgit_query* query, struct ctgit_repo_info* n)
+/* todo configure this */
+#define SHA1_HEADER <openssl/sha.h>
+#include "git/cache.h"
+#include "git/refs.h"
+
+#include <stdio.h>
+
+
+/*
+  Main (repository list) page
+*/
+static Html
+webgit_main_menu_action (struct webgit_query* query)
 {
-  return html (
-               html_tag ("a",
-                         html_attr ("href", html_fmt ("%s?repo=%s&action=summary",
-                                                      query->request->script_name,
-                                                      n->name)
-                                    )
-                         ),
-               n->name
+  return html (html_tag ("div"),
+               html (
+                     html_tag ("img",
+                               html_attr ("src", webgit_webskinpath (query, "icons/webgit_logo.png")),
+                               html_attr ("alt", "Webgit-Logo")
+                               )
+                     ), "<br />",
+               html_include (webgit_skinpath (query, "inc/site.inc")), "<br />",
+               // TODO: webgit_user_config_link (query), "<br />",
+               html (
+                     html_tag ("a",
+                               html_attr ("href", webgit_webskinpath (query, "inc/about.html"))
+                               ),
+                     "Powered by webgit"
+                     )
                );
 }
 
-
 static Html
-ctgit_main_action (struct ctgit_query* query)
+webgit_main_content_action (struct webgit_query* query)
 {
-  Html ret = html();
+  Html table = html_list ();
+
+  html_list_append (table, html(
+                          html(html_tag ("tr"),
+                               html(html_tag ("th"),"Name"),
+                               html(html_tag ("th"),"Git URL"),
+                               html(html_tag ("th"),"Description"),
+                               html(html_tag ("th"),"Owner"),
+                               html(html_tag ("th"),"Last Change"),
+                               html(html_tag ("th"),"Author"),
+                               html(html_tag ("th"),"Committer"),
+                               html(html_tag ("th"),"Action")
+                               ),
+                          html_nl()
+                          )
+              );
+
+  int row = 0;
 
   LLIST_FOREACH (&query->repos, node)
     {
-      struct ctgit_repo_info* n = (struct ctgit_repo_info*)node;
+      struct webgit_repo_info* n = (struct webgit_repo_info*)node;
 
-      //fprintf(stderr,"repo %s\n", n->name);
+      webgit_repoinfo_find_last (n);
 
-      html_append (ret, html( ctgit_summary_link (query, n), html_nl()));
+      if (n->age < n->maxage)
+        {
+          html_list_append (table, html (
+                                         html_tag ("tr",
+                                                   html_attr ("class", (++row & 1) ? "odd" : "even")
+                                                   ),
+                                         html(html_tag ("td"),
+                                              webgit_summary_link (query, n, html (n->name))),
+                                         html(html_tag ("td"), n->url ?
+                                              html (
+                                                    html_tag ("a", html_attr ("href", n->url)),
+                                                    n->url)
+                                              : html("N/A")
+                                              ),
+                                         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))
+                                              ),
+                                         html(html_tag ("td"),
+                                              webgit_email_link (
+                                                                 html (n->last_author_name),
+                                                                 html (n->last_author_email)
+                                                                 )
+                                              ),
+                                         html(html_tag ("td"),
+                                              webgit_email_link (
+                                                                 html (n->last_committer_name),
+                                                                 html (n->last_committer_email)
+                                                                 )
+                                              ),
+                                         html(html_tag ("td"),
+                                              html(
+                                                   webgit_log_link (query,
+                                                                    n->name,
+                                                                    n->last_head,
+                                                                    NULL, 0,
+                                                                    query->count,
+                                                                    html("Log")
+                                                                    ),
+                                                   " ",
+                                                   "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"))
+                                                   )
+                                              )
+                                         ),
+                            html_nl()
+                            );
+        }
     }
 
+  return html (html_tag ("table", html_attr ("id", "summary")), table);
+}
+
+static Html
+webgit_main_action (struct webgit_query* query)
+{
+  return html(
+              html(html_tag("div", html_attr("id", "sub-menu")), webgit_main_menu_action (query)), html_nl (),
+              html(html_tag("div", html_attr("id", "content")), webgit_main_content_action (query)), html_nl ()
+              );
+}
+
+
+/*
+  Summary of a single repository
+*/
+static Html
+webgit_summary_menu_action (struct webgit_repo_info* repo)
+{
+  return html (html_tag ("div"),
+               repo->logo ?
+               html (
+                     html_tag ("a",
+                               html_attr ("href", repo->logolink ? repo->logolink : "/")),
+                     html (
+                           html_tag ("img",
+                                     html_attr ("src", repo->logo),
+                                     html_attr ("alt", html (repo->name,"-logo"))
+                                     )
+                           )
+                     )
+               : html (
+                       html_tag ("img",
+                                 html_attr ("src", webgit_webskinpath (repo->query, "icons/webgit_logo.png")),
+                                 html_attr ("alt", "Webgit-Logo")
+                                 )
+                       ), "<br />",
+               repo->readme ? html_include (repo->readme) : html (), "<br />",
+               html (
+                     html_tag ("a",
+                               html_attr ("href", html_fmt ("%s?repo=%s&action=config",
+                                                            repo->query->request->script_name,
+                                                            repo->name)
+                                          )
+                               ),
+                     "Configure"
+                     ),"<br />",
+               html (
+                     html_tag ("a",
+                               html_attr ("href", repo->query->request->script_name)
+                               ),
+                     "Main Page"
+                     ),"<br />"
+               );
+}
+
+
+static Html
+webgit_summary_content_action (struct webgit_repo_info* repo)
+{
+  Html content = html_list ();
+
+  /* Title */
+  html_list_append (content, html (html_tag ("h1"), repo->description));
 
-  return html (ret, html_nl ());
+  /* Readme */
+  if (repo->readme)
+    html_list_append (content, html_include (repo->readme));
+
+  /* Info */
+  html_list_append (content,
+                    html (
+                          html (html_tag ("h2"), "Repository information"),
+                          repo->url ?
+                          html (
+                                html_tag ("div"),
+                                "Public URL: ",
+                                html (
+                                      html_tag ("a", html_attr ("href", repo->url)),
+                                      repo->url
+                                      )
+                                ) : html(),
+                          html (
+                                html_tag ("div"),
+                                "Owner: ", repo->owner
+                                ),
+                          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)),
+                               " in branch ",
+                                webgit_log_link (repo->query,
+                                                 repo->name,
+                                                 repo->last_head,
+                                                 NULL, 0,
+                                                 repo->query->count,
+                                                 html ("'", repo->last_head, "'")
+                                                 )
+                               )
+                          )
+                    );
+
+  /* log */
+  html_list_append (content,
+                    html (
+                          html (html_tag ("h2"),
+                                webgit_log_link (repo->query, repo->name, repo->last_head,
+                                                 NULL, 0, repo->query->count,
+                                                 html ("Log of branch '", repo->last_head, "'")
+                                                 )
+                                ),
+                          webgit_log_table (repo->query, repo->last_head, 5 /*TODO: config this*/, 0))
+                    );
+
+
+  /* branches */
+  html_list_append (content,
+                    html (
+                          html (html_tag ("h2"),
+                                webgit_branch_link (repo, NULL, repo->query->count,
+                                                    html ("Branches")
+                                                    )
+                                ),
+                          webgit_branch_table (repo, NULL, 5 /*TODO: config this*/)
+                          )
+                    );
+
+  /* tags */
+  html_list_append (content,
+                    html (
+                          html (html_tag ("h2"),
+                                webgit_tag_link (repo, NULL, repo->query->count,
+                                                 html ("Tags")
+                                                 )
+                                ),
+                          webgit_tag_table (repo, NULL, 5 /*TODO: config this*/)
+                          )
+                    );
+
+
+  return content;
 }
 
+
 static Html
-ctgit_summary_action (struct ctgit_query* query)
+webgit_summary_action (struct webgit_query* query)
 {
-  return html("summary");
+  struct webgit_repo_info* repo = webgit_repo_enter (query);
+  webgit_repoinfo_find_last (repo);
+
+  return html(
+              html(html_tag("div", html_attr("id", "sub-menu")), webgit_summary_menu_action (repo)), html_nl (),
+              html(html_tag("div", html_attr("id", "content")), webgit_summary_content_action (repo)), html_nl ()
+              );
 }
 
+
+/*
+  Log page
+*/
 static Html
-ctgit_log_action (struct ctgit_query* query)
+webgit_log_action (struct webgit_query* query)
 {
-  return html("log");
+  webgit_repo_enter (query);
+
+  return html(
+              html(html_tag("div"), webgit_log_menu_action (query)), html_nl (),
+              html(html_tag("div", html_attr("id", "content")), webgit_log_content_action (query)), html_nl ()
+              );
 }
 
+
+/*
+  Diff page
+*/
 static Html
-ctgit_diff_action (struct ctgit_query* query)
+webgit_diff_action (struct webgit_query* query)
 {
+  (void) query;
+#if 0
+  return html (html_tag ("div"),
+               "Webgit logo<br />",
+               "Readme.html <br />",
+               "User (cookie) config <br />",
+               "About webgit (info/admin) <br />"
+               );
+#endif
   return html("diff");
 }
 
+
+/*
+  pretty printed objects
+*/
+static Html
+webgit_object_action (struct webgit_query* query)
+{
+  struct webgit_repo_info* repo = webgit_repo_enter (query);
+
+  unsigned char sha1[20];
+  if (get_sha1 (query->object, sha1))
+    return html("error: unknown object");
+
+  switch (sha1_object_info(sha1, NULL))
+    {
+    case OBJ_COMMIT:
+      if (!query->deref_to_tree)
+        return webgit_object_commit_action (repo, sha1);
+      /* else fallthrough */
+    case OBJ_TREE:
+      return webgit_object_tree_action (repo, sha1);
+    case OBJ_BLOB:
+      return webgit_object_blob_action (repo, sha1);
+    case OBJ_TAG:
+      return webgit_object_tag_action (repo, sha1);
+
+      break;
+    default:
+      return html ("error: unknown object type");
+      break;
+    }
+}
+
+
+/*
+  treeish dereferenced to tree
+*/
+static Html
+webgit_tree_action (struct webgit_query* query)
+{
+  query->deref_to_tree = 1;
+  return webgit_object_action (query);
+}
+
+
+/*
+  spew out raw data
+*/
 static Html
-ctgit_object_action (struct ctgit_query* query)
+webgit_raw_action (struct webgit_query* query)
 {
-  return html("object");
+  webgit_repo_enter (query);
+
+  unsigned char sha1[20];
+  if (get_sha1 (query->object, sha1))
+    return html("error: unknown object");
+
+  void* buf;
+  unsigned long size;
+
+  buf = read_object_with_reference (sha1, "blob", &size, NULL);
+
+  free ((char*)query->content_type);
+  query->content_type = webgit_mimetype (query->path);
+
+  return html_binary (buf, size);
 }
 
+
+/*
+  show branch
+*/
 static Html
-ctgit_raw_action (struct ctgit_query* query)
+webgit_branch_menu_action (struct webgit_repo_info* repo)
 {
-  return html("raw");
+  (void) repo;
+  return html (html_tag ("div"),
+               "Project or webgit logo<br />",
+               "switch-branch-dropdown <br />",
+               "push/fetch/merge <br />",
+               "branch administation <br />",
+               "link to project summary <br />",
+               "link to main page <br />"
+               );
 }
 
+
 static Html
-ctgit_tag_action (struct ctgit_query* query)
+webgit_branch_action (struct webgit_query* query)
 {
+  struct webgit_repo_info* repo = webgit_repo_enter (query);
+  webgit_repoinfo_find_last (repo);
+
+  return html(
+              html(html_tag("div", html_attr("id", "sub-menu")), webgit_branch_menu_action (repo)), html_nl (),
+              html(html_tag("div", html_attr("id", "content")), webgit_branch_table (repo, query->head, query->count), html_nl ())
+              );
+}
+
+
+
+/*
+  show tag
+*/
+static Html
+webgit_tag_action (struct webgit_query* query)
+{
+  (void) query;
   return html("tag");
 }
 
+
+/*
+  show and edit config
+*/
+static Html
+webgit_config_menu_action (struct webgit_repo_info* repo)
+{
+  (void) repo;
+  return html (html_tag ("div"),
+               "Project or webgit logo<br />",
+               "manage remotes? <br />",
+               "manage subprojects? <br />",
+               "link to project summary <br />",
+               "link to main page <br />"
+               );
+}
+
+static Html
+webgit_config_content_action (struct webgit_repo_info* repo)
+{
+  int readonly = !!access (git_path("config"), W_OK);
+
+  return html (
+               html_tag ("form",
+                         html_attr ("name", "config-change"),
+                         html_attr ("enctype", "multipart/form-data"),
+                         html_attr ("method", "post"),
+                         html_attr ("action",
+                                    html_fmt ("%s",
+                                              repo->query->request->script_name
+                                              )
+                                    )
+                         ),
+
+               html_hidden ("repo", repo->name),
+               html_hidden ("action", "config"),
+               html (
+                     html_tag ("textarea",
+                               html_attr ("name", "blob"),
+                               html_attr ("rows", "40"),
+                               html_attr ("cols", "80"),
+                               readonly ? "readonly" : ""
+                               ),
+                     html_include_escaped (git_path("config"))
+                     ),
+               readonly ? html ()
+               : html (
+                       html_tag ("input",
+                                 html_attr ("type", "submit"),
+                                 html_attr ("value", "Save")
+                                 )
+                       )
+               );
+}
+
 static Html
-ctgit_edit_action (struct ctgit_query* query)
+webgit_config_action (struct webgit_query* query)
 {
+  struct webgit_repo_info* repo = webgit_repo_enter (query);
+  webgit_repoinfo_find_last (repo);
+
+  if (query->blob)
+    {
+      FILE* cfg = fopen (git_path ("config"), "w");
+
+      /*TODO rxpd check */
+      if (!cfg)
+        webgit_warn ("could not open %s for writing", git_path("config"));
+      else
+        {
+          if (fwrite (query->blob, query->blob_size, 1, cfg) < 1)
+            webgit_warn ("could not write config");
+          fclose (cfg);
+        }
+    }
+
+  return html(
+              html (html_tag ("div"), webgit_config_menu_action (repo)), html_nl (),
+              html (html_tag ("div", html_attr("id", "content")), webgit_config_content_action (repo)), html_nl ()
+              );
+}
+
+
+/*
+  edit an object
+*/
+static Html
+webgit_edit_action (struct webgit_query* query)
+{
+  (void) query;
   return html("edit");
 }
 
+/*
+  commit changes
+*/
 static Html
-ctgit_commit_action (struct ctgit_query* query)
+webgit_commit_action (struct webgit_query* query)
 {
+  (void) query;
   return html("commit");
 }
 
 
 Html
-ctgit_action_dispatch (struct ctgit_query* query)
+webgit_action_dispatch (struct webgit_query* query)
 {
-#define CTGIT_ACTION(name, _) {#name, ctgit_##name##_action},
+#define WEBGIT_ACTION(name, _)                  \
+{                                               \
+ #name,                                         \
+ webgit_##name##_action,                        \
+},
   struct action_table{
     char* name;
-    Html (*cb)(struct ctgit_query* query);
-  } cmds[] = {CTGIT_ACTIONS {"", NULL}};
-#undef CTGIT_ACTION
+    Html (*cb)(struct webgit_query* query);
+  } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
+#undef WEBGIT_ACTION
 
   for (struct action_table* j = cmds; j->cb; ++j)
     {
@@ -124,3 +587,11 @@ ctgit_action_dispatch (struct ctgit_query* query)
 }
 
 
+
+/*
+//      Local Variables:
+//      mode: C
+//      c-file-style: "gnu"
+//      indent-tabs-mode: nil
+//      End:
+*/