add a 'path' query parameter to maintain path and filename in queries
authorChristian Thaeter <ct@pipapo.org>
Tue, 8 Jan 2008 04:46:21 +0000 (05:46 +0100)
committerChristian Thaeter <ct@pipapo.org>
Tue, 8 Jan 2008 04:46:21 +0000 (05:46 +0100)
src/actions.c
src/object.c
src/object.h
src/query.c
src/query.h
src/webgit.c
src/webgit.h

index 92c3de2..6ce5472 100644 (file)
@@ -81,6 +81,7 @@ webgit_main_content_action (struct webgit_query* query)
                                           webgit_object_link (query,
                                                               n->name, strlen(n->name),
                                                               n->last_commit, 40,
+                                                              NULL,
                                                               webgit_pretty_age (n->age))
                                           ),
                                      html(html_tag ("td"),
@@ -98,9 +99,10 @@ webgit_main_content_action (struct webgit_query* query)
                                                //webgit_diff_link (query, n, n->last_head, html("Diff")),
                                                //" ",
                                                webgit_object_link (query,
-                                                                  n->name, strlen(n->name),
-                                                                  n->last_tree, 40,
-                                                                  html("Tree"))
+                                                                   n->name, strlen(n->name),
+                                                                   n->last_tree, 40,
+                                                                   NULL,
+                                                                   html("Tree"))
                                                )
                                           )
                                      ),
index d61b943..582b39a 100644 (file)
@@ -33,14 +33,18 @@ webgit_object_link (struct webgit_query* query,
                    int repo_len,
                    const char* object,
                    int object_len,
+                   const char* path,
                    Html text)
 {
   return html (
                html_tag ("a",
-                         html_attr ("href", html_fmt ("%s?repo=%.*s&object=%.*s",
-                                                      query->request->script_name,
-                                                      repo_len, repo,
-                                                      object_len, object)
+                         html_attr ("href", html (
+                                                  html_fmt ("%s?repo=%.*s&object=%.*s",
+                                                            query->request->script_name,
+                                                            repo_len, repo,
+                                                            object_len, object),
+                                                  path ? html_fmt ("&path=%s", path) : html ()
+                                                  )
                                     )
                          ),
                text
@@ -86,9 +90,10 @@ webgit_object_commit_content_action (struct webgit_query* query, unsigned char*
           html_list_append (tree,
                             "Tree: ",
                             webgit_object_link (query,
-                                               query->repo, strlen (query->repo),
-                                               i+5, 40,
-                                               html_fmt ("%.40s", i+5))
+                                                query->repo, strlen (query->repo),
+                                                i+5, 40,
+                                                NULL,
+                                                html_fmt ("%.40s", i+5))
                             );
 
           (i = strchr (i, '\n')) && ++i;
@@ -99,9 +104,10 @@ webgit_object_commit_content_action (struct webgit_query* query, unsigned char*
           html_list_append (parents, html (
                                            "Parent: ",
                                            webgit_object_link (query,
-                                                              query->repo, strlen (query->repo),
-                                                              i+7, 40,
-                                                              html_fmt ("%.40s", i+7))
+                                                               query->repo, strlen (query->repo),
+                                                               i+7, 40,
+                                                               NULL,
+                                                               html_fmt ("%.40s", i+7))
                                            )
                             );
           (i = strchr (i, '\n')) && ++i;
@@ -261,8 +267,12 @@ static Html tree_in_flight = NULL;
 
 static int
 webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
-                 const char *pathname, unsigned mode, int stage)
+                  const char *name, unsigned mode, int stage)
 {
+  const char pathname[PATH_MAX];
+
+  snprintf (pathname, PATH_MAX-1, "%.*s%s%s", baselen, base, baselen ? "/": "", name);
+
   if (S_ISGITLINK(mode))
     {
       html_list_append (tree_in_flight, html (
@@ -273,7 +283,7 @@ webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
                                                           webgit_repo_link (query_in_flight,
                                                                             query_in_flight->repo,
                                                                             strlen (query_in_flight->repo),
-                                                                            pathname, strlen (pathname),
+                                                                            name, strlen (pathname),
                                                                             sha1_to_hex (sha1), 40,
                                                                             "tree",
                                                                             html_strndup (pathname, SIZE_MAX))
@@ -291,10 +301,11 @@ webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
                                                     html (html_tag ("td"), pretty_mode (mode)),
                                                     html (html_tag ("td"),
                                                           webgit_object_link (query_in_flight,
-                                                                             query_in_flight->repo,
-                                                                             strlen (query_in_flight->repo),
-                                                                             sha1_to_hex (sha1), 40,
-                                                                             html_strndup (pathname, SIZE_MAX))
+                                                                              query_in_flight->repo,
+                                                                              strlen (query_in_flight->repo),
+                                                                              sha1_to_hex (sha1), 40,
+                                                                              pathname,
+                                                                              html_strndup (name, SIZE_MAX))
                                                           ),
                                                   html (html_tag ("td"), "history snap")
                                                   ),
@@ -309,10 +320,11 @@ webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
                                                     html (html_tag ("td"), pretty_mode (mode)),
                                                     html (html_tag ("td"),
                                                           webgit_object_link (query_in_flight,
-                                                                             query_in_flight->repo,
-                                                                             strlen (query_in_flight->repo),
-                                                                             sha1_to_hex (sha1), 40,
-                                                                             html_strndup (pathname, SIZE_MAX))
+                                                                              query_in_flight->repo,
+                                                                              strlen (query_in_flight->repo),
+                                                                              sha1_to_hex (sha1), 40,
+                                                                              pathname,
+                                                                              html_strndup (name, SIZE_MAX))
                                                           ),
                                                     html (html_tag ("td"), "history raw edit")
                                                     ),
@@ -331,7 +343,7 @@ webgit_object_tree_content_action (struct webgit_query* query, unsigned char* sh
   query_in_flight = query;
   tree_in_flight = html_list ();
 
-  read_tree_recursive (tree, "", 0, 0, NULL, webgit_html_tree);
+  read_tree_recursive (tree, query->path, query->path ? strlen (query->path): 0, 0, NULL, webgit_html_tree);
 
   return html (html_tag ("table"), tree_in_flight);
 }
index 617f0c0..31eb4a8 100644 (file)
@@ -29,6 +29,7 @@ webgit_object_link (struct webgit_query* query,
                    int repo_len,
                    const char* object,
                    int object_len,
+                   const char* path,
                    Html text);
 
 Html
index d2cac72..67289d5 100644 (file)
@@ -41,6 +41,8 @@ webgit_query_init (struct webgit_query* q)
   q->action = NULL;
   q->object = NULL;
   q->head = NULL;
+  q->path = NULL;
+
   llist_init (&q->repos);
 }
 
