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