let summary headers link to to the respective subpages
[webgit] / src / actions.c
index 69e22ae..4242ecb 100644 (file)
@@ -2,34 +2,39 @@
     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>
 
 /* todo configure this */
 #define SHA1_HEADER <openssl/sha.h>
 #include "git/cache.h"
+#include "git/refs.h"
 
 #include <stdio.h>
 
 static Html
 webgit_main_menu_action (struct webgit_query* query)
 {
-  return html ("TODO: main menu sidebar");
+  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
@@ -51,6 +71,7 @@ webgit_main_content_action (struct webgit_query* query)
   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"),
@@ -62,54 +83,86 @@ webgit_main_content_action (struct webgit_query* query)
                           )
               );
 
+  int row = 0;
+
   LLIST_FOREACH (&query->repos, node)
     {
       struct webgit_repo_info* n = (struct webgit_repo_info*)node;
-      html_list_append (table, html (
-                                     html_tag ("tr"),
-                                     html(html_tag ("td"), webgit_summary_link (query, n, html (n->name))),
-                                     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,
-                                                              webgit_pretty_age (n->age))
-                                          ),
-                                     html(html_tag ("td"),
-                                          webgit_email_link (n->last_author_name, n->last_author_email)
-                                          ),
-                                     html(html_tag ("td"),
-                                          webgit_email_link (n->last_committer_name, n->last_committer_email)
-                                          ),
-                                     html(html_tag ("td"),
-                                          html(
-                                               webgit_log_link (query, n, n->last_head, html("Log")),
-                                               " ",
-
-                                               "Diff ",
-                                               //webgit_diff_link (query, n, n->last_head, html("Diff")),
-                                               //" ",
-                                               webgit_object_link (query,
+
+      webgit_repoinfo_find_last (n);
+
+      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_tree, 40,
-                                                                  html("Tree"))
-                                               )
-                                          )
-                                     ),
-                        html_nl()
-                        );
+                                                                  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 ("border", "1")), table);
+  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"), webgit_main_menu_action (query)), html_nl (),
-              html(html_tag("div"), webgit_main_content_action (query)), html_nl ()
+              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 ()
               );
 }
 
@@ -117,10 +170,149 @@ webgit_main_action (struct webgit_query* query)
 /*
   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));
+
+  /* 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
 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 ()
+              );
 }
 
 
@@ -130,9 +322,12 @@ webgit_summary_action (struct webgit_query* query)
 static Html
 webgit_log_action (struct webgit_query* query)
 {
-  
+  webgit_repo_enter (query);
 
-  return html("log");
+  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 ()
+              );
 }
 
 
@@ -142,6 +337,15 @@ webgit_log_action (struct webgit_query* query)
 static Html
 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");
 }
 
@@ -152,34 +356,24 @@ webgit_diff_action (struct webgit_query* query)
 static Html
 webgit_object_action (struct webgit_query* query)
 {
-  LLIST_FOREACH (&query->repos, node)
-    {
-      struct webgit_repo_info* ri = (struct webgit_repo_info*) node;
-      if (!strcmp (ri->name, query->repo))
-        {
-          chdir (ri->path);
-          break;
-        }
-    }
-
-  setup_git_directory ();
+  struct webgit_repo_info* repo = webgit_repo_enter (query);
 
   unsigned char sha1[20];
-  if (get_sha1(query->object, sha1))
+  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 (query, sha1);
+        return webgit_object_commit_action (repo, sha1);
       /* else fallthrough */
     case OBJ_TREE:
-      return webgit_object_tree_action (query, sha1);
+      return webgit_object_tree_action (repo, sha1);
     case OBJ_BLOB:
-      return webgit_object_blob_action (query, sha1);
+      return webgit_object_blob_action (repo, sha1);
     case OBJ_TAG:
-      return html ("its a tag");
+      return webgit_object_tag_action (repo, sha1);
 
       break;
     default:
@@ -201,31 +395,161 @@ webgit_tree_action (struct webgit_query* query)
 
 
 /*
-  spew out raw data only
+  spew out raw data
 */
 static Html
 webgit_raw_action (struct webgit_query* query)
 {
-  return html("raw");
+  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
+webgit_branch_menu_action (struct webgit_repo_info* repo)
+{
+  (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
+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
+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");
 }
 
@@ -235,6 +559,7 @@ webgit_edit_action (struct webgit_query* query)
 static Html
 webgit_commit_action (struct webgit_query* query)
 {
+  (void) query;
   return html("commit");
 }
 
@@ -262,3 +587,11 @@ webgit_action_dispatch (struct webgit_query* query)
 }
 
 
+
+/*
+//      Local Variables:
+//      mode: C
+//      c-file-style: "gnu"
+//      indent-tabs-mode: nil
+//      End:
+*/