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