first tree display
authorChristian Thaeter <ct@pipapo.org>
Thu, 3 Jan 2008 05:23:52 +0000 (06:23 +0100)
committerChristian Thaeter <ct@pipapo.org>
Thu, 3 Jan 2008 05:23:52 +0000 (06:23 +0100)
src/object.c

index 4d3597eaec93e5dc1fc00e40326430f2138fb3b9..616e5c6975a721fe0b7d69a24252fb7ce42ceff0 100644 (file)
@@ -22,7 +22,7 @@
 #include "object.h"
 
 #define SHA1_HEADER <openssl/sha.h>
-#include "git/git-compat-util.h"
+#include "git/cache.h"
 #include "git/object.h"
 
 
@@ -236,40 +236,122 @@ ctgit_object_commit_action (struct ctgit_query* query, unsigned char* sha1)
               );
 }
 
+/*
+  Display trees
+*/
 
+static Html
+ctgit_object_tree_menu_action (struct ctgit_query* query, unsigned char* sha1, struct tree *tree)
+{
+  return html ("TODO: tree-object sidebar");
+}
 
+static const char*
+pretty_mode (unsigned mode)
+{
+  if (S_ISGITLINK (mode))
+    return "m---------";
+  else if (S_ISDIR (mode & S_IFMT))
+    return "drwxr-xr-x";
+  else if (S_ISLNK (mode))
+    return "lrwxrwxrwx";
+  else if (S_ISREG (mode))
+    {
+      if (mode & S_IXUSR)
+        return "-rwxr-xr-x";
+      else
+        return "-rw-r--r--";
+    }
+  else
+    return "----------";
+}
 
+/* callback has no user-data pointer! */
+static struct ctgit_query* query_in_flight = NULL;
+static Html tree_in_flight = NULL;
 
+static int
+ctgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
+                 const char *pathname, unsigned mode, int stage)
+{
+  if (S_ISGITLINK(mode))
+    {
+      html_list_append (tree_in_flight, html (
+                                              html (
+                                                    html_tag ("tr"),
+                                                    html (html_tag ("td"), pretty_mode (mode)),
+                                                    html (html_tag ("td"), html_strndup (pathname, SIZE_MAX)),
+                                                    html (html_tag ("td"), "history")
+                                                    ),
+                                              html_nl ()
+                                              )
+                        );
+    }
+  else if (S_ISDIR(mode))
+    {
+      html_list_append (tree_in_flight, html (
+                                              html (html_tag ("tr"),
+                                                    html (html_tag ("td"), pretty_mode (mode)),
+                                                    html (html_tag ("td"),
+                                                          ctgit_object_link (query_in_flight,
+                                                                             query_in_flight->repo,
+                                                                             strlen (query_in_flight->repo),
+                                                                             sha1_to_hex (sha1), 40,
+                                                                             html_strndup (pathname, SIZE_MAX))
+                                                          ),
+                                                  html (html_tag ("td"), "history snap")
+                                                  ),
+                                             html_nl ()
+                                             )
+                        );
+    }
+  else
+    {
+      html_list_append (tree_in_flight, html (
+                                              html (html_tag ("tr"),
+                                                    html (html_tag ("td"), pretty_mode (mode)),
+                                                    html (html_tag ("td"),
+                                                          ctgit_object_link (query_in_flight,
+                                                                             query_in_flight->repo,
+                                                                             strlen (query_in_flight->repo),
+                                                                             sha1_to_hex (sha1), 40,
+                                                                             html_strndup (pathname, SIZE_MAX))
+                                                          ),
+                                                    html (html_tag ("td"), "history raw edit")
+                                                    ),
+                                              html_nl()
+                                              )
+                        );
+    }
 
-
-
-
+  return 0;
+}
 
 
 
 static Html
-ctgit_object_tree_menu_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
+ctgit_object_tree_content_action (struct ctgit_query* query, unsigned char* sha1, struct tree *tree)
 {
-  return html ("TODO: tree-object sidebar");
-}
+  query_in_flight = query;
+  tree_in_flight = html_list ();
 
-static Html
-ctgit_object_tree_content_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
-{
+  read_tree_recursive (tree, "", 0, 0, NULL, ctgit_html_tree);
 
-  return html_fmt ("tree content %s", buf);
+  return html (html_tag ("table"), tree_in_flight);
 }
 
 Html
 ctgit_object_tree_action (struct ctgit_query* query, unsigned char* sha1)
 {
-  unsigned long size;
-  void* buf;
-  buf = read_object_with_reference (sha1, "tree", &size, NULL);
+  struct tree *tree;
+
+  tree = parse_tree_indirect (sha1);
+  if (!tree)
+    die("not a tree object");
 
   return html(
-              html(html_tag("div"), ctgit_object_tree_menu_action (query, sha1, buf, size)), html_nl (),
-              html(html_tag("div"), ctgit_object_tree_content_action (query, sha1, buf, size)), html_nl ()
+              html(html_tag("div"), ctgit_object_tree_menu_action (query, sha1, tree)), html_nl (),
+              html(html_tag("div"), ctgit_object_tree_content_action (query, sha1, tree)), html_nl ()
               );
 }