author | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 22:10:59 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 22:27:16 (UTC) |
commit | 92908af4558d7362c7deeb05254343a5a5f11a05 (patch) (unidiff) | |
tree | 6913a6f567375446fb680bc7e94dbabb5d8e496d /cgit.c | |
parent | df90b51bafec24336cf30339b2978ff6df22b075 (diff) | |
download | cgit-92908af4558d7362c7deeb05254343a5a5f11a05.zip cgit-92908af4558d7362c7deeb05254343a5a5f11a05.tar.gz cgit-92908af4558d7362c7deeb05254343a5a5f11a05.tar.bz2 |
Move cgit_version from shared.c to cgit.c
With the matching Makefile change, this makes sure that only cgit.o and cgit
proper needs to be rebuildt when VERSION has been modified.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cgit.c | 2 |
1 files changed, 2 insertions, 0 deletions
@@ -1,453 +1,455 @@ | |||
1 | /* cgit.c: cgi for the git scm | 1 | /* cgit.c: cgi for the git scm |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cmd.h" | 10 | #include "cmd.h" |
11 | #include "ui-shared.h" | 11 | #include "ui-shared.h" |
12 | 12 | ||
13 | const char *cgit_version = CGIT_VERSION; | ||
14 | |||
13 | void config_cb(const char *name, const char *value) | 15 | void config_cb(const char *name, const char *value) |
14 | { | 16 | { |
15 | if (!strcmp(name, "root-title")) | 17 | if (!strcmp(name, "root-title")) |
16 | ctx.cfg.root_title = xstrdup(value); | 18 | ctx.cfg.root_title = xstrdup(value); |
17 | else if (!strcmp(name, "css")) | 19 | else if (!strcmp(name, "css")) |
18 | ctx.cfg.css = xstrdup(value); | 20 | ctx.cfg.css = xstrdup(value); |
19 | else if (!strcmp(name, "logo")) | 21 | else if (!strcmp(name, "logo")) |
20 | ctx.cfg.logo = xstrdup(value); | 22 | ctx.cfg.logo = xstrdup(value); |
21 | else if (!strcmp(name, "index-header")) | 23 | else if (!strcmp(name, "index-header")) |
22 | ctx.cfg.index_header = xstrdup(value); | 24 | ctx.cfg.index_header = xstrdup(value); |
23 | else if (!strcmp(name, "index-info")) | 25 | else if (!strcmp(name, "index-info")) |
24 | ctx.cfg.index_info = xstrdup(value); | 26 | ctx.cfg.index_info = xstrdup(value); |
25 | else if (!strcmp(name, "logo-link")) | 27 | else if (!strcmp(name, "logo-link")) |
26 | ctx.cfg.logo_link = xstrdup(value); | 28 | ctx.cfg.logo_link = xstrdup(value); |
27 | else if (!strcmp(name, "module-link")) | 29 | else if (!strcmp(name, "module-link")) |
28 | ctx.cfg.module_link = xstrdup(value); | 30 | ctx.cfg.module_link = xstrdup(value); |
29 | else if (!strcmp(name, "virtual-root")) { | 31 | else if (!strcmp(name, "virtual-root")) { |
30 | ctx.cfg.virtual_root = trim_end(value, '/'); | 32 | ctx.cfg.virtual_root = trim_end(value, '/'); |
31 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | 33 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
32 | ctx.cfg.virtual_root = ""; | 34 | ctx.cfg.virtual_root = ""; |
33 | } else if (!strcmp(name, "nocache")) | 35 | } else if (!strcmp(name, "nocache")) |
34 | ctx.cfg.nocache = atoi(value); | 36 | ctx.cfg.nocache = atoi(value); |
35 | else if (!strcmp(name, "snapshots")) | 37 | else if (!strcmp(name, "snapshots")) |
36 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | 38 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
37 | else if (!strcmp(name, "enable-index-links")) | 39 | else if (!strcmp(name, "enable-index-links")) |
38 | ctx.cfg.enable_index_links = atoi(value); | 40 | ctx.cfg.enable_index_links = atoi(value); |
39 | else if (!strcmp(name, "enable-log-filecount")) | 41 | else if (!strcmp(name, "enable-log-filecount")) |
40 | ctx.cfg.enable_log_filecount = atoi(value); | 42 | ctx.cfg.enable_log_filecount = atoi(value); |
41 | else if (!strcmp(name, "enable-log-linecount")) | 43 | else if (!strcmp(name, "enable-log-linecount")) |
42 | ctx.cfg.enable_log_linecount = atoi(value); | 44 | ctx.cfg.enable_log_linecount = atoi(value); |
43 | else if (!strcmp(name, "cache-root")) | 45 | else if (!strcmp(name, "cache-root")) |
44 | ctx.cfg.cache_root = xstrdup(value); | 46 | ctx.cfg.cache_root = xstrdup(value); |
45 | else if (!strcmp(name, "cache-root-ttl")) | 47 | else if (!strcmp(name, "cache-root-ttl")) |
46 | ctx.cfg.cache_root_ttl = atoi(value); | 48 | ctx.cfg.cache_root_ttl = atoi(value); |
47 | else if (!strcmp(name, "cache-repo-ttl")) | 49 | else if (!strcmp(name, "cache-repo-ttl")) |
48 | ctx.cfg.cache_repo_ttl = atoi(value); | 50 | ctx.cfg.cache_repo_ttl = atoi(value); |
49 | else if (!strcmp(name, "cache-static-ttl")) | 51 | else if (!strcmp(name, "cache-static-ttl")) |
50 | ctx.cfg.cache_static_ttl = atoi(value); | 52 | ctx.cfg.cache_static_ttl = atoi(value); |
51 | else if (!strcmp(name, "cache-dynamic-ttl")) | 53 | else if (!strcmp(name, "cache-dynamic-ttl")) |
52 | ctx.cfg.cache_dynamic_ttl = atoi(value); | 54 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
53 | else if (!strcmp(name, "max-message-length")) | 55 | else if (!strcmp(name, "max-message-length")) |
54 | ctx.cfg.max_msg_len = atoi(value); | 56 | ctx.cfg.max_msg_len = atoi(value); |
55 | else if (!strcmp(name, "max-repodesc-length")) | 57 | else if (!strcmp(name, "max-repodesc-length")) |
56 | ctx.cfg.max_repodesc_len = atoi(value); | 58 | ctx.cfg.max_repodesc_len = atoi(value); |
57 | else if (!strcmp(name, "max-commit-count")) | 59 | else if (!strcmp(name, "max-commit-count")) |
58 | ctx.cfg.max_commit_count = atoi(value); | 60 | ctx.cfg.max_commit_count = atoi(value); |
59 | else if (!strcmp(name, "summary-log")) | 61 | else if (!strcmp(name, "summary-log")) |
60 | ctx.cfg.summary_log = atoi(value); | 62 | ctx.cfg.summary_log = atoi(value); |
61 | else if (!strcmp(name, "summary-branches")) | 63 | else if (!strcmp(name, "summary-branches")) |
62 | ctx.cfg.summary_branches = atoi(value); | 64 | ctx.cfg.summary_branches = atoi(value); |
63 | else if (!strcmp(name, "summary-tags")) | 65 | else if (!strcmp(name, "summary-tags")) |
64 | ctx.cfg.summary_tags = atoi(value); | 66 | ctx.cfg.summary_tags = atoi(value); |
65 | else if (!strcmp(name, "agefile")) | 67 | else if (!strcmp(name, "agefile")) |
66 | ctx.cfg.agefile = xstrdup(value); | 68 | ctx.cfg.agefile = xstrdup(value); |
67 | else if (!strcmp(name, "renamelimit")) | 69 | else if (!strcmp(name, "renamelimit")) |
68 | ctx.cfg.renamelimit = atoi(value); | 70 | ctx.cfg.renamelimit = atoi(value); |
69 | else if (!strcmp(name, "robots")) | 71 | else if (!strcmp(name, "robots")) |
70 | ctx.cfg.robots = xstrdup(value); | 72 | ctx.cfg.robots = xstrdup(value); |
71 | else if (!strcmp(name, "clone-prefix")) | 73 | else if (!strcmp(name, "clone-prefix")) |
72 | ctx.cfg.clone_prefix = xstrdup(value); | 74 | ctx.cfg.clone_prefix = xstrdup(value); |
73 | else if (!strcmp(name, "repo.group")) | 75 | else if (!strcmp(name, "repo.group")) |
74 | ctx.cfg.repo_group = xstrdup(value); | 76 | ctx.cfg.repo_group = xstrdup(value); |
75 | else if (!strcmp(name, "repo.url")) | 77 | else if (!strcmp(name, "repo.url")) |
76 | ctx.repo = cgit_add_repo(value); | 78 | ctx.repo = cgit_add_repo(value); |
77 | else if (!strcmp(name, "repo.name")) | 79 | else if (!strcmp(name, "repo.name")) |
78 | ctx.repo->name = xstrdup(value); | 80 | ctx.repo->name = xstrdup(value); |
79 | else if (ctx.repo && !strcmp(name, "repo.path")) | 81 | else if (ctx.repo && !strcmp(name, "repo.path")) |
80 | ctx.repo->path = trim_end(value, '/'); | 82 | ctx.repo->path = trim_end(value, '/'); |
81 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | 83 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
82 | ctx.repo->clone_url = xstrdup(value); | 84 | ctx.repo->clone_url = xstrdup(value); |
83 | else if (ctx.repo && !strcmp(name, "repo.desc")) | 85 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
84 | ctx.repo->desc = xstrdup(value); | 86 | ctx.repo->desc = xstrdup(value); |
85 | else if (ctx.repo && !strcmp(name, "repo.owner")) | 87 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
86 | ctx.repo->owner = xstrdup(value); | 88 | ctx.repo->owner = xstrdup(value); |
87 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | 89 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
88 | ctx.repo->defbranch = xstrdup(value); | 90 | ctx.repo->defbranch = xstrdup(value); |
89 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | 91 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
90 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 92 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
91 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | 93 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
92 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | 94 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
93 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | 95 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
94 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | 96 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
95 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | 97 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
96 | ctx.repo->module_link= xstrdup(value); | 98 | ctx.repo->module_link= xstrdup(value); |
97 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | 99 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
98 | if (*value == '/') | 100 | if (*value == '/') |
99 | ctx.repo->readme = xstrdup(value); | 101 | ctx.repo->readme = xstrdup(value); |
100 | else | 102 | else |
101 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | 103 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
102 | } else if (!strcmp(name, "include")) | 104 | } else if (!strcmp(name, "include")) |
103 | cgit_read_config(value, config_cb); | 105 | cgit_read_config(value, config_cb); |
104 | } | 106 | } |
105 | 107 | ||
106 | static void querystring_cb(const char *name, const char *value) | 108 | static void querystring_cb(const char *name, const char *value) |
107 | { | 109 | { |
108 | if (!strcmp(name,"r")) { | 110 | if (!strcmp(name,"r")) { |
109 | ctx.qry.repo = xstrdup(value); | 111 | ctx.qry.repo = xstrdup(value); |
110 | ctx.repo = cgit_get_repoinfo(value); | 112 | ctx.repo = cgit_get_repoinfo(value); |
111 | } else if (!strcmp(name, "p")) { | 113 | } else if (!strcmp(name, "p")) { |
112 | ctx.qry.page = xstrdup(value); | 114 | ctx.qry.page = xstrdup(value); |
113 | } else if (!strcmp(name, "url")) { | 115 | } else if (!strcmp(name, "url")) { |
114 | cgit_parse_url(value); | 116 | cgit_parse_url(value); |
115 | } else if (!strcmp(name, "qt")) { | 117 | } else if (!strcmp(name, "qt")) { |
116 | ctx.qry.grep = xstrdup(value); | 118 | ctx.qry.grep = xstrdup(value); |
117 | } else if (!strcmp(name, "q")) { | 119 | } else if (!strcmp(name, "q")) { |
118 | ctx.qry.search = xstrdup(value); | 120 | ctx.qry.search = xstrdup(value); |
119 | } else if (!strcmp(name, "h")) { | 121 | } else if (!strcmp(name, "h")) { |
120 | ctx.qry.head = xstrdup(value); | 122 | ctx.qry.head = xstrdup(value); |
121 | ctx.qry.has_symref = 1; | 123 | ctx.qry.has_symref = 1; |
122 | } else if (!strcmp(name, "id")) { | 124 | } else if (!strcmp(name, "id")) { |
123 | ctx.qry.sha1 = xstrdup(value); | 125 | ctx.qry.sha1 = xstrdup(value); |
124 | ctx.qry.has_sha1 = 1; | 126 | ctx.qry.has_sha1 = 1; |
125 | } else if (!strcmp(name, "id2")) { | 127 | } else if (!strcmp(name, "id2")) { |
126 | ctx.qry.sha2 = xstrdup(value); | 128 | ctx.qry.sha2 = xstrdup(value); |
127 | ctx.qry.has_sha1 = 1; | 129 | ctx.qry.has_sha1 = 1; |
128 | } else if (!strcmp(name, "ofs")) { | 130 | } else if (!strcmp(name, "ofs")) { |
129 | ctx.qry.ofs = atoi(value); | 131 | ctx.qry.ofs = atoi(value); |
130 | } else if (!strcmp(name, "path")) { | 132 | } else if (!strcmp(name, "path")) { |
131 | ctx.qry.path = trim_end(value, '/'); | 133 | ctx.qry.path = trim_end(value, '/'); |
132 | } else if (!strcmp(name, "name")) { | 134 | } else if (!strcmp(name, "name")) { |
133 | ctx.qry.name = xstrdup(value); | 135 | ctx.qry.name = xstrdup(value); |
134 | } | 136 | } |
135 | } | 137 | } |
136 | 138 | ||
137 | static void prepare_context(struct cgit_context *ctx) | 139 | static void prepare_context(struct cgit_context *ctx) |
138 | { | 140 | { |
139 | memset(ctx, 0, sizeof(ctx)); | 141 | memset(ctx, 0, sizeof(ctx)); |
140 | ctx->cfg.agefile = "info/web/last-modified"; | 142 | ctx->cfg.agefile = "info/web/last-modified"; |
141 | ctx->cfg.cache_dynamic_ttl = 5; | 143 | ctx->cfg.cache_dynamic_ttl = 5; |
142 | ctx->cfg.cache_max_create_time = 5; | 144 | ctx->cfg.cache_max_create_time = 5; |
143 | ctx->cfg.cache_repo_ttl = 5; | 145 | ctx->cfg.cache_repo_ttl = 5; |
144 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 146 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
145 | ctx->cfg.cache_root_ttl = 5; | 147 | ctx->cfg.cache_root_ttl = 5; |
146 | ctx->cfg.cache_static_ttl = -1; | 148 | ctx->cfg.cache_static_ttl = -1; |
147 | ctx->cfg.css = "/cgit.css"; | 149 | ctx->cfg.css = "/cgit.css"; |
148 | ctx->cfg.logo = "/git-logo.png"; | 150 | ctx->cfg.logo = "/git-logo.png"; |
149 | ctx->cfg.max_commit_count = 50; | 151 | ctx->cfg.max_commit_count = 50; |
150 | ctx->cfg.max_lock_attempts = 5; | 152 | ctx->cfg.max_lock_attempts = 5; |
151 | ctx->cfg.max_msg_len = 60; | 153 | ctx->cfg.max_msg_len = 60; |
152 | ctx->cfg.max_repodesc_len = 60; | 154 | ctx->cfg.max_repodesc_len = 60; |
153 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 155 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
154 | ctx->cfg.renamelimit = -1; | 156 | ctx->cfg.renamelimit = -1; |
155 | ctx->cfg.robots = "index, nofollow"; | 157 | ctx->cfg.robots = "index, nofollow"; |
156 | ctx->cfg.root_title = "Git repository browser"; | 158 | ctx->cfg.root_title = "Git repository browser"; |
157 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 159 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
158 | ctx->page.mimetype = "text/html"; | 160 | ctx->page.mimetype = "text/html"; |
159 | ctx->page.charset = PAGE_ENCODING; | 161 | ctx->page.charset = PAGE_ENCODING; |
160 | ctx->page.filename = NULL; | 162 | ctx->page.filename = NULL; |
161 | } | 163 | } |
162 | 164 | ||
163 | static int cgit_prepare_cache(struct cacheitem *item) | 165 | static int cgit_prepare_cache(struct cacheitem *item) |
164 | { | 166 | { |
165 | if (!ctx.repo && ctx.qry.repo) { | 167 | if (!ctx.repo && ctx.qry.repo) { |
166 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | 168 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
167 | "Bad request"); | 169 | "Bad request"); |
168 | cgit_print_http_headers(&ctx); | 170 | cgit_print_http_headers(&ctx); |
169 | cgit_print_docstart(&ctx); | 171 | cgit_print_docstart(&ctx); |
170 | cgit_print_pageheader(&ctx); | 172 | cgit_print_pageheader(&ctx); |
171 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 173 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
172 | cgit_print_docend(); | 174 | cgit_print_docend(); |
173 | return 0; | 175 | return 0; |
174 | } | 176 | } |
175 | 177 | ||
176 | if (!ctx.repo) { | 178 | if (!ctx.repo) { |
177 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); | 179 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
178 | item->ttl = ctx.cfg.cache_root_ttl; | 180 | item->ttl = ctx.cfg.cache_root_ttl; |
179 | return 1; | 181 | return 1; |
180 | } | 182 | } |
181 | 183 | ||
182 | if (!cgit_cmd) { | 184 | if (!cgit_cmd) { |
183 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | 185 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
184 | cache_safe_filename(ctx.repo->url), | 186 | cache_safe_filename(ctx.repo->url), |
185 | cache_safe_filename(ctx.qry.raw))); | 187 | cache_safe_filename(ctx.qry.raw))); |
186 | item->ttl = ctx.cfg.cache_repo_ttl; | 188 | item->ttl = ctx.cfg.cache_repo_ttl; |
187 | } else { | 189 | } else { |
188 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | 190 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
189 | cache_safe_filename(ctx.repo->url), | 191 | cache_safe_filename(ctx.repo->url), |
190 | ctx.qry.page, | 192 | ctx.qry.page, |
191 | cache_safe_filename(ctx.qry.raw))); | 193 | cache_safe_filename(ctx.qry.raw))); |
192 | if (ctx.qry.has_symref) | 194 | if (ctx.qry.has_symref) |
193 | item->ttl = ctx.cfg.cache_dynamic_ttl; | 195 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
194 | else if (ctx.qry.has_sha1) | 196 | else if (ctx.qry.has_sha1) |
195 | item->ttl = ctx.cfg.cache_static_ttl; | 197 | item->ttl = ctx.cfg.cache_static_ttl; |
196 | else | 198 | else |
197 | item->ttl = ctx.cfg.cache_repo_ttl; | 199 | item->ttl = ctx.cfg.cache_repo_ttl; |
198 | } | 200 | } |
199 | return 1; | 201 | return 1; |
200 | } | 202 | } |
201 | 203 | ||
202 | struct refmatch { | 204 | struct refmatch { |
203 | char *req_ref; | 205 | char *req_ref; |
204 | char *first_ref; | 206 | char *first_ref; |
205 | int match; | 207 | int match; |
206 | }; | 208 | }; |
207 | 209 | ||
208 | int find_current_ref(const char *refname, const unsigned char *sha1, | 210 | int find_current_ref(const char *refname, const unsigned char *sha1, |
209 | int flags, void *cb_data) | 211 | int flags, void *cb_data) |
210 | { | 212 | { |
211 | struct refmatch *info; | 213 | struct refmatch *info; |
212 | 214 | ||
213 | info = (struct refmatch *)cb_data; | 215 | info = (struct refmatch *)cb_data; |
214 | if (!strcmp(refname, info->req_ref)) | 216 | if (!strcmp(refname, info->req_ref)) |
215 | info->match = 1; | 217 | info->match = 1; |
216 | if (!info->first_ref) | 218 | if (!info->first_ref) |
217 | info->first_ref = xstrdup(refname); | 219 | info->first_ref = xstrdup(refname); |
218 | return info->match; | 220 | return info->match; |
219 | } | 221 | } |
220 | 222 | ||
221 | char *find_default_branch(struct cgit_repo *repo) | 223 | char *find_default_branch(struct cgit_repo *repo) |
222 | { | 224 | { |
223 | struct refmatch info; | 225 | struct refmatch info; |
224 | 226 | ||
225 | info.req_ref = repo->defbranch; | 227 | info.req_ref = repo->defbranch; |
226 | info.first_ref = NULL; | 228 | info.first_ref = NULL; |
227 | info.match = 0; | 229 | info.match = 0; |
228 | for_each_branch_ref(find_current_ref, &info); | 230 | for_each_branch_ref(find_current_ref, &info); |
229 | if (info.match) | 231 | if (info.match) |
230 | return info.req_ref; | 232 | return info.req_ref; |
231 | else | 233 | else |
232 | return info.first_ref; | 234 | return info.first_ref; |
233 | } | 235 | } |
234 | 236 | ||
235 | static int prepare_repo_cmd(struct cgit_context *ctx) | 237 | static int prepare_repo_cmd(struct cgit_context *ctx) |
236 | { | 238 | { |
237 | char *tmp; | 239 | char *tmp; |
238 | unsigned char sha1[20]; | 240 | unsigned char sha1[20]; |
239 | int nongit = 0; | 241 | int nongit = 0; |
240 | 242 | ||
241 | setenv("GIT_DIR", ctx->repo->path, 1); | 243 | setenv("GIT_DIR", ctx->repo->path, 1); |
242 | setup_git_directory_gently(&nongit); | 244 | setup_git_directory_gently(&nongit); |
243 | if (nongit) { | 245 | if (nongit) { |
244 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 246 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
245 | "config error"); | 247 | "config error"); |
246 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | 248 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
247 | ctx->repo = NULL; | 249 | ctx->repo = NULL; |
248 | cgit_print_http_headers(ctx); | 250 | cgit_print_http_headers(ctx); |
249 | cgit_print_docstart(ctx); | 251 | cgit_print_docstart(ctx); |
250 | cgit_print_pageheader(ctx); | 252 | cgit_print_pageheader(ctx); |
251 | cgit_print_error(tmp); | 253 | cgit_print_error(tmp); |
252 | cgit_print_docend(); | 254 | cgit_print_docend(); |
253 | return 1; | 255 | return 1; |
254 | } | 256 | } |
255 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 257 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
256 | 258 | ||
257 | if (!ctx->qry.head) { | 259 | if (!ctx->qry.head) { |
258 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); | 260 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
259 | ctx->repo->defbranch = ctx->qry.head; | 261 | ctx->repo->defbranch = ctx->qry.head; |
260 | } | 262 | } |
261 | 263 | ||
262 | if (!ctx->qry.head) { | 264 | if (!ctx->qry.head) { |
263 | cgit_print_http_headers(ctx); | 265 | cgit_print_http_headers(ctx); |
264 | cgit_print_docstart(ctx); | 266 | cgit_print_docstart(ctx); |
265 | cgit_print_pageheader(ctx); | 267 | cgit_print_pageheader(ctx); |
266 | cgit_print_error("Repository seems to be empty"); | 268 | cgit_print_error("Repository seems to be empty"); |
267 | cgit_print_docend(); | 269 | cgit_print_docend(); |
268 | return 1; | 270 | return 1; |
269 | } | 271 | } |
270 | 272 | ||
271 | if (get_sha1(ctx->qry.head, sha1)) { | 273 | if (get_sha1(ctx->qry.head, sha1)) { |
272 | tmp = xstrdup(ctx->qry.head); | 274 | tmp = xstrdup(ctx->qry.head); |
273 | ctx->qry.head = ctx->repo->defbranch; | 275 | ctx->qry.head = ctx->repo->defbranch; |
274 | cgit_print_http_headers(ctx); | 276 | cgit_print_http_headers(ctx); |
275 | cgit_print_docstart(ctx); | 277 | cgit_print_docstart(ctx); |
276 | cgit_print_pageheader(ctx); | 278 | cgit_print_pageheader(ctx); |
277 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 279 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
278 | cgit_print_docend(); | 280 | cgit_print_docend(); |
279 | return 1; | 281 | return 1; |
280 | } | 282 | } |
281 | return 0; | 283 | return 0; |
282 | } | 284 | } |
283 | 285 | ||
284 | static void process_request(struct cgit_context *ctx) | 286 | static void process_request(struct cgit_context *ctx) |
285 | { | 287 | { |
286 | struct cgit_cmd *cmd; | 288 | struct cgit_cmd *cmd; |
287 | 289 | ||
288 | cmd = cgit_get_cmd(ctx); | 290 | cmd = cgit_get_cmd(ctx); |
289 | if (!cmd) { | 291 | if (!cmd) { |
290 | ctx->page.title = "cgit error"; | 292 | ctx->page.title = "cgit error"; |
291 | ctx->repo = NULL; | 293 | ctx->repo = NULL; |
292 | cgit_print_http_headers(ctx); | 294 | cgit_print_http_headers(ctx); |
293 | cgit_print_docstart(ctx); | 295 | cgit_print_docstart(ctx); |
294 | cgit_print_pageheader(ctx); | 296 | cgit_print_pageheader(ctx); |
295 | cgit_print_error("Invalid request"); | 297 | cgit_print_error("Invalid request"); |
296 | cgit_print_docend(); | 298 | cgit_print_docend(); |
297 | return; | 299 | return; |
298 | } | 300 | } |
299 | 301 | ||
300 | if (cmd->want_repo && prepare_repo_cmd(ctx)) | 302 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
301 | return; | 303 | return; |
302 | 304 | ||
303 | if (cmd->want_layout) { | 305 | if (cmd->want_layout) { |
304 | cgit_print_http_headers(ctx); | 306 | cgit_print_http_headers(ctx); |
305 | cgit_print_docstart(ctx); | 307 | cgit_print_docstart(ctx); |
306 | cgit_print_pageheader(ctx); | 308 | cgit_print_pageheader(ctx); |
307 | } | 309 | } |
308 | 310 | ||
309 | cmd->fn(ctx); | 311 | cmd->fn(ctx); |
310 | 312 | ||
311 | if (cmd->want_layout) | 313 | if (cmd->want_layout) |
312 | cgit_print_docend(); | 314 | cgit_print_docend(); |
313 | } | 315 | } |
314 | 316 | ||
315 | static long ttl_seconds(long ttl) | 317 | static long ttl_seconds(long ttl) |
316 | { | 318 | { |
317 | if (ttl<0) | 319 | if (ttl<0) |
318 | return 60 * 60 * 24 * 365; | 320 | return 60 * 60 * 24 * 365; |
319 | else | 321 | else |
320 | return ttl * 60; | 322 | return ttl * 60; |
321 | } | 323 | } |
322 | 324 | ||
323 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 325 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
324 | { | 326 | { |
325 | int stdout2; | 327 | int stdout2; |
326 | 328 | ||
327 | if (use_cache) { | 329 | if (use_cache) { |
328 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 330 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
329 | "Preserving STDOUT"); | 331 | "Preserving STDOUT"); |
330 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 332 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
331 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 333 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
332 | } | 334 | } |
333 | 335 | ||
334 | ctx.page.modified = time(NULL); | 336 | ctx.page.modified = time(NULL); |
335 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | 337 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
336 | process_request(&ctx); | 338 | process_request(&ctx); |
337 | 339 | ||
338 | if (use_cache) { | 340 | if (use_cache) { |
339 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 341 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
340 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 342 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
341 | "Restoring original STDOUT"); | 343 | "Restoring original STDOUT"); |
342 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 344 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
343 | } | 345 | } |
344 | } | 346 | } |
345 | 347 | ||
346 | static void cgit_check_cache(struct cacheitem *item) | 348 | static void cgit_check_cache(struct cacheitem *item) |
347 | { | 349 | { |
348 | int i = 0; | 350 | int i = 0; |
349 | 351 | ||
350 | top: | 352 | top: |
351 | if (++i > ctx.cfg.max_lock_attempts) { | 353 | if (++i > ctx.cfg.max_lock_attempts) { |
352 | die("cgit_refresh_cache: unable to lock %s: %s", | 354 | die("cgit_refresh_cache: unable to lock %s: %s", |
353 | item->name, strerror(errno)); | 355 | item->name, strerror(errno)); |
354 | } | 356 | } |
355 | if (!cache_exist(item)) { | 357 | if (!cache_exist(item)) { |
356 | if (!cache_lock(item)) { | 358 | if (!cache_lock(item)) { |
357 | sleep(1); | 359 | sleep(1); |
358 | goto top; | 360 | goto top; |
359 | } | 361 | } |
360 | if (!cache_exist(item)) { | 362 | if (!cache_exist(item)) { |
361 | cgit_fill_cache(item, 1); | 363 | cgit_fill_cache(item, 1); |
362 | cache_unlock(item); | 364 | cache_unlock(item); |
363 | } else { | 365 | } else { |
364 | cache_cancel_lock(item); | 366 | cache_cancel_lock(item); |
365 | } | 367 | } |
366 | } else if (cache_expired(item) && cache_lock(item)) { | 368 | } else if (cache_expired(item) && cache_lock(item)) { |
367 | if (cache_expired(item)) { | 369 | if (cache_expired(item)) { |
368 | cgit_fill_cache(item, 1); | 370 | cgit_fill_cache(item, 1); |
369 | cache_unlock(item); | 371 | cache_unlock(item); |
370 | } else { | 372 | } else { |
371 | cache_cancel_lock(item); | 373 | cache_cancel_lock(item); |
372 | } | 374 | } |
373 | } | 375 | } |
374 | } | 376 | } |
375 | 377 | ||
376 | static void cgit_print_cache(struct cacheitem *item) | 378 | static void cgit_print_cache(struct cacheitem *item) |
377 | { | 379 | { |
378 | static char buf[4096]; | 380 | static char buf[4096]; |
379 | ssize_t i; | 381 | ssize_t i; |
380 | 382 | ||
381 | int fd = open(item->name, O_RDONLY); | 383 | int fd = open(item->name, O_RDONLY); |
382 | if (fd<0) | 384 | if (fd<0) |
383 | die("Unable to open cached file %s", item->name); | 385 | die("Unable to open cached file %s", item->name); |
384 | 386 | ||
385 | while((i=read(fd, buf, sizeof(buf))) > 0) | 387 | while((i=read(fd, buf, sizeof(buf))) > 0) |
386 | write(STDOUT_FILENO, buf, i); | 388 | write(STDOUT_FILENO, buf, i); |
387 | 389 | ||
388 | close(fd); | 390 | close(fd); |
389 | } | 391 | } |
390 | 392 | ||
391 | static void cgit_parse_args(int argc, const char **argv) | 393 | static void cgit_parse_args(int argc, const char **argv) |
392 | { | 394 | { |
393 | int i; | 395 | int i; |
394 | 396 | ||
395 | for (i = 1; i < argc; i++) { | 397 | for (i = 1; i < argc; i++) { |
396 | if (!strncmp(argv[i], "--cache=", 8)) { | 398 | if (!strncmp(argv[i], "--cache=", 8)) { |
397 | ctx.cfg.cache_root = xstrdup(argv[i]+8); | 399 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
398 | } | 400 | } |
399 | if (!strcmp(argv[i], "--nocache")) { | 401 | if (!strcmp(argv[i], "--nocache")) { |
400 | ctx.cfg.nocache = 1; | 402 | ctx.cfg.nocache = 1; |
401 | } | 403 | } |
402 | if (!strncmp(argv[i], "--query=", 8)) { | 404 | if (!strncmp(argv[i], "--query=", 8)) { |
403 | ctx.qry.raw = xstrdup(argv[i]+8); | 405 | ctx.qry.raw = xstrdup(argv[i]+8); |
404 | } | 406 | } |
405 | if (!strncmp(argv[i], "--repo=", 7)) { | 407 | if (!strncmp(argv[i], "--repo=", 7)) { |
406 | ctx.qry.repo = xstrdup(argv[i]+7); | 408 | ctx.qry.repo = xstrdup(argv[i]+7); |
407 | } | 409 | } |
408 | if (!strncmp(argv[i], "--page=", 7)) { | 410 | if (!strncmp(argv[i], "--page=", 7)) { |
409 | ctx.qry.page = xstrdup(argv[i]+7); | 411 | ctx.qry.page = xstrdup(argv[i]+7); |
410 | } | 412 | } |
411 | if (!strncmp(argv[i], "--head=", 7)) { | 413 | if (!strncmp(argv[i], "--head=", 7)) { |
412 | ctx.qry.head = xstrdup(argv[i]+7); | 414 | ctx.qry.head = xstrdup(argv[i]+7); |
413 | ctx.qry.has_symref = 1; | 415 | ctx.qry.has_symref = 1; |
414 | } | 416 | } |
415 | if (!strncmp(argv[i], "--sha1=", 7)) { | 417 | if (!strncmp(argv[i], "--sha1=", 7)) { |
416 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 418 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
417 | ctx.qry.has_sha1 = 1; | 419 | ctx.qry.has_sha1 = 1; |
418 | } | 420 | } |
419 | if (!strncmp(argv[i], "--ofs=", 6)) { | 421 | if (!strncmp(argv[i], "--ofs=", 6)) { |
420 | ctx.qry.ofs = atoi(argv[i]+6); | 422 | ctx.qry.ofs = atoi(argv[i]+6); |
421 | } | 423 | } |
422 | } | 424 | } |
423 | } | 425 | } |
424 | 426 | ||
425 | int main(int argc, const char **argv) | 427 | int main(int argc, const char **argv) |
426 | { | 428 | { |
427 | struct cacheitem item; | 429 | struct cacheitem item; |
428 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 430 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
429 | 431 | ||
430 | prepare_context(&ctx); | 432 | prepare_context(&ctx); |
431 | item.st.st_mtime = time(NULL); | 433 | item.st.st_mtime = time(NULL); |
432 | cgit_repolist.length = 0; | 434 | cgit_repolist.length = 0; |
433 | cgit_repolist.count = 0; | 435 | cgit_repolist.count = 0; |
434 | cgit_repolist.repos = NULL; | 436 | cgit_repolist.repos = NULL; |
435 | 437 | ||
436 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 438 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
437 | config_cb); | 439 | config_cb); |
438 | if (getenv("SCRIPT_NAME")) | 440 | if (getenv("SCRIPT_NAME")) |
439 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); | 441 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
440 | if (getenv("QUERY_STRING")) | 442 | if (getenv("QUERY_STRING")) |
441 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 443 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
442 | cgit_parse_args(argc, argv); | 444 | cgit_parse_args(argc, argv); |
443 | cgit_parse_query(ctx.qry.raw, querystring_cb); | 445 | cgit_parse_query(ctx.qry.raw, querystring_cb); |
444 | if (!cgit_prepare_cache(&item)) | 446 | if (!cgit_prepare_cache(&item)) |
445 | return 0; | 447 | return 0; |
446 | if (ctx.cfg.nocache) { | 448 | if (ctx.cfg.nocache) { |
447 | cgit_fill_cache(&item, 0); | 449 | cgit_fill_cache(&item, 0); |
448 | } else { | 450 | } else { |
449 | cgit_check_cache(&item); | 451 | cgit_check_cache(&item); |
450 | cgit_print_cache(&item); | 452 | cgit_print_cache(&item); |
451 | } | 453 | } |
452 | return 0; | 454 | return 0; |
453 | } | 455 | } |