+/*
+ * url syntax: [repo ['/' cmd [ '/' path]]]
+ * repo: any valid repo url, may contain '/'
+ * cmd: log | commit | diff | tree | view | blob | snapshot
+ * path: any valid path, may contain '/'
+ *
+ */
+void cgit_parse_url(const char *url)
+{
+ char *cmd, *p;
+
+ ctx.repo = NULL;
+ if (!url || url[0] == '\0')
+ return;
+
+ ctx.repo = cgit_get_repoinfo(url);
+ if (ctx.repo) {
+ ctx.qry.repo = ctx.repo->url;
+ return;
+ }
+
+ cmd = strchr(url, '/');
+ while (!ctx.repo && cmd) {
+ cmd[0] = '\0';
+ ctx.repo = cgit_get_repoinfo(url);
+ if (ctx.repo == NULL) {
+ cmd[0] = '/';
+ cmd = strchr(cmd + 1, '/');
+ continue;
+ }
+
+ ctx.qry.repo = ctx.repo->url;
+ p = strchr(cmd + 1, '/');
+ if (p) {
+ p[0] = '\0';
+ if (p[1])
+ ctx.qry.path = trim_end(p + 1, '/');
+ }
+ cgit_cmd = cgit_get_cmd_index(cmd + 1);
+ ctx.qry.page = xstrdup(cmd + 1);
+ return;
+ }
+}
+