Functions for displayin and handling branches
authorChristian Thaeter <ct@pipapo.org>
Wed, 16 Jan 2008 13:09:03 +0000 (14:09 +0100)
committerChristian Thaeter <ct@pipapo.org>
Wed, 16 Jan 2008 13:09:03 +0000 (14:09 +0100)
Makefile.am
src/actions.c
src/actions.h
src/branch.c [new file with mode: 0644]
src/branch.h [new file with mode: 0644]

index 9ea683c..ef5b90c 100644 (file)
@@ -46,6 +46,7 @@ webgit_SOURCES =                              \
        $(webgit_srcdir)/summary.c              \
        $(webgit_srcdir)/repo.c                 \
        $(webgit_srcdir)/object.c               \
+       $(webgit_srcdir)/branch.c               \
        $(webgit_srcdir)/log.c
 
 noinst_HEADERS =                               \
@@ -57,6 +58,7 @@ noinst_HEADERS =                              \
        $(webgit_srcdir)/summary.h              \
        $(webgit_srcdir)/repo.h                 \
        $(webgit_srcdir)/object.h               \
+       $(webgit_srcdir)/branch.h               \
        $(webgit_srcdir)/log.h
 
 webgit_DEPENDENCIES = $(abs_top_builddir)/git/libgit.a $(abs_top_builddir)/git/xdiff/lib.a
index 09c5f08..6c17d7c 100644 (file)
@@ -25,6 +25,7 @@
 #include "log.h"
 #include "repo.h"
 #include "age.h"
+#include "branch.h"
 
 #include "llist.h"
 #include <cwa.h>
@@ -32,6 +33,7 @@
 /* todo configure this */
 #define SHA1_HEADER <openssl/sha.h>
 #include "git/cache.h"
+#include "git/refs.h"
 
 #include <stdio.h>
 
@@ -246,6 +248,30 @@ webgit_raw_action (struct webgit_query* query)
 }
 
 
+/*
+  show branch
+*/
+static Html
+webgit_branch_menu_action (struct webgit_repo_info* repo)
+{
+  (void) repo;
+  return html ("TODO: branch sidebar");
+}
+
+
+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"), webgit_branch_menu_action (repo)), html_nl (),
+              html(html_tag("div"), webgit_branch_table (repo, query->head, query->count), html_nl ())
+              );
+}
+
+
 /*
   show tag
 */
index 52e9c6a..70fbfa8 100644 (file)
@@ -23,7 +23,7 @@
 
 #include "webgit.h"
 
-#include "cwa.h"
+#include <cwa.h>
 
 /*
   Pages presented to the user and other main actions
@@ -36,6 +36,7 @@
  WEBGIT_ACTION(object, "Show an object, pretty format")                 \
  WEBGIT_ACTION(tree, "Show object derefed to tree, pretty format")      \
  WEBGIT_ACTION(raw, "Show an object, raw format")                       \
+ WEBGIT_ACTION(branch, "Show and manage branches")                      \
  WEBGIT_ACTION(tag, "Show and manage tags")                             \
  WEBGIT_ACTION(edit, "Edit and object")                                 \
  WEBGIT_ACTION(commit, "Commit pending edits")
diff --git a/src/branch.c b/src/branch.c
new file mode 100644 (file)
index 0000000..c6c3dd4
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    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 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.
+
+  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.
+*/
+
+#include "branch.h"
+#include "object.h"
+#include "age.h"
+#include "log.h"
+
+#include <cwa.h>
+
+#define SHA1_HEADER <openssl/sha.h>
+#include "git/cache.h"
+#include "git/commit.h"
+#include "git/refs.h"
+
+struct branch_context
+{
+  struct webgit_repo_info* repo;
+  Html table;
+  Html cont;
+  const char* start;
+  int count;
+};
+
+Html
+webgit_branch_link (struct webgit_repo_info* repo, const char* branch, int count, Html text)
+{
+  return html (
+               html_tag ("a",
+                         html_attr ("href", html(
+                                                 html_fmt ("%s?repo=%s&action=branch",
+                                                           repo->query->request->script_name,
+                                                           repo->name),
+                                                 branch ? html_fmt("&ref=%s", branch) : html (),
+                                                 count > 0 ? html_fmt("&count=%d",
+                                                                      count) : html ()
+                                                 )
+                                    )
+                         ),
+               text
+               );
+
+}
+
+static int
+show_branch_ref (const char *refname, const unsigned char *sha1, int flags, void *data)
+{
+  (void) flags;
+
+  struct branch_context* ctx = (struct branch_context*) data;
+  struct webgit_repo_info* repo = ctx->repo;
+
+  if (ctx->start)
+    {
+      if (!strcmp (ctx->start, refname))
+        ctx->start = NULL;
+      else
+        return 0;
+    }
+
+  if (!ctx->count--)
+    {
+      /* displayed query->count lines, finish */
+      html_list_append (ctx->cont,
+                        webgit_branch_link (
+                                            repo, refname,
+                                            repo->query->count,
+                                            html ("More..."))
+                        );
+      return 1;
+    }
+
+  struct commit* commit = lookup_commit_reference (sha1);
+
+  unsigned long date =
+    webgit_commit_committer_date_parse (commit, NULL);
+
+  html_list_append (ctx->table,
+                    html (
+                          html_tag ("tr"),
+                          html (
+                                html_tag ("td"),
+                                webgit_log_link (repo->query,
+                                                 repo->name,
+                                                 refname,
+                                                 NULL, 0,
+                                                 repo->query->count,
+                                                 html ((char*)refname)
+                                                 )
+                                ),
+                          html (
+                                html_tag ("td"),
+                                webgit_object_link (repo->query,
+                                                    repo->name, strlen(repo->name),
+                                                    sha1_to_hex (sha1), 40,
+                                                    NULL,
+                                                    webgit_pretty_age (repo->query->now - date))
+                                ),
+                          html (
+                                html_tag ("td"),
+                                webgit_email_link (
+                                                   webgit_commit_committer_name_parse (commit),
+                                                   webgit_commit_committer_email_parse (commit)
+                                                   )
+                                ),
+                          html (
+                                html_tag ("td"),
+                                webgit_object_link (repo->query,
+                                                    repo->name, strlen(repo->name),
+                                                    webgit_commit_tree_parse (commit), 40,
+                                                    NULL,
+                                                    html ("Tree")
+                                                    ),
+                                html (" Snapshot")
+                                )
+                          )
+                    );
+
+  free(commit->buffer);
+  commit->buffer = NULL;
+  free_commit_list(commit->parents);
+  commit->parents = NULL;
+
+  return 0;
+}
+
+
+Html
+webgit_branch_table (struct webgit_repo_info* repo, const char* head, int count)
+{
+  struct branch_context ctx;
+  ctx.repo = repo;
+  ctx.table = html_list ();
+  ctx.cont = html_list ();
+  ctx.count = count;
+  ctx.start = head;
+
+  for_each_branch_ref (show_branch_ref, &ctx);
+
+  return html (html ( html_tag ("table"), ctx.table), ctx.cont);
+}
diff --git a/src/branch.h b/src/branch.h
new file mode 100644 (file)
index 0000000..ef7961c
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    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 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.
+
+  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.
+*/
+#ifndef WEBGIT_BRANCH_H
+#define WEBGIT_BRANCH_H
+
+#include "webgit.h"
+
+Html
+webgit_branch_table (struct webgit_repo_info* repo, const char* head, int count);
+
+Html
+webgit_branch_link (struct webgit_repo_info* repo, const char* branch, int count, Html text);
+
+#endif