summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c31
1 files changed, 15 insertions, 16 deletions
diff --git a/cgit.c b/cgit.c
index f0907a1..b270fdc 100644
--- a/cgit.c
+++ b/cgit.c
@@ -7,35 +7,35 @@
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11static int cgit_prepare_cache(struct cacheitem *item) 11static int cgit_prepare_cache(struct cacheitem *item)
12{ 12{
13 if (!cgit_repo && ctx.qry.repo) { 13 if (!ctx.repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); 14 char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request");
15 cgit_print_docstart(title, item); 15 cgit_print_docstart(title, item);
16 cgit_print_pageheader(title, 0); 16 cgit_print_pageheader(title, 0);
17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); 17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 18 cgit_print_docend();
19 return 0; 19 return 0;
20 } 20 }
21 21
22 if (!cgit_repo) { 22 if (!ctx.repo) {
23 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); 23 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
24 item->ttl = ctx.cfg.cache_root_ttl; 24 item->ttl = ctx.cfg.cache_root_ttl;
25 return 1; 25 return 1;
26 } 26 }
27 27
28 if (!cgit_cmd) { 28 if (!cgit_cmd) {
29 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, 29 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
30 cache_safe_filename(cgit_repo->url), 30 cache_safe_filename(ctx.repo->url),
31 cache_safe_filename(ctx.qry.raw))); 31 cache_safe_filename(ctx.qry.raw)));
32 item->ttl = ctx.cfg.cache_repo_ttl; 32 item->ttl = ctx.cfg.cache_repo_ttl;
33 } else { 33 } else {
34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, 34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
35 cache_safe_filename(cgit_repo->url), 35 cache_safe_filename(ctx.repo->url),
36 ctx.qry.page, 36 ctx.qry.page,
37 cache_safe_filename(ctx.qry.raw))); 37 cache_safe_filename(ctx.qry.raw)));
38 if (ctx.qry.has_symref) 38 if (ctx.qry.has_symref)
39 item->ttl = ctx.cfg.cache_dynamic_ttl; 39 item->ttl = ctx.cfg.cache_dynamic_ttl;
40 else if (ctx.qry.has_sha1) 40 else if (ctx.qry.has_sha1)
41 item->ttl = ctx.cfg.cache_static_ttl; 41 item->ttl = ctx.cfg.cache_static_ttl;
@@ -61,13 +61,13 @@ int find_current_ref(const char *refname, const unsigned char *sha1,
61 info->match = 1; 61 info->match = 1;
62 if (!info->first_ref) 62 if (!info->first_ref)
63 info->first_ref = xstrdup(refname); 63 info->first_ref = xstrdup(refname);
64 return info->match; 64 return info->match;
65} 65}
66 66
67char *find_default_branch(struct repoinfo *repo) 67char *find_default_branch(struct cgit_repo *repo)
68{ 68{
69 struct refmatch info; 69 struct refmatch info;
70 70
71 info.req_ref = repo->defbranch; 71 info.req_ref = repo->defbranch;
72 info.first_ref = NULL; 72 info.first_ref = NULL;
73 info.match = 0; 73 info.match = 0;
@@ -81,54 +81,54 @@ char *find_default_branch(struct repoinfo *repo)
81static void cgit_print_repo_page(struct cacheitem *item) 81static void cgit_print_repo_page(struct cacheitem *item)
82{ 82{
83 char *title, *tmp; 83 char *title, *tmp;
84 int show_search; 84 int show_search;
85 unsigned char sha1[20]; 85 unsigned char sha1[20];
86 86
87 if (chdir(cgit_repo->path)) { 87 if (chdir(ctx.repo->path)) {
88 title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); 88 title = fmt("%s - %s", ctx.cfg.root_title, "Bad request");
89 cgit_print_docstart(title, item); 89 cgit_print_docstart(title, item);
90 cgit_print_pageheader(title, 0); 90 cgit_print_pageheader(title, 0);
91 cgit_print_error(fmt("Unable to scan repository: %s", 91 cgit_print_error(fmt("Unable to scan repository: %s",
92 strerror(errno))); 92 strerror(errno)));
93 cgit_print_docend(); 93 cgit_print_docend();
94 return; 94 return;
95 } 95 }
96 96
97 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 97 title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc);
98 show_search = 0; 98 show_search = 0;
99 setenv("GIT_DIR", cgit_repo->path, 1); 99 setenv("GIT_DIR", ctx.repo->path, 1);
100 100
101 if (!ctx.qry.head) { 101 if (!ctx.qry.head) {
102 ctx.qry.head = xstrdup(find_default_branch(cgit_repo)); 102 ctx.qry.head = xstrdup(find_default_branch(ctx.repo));
103 cgit_repo->defbranch = ctx.qry.head; 103 ctx.repo->defbranch = ctx.qry.head;
104 } 104 }
105 105
106 if (!ctx.qry.head) { 106 if (!ctx.qry.head) {
107 cgit_print_docstart(title, item); 107 cgit_print_docstart(title, item);
108 cgit_print_pageheader(title, 0); 108 cgit_print_pageheader(title, 0);
109 cgit_print_error("Repository seems to be empty"); 109 cgit_print_error("Repository seems to be empty");
110 cgit_print_docend(); 110 cgit_print_docend();
111 return; 111 return;
112 } 112 }
113 113
114 if (get_sha1(ctx.qry.head, sha1)) { 114 if (get_sha1(ctx.qry.head, sha1)) {
115 tmp = xstrdup(ctx.qry.head); 115 tmp = xstrdup(ctx.qry.head);
116 ctx.qry.head = cgit_repo->defbranch; 116 ctx.qry.head = ctx.repo->defbranch;
117 cgit_print_docstart(title, item); 117 cgit_print_docstart(title, item);
118 cgit_print_pageheader(title, 0); 118 cgit_print_pageheader(title, 0);
119 cgit_print_error(fmt("Invalid branch: %s", tmp)); 119 cgit_print_error(fmt("Invalid branch: %s", tmp));
120 cgit_print_docend(); 120 cgit_print_docend();
121 return; 121 return;
122 } 122 }
123 123
124 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { 124 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) {
125 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, 125 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1,
126 cgit_repobasename(cgit_repo->url), 126 cgit_repobasename(ctx.repo->url),
127 ctx.qry.path, 127 ctx.qry.path,
128 cgit_repo->snapshots ); 128 ctx.repo->snapshots );
129 return; 129 return;
130 } 130 }
131 131
132 if (cgit_cmd == CMD_PATCH) { 132 if (cgit_cmd == CMD_PATCH) {
133 cgit_print_patch(ctx.qry.sha1, item); 133 cgit_print_patch(ctx.qry.sha1, item);
134 return; 134 return;
@@ -189,13 +189,13 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
189 stdout2 = chk_positive(dup(STDOUT_FILENO), 189 stdout2 = chk_positive(dup(STDOUT_FILENO),
190 "Preserving STDOUT"); 190 "Preserving STDOUT");
191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
193 } 193 }
194 194
195 if (cgit_repo) 195 if (ctx.repo)
196 cgit_print_repo_page(item); 196 cgit_print_repo_page(item);
197 else 197 else
198 cgit_print_repolist(item); 198 cgit_print_repolist(item);
199 199
200 if (use_cache) { 200 if (use_cache) {
201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
@@ -297,13 +297,12 @@ int main(int argc, const char **argv)
297 cgit_repolist.length = 0; 297 cgit_repolist.length = 0;
298 cgit_repolist.count = 0; 298 cgit_repolist.count = 0;
299 cgit_repolist.repos = NULL; 299 cgit_repolist.repos = NULL;
300 300
301 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 301 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
302 cgit_global_config_cb); 302 cgit_global_config_cb);
303 cgit_repo = NULL;
304 if (getenv("SCRIPT_NAME")) 303 if (getenv("SCRIPT_NAME"))
305 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 304 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
306 if (getenv("QUERY_STRING")) 305 if (getenv("QUERY_STRING"))
307 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 306 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
308 cgit_parse_args(argc, argv); 307 cgit_parse_args(argc, argv);
309 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); 308 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);