remove 'deref_to_tree', not needed anymore
[webgit] / src / actions.c
index 9801597..fa9d754 100644 (file)
@@ -2,31 +2,33 @@
     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 "date.h"
 #include "branch.h"
 #include "tag.h"
+#include "account.h"
+#include "edit.h"
 
 #include "llist.h"
 #include <cwa.h>
 #include <stdio.h>
 
 
+Html
+webgit_main_link (struct webgit_query* query, Html text)
+{
+  return html (
+               html_tag ("a",
+                         html_attr ("href", html_str (query->request->script_name))
+                         ),
+               text
+               );
+}
+
+
 /*
   Main (repository list) page
 */
 static Html
 webgit_main_menu_action (struct webgit_query* query)
 {
-  (void) query;
-  return html ("TODO: main menu");
-}
-
-static Html
-webgit_header_action (struct webgit_query* query)
-{
-  (void) query;
-  return html (
-                          html_tag("h1"),
-                          "webgit"
-                          );
+  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 />",
+               query->ssign
+               ? html (
+                       webgit_account_link (query, html ("Preferences")), "<br />",
+                       webgit_account_logout_link (query, html ("Logout")), "<br />"
+                       )
+               : html (
+                       webgit_account_link (query, html ("Account")), "<br />"
+                       ),
+               html (
+                     html_tag ("a",
+                               html_attr ("href", webgit_webskinpath (query, "inc/about.html"))
+                               ),
+                     "Powered by webgit"
+                     )
+               );
 }
 
 static Html
@@ -79,81 +104,84 @@ 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;
 
       webgit_repoinfo_find_last (n);
 
-      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->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()
-                        );
+      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, webgit_pretty_age (n->age),
+                                                                  "repo", n->name,
+                                                                  "ref", n->last_head,
+                                                                  "commit", n->last_commit)
+                                              ),
+                                         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, html("Tree"),
+                                                                       "repo", n->name,
+                                                                       "ref", n->last_head,
+                                                                       "commit", n->last_commit,
+                                                                       "path", ""
+                                                                       )
+                                                   )
+                                              )
+                                         ),
+                            html_nl()
+                            );
+        }
     }
 
-  return html (html_tag ("table", html_attr ("id", "summary")), table);
+  return html (html_tag ("table", html_attr ("id", "summary"), html_attr ("class", "sortable"), html_attr ("cellpadding", "0"), html_attr ("cellspacing", "0")), table);
 }
 
 static Html
 webgit_main_action (struct webgit_query* query)
 {
   return html(
-              html(html_tag("div", html_attr("id", "header")), webgit_header_action (query)) , html_nl (),
-              html(html_tag("div", html_attr("id", "main-menu")), 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 ()
               );
 }
 
@@ -164,8 +192,20 @@ webgit_main_action (struct webgit_query* query)
 static Html
 webgit_summary_menu_action (struct webgit_repo_info* repo)
 {
-  (void) repo;
-  return html ("TODO: summary");
+  return html (html_tag ("div"),
+               webgit_repo_logo (repo), "<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 />",
+               webgit_main_link (repo->query, html ("Main")), "<br />"
+               );
 }
 
 
@@ -201,12 +241,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,
@@ -222,15 +260,24 @@ webgit_summary_content_action (struct webgit_repo_info* repo)
   /* log */
   html_list_append (content,
                     html (
-                          html (html_tag ("h2"), "Log of branch '", repo->last_head, "'"),
-                          webgit_log_table (repo->query, repo->last_head, 5 /*TODO: config this*/, 0))
+                          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, repo->last_head, 5 /*TODO: config this*/, 0))
                     );
 
 
   /* branches */
   html_list_append (content,
                     html (
-                          html (html_tag ("h2"), "Branches"),
+                          html (html_tag ("h2"),
+                                webgit_branch_link (repo, NULL, repo->query->count,
+                                                    html ("Branches")
+                                                    )
+                                ),
                           webgit_branch_table (repo, NULL, 5 /*TODO: config this*/)
                           )
                     );
