aboutsummaryrefslogtreecommitdiffstats
path: root/cgit.c
diff options
context:
space:
mode:
Diffstat (limited to 'cgit.c')
-rw-r--r--cgit.c64
1 files changed, 49 insertions, 15 deletions
diff --git a/cgit.c b/cgit.c
index 9535abc..0fa203c 100644
--- a/cgit.c
+++ b/cgit.c
@@ -10,18 +10,45 @@
10 10
11const char cgit_version[] = CGIT_VERSION; 11const char cgit_version[] = CGIT_VERSION;
12 12
13static void cgit_prepare_cache(struct cacheitem *item) 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)
14{ 29{
15 if (!cgit_query_repo) { 30 if (!cgit_query_repo) {
16 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); 31 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
17 item->ttl = cgit_cache_root_ttl; 32 item->ttl = cgit_cache_root_ttl;
18 } else if (!cgit_query_page) { 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");
38 cgit_print_docstart(title, item);
39 cgit_print_pageheader(title, 0);
40 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo));
41 cgit_print_docend();
42 return 0;
43 }
44
45 if (!cgit_query_page) {
19 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, 46 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
20 cgit_query_repo)); 47 cgit_repo->url));
21 item->ttl = cgit_cache_repo_ttl; 48 item->ttl = cgit_cache_repo_ttl;
22 } else { 49 } else {
23 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 50 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
24 cgit_query_repo, cgit_query_page, 51 cgit_repo->url, cgit_query_page,
25 cache_safe_filename(cgit_querystring))); 52 cache_safe_filename(cgit_querystring)));
26 if (cgit_query_has_symref) 53 if (cgit_query_has_symref)
27 item->ttl = cgit_cache_dynamic_ttl; 54 item->ttl = cgit_cache_dynamic_ttl;
@@ -30,13 +57,16 @@ static void cgit_prepare_cache(struct cacheitem *item)
30 else 57 else
31 item->ttl = cgit_cache_repo_ttl; 58 item->ttl = cgit_cache_repo_ttl;
32 } 59 }
60 return 1;
33} 61}
34 62
35static void cgit_print_repo_page(struct cacheitem *item) 63static void cgit_print_repo_page(struct cacheitem *item)
36{ 64{
37 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || 65 char *title;
38 cgit_read_config("info/cgit", cgit_repo_config_cb)) { 66 int show_search;
39 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 67
68 if (chdir(cgit_repo->path)) {
69 title = fmt("%s - %s", cgit_root_title, "Bad request");
40 cgit_print_docstart(title, item); 70 cgit_print_docstart(title, item);
41 cgit_print_pageheader(title, 0); 71 cgit_print_pageheader(title, 0);
42 cgit_print_error(fmt("Unable to scan repository: %s", 72 cgit_print_error(fmt("Unable to scan repository: %s",
@@ -44,9 +74,10 @@ static void cgit_print_repo_page(struct cacheitem *item)
44 cgit_print_docend(); 74 cgit_print_docend();
45 return; 75 return;
46 } 76 }
47 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); 77
48 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); 78 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
49 int show_search = 0; 79 show_search = 0;
80 setenv("GIT_DIR", cgit_repo->path, 1);
50 if (cgit_query_page && !strcmp(cgit_query_page, "log")) 81 if (cgit_query_page && !strcmp(cgit_query_page, "log"))
51 show_search = 1; 82 show_search = 1;
52 cgit_print_docstart(title, item); 83 cgit_print_docstart(title, item);
@@ -131,9 +162,6 @@ static void cgit_parse_args(int argc, const char **argv)
131 int i; 162 int i;
132 163
133 for (i = 1; i < argc; i++) { 164 for (i = 1; i < argc; i++) {
134 if (!strncmp(argv[i], "--root=", 7)) {
135 cgit_root = xstrdup(argv[i]+7);
136 }
137 if (!strncmp(argv[i], "--cache=", 8)) { 165 if (!strncmp(argv[i], "--cache=", 8)) {
138 cgit_cache_root = xstrdup(argv[i]+8); 166 cgit_cache_root = xstrdup(argv[i]+8);
139 } 167 }
@@ -167,13 +195,19 @@ int main(int argc, const char **argv)
167{ 195{
168 struct cacheitem item; 196 struct cacheitem item;
169 197
198 htmlfd = STDOUT_FILENO;
199 item.st.st_mtime = time(NULL);
200 cgit_repolist.length = 0;
201 cgit_repolist.count = 0;
202 cgit_repolist.repos = NULL;
203
170 cgit_read_config("/etc/cgitrc", cgit_global_config_cb); 204 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
171 if (getenv("QUERY_STRING")) 205 if (getenv("QUERY_STRING"))
172 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 206 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
173 cgit_parse_args(argc, argv); 207 cgit_parse_args(argc, argv);
174 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 208 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
175 209 if (!cgit_prepare_cache(&item))
176 cgit_prepare_cache(&item); 210 return 0;
177 if (cgit_nocache) { 211 if (cgit_nocache) {
178 item.fd = STDOUT_FILENO; 212 item.fd = STDOUT_FILENO;
179 cgit_fill_cache(&item); 213 cgit_fill_cache(&item);