]> gitweb.ps.run Git - ps-cgit/blob - shared.c
git: update to v2.10.2 again
[ps-cgit] / shared.c
1 /* shared.c: global vars + some callback functions
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
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
14 int chk_zero(int result, char *msg)
15 {
16         if (result != 0)
17                 die_errno("%s", msg);
18         return result;
19 }
20
21 int chk_positive(int result, char *msg)
22 {
23         if (result <= 0)
24                 die_errno("%s", msg);
25         return result;
26 }
27
28 int chk_non_negative(int result, char *msg)
29 {
30         if (result < 0)
31                 die_errno("%s", msg);
32         return result;
33 }
34
35 char *cgit_default_repo_desc = "[no description]";
36 struct cgit_repo *cgit_add_repo(const char *url)
37 {
38         struct cgit_repo *ret;
39
40         if (++cgit_repolist.count > cgit_repolist.length) {
41                 if (cgit_repolist.length == 0)
42                         cgit_repolist.length = 8;
43                 else
44                         cgit_repolist.length *= 2;
45                 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
46                                                cgit_repolist.length *
47                                                sizeof(struct cgit_repo));
48         }
49
50         ret = &cgit_repolist.repos[cgit_repolist.count-1];
51         memset(ret, 0, sizeof(struct cgit_repo));
52         ret->url = trim_end(url, '/');
53         ret->name = ret->url;
54         ret->path = NULL;
55         ret->desc = cgit_default_repo_desc;
56         ret->owner = NULL;
57         ret->homepage = NULL;
58         ret->section = ctx.cfg.section;
59         ret->snapshots = ctx.cfg.snapshots;
60         ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
61         ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
62         ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
63         ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
64         ret->enable_subject_links = ctx.cfg.enable_subject_links;
65         ret->enable_html_serving = ctx.cfg.enable_html_serving;
66         ret->max_stats = ctx.cfg.max_stats;
67         ret->branch_sort = ctx.cfg.branch_sort;
68         ret->commit_sort = ctx.cfg.commit_sort;
69         ret->module_link = ctx.cfg.module_link;
70         ret->readme = ctx.cfg.readme;
71         ret->mtime = -1;
72         ret->about_filter = ctx.cfg.about_filter;
73         ret->commit_filter = ctx.cfg.commit_filter;
74         ret->source_filter = ctx.cfg.source_filter;
75         ret->email_filter = ctx.cfg.email_filter;
76         ret->owner_filter = ctx.cfg.owner_filter;
77         ret->clone_url = ctx.cfg.clone_url;
78         ret->submodules.strdup_strings = 1;
79         ret->hide = ret->ignore = 0;
80         return ret;
81 }
82
83 struct cgit_repo *cgit_get_repoinfo(const char *url)
84 {
85         int i;
86         struct cgit_repo *repo;
87
88         for (i = 0; i < cgit_repolist.count; i++) {
89                 repo = &cgit_repolist.repos[i];
90                 if (repo->ignore)
91                         continue;
92                 if (!strcmp(repo->url, url))
93                         return repo;
94         }
95         return NULL;
96 }
97
98 void cgit_free_commitinfo(struct commitinfo *info)
99 {
100         free(info->author);
101         free(info->author_email);
102         free(info->committer);
103         free(info->committer_email);
104         free(info->subject);
105         free(info->msg);
106         free(info->msg_encoding);
107         free(info);
108 }
109
110 char *trim_end(const char *str, char c)
111 {
112         int len;
113
114         if (str == NULL)
115                 return NULL;
116         len = strlen(str);
117         while (len > 0 && str[len - 1] == c)
118                 len--;
119         if (len == 0)
120                 return NULL;
121         return xstrndup(str, len);
122 }
123
124 char *ensure_end(const char *str, char c)
125 {
126         size_t len = strlen(str);
127         char *result;
128
129         if (len && str[len - 1] == c)
130                 return xstrndup(str, len);
131
132         result = xmalloc(len + 2);
133         memcpy(result, str, len);
134         result[len] = '/';
135         result[len + 1] = '\0';
136         return result;
137 }
138
139 void strbuf_ensure_end(struct strbuf *sb, char c)
140 {
141         if (!sb->len || sb->buf[sb->len - 1] != c)
142                 strbuf_addch(sb, c);
143 }
144
145 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
146 {
147         size_t size;
148
149         if (list->count >= list->alloc) {
150                 list->alloc += (list->alloc ? list->alloc : 4);
151                 size = list->alloc * sizeof(struct refinfo *);
152                 list->refs = xrealloc(list->refs, size);
153         }
154         list->refs[list->count++] = ref;
155 }
156
157 static struct refinfo *cgit_mk_refinfo(const char *refname, const struct object_id *oid)
158 {
159         struct refinfo *ref;
160
161         ref = xmalloc(sizeof (struct refinfo));
162         ref->refname = xstrdup(refname);
163         ref->object = parse_object(oid->hash);
164         switch (ref->object->type) {
165         case OBJ_TAG:
166                 ref->tag = cgit_parse_tag((struct tag *)ref->object);
167                 break;
168         case OBJ_COMMIT:
169                 ref->commit = cgit_parse_commit((struct commit *)ref->object);
170                 break;
171         }
172         return ref;
173 }
174
175 void cgit_free_taginfo(struct taginfo *tag)
176 {
177         if (tag->tagger)
178                 free(tag->tagger);
179         if (tag->tagger_email)
180                 free(tag->tagger_email);
181         if (tag->msg)
182                 free(tag->msg);
183         free(tag);
184 }
185
186 static void cgit_free_refinfo(struct refinfo *ref)
187 {
188         if (ref->refname)
189                 free((char *)ref->refname);
190         switch (ref->object->type) {
191         case OBJ_TAG:
192                 cgit_free_taginfo(ref->tag);
193                 break;
194         case OBJ_COMMIT:
195                 cgit_free_commitinfo(ref->commit);
196                 break;
197         }
198         free(ref);
199 }
200
201 void cgit_free_reflist_inner(struct reflist *list)
202 {
203         int i;
204
205         for (i = 0; i < list->count; i++) {
206                 cgit_free_refinfo(list->refs[i]);
207         }
208         free(list->refs);
209 }
210
211 int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
212                   void *cb_data)
213 {
214         struct reflist *list = (struct reflist *)cb_data;
215         struct refinfo *info = cgit_mk_refinfo(refname, oid);
216
217         if (info)
218                 cgit_add_ref(list, info);
219         return 0;
220 }
221
222 void cgit_diff_tree_cb(struct diff_queue_struct *q,
223                        struct diff_options *options, void *data)
224 {
225         int i;
226
227         for (i = 0; i < q->nr; i++) {
228                 if (q->queue[i]->status == 'U')
229                         continue;
230                 ((filepair_fn)data)(q->queue[i]);
231         }
232 }
233
234 static int load_mmfile(mmfile_t *file, const struct object_id *oid)
235 {
236         enum object_type type;
237
238         if (is_null_oid(oid)) {
239                 file->ptr = (char *)"";
240                 file->size = 0;
241         } else {
242                 file->ptr = read_sha1_file(oid->hash, &type,
243                                            (unsigned long *)&file->size);
244         }
245         return 1;
246 }
247
248 /*
249  * Receive diff-buffers from xdiff and concatenate them as
250  * needed across multiple callbacks.
251  *
252  * This is basically a copy of xdiff-interface.c/xdiff_outf(),
253  * ripped from git and modified to use globals instead of
254  * a special callback-struct.
255  */
256 static char *diffbuf = NULL;
257 static int buflen = 0;
258
259 static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
260 {
261         int i;
262
263         for (i = 0; i < nbuf; i++) {
264                 if (mb[i].ptr[mb[i].size-1] != '\n') {
265                         /* Incomplete line */
266                         diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
267                         memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
268                         buflen += mb[i].size;
269                         continue;
270                 }
271
272                 /* we have a complete line */
273                 if (!diffbuf) {
274                         ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
275                         continue;
276                 }
277                 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
278                 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
279                 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
280                 free(diffbuf);
281                 diffbuf = NULL;
282                 buflen = 0;
283         }
284         if (diffbuf) {
285                 ((linediff_fn)priv)(diffbuf, buflen);
286                 free(diffbuf);
287                 diffbuf = NULL;
288                 buflen = 0;
289         }
290         return 0;
291 }
292
293 int cgit_diff_files(const struct object_id *old_oid,
294                     const struct object_id *new_oid, unsigned long *old_size,
295                     unsigned long *new_size, int *binary, int context,
296                     int ignorews, linediff_fn fn)
297 {
298         mmfile_t file1, file2;
299         xpparam_t diff_params;
300         xdemitconf_t emit_params;
301         xdemitcb_t emit_cb;
302
303         if (!load_mmfile(&file1, old_oid) || !load_mmfile(&file2, new_oid))
304                 return 1;
305
306         *old_size = file1.size;
307         *new_size = file2.size;
308
309         if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
310             (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
311                 *binary = 1;
312                 if (file1.size)
313                         free(file1.ptr);
314                 if (file2.size)
315                         free(file2.ptr);
316                 return 0;
317         }
318
319         memset(&diff_params, 0, sizeof(diff_params));
320         memset(&emit_params, 0, sizeof(emit_params));
321         memset(&emit_cb, 0, sizeof(emit_cb));
322         diff_params.flags = XDF_NEED_MINIMAL;
323         if (ignorews)
324                 diff_params.flags |= XDF_IGNORE_WHITESPACE;
325         emit_params.ctxlen = context > 0 ? context : 3;
326         emit_params.flags = XDL_EMIT_FUNCNAMES;
327         emit_cb.outf = filediff_cb;
328         emit_cb.priv = fn;
329         xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
330         if (file1.size)
331                 free(file1.ptr);
332         if (file2.size)
333                 free(file2.ptr);
334         return 0;
335 }
336
337 void cgit_diff_tree(const struct object_id *old_oid,
338                     const struct object_id *new_oid,
339                     filepair_fn fn, const char *prefix, int ignorews)
340 {
341         struct diff_options opt;
342         struct pathspec_item item;
343
344         memset(&item, 0, sizeof(item));
345         diff_setup(&opt);
346         opt.output_format = DIFF_FORMAT_CALLBACK;
347         opt.detect_rename = 1;
348         opt.rename_limit = ctx.cfg.renamelimit;
349         DIFF_OPT_SET(&opt, RECURSIVE);
350         if (ignorews)
351                 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
352         opt.format_callback = cgit_diff_tree_cb;
353         opt.format_callback_data = fn;
354         if (prefix) {
355                 item.match = prefix;
356                 item.len = strlen(prefix);
357                 opt.pathspec.nr = 1;
358                 opt.pathspec.items = &item;
359         }
360         diff_setup_done(&opt);
361
362         if (old_oid && !is_null_oid(old_oid))
363                 diff_tree_sha1(old_oid->hash, new_oid->hash, "", &opt);
364         else
365                 diff_root_tree_sha1(new_oid->hash, "", &opt);
366         diffcore_std(&opt);
367         diff_flush(&opt);
368 }
369
370 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
371 {
372         const struct object_id *old_oid = NULL;
373
374         if (commit->parents)
375                 old_oid = &commit->parents->item->object.oid;
376         cgit_diff_tree(old_oid, &commit->object.oid, fn, prefix,
377                        ctx.qry.ignorews);
378 }
379
380 int cgit_parse_snapshots_mask(const char *str)
381 {
382         struct string_list tokens = STRING_LIST_INIT_DUP;
383         struct string_list_item *item;
384         const struct cgit_snapshot_format *f;
385         int rv = 0;
386
387         /* favor legacy setting */
388         if (atoi(str))
389                 return 1;
390
391         string_list_split(&tokens, str, ' ', -1);
392         string_list_remove_empty_items(&tokens, 0);
393
394         for_each_string_list_item(item, &tokens) {
395                 for (f = cgit_snapshot_formats; f->suffix; f++) {
396                         if (!strcmp(item->string, f->suffix) ||
397                             !strcmp(item->string, f->suffix + 1)) {
398                                 rv |= f->bit;
399                                 break;
400                         }
401                 }
402         }
403
404         string_list_clear(&tokens, 0);
405         return rv;
406 }
407
408 typedef struct {
409         char * name;
410         char * value;
411 } cgit_env_var;
412
413 void cgit_prepare_repo_env(struct cgit_repo * repo)
414 {
415         cgit_env_var env_vars[] = {
416                 { .name = "CGIT_REPO_URL", .value = repo->url },
417                 { .name = "CGIT_REPO_NAME", .value = repo->name },
418                 { .name = "CGIT_REPO_PATH", .value = repo->path },
419                 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
420                 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
421                 { .name = "CGIT_REPO_SECTION", .value = repo->section },
422                 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
423         };
424         int env_var_count = ARRAY_SIZE(env_vars);
425         cgit_env_var *p, *q;
426         static char *warn = "cgit warning: failed to set env: %s=%s\n";
427
428         p = env_vars;
429         q = p + env_var_count;
430         for (; p < q; p++)
431                 if (p->value && setenv(p->name, p->value, 1))
432                         fprintf(stderr, warn, p->name, p->value);
433 }
434
435 /* Read the content of the specified file into a newly allocated buffer,
436  * zeroterminate the buffer and return 0 on success, errno otherwise.
437  */
438 int readfile(const char *path, char **buf, size_t *size)
439 {
440         int fd, e;
441         struct stat st;
442
443         fd = open(path, O_RDONLY);
444         if (fd == -1)
445                 return errno;
446         if (fstat(fd, &st)) {
447                 e = errno;
448                 close(fd);
449                 return e;
450         }
451         if (!S_ISREG(st.st_mode)) {
452                 close(fd);
453                 return EISDIR;
454         }
455         *buf = xmalloc(st.st_size + 1);
456         *size = read_in_full(fd, *buf, st.st_size);
457         e = errno;
458         (*buf)[*size] = '\0';
459         close(fd);
460         return (*size == st.st_size ? 0 : e);
461 }
462
463 static int is_token_char(char c)
464 {
465         return isalnum(c) || c == '_';
466 }
467
468 /* Replace name with getenv(name), return pointer to zero-terminating char
469  */
470 static char *expand_macro(char *name, int maxlength)
471 {
472         char *value;
473         int len;
474
475         len = 0;
476         value = getenv(name);
477         if (value) {
478                 len = strlen(value);
479                 if (len > maxlength)
480                         len = maxlength;
481                 strncpy(name, value, len);
482         }
483         return name + len;
484 }
485
486 #define EXPBUFSIZE (1024 * 8)
487
488 /* Replace all tokens prefixed by '$' in the specified text with the
489  * value of the named environment variable.
490  * NB: the return value is a static buffer, i.e. it must be strdup'd
491  * by the caller.
492  */
493 char *expand_macros(const char *txt)
494 {
495         static char result[EXPBUFSIZE];
496         char *p, *start;
497         int len;
498
499         p = result;
500         start = NULL;
501         while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
502                 *p = *txt;
503                 if (start) {
504                         if (!is_token_char(*txt)) {
505                                 if (p - start > 0) {
506                                         *p = '\0';
507                                         len = result + EXPBUFSIZE - start - 1;
508                                         p = expand_macro(start, len) - 1;
509                                 }
510                                 start = NULL;
511                                 txt--;
512                         }
513                         p++;
514                         txt++;
515                         continue;
516                 }
517                 if (*txt == '$') {
518                         start = p;
519                         txt++;
520                         continue;
521                 }
522                 p++;
523                 txt++;
524         }
525         *p = '\0';
526         if (start && p - start > 0) {
527                 len = result + EXPBUFSIZE - start - 1;
528                 p = expand_macro(start, len);
529                 *p = '\0';
530         }
531         return result;
532 }
533
534 char *get_mimetype_for_filename(const char *filename)
535 {
536         char *ext, *mimetype, *token, line[1024], *saveptr;
537         FILE *file;
538         struct string_list_item *mime;
539
540         if (!filename)
541                 return NULL;
542
543         ext = strrchr(filename, '.');
544         if (!ext)
545                 return NULL;
546         ++ext;
547         if (!ext[0])
548                 return NULL;
549         mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
550         if (mime)
551                 return xstrdup(mime->util);
552
553         if (!ctx.cfg.mimetype_file)
554                 return NULL;
555         file = fopen(ctx.cfg.mimetype_file, "r");
556         if (!file)
557                 return NULL;
558         while (fgets(line, sizeof(line), file)) {
559                 if (!line[0] || line[0] == '#')
560                         continue;
561                 mimetype = strtok_r(line, " \t\r\n", &saveptr);
562                 while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) {
563                         if (!strcasecmp(ext, token)) {
564                                 fclose(file);
565                                 return xstrdup(mimetype);
566                         }
567                 }
568         }
569         fclose(file);
570         return NULL;
571 }