@@ -54,6 +56,7 @@ webgit_query_destroy (struct webgit_query* q)
   free (q->action);
   free (q->object);
   free (q->head);
+  free (q->path);
 
   LLIST_WHILE_HEAD (&q->repos, head)
     webgit_repoinfo_free ((struct webgit_repo_info*) head);
@@ -152,6 +155,20 @@ webgit_ref_param (const Cgi self, const char* v, size_t v_sz, void* u_dat)
     }
 }
 
+static void
+webgit_path_param (const Cgi self, const char* v, size_t v_sz, void* u_dat)
+{
+  (void) self;
+  struct webgit_query* q = (struct webgit_query*) u_dat;
+
+  /* TODO validate that v is a probably legal reference (alnum() || one of '_/.') */
+  if (webgit_validate_string (v, v_sz))
+    {
+      free (q->path);
+      q->path = cwa_strndup (v, v_sz);
+    }
+}
+
 
 void
 webgit_param_dispatch (const Cgi self,
index 9eb2294..a3808f9 100644 (file)
@@ -33,6 +33,7 @@
  WEBGIT_PARAM(repo, "Repository to be queried")                  \
  WEBGIT_PARAM(action, "Action to be performed")                  \
  WEBGIT_PARAM(ref, "Selected ref (branch, tag, ..) for query")   \
+ WEBGIT_PARAM(path, "Path and filename for the queried object")  \
  WEBGIT_PARAM(object, "Object to perform on")
 
 
index a2f1823..ffbf9bf 100644 (file)
@@ -170,6 +170,8 @@ main (int argc, char**argv)
 
   cgi_run_query (query.request, webgit_param_dispatch, &query);
 
+  /* TODO: if no object given but repo+path+ref are given then compute object */
+
   /* default action is to show the main page with the list of repos */
   if (!query.action)
     query.action = cwa_strndup ("main", SIZE_MAX);
index 8072c31..2d72e8b 100644 (file)
@@ -47,6 +47,7 @@ struct webgit_query
   char* action;
   char* object;
   char* head;
+  char* path;
 
   /* list of repositories */
   llist repos;