@@ -238,12 +285,15 @@ webgit_summary_content_action (struct webgit_repo_info* repo)
   /* tags */
   html_list_append (content,
                     html (
-                          html (html_tag ("h2"), "Tags"),
+                          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;
 }
 
@@ -255,8 +305,8 @@ webgit_summary_action (struct webgit_query* query)
   webgit_repoinfo_find_last (repo);
 
   return html(
-              html(html_tag("div"), webgit_summary_menu_action (repo)), html_nl (),
-              html(html_tag("div"), webgit_summary_content_action (repo)), html_nl ()
+              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 ()
               );
 }
 
@@ -267,11 +317,11 @@ webgit_summary_action (struct webgit_query* query)
 static Html
 webgit_log_action (struct webgit_query* query)
 {
-  webgit_repo_enter (query);
+  struct webgit_repo_info* repo = webgit_repo_enter (query);
 
   return html(
-              html(html_tag("div"), webgit_log_menu_action (query)), html_nl (),
-              html(html_tag("div"), webgit_log_content_action (query)), html_nl ()
+              html(html_tag("div", html_attr ("id", "sub-menu")), webgit_log_menu_action (repo)), html_nl (),
+              html(html_tag("div", html_attr ("id", "content")), webgit_log_content_action (repo)), html_nl ()
               );
 }
 
@@ -283,6 +333,14 @@ 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");
 }
 
@@ -294,6 +352,7 @@ static Html
 webgit_object_action (struct webgit_query* query)
 {
   struct webgit_repo_info* repo = webgit_repo_enter (query);
+  webgit_object_deduce (query);
 
   unsigned char sha1[20];
   if (get_sha1 (query->object, sha1))
@@ -302,9 +361,7 @@ webgit_object_action (struct webgit_query* query)
   switch (sha1_object_info(sha1, NULL))
     {
     case OBJ_COMMIT:
-      if (!query->deref_to_tree)
-        return webgit_object_commit_action (repo, sha1);
-      /* else fallthrough */
+      return webgit_object_commit_action (repo, sha1);
     case OBJ_TREE:
       return webgit_object_tree_action (repo, sha1);
     case OBJ_BLOB:
@@ -320,17 +377,6 @@ webgit_object_action (struct webgit_query* query)
 }
 
 
-/*
-  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
 */
@@ -338,6 +384,7 @@ static Html
 webgit_raw_action (struct webgit_query* query)
 {
   webgit_repo_enter (query);
+  webgit_object_deduce (query);
 
   unsigned char sha1[20];
   if (get_sha1 (query->object, sha1))
@@ -361,8 +408,14 @@ webgit_raw_action (struct webgit_query* query)
 static Html
 webgit_branch_menu_action (struct webgit_repo_info* repo)
 {
-  (void) repo;
-  return html ("TODO: branch sidebar");
+  return html (html_tag ("div"),
+               webgit_repo_logo (repo), "<br />",
+               // TODO: "switch-branch-dropdown <br />",
+               // TODO: "push/fetch/merge <br />",
+               // TODO: "branch administation <br />",
+               webgit_summary_link (repo->query, repo, html("Summary")), "<br />",
+               webgit_main_link (repo->query, html ("Main"))
+               );
 }
 
 
@@ -373,9 +426,9 @@ webgit_branch_action (struct webgit_query* query)
   webgit_repoinfo_find_last (repo);
 
   return html(
-                         html(html_tag("div", html_attr("id", "header")), webgit_header_action (query)) , html_nl (),
-              html(html_tag("div"), webgit_branch_menu_action (repo)), html_nl (),
-              html(html_tag("div"), webgit_branch_table (repo, query->head, query->count), html_nl ())
+              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 ())
               );
 }
 
@@ -383,11 +436,30 @@ webgit_branch_action (struct webgit_query* query)
 /*
   show tag
 */
