Simple mainpage generation, skeletons for further work
authorChristian Thaeter <ct@pipapo.org>
Mon, 24 Dec 2007 00:08:17 +0000 (01:08 +0100)
committerChristian Thaeter <ct@pipapo.org>
Mon, 24 Dec 2007 00:08:17 +0000 (01:08 +0100)
13 files changed:
Makefile.am
src/actions.c
src/age.c [new file with mode: 0644]
src/age.h [new file with mode: 0644]
src/log.c [new file with mode: 0644]
src/log.h [new file with mode: 0644]
src/object.c [new file with mode: 0644]
src/object.h [new file with mode: 0644]
src/query.c
src/repo.c [new file with mode: 0644]
src/repo.h [new file with mode: 0644]
src/summary.c [new file with mode: 0644]
src/summary.h [new file with mode: 0644]

index f829a8c..9887c8a 100644 (file)
@@ -30,15 +30,25 @@ ctgit_SOURCES =                                     \
        $(ctgit_srcdir)/ctgit.c                 \
        $(ctgit_srcdir)/options.c               \
        $(ctgit_srcdir)/query.c                 \
-       $(ctgit_srcdir)/actions.c
+       $(ctgit_srcdir)/actions.c               \
+       $(ctgit_srcdir)/age.c                   \
+       $(ctgit_srcdir)/summary.c               \
+       $(ctgit_srcdir)/repo.c                  \
+       $(ctgit_srcdir)/object.c                \
+       $(ctgit_srcdir)/log.c
 
 noinst_HEADERS =                               \
        $(ctgit_srcdir)/ctgit.h                 \
        $(ctgit_srcdir)/options.h               \
        $(ctgit_srcdir)/query.h                 \
-       $(ctgit_srcdir)/actions.h
-
-ctgit_LDADD = cwa/libcwa.a git/libgit.a
+       $(ctgit_srcdir)/actions.h               \
+       $(ctgit_srcdir)/age.h                   \
+       $(ctgit_srcdir)/summary.h               \
+       $(ctgit_srcdir)/repo.h                  \
+       $(ctgit_srcdir)/object.h                \
+       $(ctgit_srcdir)/log.h
+
+ctgit_LDADD = -lcwa git/libgit.a
 
 #### test suite
 ##check_PROGRAMS =
index 13c27aa..9708eef 100644 (file)
 */
 
 #include "actions.h"
+#include "summary.h"
+#include "object.h"
+#include "log.h"
+#include "repo.h"
 
 #include "cwa.h"
 
-Html
-ctgit_summary_link (struct ctgit_query* query, struct ctgit_repo_info* n)
+#include <stdio.h>
+
+
+static Html
+ctgit_main_menu_action (struct ctgit_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("TODO");
 }
 
-
 static Html
-ctgit_main_action (struct ctgit_query* query)
+ctgit_main_content_action (struct ctgit_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"),"Description"),
+                               html(html_tag ("th"),"Owner"),
+                               html(html_tag ("th"),"Last Change"),
+                               html(html_tag ("th"),"Action")
+                               ),
+                          html_nl()
+                          )
+              );
 
   LLIST_FOREACH (&query->repos, node)
     {
       struct ctgit_repo_info* n = (struct ctgit_repo_info*)node;
-
-      //fprintf(stderr,"repo %s\n", n->name);
-
-      html_append (ret, html( ctgit_summary_link (query, n), html_nl()));
+      html_list_append (table, html (
+                                     html_tag ("tr"),
+                                     html(html_tag ("td"), ctgit_summary_link (query, n, html (n->name))),
+                                     html(html_tag ("td"), n->description),
+                                     html(html_tag ("td"), n->owner),
+                                     html(html_tag ("td"),
+                                          ctgit_object_link (query, n, "NA", ctgit_repo_age (query, n))
+                                          ),
+                                     html(html_tag ("td"),
+                                          html(
+                                               ctgit_log_link (query, n, html("Log")),
+                                               " ",
+                                               ctgit_object_link (query, n, "NA", html("Tree"))
+                                               )
+                                          )
+                                     ),
+                        html_nl()
+                        );
     }
 
