1 /* shared.c: global vars + some callback functions
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 struct repolist cgit_repolist;
12 struct repoinfo *cgit_repo;
13 struct cgit_context ctx;
16 const char *cgit_version = CGIT_VERSION;
20 void cgit_prepare_context(struct cgit_context *ctx)
22 memset(ctx, 0, sizeof(ctx));
23 ctx->cfg.agefile = "info/web/last-modified";
24 ctx->cfg.cache_dynamic_ttl = 5;
25 ctx->cfg.cache_max_create_time = 5;
26 ctx->cfg.cache_repo_ttl = 5;
27 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
28 ctx->cfg.cache_root_ttl = 5;
29 ctx->cfg.cache_static_ttl = -1;
30 ctx->cfg.css = "/cgit.css";
31 ctx->cfg.logo = "/git-logo.png";
32 ctx->cfg.max_commit_count = 50;
33 ctx->cfg.max_lock_attempts = 5;
34 ctx->cfg.max_msg_len = 60;
35 ctx->cfg.max_repodesc_len = 60;
36 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
37 ctx->cfg.renamelimit = -1;
38 ctx->cfg.robots = "index, nofollow";
39 ctx->cfg.root_title = "Git repository browser";
40 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
43 int cgit_get_cmd_index(const char *cmd)
45 static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
46 "snapshot", "tag", "refs", "patch", NULL};
49 for(i = 0; cmds[i]; i++)
50 if (!strcmp(cmd, cmds[i]))
55 int chk_zero(int result, char *msg)
58 die("%s: %s", msg, strerror(errno));
62 int chk_positive(int result, char *msg)
65 die("%s: %s", msg, strerror(errno));
69 int chk_non_negative(int result, char *msg)
72 die("%s: %s",msg, strerror(errno));
76 struct repoinfo *add_repo(const char *url)
80 if (++cgit_repolist.count > cgit_repolist.length) {
81 if (cgit_repolist.length == 0)
82 cgit_repolist.length = 8;
84 cgit_repolist.length *= 2;
85 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
86 cgit_repolist.length *
87 sizeof(struct repoinfo));
90 ret = &cgit_repolist.repos[cgit_repolist.count-1];
91 ret->url = trim_end(url, '/');
94 ret->desc = "[no description]";
96 ret->group = ctx.cfg.repo_group;
97 ret->defbranch = "master";
98 ret->snapshots = ctx.cfg.snapshots;
99 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
100 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
101 ret->module_link = ctx.cfg.module_link;
106 struct repoinfo *cgit_get_repoinfo(const char *url)
109 struct repoinfo *repo;
111 for (i=0; i<cgit_repolist.count; i++) {
112 repo = &cgit_repolist.repos[i];
113 if (!strcmp(repo->url, url))
119 void cgit_global_config_cb(const char *name, const char *value)
121 if (!strcmp(name, "root-title"))
122 ctx.cfg.root_title = xstrdup(value);
123 else if (!strcmp(name, "css"))
124 ctx.cfg.css = xstrdup(value);
125 else if (!strcmp(name, "logo"))
126 ctx.cfg.logo = xstrdup(value);
127 else if (!strcmp(name, "index-header"))
128 ctx.cfg.index_header = xstrdup(value);
129 else if (!strcmp(name, "index-info"))
130 ctx.cfg.index_info = xstrdup(value);
131 else if (!strcmp(name, "logo-link"))
132 ctx.cfg.logo_link = xstrdup(value);
133 else if (!strcmp(name, "module-link"))
134 ctx.cfg.module_link = xstrdup(value);
135 else if (!strcmp(name, "virtual-root")) {
136 ctx.cfg.virtual_root = trim_end(value, '/');
137 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
138 ctx.cfg.virtual_root = "";
139 } else if (!strcmp(name, "nocache"))
140 ctx.cfg.nocache = atoi(value);
141 else if (!strcmp(name, "snapshots"))
142 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
143 else if (!strcmp(name, "enable-index-links"))
144 ctx.cfg.enable_index_links = atoi(value);
145 else if (!strcmp(name, "enable-log-filecount"))
146 ctx.cfg.enable_log_filecount = atoi(value);
147 else if (!strcmp(name, "enable-log-linecount"))
148 ctx.cfg.enable_log_linecount = atoi(value);
149 else if (!strcmp(name, "cache-root"))
150 ctx.cfg.cache_root = xstrdup(value);
151 else if (!strcmp(name, "cache-root-ttl"))
152 ctx.cfg.cache_root_ttl = atoi(value);
153 else if (!strcmp(name, "cache-repo-ttl"))
154 ctx.cfg.cache_repo_ttl = atoi(value);
155 else if (!strcmp(name, "cache-static-ttl"))
156 ctx.cfg.cache_static_ttl = atoi(value);
157 else if (!strcmp(name, "cache-dynamic-ttl"))
158 ctx.cfg.cache_dynamic_ttl = atoi(value);
159 else if (!strcmp(name, "max-message-length"))
160 ctx.cfg.max_msg_len = atoi(value);
161 else if (!strcmp(name, "max-repodesc-length"))
162 ctx.cfg.max_repodesc_len = atoi(value);
163 else if (!strcmp(name, "max-commit-count"))
164 ctx.cfg.max_commit_count = atoi(value);
165 else if (!strcmp(name, "summary-log"))
166 ctx.cfg.summary_log = atoi(value);
167 else if (!strcmp(name, "summary-branches"))
168 ctx.cfg.summary_branches = atoi(value);
169 else if (!strcmp(name, "summary-tags"))
170 ctx.cfg.summary_tags = atoi(value);
171 else if (!strcmp(name, "agefile"))
172 ctx.cfg.agefile = xstrdup(value);
173 else if (!strcmp(name, "renamelimit"))
174 ctx.cfg.renamelimit = atoi(value);
175 else if (!strcmp(name, "robots"))
176 ctx.cfg.robots = xstrdup(value);
177 else if (!strcmp(name, "clone-prefix"))
178 ctx.cfg.clone_prefix = xstrdup(value);
179 else if (!strcmp(name, "repo.group"))
180 ctx.cfg.repo_group = xstrdup(value);
181 else if (!strcmp(name, "repo.url"))
182 cgit_repo = add_repo(value);
183 else if (!strcmp(name, "repo.name"))
184 cgit_repo->name = xstrdup(value);
185 else if (cgit_repo && !strcmp(name, "repo.path"))
186 cgit_repo->path = trim_end(value, '/');
187 else if (cgit_repo && !strcmp(name, "repo.clone-url"))
188 cgit_repo->clone_url = xstrdup(value);
189 else if (cgit_repo && !strcmp(name, "repo.desc"))
190 cgit_repo->desc = xstrdup(value);
191 else if (cgit_repo && !strcmp(name, "repo.owner"))
192 cgit_repo->owner = xstrdup(value);
193 else if (cgit_repo && !strcmp(name, "repo.defbranch"))
194 cgit_repo->defbranch = xstrdup(value);
195 else if (cgit_repo && !strcmp(name, "repo.snapshots"))
196 cgit_repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
197 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount"))
198 cgit_repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
199 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount"))
200 cgit_repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
201 else if (cgit_repo && !strcmp(name, "repo.module-link"))
202 cgit_repo->module_link= xstrdup(value);
203 else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) {
205 cgit_repo->readme = xstrdup(value);
207 cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value));
208 } else if (!strcmp(name, "include"))
209 cgit_read_config(value, cgit_global_config_cb);
212 void cgit_querystring_cb(const char *name, const char *value)
214 if (!strcmp(name,"r")) {
215 ctx.qry.repo = xstrdup(value);
216 cgit_repo = cgit_get_repoinfo(value);
217 } else if (!strcmp(name, "p")) {
218 ctx.qry.page = xstrdup(value);
219 cgit_cmd = cgit_get_cmd_index(value);
220 } else if (!strcmp(name, "url")) {
221 cgit_parse_url(value);
222 } else if (!strcmp(name, "qt")) {
223 ctx.qry.grep = xstrdup(value);
224 } else if (!strcmp(name, "q")) {
225 ctx.qry.search = xstrdup(value);
226 } else if (!strcmp(name, "h")) {
227 ctx.qry.head = xstrdup(value);
228 ctx.qry.has_symref = 1;
229 } else if (!strcmp(name, "id")) {
230 ctx.qry.sha1 = xstrdup(value);
231 ctx.qry.has_sha1 = 1;
232 } else if (!strcmp(name, "id2")) {
233 ctx.qry.sha2 = xstrdup(value);
234 ctx.qry.has_sha1 = 1;
235 } else if (!strcmp(name, "ofs")) {
236 ctx.qry.ofs = atoi(value);
237 } else if (!strcmp(name, "path")) {
238 ctx.qry.path = trim_end(value, '/');
239 } else if (!strcmp(name, "name")) {
240 ctx.qry.name = xstrdup(value);
244 void *cgit_free_commitinfo(struct commitinfo *info)
247 free(info->author_email);
248 free(info->committer);
249 free(info->committer_email);
252 free(info->msg_encoding);
259 if (c >= 'a' && c <= 'f')
261 else if (c >= 'A' && c <= 'F')
263 else if (c >= '0' && c <= '9')
269 char *trim_end(const char *str, char c)
278 while(len > 0 && t[len - 1] == c)
291 char *strlpart(char *txt, int maxlen)
298 if (strlen(txt) <= maxlen)
300 result = xmalloc(maxlen + 1);
301 memcpy(result, txt, maxlen - 3);
302 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
303 result[maxlen] = '\0';
307 char *strrpart(char *txt, int maxlen)
314 if (strlen(txt) <= maxlen)
316 result = xmalloc(maxlen + 1);
317 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
318 result[0] = result[1] = result[2] = '.';
322 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
326 if (list->count >= list->alloc) {
327 list->alloc += (list->alloc ? list->alloc : 4);
328 size = list->alloc * sizeof(struct refinfo *);
329 list->refs = xrealloc(list->refs, size);
331 list->refs[list->count++] = ref;
334 struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
338 ref = xmalloc(sizeof (struct refinfo));
339 ref->refname = xstrdup(refname);
340 ref->object = parse_object(sha1);
341 switch (ref->object->type) {
343 ref->tag = cgit_parse_tag((struct tag *)ref->object);
346 ref->commit = cgit_parse_commit((struct commit *)ref->object);
352 int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
355 struct reflist *list = (struct reflist *)cb_data;
356 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
359 cgit_add_ref(list, info);
363 void cgit_diff_tree_cb(struct diff_queue_struct *q,
364 struct diff_options *options, void *data)
368 for (i = 0; i < q->nr; i++) {
369 if (q->queue[i]->status == 'U')
371 ((filepair_fn)data)(q->queue[i]);
375 static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
377 enum object_type type;
379 if (is_null_sha1(sha1)) {
380 file->ptr = (char *)"";
383 file->ptr = read_sha1_file(sha1, &type,
384 (unsigned long *)&file->size);
390 * Receive diff-buffers from xdiff and concatenate them as
391 * needed across multiple callbacks.
393 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
394 * ripped from git and modified to use globals instead of
395 * a special callback-struct.
397 char *diffbuf = NULL;
400 int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
404 for (i = 0; i < nbuf; i++) {
405 if (mb[i].ptr[mb[i].size-1] != '\n') {
406 /* Incomplete line */
407 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
408 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
409 buflen += mb[i].size;
413 /* we have a complete line */
415 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
418 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
419 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
420 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
426 ((linediff_fn)priv)(diffbuf, buflen);
434 int cgit_diff_files(const unsigned char *old_sha1,
435 const unsigned char *new_sha1,
438 mmfile_t file1, file2;
439 xpparam_t diff_params;
440 xdemitconf_t emit_params;
443 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
446 diff_params.flags = XDF_NEED_MINIMAL;
447 emit_params.ctxlen = 3;
448 emit_params.flags = XDL_EMIT_FUNCNAMES;
449 emit_params.find_func = NULL;
450 emit_cb.outf = filediff_cb;
452 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
456 void cgit_diff_tree(const unsigned char *old_sha1,
457 const unsigned char *new_sha1,
458 filepair_fn fn, const char *prefix)
460 struct diff_options opt;
465 opt.output_format = DIFF_FORMAT_CALLBACK;
466 opt.detect_rename = 1;
467 opt.rename_limit = ctx.cfg.renamelimit;
468 DIFF_OPT_SET(&opt, RECURSIVE);
469 opt.format_callback = cgit_diff_tree_cb;
470 opt.format_callback_data = fn;
474 prefixlen = strlen(prefix);
475 opt.pathlens = &prefixlen;
477 diff_setup_done(&opt);
479 if (old_sha1 && !is_null_sha1(old_sha1))
480 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
482 ret = diff_root_tree_sha1(new_sha1, "", &opt);
487 void cgit_diff_commit(struct commit *commit, filepair_fn fn)
489 unsigned char *old_sha1 = NULL;
492 old_sha1 = commit->parents->item->object.sha1;
493 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);