+static Html
+webgit_tag_menu_action (struct webgit_repo_info* repo)
+{
+  (void) repo;
+  return html (html_tag ("div"),
+               webgit_repo_logo (repo), "<br />",
+               // TODO: "tag administation <br />",
+               webgit_summary_link (repo->query, repo, html("Summary")), "<br />",
+               webgit_main_link (repo->query, html ("Main"))
+               );
+}
+
+
 static Html
 webgit_tag_action (struct webgit_query* query)
 {
-  (void) query;
-  return html("tag");
+  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_tag_menu_action (repo)), html_nl (),
+              html(html_tag("div", html_attr ("id", "content")),
+                   webgit_tag_table (repo, query->head, query->count), html_nl ())
+              );
 }
 
 
@@ -398,12 +470,20 @@ static Html
 webgit_config_menu_action (struct webgit_repo_info* repo)
 {
   (void) repo;
-  return html ("TODO: config menu");
+  return html (html_tag ("div"),
+               webgit_repo_logo (repo), "<br />",
+               // TODO: "manage remotes? <br />",
+               // TODO: "manage subprojects? <br />",
+               webgit_summary_link (repo->query, repo, html("Summary")), "<br />",
+               webgit_main_link (repo->query, html ("Main"))
+               );
 }
 
 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"),
@@ -422,16 +502,18 @@ webgit_config_content_action (struct webgit_repo_info* repo)
                      html_tag ("textarea",
                                html_attr ("name", "blob"),
                                html_attr ("rows", "40"),
-                               html_attr ("cols", "80")
-                               )//,
-                     //html_include_escaped (git_path("config"))
+                               html_attr ("cols", "80"),
+                               readonly ? "readonly" : ""
+                               ),
+                     html_include_escaped (git_path("config"))
                      ),
-               html (
-                     html_tag ("input",
-                               html_attr ("type", "submit"),
-                               html_attr ("value", "Save")
-                               )
-                     )
+               readonly ? html ()
+               : html (
+                       html_tag ("input",
+                                 html_attr ("type", "submit"),
+                                 html_attr ("value", "Save")
+                                 )
+                       )
                );
 }
 
@@ -444,7 +526,7 @@ webgit_config_action (struct webgit_query* query)
   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"));
@@ -457,8 +539,21 @@ webgit_config_action (struct webgit_query* query)
     }
 
   return html(
-              html (html_tag ("div"), webgit_config_menu_action (repo)), html_nl (),
-              html (html_tag ("div"), webgit_config_content_action (repo)), html_nl ()
+              html (html_tag ("div", html_attr("id", "sub-menu")), webgit_config_menu_action (repo)), html_nl (),
+              html (html_tag ("div", html_attr("id", "content")), webgit_config_content_action (repo)), html_nl ()
+              );
+}
+
+
+/*
+  account management
+*/
+static Html
+webgit_account_action (struct webgit_query* query)
+{
+  return html(
+              html(html_tag("div", html_attr ("id", "sub-menu")), webgit_account_menu_action (query)), html_nl (),
+              html(html_tag("div", html_attr ("id", "content")), webgit_account_content_action (query)), html_nl ()
               );
 }
 
@@ -469,10 +564,29 @@ webgit_config_action (struct webgit_query* query)
 static Html
 webgit_edit_action (struct webgit_query* query)
 {
-  (void) query;
-  return html("edit");
+  struct webgit_repo_info* repo = webgit_repo_enter (query);
+  webgit_object_deduce (query);
+
+  unsigned char sha1[20];
+  if (get_sha1 (query->object, sha1))
+    return html("error: unknown object");
+
+  switch (sha1_object_info(sha1, NULL))
+    {
+    case OBJ_BLOB:
+      return webgit_edit_blob_action (repo, sha1);
+      //case OBJ_COMMIT:
+      //case OBJ_TREE:
+      //case OBJ_TAG:
+
+      break;
+    default:
+      return html ("error: unknown object type");
+      break;
+    }
 }
 
+
 /*
   commit changes
 */
@@ -507,3 +621,11 @@ webgit_action_dispatch (struct webgit_query* query)
 }
 
 
+
+/*
+//      Local Variables:
+//      mode: C
+//      c-file-style: "gnu"
+//      indent-tabs-mode: nil
+//      End:
+*/