+  return html (html_tag ("table", html_attr ("border", "1")), table);
+}
 
-  return html (ret, html_nl ());
+static Html
+ctgit_main_action (struct ctgit_query* query)
+{
+  return html(
+              html(html_tag("div"), ctgit_main_menu_action (query)), html_nl (),
+              html(html_tag("div"), ctgit_main_content_action (query)), html_nl ()
+              );
 }
 
 static Html
@@ -108,7 +140,11 @@ ctgit_commit_action (struct ctgit_query* query)
 Html
 ctgit_action_dispatch (struct ctgit_query* query)
 {
-#define CTGIT_ACTION(name, _) {#name, ctgit_##name##_action},
+#define CTGIT_ACTION(name, _)                   \
+{                                               \
+ #name,                                         \
+ ctgit_##name##_action,                         \
+},
   struct action_table{
     char* name;
     Html (*cb)(struct ctgit_query* query);
diff --git a/src/age.c b/src/age.c
new file mode 100644 (file)
index 0000000..0f28987
--- /dev/null
+++ b/src/age.c
@@ -0,0 +1,53 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 "age.h"
+
+#include <time.h>
+
+Html
+ctgit_pretty_age (time_t now, time_t past)
+{
+  unsigned long age = difftime (now, past);
+
+  if (age < 2 * 60)
+    return html_fmt("%d secs ago", age / (1));
+
+  else if (age < 2 * 60 * 60)
+    return html_fmt("%d mins ago", age / (60));
+
+  else if (age < 2 * 60 * 60 * 24)
+    return html_fmt("%d hours ago", age / (60 * 60));
+
+  else if (age < 2 * 60 * 60 * 24 * 7)
+    return html_fmt("%d days ago", age / (60 * 60 * 24));
+
+  else if (age < 2 * 60 * 60 * 24 * 31)
+    return html_fmt("%d weeks ago", age / (60 * 60 * 24 * 7));
+
+  else if (age < 2 * 60 * 60 * 24 * 365)
+    return html_fmt("%d months ago", age / (60 * 60 * 24 * 31));
+
+  else
+    return html_fmt("%d years ago", age / (60 * 60 * 24 * 365));
+}
+
+
diff --git a/src/age.h b/src/age.h
new file mode 100644 (file)
index 0000000..74187f8
--- /dev/null
+++ b/src/age.h
@@ -0,0 +1,30 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 CTGIT_AGE_H
+#define CTGIT_AGE_H
+
+#include "ctgit.h"
+
+Html
+ctgit_pretty_age (time_t now, time_t past);
+
+
+#endif
diff --git a/src/log.c b/src/log.c
new file mode 100644 (file)
index 0000000..d27a986
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,39 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 "log.h"
+
+Html
+ctgit_log_link (struct ctgit_query* query, struct ctgit_repo_info* n, Html text)
+{
+  return html (
+               html_tag ("a",
+                         html_attr ("href", html_fmt ("%s?repo=%s&action=log",
+                                                      query->request->script_name,
+                                                      n->name)
+                                    )
+                         ),
+               text
+               );
+}
+
+
+
diff --git a/src/log.h b/src/log.h
new file mode 100644 (file)
index 0000000..c5f418b
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,29 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 CTGIT_LOG_H
+#define CTGIT_LOG_H
+
+#include "ctgit.h"
+
+Html
+ctgit_log_link (struct ctgit_query* query, struct ctgit_repo_info* n, Html text);
+
+#endif
diff --git a/src/object.c b/src/object.c
new file mode 100644 (file)
index 0000000..c3579aa
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 "object.h"
+
+Html
+ctgit_object_link (struct ctgit_query* query, struct ctgit_repo_info* n, const char* object, Html text)
+{
+  return html (
+               html_tag ("a",
+                         html_attr ("href", html_fmt ("%s?repo=%s&action=object&object=%s",
+                                                      query->request->script_name,
+                                                      n->name,
+                                                      object)
+                                    )
+                         ),
+               text
+               );
+}
+
+
+
diff --git a/src/object.h b/src/object.h
new file mode 100644 (file)
index 0000000..082b836
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 CTGIT_OBJECT_H
+#define CTGIT_OBJECT_H
+
+#include "ctgit.h"
+
+Html
+ctgit_object_link (struct ctgit_query* query, struct ctgit_repo_info* n, const char* object, Html text);
+
+#endif
index 08eb118..b5c889f 100644 (file)
 
 
 #include "query.h"
+#include "repo.h"
 
 #include "cwa.h"
 
+#include <time.h>
+#include <stdint.h>
 
 void
 ctgit_query_init (struct ctgit_query* q)
@@ -32,7 +35,7 @@ ctgit_query_init (struct ctgit_query* q)
   q->now = time (NULL);
 
   q->repo = NULL;
-  q->action = cwa_strdup ("main");
+  q->action = cwa_strndup ("main", SIZE_MAX);
   q->object = NULL;
   q->head = NULL;
   llist_init (&q->repos);
@@ -70,43 +73,20 @@ ctgit_query_destroy (struct ctgit_query* q)
 void
 ctgit_query_add_repo (struct ctgit_query* q, const char* path)
 {
-  if (!path)
-    return;
-
-  // TODO check path == git repo
-
-  struct ctgit_repo_info* self = cwa_malloc (sizeof(struct ctgit_repo_info));
-
-  llist_init (&self->node);
-  self->path = cwa_strdup (path);
-
-  char* n;
-  while (n = strrchr(path, '/'))
-    if (n[1])
-      {
-        ++n;
-        break;
-      }
-
-  if (!n)
-    n = path;
-
-  self->name = cwa_strdup (n);       // TODO
 
-  self->owner = NULL;
-  self->description = NULL;
-  self->pull_url = NULL;
+  struct ctgit_repo_info* ri = ctgit_repoinfo_new (path);
+  if (!ri)
+    return;
 
   LList p = &q->repos;
   LLIST_FOREACH (&q->repos, node)
     {
       struct ctgit_repo_info* n = (struct ctgit_repo_info*)node;
-      if (strcmp (n->name, self->name) > 0)
+      if (strcmp (n->name, ri->name) > 0)
         break;
       p = node;
     }
-  llist_insert_next (p, &self->node);
-
+  llist_insert_next (p, &ri->node);
 }
 
 
@@ -116,7 +96,7 @@ ctgit_repo_param (const Cgi self, const char* v, size_t v_sz, void* u_dat)
   struct ctgit_query* q = (struct ctgit_query*) u_dat;
 
   free (q->repo);
-  q->repo = strdup(v);
+  q->repo = cwa_strndup(v, SIZE_MAX);
 
   fprintf (stderr,"repo='%s'\n", v);
 }
@@ -127,7 +107,7 @@ ctgit_action_param (const Cgi self, const char* v, size_t v_sz, void* u_dat)
   struct ctgit_query* q = (struct ctgit_query*) u_dat;
 
   free (q->action);
-  q->action = strdup(v);
+  q->action = cwa_strndup(v, SIZE_MAX);
 
   fprintf (stderr,"action='%s'\n", v);
 }
