summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--cache.c20
-rw-r--r--cgit.c70
-rw-r--r--cgit.h15
-rw-r--r--parsing.c44
-rw-r--r--shared.c30
-rw-r--r--ui-shared.c3
6 files changed, 135 insertions, 47 deletions
diff --git a/cache.c b/cache.c
index 8df7c26..372e38d 100644
--- a/cache.c
+++ b/cache.c
@@ -14,6 +14,11 @@ char *cache_safe_filename(const char *unsafe)
14{ 14{
15 static char buf[PATH_MAX]; 15 static char buf[4][PATH_MAX];
16 char *s = buf; 16 static int bufidx;
17 char *s;
17 char c; 18 char c;
18 19
20 bufidx++;
21 bufidx &= 3;
22 s = buf[bufidx];
23
19 while(unsafe && (c = *unsafe++) != 0) { 24 while(unsafe && (c = *unsafe++) != 0) {
@@ -25,3 +30,3 @@ char *cache_safe_filename(const char *unsafe)
25 *s = '\0'; 30 *s = '\0';
26 return buf; 31 return buf[bufidx];
27} 32}
@@ -45,6 +50,8 @@ int cache_create_dirs()
45 50
46 if (!cgit_query_repo) 51 if (!cgit_repo)
47 return 0; 52 return 0;
48 53
49 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); 54 path = fmt("%s/%s", cgit_cache_root,
55 cache_safe_filename(cgit_repo->url));
56
50 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 57 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
@@ -53,3 +60,4 @@ int cache_create_dirs()
53 if (cgit_query_page) { 60 if (cgit_query_page) {
54 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, 61 path = fmt("%s/%s/%s", cgit_cache_root,
62 cache_safe_filename(cgit_repo->url),
55 cgit_query_page); 63 cgit_query_page);
diff --git a/cgit.c b/cgit.c
index 3e7e595..e5d8fbd 100644
--- a/cgit.c
+++ b/cgit.c
@@ -13,25 +13,5 @@ const char cgit_version[] = CGIT_VERSION;
13 13
14static struct repoinfo *cgit_get_repoinfo(char *url)
15{
16 int i;
17 struct repoinfo *repo;
18
19 for (i=0; i<cgit_repolist.count; i++) {
20 repo = &cgit_repolist.repos[i];
21 if (!strcmp(repo->url, url))
22 return repo;
23 }
24 return NULL;
25}
26
27
28static int cgit_prepare_cache(struct cacheitem *item) 14static int cgit_prepare_cache(struct cacheitem *item)
29{ 15{
30 if (!cgit_query_repo) { 16 if (!cgit_repo && cgit_query_repo) {
31 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
32 item->ttl = cgit_cache_root_ttl;
33 return 1;
34 }
35 cgit_repo = cgit_get_repoinfo(cgit_query_repo);
36 if (!cgit_repo) {
37 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 17 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
@@ -44,5 +24,11 @@ static int cgit_prepare_cache(struct cacheitem *item)
44 24
45 if (!cgit_query_page) { 25 if (!cgit_repo) {
26 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
27 item->ttl = cgit_cache_root_ttl;
28 return 1;
29 }
30
31 if (!cgit_cmd) {
46 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, 32 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
47 cgit_repo->url)); 33 cache_safe_filename(cgit_repo->url)));
48 item->ttl = cgit_cache_repo_ttl; 34 item->ttl = cgit_cache_repo_ttl;
@@ -50,3 +36,3 @@ static int cgit_prepare_cache(struct cacheitem *item)
50 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 36 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
51 cgit_repo->url, cgit_query_page, 37 cache_safe_filename(cgit_repo->url), cgit_query_page,
52 cache_safe_filename(cgit_querystring))); 38 cache_safe_filename(cgit_querystring)));
@@ -84,4 +70,3 @@ static void cgit_print_repo_page(struct cacheitem *item)
84 70
85 if (cgit_query_page) { 71 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) {
86 if (cgit_repo->snapshots && !strcmp(cgit_query_page, "snapshot")) {
87 cgit_print_snapshot(item, cgit_query_sha1, "zip", 72 cgit_print_snapshot(item, cgit_query_sha1, "zip",
@@ -90,3 +75,4 @@ static void cgit_print_repo_page(struct cacheitem *item)
90 } 75 }
91 if (!strcmp(cgit_query_page, "blob")) { 76
77 if (cgit_cmd == CMD_BLOB) {
92 cgit_print_blob(item, cgit_query_sha1, cgit_query_path); 78 cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
@@ -94,11 +80,6 @@ static void cgit_print_repo_page(struct cacheitem *item)
94 } 80 }
95 }
96
97 if (cgit_query_page && !strcmp(cgit_query_page, "log"))
98 show_search = 1;
99 81
82 show_search = (cgit_cmd == CMD_LOG);
100 cgit_print_docstart(title, item); 83 cgit_print_docstart(title, item);
101 84 if (!cgit_cmd) {
102
103 if (!cgit_query_page) {
104 cgit_print_pageheader("summary", show_search); 85 cgit_print_pageheader("summary", show_search);
@@ -111,3 +92,4 @@ static void cgit_print_repo_page(struct cacheitem *item)
111 92
112 if (!strcmp(cgit_query_page, "log")) { 93 switch(cgit_cmd) {
94 case CMD_LOG:
113 cgit_print_log(cgit_query_head, cgit_query_ofs, 95 cgit_print_log(cgit_query_head, cgit_query_ofs,
@@ -115,12 +97,17 @@ static void cgit_print_repo_page(struct cacheitem *item)
115 cgit_query_path); 97 cgit_query_path);
116 } else if (!strcmp(cgit_query_page, "tree")) { 98 break;
99 case CMD_TREE:
117 cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path); 100 cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path);
118 } else if (!strcmp(cgit_query_page, "commit")) { 101 break;
102 case CMD_COMMIT:
119 cgit_print_commit(cgit_query_head); 103 cgit_print_commit(cgit_query_head);
120 } else if (!strcmp(cgit_query_page, "view")) { 104 break;
105 case CMD_VIEW:
121 cgit_print_view(cgit_query_sha1, cgit_query_path); 106 cgit_print_view(cgit_query_sha1, cgit_query_path);
122 } else if (!strcmp(cgit_query_page, "diff")) { 107 break;
108 case CMD_DIFF:
123 cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2, 109 cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2,
124 cgit_query_path); 110 cgit_query_path);
125 } else { 111 break;
112 default:
126 cgit_print_error("Invalid request"); 113 cgit_print_error("Invalid request");
@@ -145,3 +132,3 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
145 132
146 if (cgit_query_repo) 133 if (cgit_repo)
147 cgit_print_repo_page(item); 134 cgit_print_repo_page(item);
@@ -250,2 +237,3 @@ int main(int argc, const char **argv)
250 cgit_read_config(CGIT_CONFIG, cgit_global_config_cb); 237 cgit_read_config(CGIT_CONFIG, cgit_global_config_cb);
238 cgit_repo = NULL;
251 if (getenv("SCRIPT_NAME")) 239 if (getenv("SCRIPT_NAME"))
diff --git a/cgit.h b/cgit.h
index 3ee11bb..e0879bd 100644
--- a/cgit.h
+++ b/cgit.h
@@ -20,2 +20,13 @@
20 20
21/*
22 * The valid cgit repo-commands
23 */
24#define CMD_LOG 1
25#define CMD_COMMIT 2
26#define CMD_DIFF 3
27#define CMD_TREE 4
28#define CMD_VIEW 5
29#define CMD_BLOB 6
30#define CMD_SNAPSHOT 7
31
21typedef void (*configfn)(const char *name, const char *value); 32typedef void (*configfn)(const char *name, const char *value);
@@ -73,2 +84,3 @@ extern struct repolist cgit_repolist;
73extern struct repoinfo *cgit_repo; 84extern struct repoinfo *cgit_repo;
85extern int cgit_cmd;
74 86
@@ -115,2 +127,4 @@ extern int htmlfd;
115 127
128extern int cgit_get_cmd_index(const char *cmd);
129extern struct repoinfo *cgit_get_repoinfo(const char *url);
116extern void cgit_global_config_cb(const char *name, const char *value); 130extern void cgit_global_config_cb(const char *name, const char *value);
@@ -153,2 +167,3 @@ extern struct commitinfo *cgit_parse_commit(struct commit *commit);
153extern struct taginfo *cgit_parse_tag(struct tag *tag); 167extern struct taginfo *cgit_parse_tag(struct tag *tag);
168extern void cgit_parse_url(const char *url);
154 169
diff --git a/parsing.c b/parsing.c
index 36b0f0c..4420e58 100644
--- a/parsing.c
+++ b/parsing.c
@@ -134,2 +134,46 @@ int cgit_parse_query(char *txt, configfn fn)
134 134
135/*
136 * url syntax: [repo ['/' cmd [ '/' path]]]
137 * repo: any valid repo url, may contain '/'
138 * cmd: log | commit | diff | tree | view | blob | snapshot
139 * path: any valid path, may contain '/'
140 *
141 */
142void cgit_parse_url(const char *url)
143{
144 char *cmd, *p;
145
146 cgit_repo = NULL;
147 if (!url || url[0] == '\0')
148 return;
149
150 cgit_repo = cgit_get_repoinfo(url);
151 if (cgit_repo) {
152 cgit_query_repo = cgit_repo->url;
153 return;
154 }
155
156 cmd = strchr(url, '/');
157 while (!cgit_repo && cmd) {
158 cmd[0] = '\0';
159 cgit_repo = cgit_get_repoinfo(url);
160 if (cgit_repo == NULL) {
161 cmd[0] = '/';
162 cmd = strchr(cmd + 1, '/');
163 continue;
164 }
165
166 cgit_query_repo = cgit_repo->url;
167 p = strchr(cmd + 1, '/');
168 if (p) {
169 p[0] = '\0';
170 if (p[1])
171 cgit_query_path = xstrdup(p + 1);
172 }
173 cgit_cmd = cgit_get_cmd_index(cmd + 1);
174 cgit_query_page = xstrdup(cmd + 1);
175 return;
176 }
177}
178
135char *substr(const char *head, const char *tail) 179char *substr(const char *head, const char *tail)
diff --git a/shared.c b/shared.c
index 54b1813..45fde7f 100644
--- a/shared.c
+++ b/shared.c
@@ -12,2 +12,3 @@ struct repolist cgit_repolist;
12struct repoinfo *cgit_repo; 12struct repoinfo *cgit_repo;
13int cgit_cmd;
13 14
@@ -54,2 +55,14 @@ int htmlfd = 0;
54 55
56
57int cgit_get_cmd_index(const char *cmd)
58{
59 static char *cmds[] = {"log", "commit", "diff", "tree", "view", "blob", "snapshot", NULL};
60 int i;
61
62 for(i = 0; cmds[i]; i++)
63 if (!strcmp(cmd, cmds[i]))
64 return i + 1;
65 return 0;
66}
67
55int chk_zero(int result, char *msg) 68int chk_zero(int result, char *msg)
@@ -96,2 +109,15 @@ struct repoinfo *add_repo(const char *url)
96 109
110struct repoinfo *cgit_get_repoinfo(const char *url)
111{
112 int i;
113 struct repoinfo *repo;
114
115 for (i=0; i<cgit_repolist.count; i++) {
116 repo = &cgit_repolist.repos[i];
117 if (!strcmp(repo->url, url))
118 return repo;
119 }
120 return NULL;
121}
122
97void cgit_global_config_cb(const char *name, const char *value) 123void cgit_global_config_cb(const char *name, const char *value)
@@ -164,4 +190,8 @@ void cgit_querystring_cb(const char *name, const char *value)
164 cgit_query_repo = xstrdup(value); 190 cgit_query_repo = xstrdup(value);
191 cgit_repo = cgit_get_repoinfo(value);
165 } else if (!strcmp(name, "p")) { 192 } else if (!strcmp(name, "p")) {
166 cgit_query_page = xstrdup(value); 193 cgit_query_page = xstrdup(value);
194 cgit_cmd = cgit_get_cmd_index(value);
195 } else if (!strcmp(name, "url")) {
196 cgit_parse_url(value);
167 } else if (!strcmp(name, "q")) { 197 } 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,
70 } else { 70 } else {
71 if (query)
71 return fmt("?r=%s&p=%s&%s", reponame, pagename, query); 72 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
73 else
74 return fmt("?r=%s&p=%s", reponame, pagename);
72 } 75 }