]> gitweb.ps.run Git - ps-cgit/blob - shared.c
Introduce html.h
[ps-cgit] / shared.c
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
11 struct cgit_repolist cgit_repolist;
12 struct cgit_context ctx;
13 int cgit_cmd;
14
15 const char *cgit_version = CGIT_VERSION;
16
17 void cgit_prepare_context(struct cgit_context *ctx)
18 {
19         memset(ctx, 0, sizeof(ctx));
20         ctx->cfg.agefile = "info/web/last-modified";
21         ctx->cfg.cache_dynamic_ttl = 5;
22         ctx->cfg.cache_max_create_time = 5;
23         ctx->cfg.cache_repo_ttl = 5;
24         ctx->cfg.cache_root = CGIT_CACHE_ROOT;
25         ctx->cfg.cache_root_ttl = 5;
26         ctx->cfg.cache_static_ttl = -1;
27         ctx->cfg.css = "/cgit.css";
28         ctx->cfg.logo = "/git-logo.png";
29         ctx->cfg.max_commit_count = 50;
30         ctx->cfg.max_lock_attempts = 5;
31         ctx->cfg.max_msg_len = 60;
32         ctx->cfg.max_repodesc_len = 60;
33         ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
34         ctx->cfg.renamelimit = -1;
35         ctx->cfg.robots = "index, nofollow";
36         ctx->cfg.root_title = "Git repository browser";
37         ctx->cfg.script_name = CGIT_SCRIPT_NAME;
38 }
39
40 int cgit_get_cmd_index(const char *cmd)
41 {
42         static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
43                                "snapshot", "tag", "refs", "patch", NULL};
44         int i;
45
46         for(i = 0; cmds[i]; i++)
47                 if (!strcmp(cmd, cmds[i]))
48                         return i + 1;
49         return 0;
50 }
51
52 int chk_zero(int result, char *msg)
53 {
54         if (result != 0)
55                 die("%s: %s", msg, strerror(errno));
56         return result;
57 }
58
59 int chk_positive(int result, char *msg)
60 {
61         if (result <= 0)
62                 die("%s: %s", msg, strerror(errno));
63         return result;
64 }
65
66 int chk_non_negative(int result, char *msg)
67 {
68         if (result < 0)
69                 die("%s: %s",msg, strerror(errno));
70         return result;
71 }
72
73 struct cgit_repo *add_repo(const char *url)
74 {
75         struct cgit_repo *ret;
76
77         if (++cgit_repolist.count > cgit_repolist.length) {
78                 if (cgit_repolist.length == 0)
79                         cgit_repolist.length = 8;
80                 else
81                         cgit_repolist.length *= 2;
82                 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
83                                                cgit_repolist.length *
84                                                sizeof(struct cgit_repo));
85         }
86
87         ret = &cgit_repolist.repos[cgit_repolist.count-1];
88         ret->url = trim_end(url, '/');
89         ret->name = ret->url;
90         ret->path = NULL;
91         ret->desc = "[no description]";
92         ret->owner = NULL;
93         ret->group = ctx.cfg.repo_group;
94         ret->defbranch = "master";
95         ret->snapshots = ctx.cfg.snapshots;
96         ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
97         ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
98         ret->module_link = ctx.cfg.module_link;
99         ret->readme = NULL;
100         return ret;
101 }
102
103 struct cgit_repo *cgit_get_repoinfo(const char *url)
104 {
105         int i;
106         struct cgit_repo *repo;
107
108         for (i=0; i<cgit_repolist.count; i++) {
109                 repo = &cgit_repolist.repos[i];
110                 if (!strcmp(repo->url, url))
111                         return repo;
112         }
113         return NULL;
114 }
115
116 void cgit_global_config_cb(const char *name, const char *value)
117 {
118         if (!strcmp(name, "root-title"))
119                 ctx.cfg.root_title = xstrdup(value);
120         else if (!strcmp(name, "css"))
121                 ctx.cfg.css = xstrdup(value);
122         else if (!strcmp(name, "logo"))
123                 ctx.cfg.logo = xstrdup(value);
124         else if (!strcmp(name, "index-header"))
125                 ctx.cfg.index_header = xstrdup(value);
126         else if (!strcmp(name, "index-info"))
127                 ctx.cfg.index_info = xstrdup(value);
128         else if (!strcmp(name, "logo-link"))
129                 ctx.cfg.logo_link = xstrdup(value);
130         else if (!strcmp(name, "module-link"))
131                 ctx.cfg.module_link = xstrdup(value);
132         else if (!strcmp(name, "virtual-root")) {
133                 ctx.cfg.virtual_root = trim_end(value, '/');
134                 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
135                         ctx.cfg.virtual_root = "";
136         } else if (!strcmp(name, "nocache"))
137                 ctx.cfg.nocache = atoi(value);
138         else if (!strcmp(name, "snapshots"))
139                 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
140         else if (!strcmp(name, "enable-index-links"))
141                 ctx.cfg.enable_index_links = atoi(value);
142         else if (!strcmp(name, "enable-log-filecount"))
143                 ctx.cfg.enable_log_filecount = atoi(value);
144         else if (!strcmp(name, "enable-log-linecount"))
145                 ctx.cfg.enable_log_linecount = atoi(value);
146         else if (!strcmp(name, "cache-root"))
147                 ctx.cfg.cache_root = xstrdup(value);
148         else if (!strcmp(name, "cache-root-ttl"))
149                 ctx.cfg.cache_root_ttl = atoi(value);
150         else if (!strcmp(name, "cache-repo-ttl"))
151                 ctx.cfg.cache_repo_ttl = atoi(value);
152         else if (!strcmp(name, "cache-static-ttl"))
153                 ctx.cfg.cache_static_ttl = atoi(value);
154         else if (!strcmp(name, "cache-dynamic-ttl"))
155                 ctx.cfg.cache_dynamic_ttl = atoi(value);
156         else if (!strcmp(name, "max-message-length"))
157                 ctx.cfg.max_msg_len = atoi(value);
158         else if (!strcmp(name, "max-repodesc-length"))
159                 ctx.cfg.max_repodesc_len = atoi(value);
160         else if (!strcmp(name, "max-commit-count"))
161                 ctx.cfg.max_commit_count = atoi(value);
162         else if (!strcmp(name, "summary-log"))
163                 ctx.cfg.summary_log = atoi(value);
164         else if (!strcmp(name, "summary-branches"))
165                 ctx.cfg.summary_branches = atoi(value);
166         else if (!strcmp(name, "summary-tags"))
167                 ctx.cfg.summary_tags = atoi(value);
168         else if (!strcmp(name, "agefile"))
169                 ctx.cfg.agefile = xstrdup(value);
170         else if (!strcmp(name, "renamelimit"))
171                 ctx.cfg.renamelimit = atoi(value);
172         else if (!strcmp(name, "robots"))
173                 ctx.cfg.robots = xstrdup(value);
174         else if (!strcmp(name, "clone-prefix"))
175                 ctx.cfg.clone_prefix = xstrdup(value);
176         else if (!strcmp(name, "repo.group"))
177                 ctx.cfg.repo_group = xstrdup(value);
178         else if (!strcmp(name, "repo.url"))
179                 ctx.repo = add_repo(value);
180         else if (!strcmp(name, "repo.name"))
181                 ctx.repo->name = xstrdup(value);
182         else if (ctx.repo && !strcmp(name, "repo.path"))
183                 ctx.repo->path = trim_end(value, '/');
184         else if (ctx.repo && !strcmp(name, "repo.clone-url"))
185                 ctx.repo->clone_url = xstrdup(value);
186         else if (ctx.repo && !strcmp(name, "repo.desc"))
187                 ctx.repo->desc = xstrdup(value);
188         else if (ctx.repo && !strcmp(name, "repo.owner"))
189                 ctx.repo->owner = xstrdup(value);
190         else if (ctx.repo && !strcmp(name, "repo.defbranch"))
191                 ctx.repo->defbranch = xstrdup(value);
192         else if (ctx.repo && !strcmp(name, "repo.snapshots"))
193                 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
194         else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
195                 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
196         else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
197                 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
198         else if (ctx.repo && !strcmp(name, "repo.module-link"))
199                 ctx.repo->module_link= xstrdup(value);
200         else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
201                 if (*value == '/')
202                         ctx.repo->readme = xstrdup(value);
203                 else
204                         ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
205         } else if (!strcmp(name, "include"))
206                 cgit_read_config(value, cgit_global_config_cb);
207 }
208
209 void cgit_querystring_cb(const char *name, const char *value)
210 {
211         if (!strcmp(name,"r")) {
212                 ctx.qry.repo = xstrdup(value);
213                 ctx.repo = cgit_get_repoinfo(value);
214         } else if (!strcmp(name, "p")) {
215                 ctx.qry.page = xstrdup(value);
216                 cgit_cmd = cgit_get_cmd_index(value);
217         } else if (!strcmp(name, "url")) {
218                 cgit_parse_url(value);
219         } else if (!strcmp(name, "qt")) {
220                 ctx.qry.grep = xstrdup(value);
221         } else if (!strcmp(name, "q")) {
222                 ctx.qry.search = xstrdup(value);
223         } else if (!strcmp(name, "h")) {
224                 ctx.qry.head = xstrdup(value);
225                 ctx.qry.has_symref = 1;
226         } else if (!strcmp(name, "id")) {
227                 ctx.qry.sha1 = xstrdup(value);
228                 ctx.qry.has_sha1 = 1;
229         } else if (!strcmp(name, "id2")) {
230                 ctx.qry.sha2 = xstrdup(value);
231                 ctx.qry.has_sha1 = 1;
232         } else if (!strcmp(name, "ofs")) {
233                 ctx.qry.ofs = atoi(value);
234         } else if (!strcmp(name, "path")) {
235                 ctx.qry.path = trim_end(value, '/');
236         } else if (!strcmp(name, "name")) {
237                 ctx.qry.name = xstrdup(value);
238         }
239 }
240
241 void *cgit_free_commitinfo(struct commitinfo *info)
242 {
243         free(info->author);
244         free(info->author_email);
245         free(info->committer);
246         free(info->committer_email);
247         free(info->subject);
248         free(info->msg);
249         free(info->msg_encoding);
250         free(info);
251         return NULL;
252 }
253
254 int hextoint(char c)
255 {
256         if (c >= 'a' && c <= 'f')
257                 return 10 + c - 'a';
258         else if (c >= 'A' && c <= 'F')
259                 return 10 + c - 'A';
260         else if (c >= '0' && c <= '9')
261                 return c - '0';
262         else
263                 return -1;
264 }
265
266 char *trim_end(const char *str, char c)
267 {
268         int len;
269         char *s, *t;
270
271         if (str == NULL)
272                 return NULL;
273         t = (char *)str;
274         len = strlen(t);
275         while(len > 0 && t[len - 1] == c)
276                 len--;
277
278         if (len == 0)
279                 return NULL;
280
281         c = t[len];
282         t[len] = '\0';
283         s = xstrdup(t);
284         t[len] = c;
285         return s;
286 }
287
288 char *strlpart(char *txt, int maxlen)
289 {
290         char *result;
291
292         if (!txt)
293                 return txt;
294
295         if (strlen(txt) <= maxlen)
296                 return txt;
297         result = xmalloc(maxlen + 1);
298         memcpy(result, txt, maxlen - 3);
299         result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
300         result[maxlen] = '\0';
301         return result;
302 }
303
304 char *strrpart(char *txt, int maxlen)
305 {
306         char *result;
307
308         if (!txt)
309                 return txt;
310
311         if (strlen(txt) <= maxlen)
312                 return txt;
313         result = xmalloc(maxlen + 1);
314         memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
315         result[0] = result[1] = result[2] = '.';
316         return result;
317 }
318
319 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
320 {
321         size_t size;
322
323         if (list->count >= list->alloc) {
324                 list->alloc += (list->alloc ? list->alloc : 4);
325                 size = list->alloc * sizeof(struct refinfo *);
326                 list->refs = xrealloc(list->refs, size);
327         }
328         list->refs[list->count++] = ref;
329 }
330
331 struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
332 {
333         struct refinfo *ref;
334
335         ref = xmalloc(sizeof (struct refinfo));
336         ref->refname = xstrdup(refname);
337         ref->object = parse_object(sha1);
338         switch (ref->object->type) {
339         case OBJ_TAG:
340                 ref->tag = cgit_parse_tag((struct tag *)ref->object);
341                 break;
342         case OBJ_COMMIT:
343                 ref->commit = cgit_parse_commit((struct commit *)ref->object);
344                 break;
345         }
346         return ref;
347 }
348
349 int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
350                   void *cb_data)
351 {
352         struct reflist *list = (struct reflist *)cb_data;
353         struct refinfo *info = cgit_mk_refinfo(refname, sha1);
354
355         if (info)
356                 cgit_add_ref(list, info);
357         return 0;
358 }
359
360 void cgit_diff_tree_cb(struct diff_queue_struct *q,
361                        struct diff_options *options, void *data)
362 {
363         int i;
364
365         for (i = 0; i < q->nr; i++) {
366                 if (q->queue[i]->status == 'U')
367                         continue;
368                 ((filepair_fn)data)(q->queue[i]);
369         }
370 }
371
372 static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
373 {
374         enum object_type type;
375
376         if (is_null_sha1(sha1)) {
377                 file->ptr = (char *)"";
378                 file->size = 0;
379         } else {
380                 file->ptr = read_sha1_file(sha1, &type, 
381                                            (unsigned long *)&file->size);
382         }
383         return 1;
384 }
385
386 /*
387  * Receive diff-buffers from xdiff and concatenate them as
388  * needed across multiple callbacks.
389  *
390  * This is basically a copy of xdiff-interface.c/xdiff_outf(),
391  * ripped from git and modified to use globals instead of
392  * a special callback-struct.
393  */
394 char *diffbuf = NULL;
395 int buflen = 0;
396
397 int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
398 {
399         int i;
400
401         for (i = 0; i < nbuf; i++) {
402                 if (mb[i].ptr[mb[i].size-1] != '\n') {
403                         /* Incomplete line */
404                         diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
405                         memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
406                         buflen += mb[i].size;
407                         continue;
408                 }
409
410                 /* we have a complete line */
411                 if (!diffbuf) {
412                         ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
413                         continue;
414                 }
415                 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
416                 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
417                 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
418                 free(diffbuf);
419                 diffbuf = NULL;
420                 buflen = 0;
421         }
422         if (diffbuf) {
423                 ((linediff_fn)priv)(diffbuf, buflen);
424                 free(diffbuf);
425                 diffbuf = NULL;
426                 buflen = 0;
427         }
428         return 0;
429 }
430
431 int cgit_diff_files(const unsigned char *old_sha1,
432                      const unsigned char *new_sha1,
433                      linediff_fn fn)
434 {
435         mmfile_t file1, file2;
436         xpparam_t diff_params;
437         xdemitconf_t emit_params;
438         xdemitcb_t emit_cb;
439
440         if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
441                 return 1;
442
443         diff_params.flags = XDF_NEED_MINIMAL;
444         emit_params.ctxlen = 3;
445         emit_params.flags = XDL_EMIT_FUNCNAMES;
446         emit_params.find_func = NULL;
447         emit_cb.outf = filediff_cb;
448         emit_cb.priv = fn;
449         xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
450         return 0;
451 }
452
453 void cgit_diff_tree(const unsigned char *old_sha1,
454                     const unsigned char *new_sha1,
455                     filepair_fn fn, const char *prefix)
456 {
457         struct diff_options opt;
458         int ret;
459         int prefixlen;
460
461         diff_setup(&opt);
462         opt.output_format = DIFF_FORMAT_CALLBACK;
463         opt.detect_rename = 1;
464         opt.rename_limit = ctx.cfg.renamelimit;
465         DIFF_OPT_SET(&opt, RECURSIVE);
466         opt.format_callback = cgit_diff_tree_cb;
467         opt.format_callback_data = fn;
468         if (prefix) {
469                 opt.nr_paths = 1;
470                 opt.paths = &prefix;
471                 prefixlen = strlen(prefix);
472                 opt.pathlens = &prefixlen;
473         }
474         diff_setup_done(&opt);
475
476         if (old_sha1 && !is_null_sha1(old_sha1))
477                 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
478         else
479                 ret = diff_root_tree_sha1(new_sha1, "", &opt);
480         diffcore_std(&opt);
481         diff_flush(&opt);
482 }
483
484 void cgit_diff_commit(struct commit *commit, filepair_fn fn)
485 {
486         unsigned char *old_sha1 = NULL;
487
488         if (commit->parents)
489                 old_sha1 = commit->parents->item->object.sha1;
490         cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
491 }