@@ -138,7 +118,7 @@ ctgit_object_param (const Cgi self, const char* v, size_t v_sz, void* u_dat)
   struct ctgit_query* q = (struct ctgit_query*) u_dat;
 
   free (q->object);
-  q->object = strdup (v);
+  q->object = cwa_strndup (v, SIZE_MAX);
 
   fprintf (stderr,"object='%s'\n", v);
 }
@@ -149,7 +129,7 @@ ctgit_ref_param (const Cgi self, const char* v, size_t v_sz, void* u_dat)
   struct ctgit_query* q = (struct ctgit_query*) u_dat;
 
   free (q->head);
-  q->head = strdup(v);
+  q->head = cwa_strndup(v, SIZE_MAX);
 
   fprintf (stderr,"head='%s'\n", v);
 }
diff --git a/src/repo.c b/src/repo.c
new file mode 100644 (file)
index 0000000..7b11fd0
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 "repo.h"
+#include "age.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <stdint.h>
+
+Html
+ctgit_repo_age (struct ctgit_query* query, struct ctgit_repo_info* n)
+{
+  char buf[512];
+
+  snprintf (buf, 512, "%s/.git/refs/heads", n->path);
+
+  struct stat st;
+
+  if (!stat (buf, &st))
+    return ctgit_pretty_age (query->now, st.st_mtime);
+  else
+    return html("file not found");
+}
+
+
+struct ctgit_repo_info*
+ctgit_repoinfo_new (const char* path)
+{
+  if (!path)
+    return NULL;
+
+  // TODO check path == git repo
+
+  struct ctgit_repo_info* self = cwa_malloc (sizeof(struct ctgit_repo_info));
+
+  llist_init (&self->node);
+  self->path = cwa_strndup (path, SIZE_MAX);
+
+  const char* n;
+  while ((n = strrchr (path, '/')))
+    if (n[1])
+      {
+        ++n;
+        break;
+      }
+
+  if (!n)
+    n = path;
+
+  char buf[512];
+  struct stat st;
+  if (stat (path, &st))
+    return NULL; // error ILLEGAL dir
+
+  self->name = cwa_strndup (n, SIZE_MAX);       // TODO
+
+  struct passwd* pw;
+  if ((pw = getpwuid (st.st_uid)) != NULL)
+    self->owner = cwa_strndup (pw->pw_gecos, strchr (pw->pw_gecos, ',') - pw->pw_gecos);
+  else
+    self->owner = cwa_strndup ("unknown", 100);
+
+
+
+  self->description = NULL;
+
+  snprintf (buf, 512, "%s/.git/description", path);
+
+  FILE* desc = fopen (buf, "r");
+  if (desc)
+    {
+      fgets (buf, 512, desc);
+      fclose (desc);
+      char* e = strchr (buf, '\n');
+      if (e)
+        *e = '\0';
+
+      self->description = cwa_strndup (buf, 40);
+      size_t l = strlen (self->description);
+      if (l >= 39)
+        {
+          self->description[l-1] = '.';
+          self->description[l-2] = '.';
+          self->description[l-3] = '.';
+        }
+    }
+
+
+
+
+
+
+  self->pull_url = NULL;
+
+  return self;
+}
diff --git a/src/repo.h b/src/repo.h
new file mode 100644 (file)
index 0000000..96a8135
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 CTGIT_REPO_H
+#define CTGIT_REPO_H
+
+#include "ctgit.h"
+
+Html
+ctgit_repo_age (struct ctgit_query* query, struct ctgit_repo_info* n);
+
+struct ctgit_repo_info*
+ctgit_repoinfo_new (const char* path);
+
+#endif
diff --git a/src/summary.c b/src/summary.c
new file mode 100644 (file)
index 0000000..99a2cb8
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 "summary.h"
+
+Html
+ctgit_summary_link (struct ctgit_query* query, struct ctgit_repo_info* n, Html text)
+{
+  return html (
+               html_tag ("a",
+                         html_attr ("href", html_fmt ("%s?repo=%s&action=summary",
+                                                      query->request->script_name,
+                                                      n->name)
+                                    )
+                         ),
+               text
+               );
+}
+
+
+
diff --git a/src/summary.h b/src/summary.h
new file mode 100644 (file)
index 0000000..46e1cfe
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+    cehtehs git web frontend
+
+  Copyright (C)
+    2007,               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 CTGIT_SUMMARY_H
+#define CTGIT_SUMMARY_H
+
+#include "ctgit.h"
+
+Html
+ctgit_summary_link (struct ctgit_query* query, struct ctgit_repo_info* n, Html text);
+
+#endif