]> git.cameronkatri.com Git - cgit.git/blob - shared.c
shared: make cgit_free_taginfo() public
[cgit.git] / 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 char *strlpart(char *txt, int maxlen)
146 {
147 char *result;
148
149 if (!txt)
150 return txt;
151
152 if (strlen(txt) <= maxlen)
153 return txt;
154 result = xmalloc(maxlen + 1);
155 memcpy(result, txt, maxlen - 3);
156 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
157 result[maxlen] = '\0';
158 return result;
159 }
160
161 char *strrpart(char *txt, int maxlen)
162 {
163 char *result;
164
165 if (!txt)
166 return txt;
167
168 if (strlen(txt) <= maxlen)
169 return txt;
170 result = xmalloc(maxlen + 1);
171 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
172 result[0] = result[1] = result[2] = '.';
173 return result;
174 }
175
176 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
177 {
178 size_t size;
179
180 if (list->count >= list->alloc) {
181 list->alloc += (list->alloc ? list->alloc : 4);
182 size = list->alloc * sizeof(struct refinfo *);
183 list->refs = xrealloc(list->refs, size);
184 }
185 list->refs[list->count++] = ref;
186 }
187
188 static struct refinfo *cgit_mk_refinfo(const char *refname, const struct object_id *oid)
189 {
190 struct refinfo *ref;
191
192 ref = xmalloc(sizeof (struct refinfo));
193 ref->refname = xstrdup(refname);
194 ref->object = parse_object(oid->hash);
195 switch (ref->object->type) {
196 case OBJ_TAG:
197 ref->tag = cgit_parse_tag((struct tag *)ref->object);
198 break;
199 case OBJ_COMMIT:
200 ref->commit = cgit_parse_commit((struct commit *)ref->object);
201 break;
202 }
203 return ref;
204 }
205
206 void cgit_free_taginfo(struct taginfo *tag)
207 {
208 if (tag->tagger)
209 free(tag->tagger);
210 if (tag->tagger_email)
211 free(tag->tagger_email);
212 if (tag->msg)
213 free(tag->msg);
214 free(tag);
215 }
216
217 static void cgit_free_refinfo(struct refinfo *ref)
218 {
219 if (ref->refname)
220 free((char *)ref->refname);
221 switch (ref->object->type) {
222 case OBJ_TAG:
223 cgit_free_taginfo(ref->tag);
224 break;
225 case OBJ_COMMIT:
226 cgit_free_commitinfo(ref->commit);
227 break;
228 }
229 free(ref);
230 }
231
232 void cgit_free_reflist_inner(struct reflist *list)
233 {
234 int i;
235
236 for (i = 0; i < list->count; i++) {
237 cgit_free_refinfo(list->refs[i]);
238 }
239 free(list->refs);
240 }
241
242 int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
243 void *cb_data)
244 {
245 struct reflist *list = (struct reflist *)cb_data;
246 struct refinfo *info = cgit_mk_refinfo(refname, oid);
247
248 if (info)
249 cgit_add_ref(list, info);
250 return 0;
251 }
252
253 void cgit_diff_tree_cb(struct diff_queue_struct *q,
254 struct diff_options *options, void *data)
255 {
256 int i;
257
258 for (i = 0; i < q->nr; i++) {
259 if (q->queue[i]->status == 'U')
260 continue;
261 ((filepair_fn)data)(q->queue[i]);
262 }
263 }
264
265 static int load_mmfile(mmfile_t *file, const struct object_id *oid)
266 {
267 enum object_type type;
268
269 if (is_null_oid(oid)) {
270 file->ptr = (char *)"";
271 file->size = 0;
272 } else {
273 file->ptr = read_sha1_file(oid->hash, &type,
274 (unsigned long *)&file->size);
275 }
276 return 1;
277 }
278
279 /*
280 * Receive diff-buffers from xdiff and concatenate them as
281 * needed across multiple callbacks.
282 *
283 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
284 * ripped from git and modified to use globals instead of
285 * a special callback-struct.
286 */
287 static char *diffbuf = NULL;
288 static int buflen = 0;
289
290 static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
291 {
292 int i;
293
294 for (i = 0; i < nbuf; i++) {
295 if (mb[i].ptr[mb[i].size-1] != '\n') {
296 /* Incomplete line */
297 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
298 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
299 buflen += mb[i].size;
300 continue;
301 }
302
303 /* we have a complete line */
304 if (!diffbuf) {
305 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
306 continue;
307 }
308 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
309 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
310 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
311 free(diffbuf);
312 diffbuf = NULL;
313 buflen = 0;
314 }
315 if (diffbuf) {
316 ((linediff_fn)priv)(diffbuf, buflen);
317 free(diffbuf);
318 diffbuf = NULL;
319 buflen = 0;
320 }
321 return 0;
322 }
323
324 int cgit_diff_files(const struct object_id *old_oid,
325 const struct object_id *new_oid, unsigned long *old_size,
326 unsigned long *new_size, int *binary, int context,
327 int ignorews, linediff_fn fn)
328 {
329 mmfile_t file1, file2;
330 xpparam_t diff_params;
331 xdemitconf_t emit_params;
332 xdemitcb_t emit_cb;
333
334 if (!load_mmfile(&file1, old_oid) || !load_mmfile(&file2, new_oid))
335 return 1;
336
337 *old_size = file1.size;
338 *new_size = file2.size;
339
340 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
341 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
342 *binary = 1;
343 if (file1.size)
344 free(file1.ptr);
345 if (file2.size)
346 free(file2.ptr);
347 return 0;
348 }
349
350 memset(&diff_params, 0, sizeof(diff_params));
351 memset(&emit_params, 0, sizeof(emit_params));
352 memset(&emit_cb, 0, sizeof(emit_cb));
353 diff_params.flags = XDF_NEED_MINIMAL;
354 if (ignorews)
355 diff_params.flags |= XDF_IGNORE_WHITESPACE;
356 emit_params.ctxlen = context > 0 ? context : 3;
357 emit_params.flags = XDL_EMIT_FUNCNAMES;
358 emit_cb.outf = filediff_cb;
359 emit_cb.priv = fn;
360 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
361 if (file1.size)
362 free(file1.ptr);
363 if (file2.size)
364 free(file2.ptr);
365 return 0;
366 }
367
368 void cgit_diff_tree(const struct object_id *old_oid,
369 const struct object_id *new_oid,
370 filepair_fn fn, const char *prefix, int ignorews)
371 {
372 struct diff_options opt;
373 struct pathspec_item item;
374
375 memset(&item, 0, sizeof(item));
376 diff_setup(&opt);
377 opt.output_format = DIFF_FORMAT_CALLBACK;
378 opt.detect_rename = 1;
379 opt.rename_limit = ctx.cfg.renamelimit;
380 DIFF_OPT_SET(&opt, RECURSIVE);
381 if (ignorews)
382 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
383 opt.format_callback = cgit_diff_tree_cb;
384 opt.format_callback_data = fn;
385 if (prefix) {
386 item.match = prefix;
387 item.len = strlen(prefix);
388 opt.pathspec.nr = 1;
389 opt.pathspec.items = &item;
390 }
391 diff_setup_done(&opt);
392
393 if (old_oid && !is_null_oid(old_oid))
394 diff_tree_sha1(old_oid->hash, new_oid->hash, "", &opt);
395 else
396 diff_root_tree_sha1(new_oid->hash, "", &opt);
397 diffcore_std(&opt);
398 diff_flush(&opt);
399 }
400
401 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
402 {
403 const struct object_id *old_oid = NULL;
404
405 if (commit->parents)
406 old_oid = &commit->parents->item->object.oid;
407 cgit_diff_tree(old_oid, &commit->object.oid, fn, prefix,
408 ctx.qry.ignorews);
409 }
410
411 int cgit_parse_snapshots_mask(const char *str)
412 {
413 struct string_list tokens = STRING_LIST_INIT_DUP;
414 struct string_list_item *item;
415 const struct cgit_snapshot_format *f;
416 int rv = 0;
417
418 /* favor legacy setting */
419 if (atoi(str))
420 return 1;
421
422 string_list_split(&tokens, str, ' ', -1);
423 string_list_remove_empty_items(&tokens, 0);
424
425 for_each_string_list_item(item, &tokens) {
426 for (f = cgit_snapshot_formats; f->suffix; f++) {
427 if (!strcmp(item->string, f->suffix) ||
428 !strcmp(item->string, f->suffix + 1)) {
429 rv |= f->bit;
430 break;
431 }
432 }
433 }
434
435 string_list_clear(&tokens, 0);
436 return rv;
437 }
438
439 typedef struct {
440 char * name;
441 char * value;
442 } cgit_env_var;
443
444 void cgit_prepare_repo_env(struct cgit_repo * repo)
445 {
446 cgit_env_var env_vars[] = {
447 { .name = "CGIT_REPO_URL", .value = repo->url },
448 { .name = "CGIT_REPO_NAME", .value = repo->name },
449 { .name = "CGIT_REPO_PATH", .value = repo->path },
450 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
451 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
452 { .name = "CGIT_REPO_SECTION", .value = repo->section },
453 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
454 };
455 int env_var_count = ARRAY_SIZE(env_vars);
456 cgit_env_var *p, *q;
457 static char *warn = "cgit warning: failed to set env: %s=%s\n";
458
459 p = env_vars;
460 q = p + env_var_count;
461 for (; p < q; p++)
462 if (p->value && setenv(p->name, p->value, 1))
463 fprintf(stderr, warn, p->name, p->value);
464 }
465
466 /* Read the content of the specified file into a newly allocated buffer,
467 * zeroterminate the buffer and return 0 on success, errno otherwise.
468 */
469 int readfile(const char *path, char **buf, size_t *size)
470 {
471 int fd, e;
472 struct stat st;
473
474 fd = open(path, O_RDONLY);
475 if (fd == -1)
476 return errno;
477 if (fstat(fd, &st)) {
478 e = errno;
479 close(fd);
480 return e;
481 }
482 if (!S_ISREG(st.st_mode)) {
483 close(fd);
484 return EISDIR;
485 }
486 *buf = xmalloc(st.st_size + 1);
487 *size = read_in_full(fd, *buf, st.st_size);
488 e = errno;
489 (*buf)[*size] = '\0';
490 close(fd);
491 return (*size == st.st_size ? 0 : e);
492 }
493
494 static int is_token_char(char c)
495 {
496 return isalnum(c) || c == '_';
497 }
498
499 /* Replace name with getenv(name), return pointer to zero-terminating char
500 */
501 static char *expand_macro(char *name, int maxlength)
502 {
503 char *value;
504 int len;
505
506 len = 0;
507 value = getenv(name);
508 if (value) {
509 len = strlen(value);
510 if (len > maxlength)
511 len = maxlength;
512 strncpy(name, value, len);
513 }
514 return name + len;
515 }
516
517 #define EXPBUFSIZE (1024 * 8)
518
519 /* Replace all tokens prefixed by '$' in the specified text with the
520 * value of the named environment variable.
521 * NB: the return value is a static buffer, i.e. it must be strdup'd
522 * by the caller.
523 */
524 char *expand_macros(const char *txt)
525 {
526 static char result[EXPBUFSIZE];
527 char *p, *start;
528 int len;
529
530 p = result;
531 start = NULL;
532 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
533 *p = *txt;
534 if (start) {
535 if (!is_token_char(*txt)) {
536 if (p - start > 0) {
537 *p = '\0';
538 len = result + EXPBUFSIZE - start - 1;
539 p = expand_macro(start, len) - 1;
540 }
541 start = NULL;
542 txt--;
543 }
544 p++;
545 txt++;
546 continue;
547 }
548 if (*txt == '$') {
549 start = p;
550 txt++;
551 continue;
552 }
553 p++;
554 txt++;
555 }
556 *p = '\0';
557 if (start && p - start > 0) {
558 len = result + EXPBUFSIZE - start - 1;
559 p = expand_macro(start, len);
560 *p = '\0';
561 }
562 return result;
563 }
564
565 char *get_mimetype_for_filename(const char *filename)
566 {
567 char *ext, *mimetype, *token, line[1024], *saveptr;
568 FILE *file;
569 struct string_list_item *mime;
570
571 if (!filename)
572 return NULL;
573
574 ext = strrchr(filename, '.');
575 if (!ext)
576 return NULL;
577 ++ext;
578 if (!ext[0])
579 return NULL;
580 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
581 if (mime)
582 return xstrdup(mime->util);
583
584 if (!ctx.cfg.mimetype_file)
585 return NULL;
586 file = fopen(ctx.cfg.mimetype_file, "r");
587 if (!file)
588 return NULL;
589 while (fgets(line, sizeof(line), file)) {
590 if (!line[0] || line[0] == '#')
591 continue;
592 mimetype = strtok_r(line, " \t\r\n", &saveptr);
593 while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) {
594 if (!strcasecmp(ext, token)) {
595 fclose(file);
596 return xstrdup(mimetype);
597 }
598 }
599 }
600 fclose(file);
601 return NULL;
602 }