completing the links for the repo listing page
authorChristian Thaeter <ct@pipapo.org>
Sun, 30 Dec 2007 20:57:55 +0000 (21:57 +0100)
committerChristian Thaeter <ct@pipapo.org>
Sun, 30 Dec 2007 20:57:55 +0000 (21:57 +0100)
src/actions.c
src/ctgit.h
src/log.c
src/log.h
src/repo.c

index c4a1353..7be07dc 100644 (file)
@@ -62,13 +62,13 @@ ctgit_main_content_action (struct ctgit_query* query)
                                      html(html_tag ("td"), n->description),
                                      html(html_tag ("td"), n->owner),
                                      html(html_tag ("td"),
-                                          ctgit_object_link (query, n, "TODO: get last commit", ctgit_pretty_age (n->age))
+                                          ctgit_object_link (query, n, n->last_commit, ctgit_pretty_age (n->age))
                                           ),
                                      html(html_tag ("td"),
                                           html(
-                                               ctgit_log_link (query, n, html("Log")),
+                                               ctgit_log_link (query, n, n->last_head, html("Log")),
                                                " ",
-                                               ctgit_object_link (query, n, "TODO: get last tree", html("Tree"))
+                                               ctgit_object_link (query, n, n->last_tree, html("Tree"))
                                                )
                                           )
                                      ),
index ce11871..3849f2a 100644 (file)
@@ -39,6 +39,10 @@ struct ctgit_repo_info
   char* giturl;
   char* readme;
 
+  char* last_commit;
+  char* last_tree;
+  char* last_head;
+
   unsigned long age;
 };
 
index d27a986..24196f7 100644 (file)
--- a/src/log.c
+++ b/src/log.c
 #include "log.h"
 
 Html
-ctgit_log_link (struct ctgit_query* query, struct ctgit_repo_info* n, Html text)
+ctgit_log_link (struct ctgit_query* query, struct ctgit_repo_info* n, const char* head, Html text)
 {
   return html (
                html_tag ("a",
-                         html_attr ("href", html_fmt ("%s?repo=%s&action=log",
+                         html_attr ("href", html_fmt ("%s?repo=%s&action=log&head=%s",
                                                       query->request->script_name,
-                                                      n->name)
+                                                      n->name,
+                                                      head?head:"master")
                                     )
                          ),
                text
index c5f418b..dbe311d 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -24,6 +24,6 @@
 #include "ctgit.h"
 
 Html
-ctgit_log_link (struct ctgit_query* query, struct ctgit_repo_info* n, Html text);
+ctgit_log_link (struct ctgit_query* query, struct ctgit_repo_info* n, const char* head, Html text);
 
 #endif
index b3549fd..b3a6b42 100644 (file)
@@ -101,6 +101,10 @@ ctgit_repoinfo_new (struct ctgit_query* query, const char* path)
   self->giturl = NULL;
   self->readme = NULL;
 
+  self->last_commit = NULL;
+  self->last_tree = NULL;
+  self->last_head = NULL;
+
   llist_init (&self->node);
   self->path = cwa_strndup (path, SIZE_MAX);
 
@@ -117,6 +121,32 @@ ctgit_repoinfo_new (struct ctgit_query* query, const char* path)
   else
     self->age = ~0;
 
+  /* find the head which points to the last commit */
+  FILE* branches = ctgit_git_open ("branch --no-color --no-abbrev -v");
+  char name[128] = ".git/refs/heads/";
+  char sha1[41];
+  char* head = name + 16;
+
+  while (fscanf (branches, "%*[* ] %111s %40s", head, sha1) == 2)
+    {
+      while (fgetc(branches) != '\n');
+
+      if (!stat (name, &st))
+        if (difftime (query->now, st.st_mtime) == self->age)
+          break;
+    }
+  ctgit_git_close (branches);
+  self->last_commit = cwa_strndup (sha1, 40);
+  self->last_head = cwa_strndup (name + 5, SIZE_MAX);
+
+  /* find the tree for the last commit */
+  FILE* commit = ctgit_git_open ("cat-file commit %s", sha1);
+  if (fscanf (commit, "tree %40s", sha1) == 1)
+    {
+      self->last_tree = cwa_strndup (sha1, 40);
+    }
+  ctgit_git_close (commit);
+
   /* get [ctgit] subsection out of .git/config */
   in_flight = self;
   git_config (ctgit_repo_conf_cb);
@@ -191,6 +221,8 @@ ctgit_repoinfo_free (struct ctgit_repo_info* self)
 {
   if (self)
     {
+      llist_unlink (&self->node);
+
       free (self->path);
       free (self->name);
 
@@ -199,7 +231,10 @@ ctgit_repoinfo_free (struct ctgit_repo_info* self)
       free (self->giturl);
       free (self->readme);
 
-      llist_unlink (&self->node);
+      free (self->last_commit);
+      free (self->last_tree);
+      free (self->last_head);
+
       free (self);
     }
 }