summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2007-05-18 01:00:54 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2007-05-18 20:51:01 (UTC)
commit30ccdcaa74ebc0aab2b7843b0db8251d0ddf56de (patch) (side-by-side diff)
treeefb3eddd091117281d5592c9eab44bc63121edf6
parent43d40f2b704151d145a1383b2b964210915ecae4 (diff)
downloadcgit-30ccdcaa74ebc0aab2b7843b0db8251d0ddf56de.zip
cgit-30ccdcaa74ebc0aab2b7843b0db8251d0ddf56de.tar.gz
cgit-30ccdcaa74ebc0aab2b7843b0db8251d0ddf56de.tar.bz2
Enable url=value querystring parameter
This makes is possible to use repo-urls like '/pub/scm/git/git.git' and even add path specifications, like '/pub/scm/git/git.git/log/documentation'. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c9
-rw-r--r--cgit.c60
-rw-r--r--cgit.h16
-rw-r--r--parsing.c43
-rw-r--r--shared.c5
-rw-r--r--ui-shared.c5
6 files changed, 98 insertions, 40 deletions
diff --git a/cache.c b/cache.c
index 8df7c26..7cdea9b 100644
--- a/cache.c
+++ b/cache.c
@@ -45,6 +45,8 @@ int cache_create_dirs()
- if (!cgit_query_repo)
+ if (!cgit_repo)
return 0;
- path = fmt("%s/%s", cgit_cache_root, cgit_query_repo);
+ path = fmt("%s/%s", cgit_cache_root,
+ cache_safe_filename(cgit_repo->url));
+
if (mkdir(path, S_IRWXU) && errno!=EEXIST)
@@ -53,3 +55,4 @@ int cache_create_dirs()
if (cgit_query_page) {
- path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo,
+ path = fmt("%s/%s/%s", cgit_cache_root,
+ cache_safe_filename(cgit_repo->url),
cgit_query_page);
diff --git a/cgit.c b/cgit.c
index 431e8fb..e5d8fbd 100644
--- a/cgit.c
+++ b/cgit.c
@@ -15,9 +15,3 @@ static int cgit_prepare_cache(struct cacheitem *item)
{
- if (!cgit_query_repo) {
- item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
- item->ttl = cgit_cache_root_ttl;
- return 1;
- }
- cgit_repo = cgit_get_repoinfo(cgit_query_repo);
- if (!cgit_repo) {
+ if (!cgit_repo && cgit_query_repo) {
char *title = fmt("%s - %s", cgit_root_title, "Bad request");
@@ -30,5 +24,11 @@ static int cgit_prepare_cache(struct cacheitem *item)
- if (!cgit_query_page) {
+ if (!cgit_repo) {
+ item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
+ item->ttl = cgit_cache_root_ttl;
+ return 1;
+ }
+
+ if (!cgit_cmd) {
item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
- cgit_repo->url));
+ cache_safe_filename(cgit_repo->url)));
item->ttl = cgit_cache_repo_ttl;
@@ -36,3 +36,3 @@ static int cgit_prepare_cache(struct cacheitem *item)
item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
- cgit_repo->url, cgit_query_page,
+ cache_safe_filename(cgit_repo->url), cgit_query_page,
cache_safe_filename(cgit_querystring)));
@@ -70,4 +70,3 @@ static void cgit_print_repo_page(struct cacheitem *item)
- if (cgit_query_page) {
- if (cgit_repo->snapshots && !strcmp(cgit_query_page, "snapshot")) {
+ if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) {
cgit_print_snapshot(item, cgit_query_sha1, "zip",
@@ -75,16 +74,12 @@ static void cgit_print_repo_page(struct cacheitem *item)
return;
- }
- if (!strcmp(cgit_query_page, "blob")) {
- cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
- return;
- }
}
- if (cgit_query_page && !strcmp(cgit_query_page, "log"))
- show_search = 1;
+ if (cgit_cmd == CMD_BLOB) {
+ cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
+ return;
+ }
+ show_search = (cgit_cmd == CMD_LOG);
cgit_print_docstart(title, item);
-
-
- if (!cgit_query_page) {
+ if (!cgit_cmd) {
cgit_print_pageheader("summary", show_search);
@@ -97,3 +92,4 @@ static void cgit_print_repo_page(struct cacheitem *item)
- if (!strcmp(cgit_query_page, "log")) {
+ switch(cgit_cmd) {
+ case CMD_LOG:
cgit_print_log(cgit_query_head, cgit_query_ofs,
@@ -101,12 +97,17 @@ static void cgit_print_repo_page(struct cacheitem *item)
cgit_query_path);
- } else if (!strcmp(cgit_query_page, "tree")) {
+ break;
+ case CMD_TREE:
cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path);
- } else if (!strcmp(cgit_query_page, "commit")) {
+ break;
+ case CMD_COMMIT:
cgit_print_commit(cgit_query_head);
- } else if (!strcmp(cgit_query_page, "view")) {
+ break;
+ case CMD_VIEW:
cgit_print_view(cgit_query_sha1, cgit_query_path);
- } else if (!strcmp(cgit_query_page, "diff")) {
+ break;
+ case CMD_DIFF:
cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2,
cgit_query_path);
- } else {
+ break;
+ default:
cgit_print_error("Invalid request");
@@ -131,3 +132,3 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
- if (cgit_query_repo)
+ if (cgit_repo)
cgit_print_repo_page(item);
@@ -236,2 +237,3 @@ int main(int argc, const char **argv)
cgit_read_config(CGIT_CONFIG, cgit_global_config_cb);
+ cgit_repo = NULL;
if (getenv("SCRIPT_NAME"))
diff --git a/cgit.h b/cgit.h
index f402466..e5b3f5e 100644
--- a/cgit.h
+++ b/cgit.h
@@ -23,9 +23,9 @@
*/
-#define CMD_LOG = 1;
-#define CMD_COMMIT = 1;
-#define CMD_DIFF = 1;
-#define CMD_TREE = 1;
-#define CMD_VIEW = 1;
-#define CMD_BLOB = 1;
-#define CMD_SNAPSHOT = 1;
+#define CMD_LOG 1
+#define CMD_COMMIT 2
+#define CMD_DIFF 3
+#define CMD_TREE 4
+#define CMD_VIEW 5
+#define CMD_BLOB 6
+#define CMD_SNAPSHOT 7
@@ -84,2 +84,3 @@ extern struct repolist cgit_repolist;
extern struct repoinfo *cgit_repo;
+extern int cgit_cmd;
@@ -164,2 +165,3 @@ extern struct commitinfo *cgit_parse_commit(struct commit *commit);
extern struct taginfo *cgit_parse_tag(struct tag *tag);
+extern void cgit_parse_url(const char *url);
diff --git a/parsing.c b/parsing.c
index 36b0f0c..a028625 100644
--- a/parsing.c
+++ b/parsing.c
@@ -134,2 +134,45 @@ int cgit_parse_query(char *txt, configfn fn)
+/*
+ * 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;
+
+ cgit_repo = NULL;
+ if (!url || url[0] == '\0')
+ return;
+
+ cgit_repo = cgit_get_repoinfo(url);
+ if (cgit_repo) {
+ cgit_query_repo = cgit_repo->url;
+ return;
+ }
+
+ cmd = strchr(url, '/');
+ while (!cgit_repo && cmd) {
+ cmd[0] = '\0';
+ cgit_repo = cgit_get_repoinfo(url);
+ if (cgit_repo == NULL) {
+ cmd[0] = '/';
+ cmd = strchr(cmd + 1, '/');
+ continue;
+ }
+
+ cgit_query_repo = cgit_repo->url;
+ p = strchr(cmd + 1, '/');
+ if (p) {
+ p[0] = '\0';
+ cgit_query_path = xstrdup(p + 1);
+ }
+ cgit_cmd = cgit_get_cmd_index(cmd + 1);
+ cgit_query_page = xstrdup(cmd + 1);
+ return;
+ }
+}
+
char *substr(const char *head, const char *tail)
diff --git a/shared.c b/shared.c
index fb05427..0b074da 100644
--- a/shared.c
+++ b/shared.c
@@ -12,2 +12,3 @@ struct repolist cgit_repolist;
struct repoinfo *cgit_repo;
+int cgit_cmd;
@@ -186,4 +187,8 @@ void cgit_querystring_cb(const char *name, const char *value)
cgit_query_repo = xstrdup(value);
+ cgit_repo = cgit_get_repoinfo(value);
} else if (!strcmp(name, "p")) {
cgit_query_page = xstrdup(value);
+ cgit_cmd = cgit_get_cmd_index(value);
+ } else if (!strcmp(name, "url")) {
+ cgit_parse_url(value);
} else if (!strcmp(name, "q")) {
diff --git a/ui-shared.c b/ui-shared.c
index 6211056..c7fbc5e 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -70,3 +70,6 @@ char *cgit_pageurl(const char *reponame, const char *pagename,
} else {
- return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
+ if (query)
+ return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
+ else
+ return fmt("?r=%s&p=%s", reponame, pagename);
}