Factored the 'dwim' code out
authorChristian Thaeter <ct@pipapo.org>
Wed, 20 Feb 2008 19:01:12 +0000 (20:01 +0100)
committerChristian Thaeter <ct@pipapo.org>
Wed, 20 Feb 2008 19:01:12 +0000 (20:01 +0100)
dwim sanitizes/completes actions depending on supplied minimal query
parameters

src/actions.c
src/actions.h
src/webgit.c

index d7ed7ed..96f95bb 100644 (file)
 #include <stdio.h>
 
 
 #include <stdio.h>
 
 
+void
+webgit_action_dwim (struct webgit_query* query)
+{
+  const char* default_action = "object";
+
+  if (!query->repo)
+    default_action = "main";
+  else
+    {
+      if (!query->object)
+        {
+          if (!query->head)
+            {
+              default_action = "summary";
+            }
+          else
+            {
+              if (!query->commit)
+                default_action = "log";
+              else
+                {
+                  if (!query->path)
+                    {
+                      query->object = cwa_strndup (query->commit, SIZE_MAX);
+                    }
+                }
+            }
+        }
+    }
+
+  if (!query->action)
+    query->action = cwa_strndup (default_action, SIZE_MAX);
+  else if (!strcmp (query->action, "cancel"))
+    {
+      free (query->action);
+      query->action = cwa_strndup ("object", SIZE_MAX);
+      if (query->path)
+        {
+          char* r = strrchr (query->path, '/');
+          if (r)
+            *r = '\0';
+          else
+            query->path[0] = '\0';
+        }
+    }
+}
+
+
 Html
 webgit_main_link (struct webgit_query* query, Html text)
 {
 Html
 webgit_main_link (struct webgit_query* query, Html text)
 {
index 6bc62d8..ddc54ac 100644 (file)
@@ -47,6 +47,9 @@ webgit_action_dispatch (struct webgit_query* query);
 Html
 webgit_main_link (struct webgit_query* query, Html text);
 
 Html
 webgit_main_link (struct webgit_query* query, Html text);
 
+void
+webgit_action_dwim (struct webgit_query* query);
+
 #endif
 
 /*
 #endif
 
 /*
index ad7d701..93e70a0 100644 (file)
@@ -213,35 +213,7 @@ main (int argc, char**argv)
     query.count = query.count_def;
 
   /* dwim, select/complete actions and parameters */
     query.count = query.count_def;
 
   /* dwim, select/complete actions and parameters */
-  const char* default_action = "object";
-
-  if (!query.repo)
-    default_action = "main";
-  else
-    {
-      if (!query.object)
-        {
-          if (!query.head)
-            {
-              default_action = "summary";
-            }
-          else
-            {
-              if (!query.commit)
-                default_action = "log";
-              else
-                {
-                  if (!query.path)
-                    {
-                      query.object = cwa_strndup (query.commit, SIZE_MAX);
-                    }
-                }
-            }
-        }
-    }
-
-  if (!query.action)
-    query.action = cwa_strndup (default_action, SIZE_MAX);
+  webgit_action_dwim (&query);
 
   /* initial rxpd check, just a 'global' access tuple */
   if (query.rxpd)
 
   /* initial rxpd check, just a 'global' access tuple */
   if (query.rxpd)