1 /* shared.c: global vars + some callback functions
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 struct cgit_repolist cgit_repolist;
13 struct cgit_context ctx;
15 int chk_zero(int result, char *msg)
22 int chk_positive(int result, char *msg)
29 int chk_non_negative(int result, char *msg)
36 char *cgit_default_repo_desc = "[no description]";
37 struct cgit_repo *cgit_add_repo(const char *url)
39 struct cgit_repo *ret;
41 if (++cgit_repolist.count > cgit_repolist.length) {
42 if (cgit_repolist.length == 0)
43 cgit_repolist.length = 8;
45 cgit_repolist.length *= 2;
46 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
47 cgit_repolist.length *
48 sizeof(struct cgit_repo));
51 ret = &cgit_repolist.repos[cgit_repolist.count-1];
52 memset(ret, 0, sizeof(struct cgit_repo));
53 ret->url = trim_end(url, '/');
56 ret->desc = cgit_default_repo_desc;
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->max_stats = ctx.cfg.max_stats;
66 ret->branch_sort = ctx.cfg.branch_sort;
67 ret->commit_sort = ctx.cfg.commit_sort;
68 ret->module_link = ctx.cfg.module_link;
69 ret->readme = ctx.cfg.readme;
71 ret->about_filter = ctx.cfg.about_filter;
72 ret->commit_filter = ctx.cfg.commit_filter;
73 ret->source_filter = ctx.cfg.source_filter;
74 ret->email_filter = ctx.cfg.email_filter;
75 ret->clone_url = ctx.cfg.clone_url;
76 ret->submodules.strdup_strings = 1;
80 struct cgit_repo *cgit_get_repoinfo(const char *url)
83 struct cgit_repo *repo;
85 for (i = 0; i < cgit_repolist.count; i++) {
86 repo = &cgit_repolist.repos[i];
87 if (!strcmp(repo->url, url))
93 void *cgit_free_commitinfo(struct commitinfo *info)
96 free(info->author_email);
97 free(info->committer);
98 free(info->committer_email);
101 free(info->msg_encoding);
106 char *trim_end(const char *str, char c)
113 while (len > 0 && str[len - 1] == c)
117 return xstrndup(str, len);
120 char *ensure_end(const char *str, char c)
122 size_t len = strlen(str);
125 if (len && str[len - 1] == c)
126 return xstrndup(str, len);
128 result = xmalloc(len + 2);
129 memcpy(result, str, len);
131 result[len + 1] = '\0';
135 void strbuf_ensure_end(struct strbuf *sb, char c)
137 if (!sb->len || sb->buf[sb->len - 1] != c)
141 char *strlpart(char *txt, int maxlen)
148 if (strlen(txt) <= maxlen)
150 result = xmalloc(maxlen + 1);
151 memcpy(result, txt, maxlen - 3);
152 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
153 result[maxlen] = '\0';
157 char *strrpart(char *txt, int maxlen)
164 if (strlen(txt) <= maxlen)
166 result = xmalloc(maxlen + 1);
167 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
168 result[0] = result[1] = result[2] = '.';
172 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
176 if (list->count >= list->alloc) {
177 list->alloc += (list->alloc ? list->alloc : 4);
178 size = list->alloc * sizeof(struct refinfo *);
179 list->refs = xrealloc(list->refs, size);
181 list->refs[list->count++] = ref;
184 static struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
188 ref = xmalloc(sizeof (struct refinfo));
189 ref->refname = xstrdup(refname);
190 ref->object = parse_object(sha1);
191 switch (ref->object->type) {
193 ref->tag = cgit_parse_tag((struct tag *)ref->object);
196 ref->commit = cgit_parse_commit((struct commit *)ref->object);
202 static void cgit_free_taginfo(struct taginfo *tag)
206 if (tag->tagger_email)
207 free(tag->tagger_email);
213 static void cgit_free_refinfo(struct refinfo *ref)
216 free((char *)ref->refname);
217 switch (ref->object->type) {
219 cgit_free_taginfo(ref->tag);
222 cgit_free_commitinfo(ref->commit);
228 void cgit_free_reflist_inner(struct reflist *list)
232 for (i = 0; i < list->count; i++) {
233 cgit_free_refinfo(list->refs[i]);
238 int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
241 struct reflist *list = (struct reflist *)cb_data;
242 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
245 cgit_add_ref(list, info);
249 static void cgit_diff_tree_cb(struct diff_queue_struct *q,
250 struct diff_options *options, void *data)
254 for (i = 0; i < q->nr; i++) {
255 if (q->queue[i]->status == 'U')
257 ((filepair_fn)data)(q->queue[i]);
261 static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
263 enum object_type type;
265 if (is_null_sha1(sha1)) {
266 file->ptr = (char *)"";
269 file->ptr = read_sha1_file(sha1, &type,
270 (unsigned long *)&file->size);
276 * Receive diff-buffers from xdiff and concatenate them as
277 * needed across multiple callbacks.
279 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
280 * ripped from git and modified to use globals instead of
281 * a special callback-struct.
283 char *diffbuf = NULL;
286 static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
290 for (i = 0; i < nbuf; i++) {
291 if (mb[i].ptr[mb[i].size-1] != '\n') {
292 /* Incomplete line */
293 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
294 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
295 buflen += mb[i].size;
299 /* we have a complete line */
301 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
304 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
305 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
306 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
312 ((linediff_fn)priv)(diffbuf, buflen);
320 int cgit_diff_files(const unsigned char *old_sha1,
321 const unsigned char *new_sha1, unsigned long *old_size,
322 unsigned long *new_size, int *binary, int context,
323 int ignorews, linediff_fn fn)
325 mmfile_t file1, file2;
326 xpparam_t diff_params;
327 xdemitconf_t emit_params;
330 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
333 *old_size = file1.size;
334 *new_size = file2.size;
336 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
337 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
346 memset(&diff_params, 0, sizeof(diff_params));
347 memset(&emit_params, 0, sizeof(emit_params));
348 memset(&emit_cb, 0, sizeof(emit_cb));
349 diff_params.flags = XDF_NEED_MINIMAL;
351 diff_params.flags |= XDF_IGNORE_WHITESPACE;
352 emit_params.ctxlen = context > 0 ? context : 3;
353 emit_params.flags = XDL_EMIT_FUNCNAMES;
354 emit_cb.outf = filediff_cb;
356 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
364 void cgit_diff_tree(const unsigned char *old_sha1,
365 const unsigned char *new_sha1,
366 filepair_fn fn, const char *prefix, int ignorews)
368 struct diff_options opt;
369 struct pathspec_item item;
371 memset(&item, 0, sizeof(item));
373 opt.output_format = DIFF_FORMAT_CALLBACK;
374 opt.detect_rename = 1;
375 opt.rename_limit = ctx.cfg.renamelimit;
376 DIFF_OPT_SET(&opt, RECURSIVE);
378 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
379 opt.format_callback = cgit_diff_tree_cb;
380 opt.format_callback_data = fn;
383 item.len = strlen(prefix);
385 opt.pathspec.items = &item;
387 diff_setup_done(&opt);
389 if (old_sha1 && !is_null_sha1(old_sha1))
390 diff_tree_sha1(old_sha1, new_sha1, "", &opt);
392 diff_root_tree_sha1(new_sha1, "", &opt);
397 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
399 unsigned char *old_sha1 = NULL;
402 old_sha1 = commit->parents->item->object.sha1;
403 cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
407 int cgit_parse_snapshots_mask(const char *str)
409 struct string_list tokens = STRING_LIST_INIT_DUP;
410 struct string_list_item *item;
411 const struct cgit_snapshot_format *f;
414 /* favor legacy setting */
418 string_list_split(&tokens, str, ' ', -1);
419 string_list_remove_empty_items(&tokens, 0);
421 for_each_string_list_item(item, &tokens) {
422 for (f = cgit_snapshot_formats; f->suffix; f++) {
423 if (!strcmp(item->string, f->suffix) ||
424 !strcmp(item->string, f->suffix + 1)) {
431 string_list_clear(&tokens, 0);
440 void cgit_prepare_repo_env(struct cgit_repo * repo)
442 cgit_env_var env_vars[] = {
443 { .name = "CGIT_REPO_URL", .value = repo->url },
444 { .name = "CGIT_REPO_NAME", .value = repo->name },
445 { .name = "CGIT_REPO_PATH", .value = repo->path },
446 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
447 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
448 { .name = "CGIT_REPO_SECTION", .value = repo->section },
449 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
451 int env_var_count = ARRAY_SIZE(env_vars);
453 static char *warn = "cgit warning: failed to set env: %s=%s\n";
456 q = p + env_var_count;
458 if (p->value && setenv(p->name, p->value, 1))
459 fprintf(stderr, warn, p->name, p->value);
462 /* Read the content of the specified file into a newly allocated buffer,
463 * zeroterminate the buffer and return 0 on success, errno otherwise.
465 int readfile(const char *path, char **buf, size_t *size)
470 fd = open(path, O_RDONLY);
473 if (fstat(fd, &st)) {
478 if (!S_ISREG(st.st_mode)) {
482 *buf = xmalloc(st.st_size + 1);
483 *size = read_in_full(fd, *buf, st.st_size);
485 (*buf)[*size] = '\0';
487 return (*size == st.st_size ? 0 : e);
490 static int is_token_char(char c)
492 return isalnum(c) || c == '_';
495 /* Replace name with getenv(name), return pointer to zero-terminating char
497 static char *expand_macro(char *name, int maxlength)
503 value = getenv(name);
508 strncpy(name, value, len);
513 #define EXPBUFSIZE (1024 * 8)
515 /* Replace all tokens prefixed by '$' in the specified text with the
516 * value of the named environment variable.
517 * NB: the return value is a static buffer, i.e. it must be strdup'd
520 char *expand_macros(const char *txt)
522 static char result[EXPBUFSIZE];
528 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
531 if (!is_token_char(*txt)) {
534 len = result + EXPBUFSIZE - start - 1;
535 p = expand_macro(start, len) - 1;
553 if (start && p - start > 0) {
554 len = result + EXPBUFSIZE - start - 1;
555 p = expand_macro(start, len);