shared.c (view raw)
1/* shared.c: global vars + some callback functions
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10
11struct repolist cgit_repolist;
12struct repoinfo *cgit_repo;
13int cgit_cmd;
14
15const char *cgit_version = CGIT_VERSION;
16
17char *cgit_root_title = "Git repository browser";
18char *cgit_css = "/cgit.css";
19char *cgit_logo = "/git-logo.png";
20char *cgit_index_header = NULL;
21char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
22char *cgit_module_link = "./?repo=%s&page=commit&id=%s";
23char *cgit_agefile = "info/web/last-modified";
24char *cgit_virtual_root = NULL;
25char *cgit_script_name = CGIT_SCRIPT_NAME;
26char *cgit_cache_root = "/var/cache/cgit";
27char *cgit_repo_group = NULL;
28
29int cgit_nocache = 0;
30int cgit_snapshots = 0;
31int cgit_enable_log_filecount = 0;
32int cgit_enable_log_linecount = 0;
33int cgit_max_lock_attempts = 5;
34int cgit_cache_root_ttl = 5;
35int cgit_cache_repo_ttl = 5;
36int cgit_cache_dynamic_ttl = 5;
37int cgit_cache_static_ttl = -1;
38int cgit_cache_max_create_time = 5;
39int cgit_summary_log = 0;
40
41int cgit_max_msg_len = 60;
42int cgit_max_repodesc_len = 60;
43int cgit_max_commit_count = 50;
44
45int cgit_query_has_symref = 0;
46int cgit_query_has_sha1 = 0;
47
48char *cgit_querystring = NULL;
49char *cgit_query_repo = NULL;
50char *cgit_query_page = NULL;
51char *cgit_query_head = NULL;
52char *cgit_query_search = NULL;
53char *cgit_query_sha1 = NULL;
54char *cgit_query_sha2 = NULL;
55char *cgit_query_path = NULL;
56char *cgit_query_name = NULL;
57int cgit_query_ofs = 0;
58
59int htmlfd = 0;
60
61
62int cgit_get_cmd_index(const char *cmd)
63{
64 static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
65 "snapshot", NULL};
66 int i;
67
68 for(i = 0; cmds[i]; i++)
69 if (!strcmp(cmd, cmds[i]))
70 return i + 1;
71 return 0;
72}
73
74int chk_zero(int result, char *msg)
75{
76 if (result != 0)
77 die("%s: %s", msg, strerror(errno));
78 return result;
79}
80
81int chk_positive(int result, char *msg)
82{
83 if (result <= 0)
84 die("%s: %s", msg, strerror(errno));
85 return result;
86}
87
88struct repoinfo *add_repo(const char *url)
89{
90 struct repoinfo *ret;
91
92 if (++cgit_repolist.count > cgit_repolist.length) {
93 if (cgit_repolist.length == 0)
94 cgit_repolist.length = 8;
95 else
96 cgit_repolist.length *= 2;
97 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
98 cgit_repolist.length *
99 sizeof(struct repoinfo));
100 }
101
102 ret = &cgit_repolist.repos[cgit_repolist.count-1];
103 ret->url = xstrdup(url);
104 ret->name = ret->url;
105 ret->path = NULL;
106 ret->desc = NULL;
107 ret->owner = NULL;
108 ret->group = cgit_repo_group;
109 ret->defbranch = "master";
110 ret->snapshots = cgit_snapshots;
111 ret->enable_log_filecount = cgit_enable_log_filecount;
112 ret->enable_log_linecount = cgit_enable_log_linecount;
113 ret->module_link = cgit_module_link;
114 ret->readme = NULL;
115 return ret;
116}
117
118struct repoinfo *cgit_get_repoinfo(const char *url)
119{
120 int i;
121 struct repoinfo *repo;
122
123 for (i=0; i<cgit_repolist.count; i++) {
124 repo = &cgit_repolist.repos[i];
125 if (!strcmp(repo->url, url))
126 return repo;
127 }
128 return NULL;
129}
130
131void cgit_global_config_cb(const char *name, const char *value)
132{
133 if (!strcmp(name, "root-title"))
134 cgit_root_title = xstrdup(value);
135 else if (!strcmp(name, "css"))
136 cgit_css = xstrdup(value);
137 else if (!strcmp(name, "logo"))
138 cgit_logo = xstrdup(value);
139 else if (!strcmp(name, "index-header"))
140 cgit_index_header = xstrdup(value);
141 else if (!strcmp(name, "logo-link"))
142 cgit_logo_link = xstrdup(value);
143 else if (!strcmp(name, "module-link"))
144 cgit_module_link = xstrdup(value);
145 else if (!strcmp(name, "virtual-root"))
146 cgit_virtual_root = xstrdup(value);
147 else if (!strcmp(name, "nocache"))
148 cgit_nocache = atoi(value);
149 else if (!strcmp(name, "snapshots"))
150 cgit_snapshots = atoi(value);
151 else if (!strcmp(name, "enable-log-filecount"))
152 cgit_enable_log_filecount = atoi(value);
153 else if (!strcmp(name, "enable-log-linecount"))
154 cgit_enable_log_linecount = atoi(value);
155 else if (!strcmp(name, "cache-root"))
156 cgit_cache_root = xstrdup(value);
157 else if (!strcmp(name, "cache-root-ttl"))
158 cgit_cache_root_ttl = atoi(value);
159 else if (!strcmp(name, "cache-repo-ttl"))
160 cgit_cache_repo_ttl = atoi(value);
161 else if (!strcmp(name, "cache-static-ttl"))
162 cgit_cache_static_ttl = atoi(value);
163 else if (!strcmp(name, "cache-dynamic-ttl"))
164 cgit_cache_dynamic_ttl = atoi(value);
165 else if (!strcmp(name, "max-message-length"))
166 cgit_max_msg_len = atoi(value);
167 else if (!strcmp(name, "max-repodesc-length"))
168 cgit_max_repodesc_len = atoi(value);
169 else if (!strcmp(name, "max-commit-count"))
170 cgit_max_commit_count = atoi(value);
171 else if (!strcmp(name, "summary-log"))
172 cgit_summary_log = atoi(value);
173 else if (!strcmp(name, "agefile"))
174 cgit_agefile = xstrdup(value);
175 else if (!strcmp(name, "repo.group"))
176 cgit_repo_group = xstrdup(value);
177 else if (!strcmp(name, "repo.url"))
178 cgit_repo = add_repo(value);
179 else if (!strcmp(name, "repo.name"))
180 cgit_repo->name = xstrdup(value);
181 else if (cgit_repo && !strcmp(name, "repo.path"))
182 cgit_repo->path = xstrdup(value);
183 else if (cgit_repo && !strcmp(name, "repo.desc"))
184 cgit_repo->desc = xstrdup(value);
185 else if (cgit_repo && !strcmp(name, "repo.owner"))
186 cgit_repo->owner = xstrdup(value);
187 else if (cgit_repo && !strcmp(name, "repo.defbranch"))
188 cgit_repo->defbranch = xstrdup(value);
189 else if (cgit_repo && !strcmp(name, "repo.snapshots"))
190 cgit_repo->snapshots = cgit_snapshots * atoi(value);
191 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount"))
192 cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value);
193 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount"))
194 cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value);
195 else if (cgit_repo && !strcmp(name, "repo.module-link"))
196 cgit_repo->module_link= xstrdup(value);
197 else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) {
198 if (*value == '/')
199 cgit_repo->readme = xstrdup(value);
200 else
201 cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value));
202 } else if (!strcmp(name, "include"))
203 cgit_read_config(value, cgit_global_config_cb);
204}
205
206void cgit_querystring_cb(const char *name, const char *value)
207{
208 if (!strcmp(name,"r")) {
209 cgit_query_repo = xstrdup(value);
210 cgit_repo = cgit_get_repoinfo(value);
211 } else if (!strcmp(name, "p")) {
212 cgit_query_page = xstrdup(value);
213 cgit_cmd = cgit_get_cmd_index(value);
214 } else if (!strcmp(name, "url")) {
215 cgit_parse_url(value);
216 } else if (!strcmp(name, "q")) {
217 cgit_query_search = xstrdup(value);
218 } else if (!strcmp(name, "h")) {
219 cgit_query_head = xstrdup(value);
220 cgit_query_has_symref = 1;
221 } else if (!strcmp(name, "id")) {
222 cgit_query_sha1 = xstrdup(value);
223 cgit_query_has_sha1 = 1;
224 } else if (!strcmp(name, "id2")) {
225 cgit_query_sha2 = xstrdup(value);
226 cgit_query_has_sha1 = 1;
227 } else if (!strcmp(name, "ofs")) {
228 cgit_query_ofs = atoi(value);
229 } else if (!strcmp(name, "path")) {
230 cgit_query_path = xstrdup(value);
231 } else if (!strcmp(name, "name")) {
232 cgit_query_name = xstrdup(value);
233 }
234}
235
236void *cgit_free_commitinfo(struct commitinfo *info)
237{
238 free(info->author);
239 free(info->author_email);
240 free(info->committer);
241 free(info->committer_email);
242 free(info->subject);
243 free(info);
244 return NULL;
245}
246
247int hextoint(char c)
248{
249 if (c >= 'a' && c <= 'f')
250 return 10 + c - 'a';
251 else if (c >= 'A' && c <= 'F')
252 return 10 + c - 'A';
253 else if (c >= '0' && c <= '9')
254 return c - '0';
255 else
256 return -1;
257}
258
259void cgit_diff_tree_cb(struct diff_queue_struct *q,
260 struct diff_options *options, void *data)
261{
262 int i;
263
264 for (i = 0; i < q->nr; i++) {
265 if (q->queue[i]->status == 'U')
266 continue;
267 ((filepair_fn)data)(q->queue[i]);
268 }
269}
270
271static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
272{
273 enum object_type type;
274
275 if (is_null_sha1(sha1)) {
276 file->ptr = (char *)"";
277 file->size = 0;
278 } else {
279 file->ptr = read_sha1_file(sha1, &type, &file->size);
280 }
281 return 1;
282}
283
284/*
285 * Receive diff-buffers from xdiff and concatenate them as
286 * needed across multiple callbacks.
287 *
288 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
289 * ripped from git and modified to use globals instead of
290 * a special callback-struct.
291 */
292char *diffbuf = NULL;
293int buflen = 0;
294
295int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
296{
297 int i;
298
299 for (i = 0; i < nbuf; i++) {
300 if (mb[i].ptr[mb[i].size-1] != '\n') {
301 /* Incomplete line */
302 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
303 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
304 buflen += mb[i].size;
305 continue;
306 }
307
308 /* we have a complete line */
309 if (!diffbuf) {
310 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
311 continue;
312 }
313 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
314 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
315 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
316 free(diffbuf);
317 diffbuf = NULL;
318 buflen = 0;
319 }
320 if (diffbuf) {
321 ((linediff_fn)priv)(diffbuf, buflen);
322 free(diffbuf);
323 diffbuf = NULL;
324 buflen = 0;
325 }
326 return 0;
327}
328
329int cgit_diff_files(const unsigned char *old_sha1,
330 const unsigned char *new_sha1,
331 linediff_fn fn)
332{
333 mmfile_t file1, file2;
334 xpparam_t diff_params;
335 xdemitconf_t emit_params;
336 xdemitcb_t emit_cb;
337
338 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
339 return 1;
340
341 diff_params.flags = XDF_NEED_MINIMAL;
342 emit_params.ctxlen = 3;
343 emit_params.flags = XDL_EMIT_FUNCNAMES;
344 emit_cb.outf = filediff_cb;
345 emit_cb.priv = fn;
346 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
347 return 0;
348}
349
350void cgit_diff_tree(const unsigned char *old_sha1,
351 const unsigned char *new_sha1,
352 filepair_fn fn)
353{
354 struct diff_options opt;
355 int ret;
356
357 diff_setup(&opt);
358 opt.output_format = DIFF_FORMAT_CALLBACK;
359 opt.detect_rename = 1;
360 opt.recursive = 1;
361 opt.format_callback = cgit_diff_tree_cb;
362 opt.format_callback_data = fn;
363 diff_setup_done(&opt);
364
365 if (old_sha1 && !is_null_sha1(old_sha1))
366 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
367 else
368 ret = diff_root_tree_sha1(new_sha1, "", &opt);
369 diffcore_std(&opt);
370 diff_flush(&opt);
371}
372
373void cgit_diff_commit(struct commit *commit, filepair_fn fn)
374{
375 unsigned char *old_sha1 = NULL;
376
377 if (commit->parents)
378 old_sha1 = commit->parents->item->object.sha1;
379 cgit_diff_tree(old_sha1, commit->object.sha1, fn);
380}