]> git.cameronkatri.com Git - cgit.git/blob - shared.c
ui-tree.c: add support for path-selected submodule links
[cgit.git] / shared.c
1 /* shared.c: global vars + some callback functions
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include <stdio.h>
11 #include <linux/limits.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("%s: %s", msg, strerror(errno));
20 return result;
21 }
22
23 int chk_positive(int result, char *msg)
24 {
25 if (result <= 0)
26 die("%s: %s", msg, strerror(errno));
27 return result;
28 }
29
30 int chk_non_negative(int result, char *msg)
31 {
32 if (result < 0)
33 die("%s: %s",msg, strerror(errno));
34 return result;
35 }
36
37 struct cgit_repo *cgit_add_repo(const char *url)
38 {
39 struct cgit_repo *ret;
40
41 if (++cgit_repolist.count > cgit_repolist.length) {
42 if (cgit_repolist.length == 0)
43 cgit_repolist.length = 8;
44 else
45 cgit_repolist.length *= 2;
46 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
47 cgit_repolist.length *
48 sizeof(struct cgit_repo));
49 }
50
51 ret = &cgit_repolist.repos[cgit_repolist.count-1];
52 memset(ret, 0, sizeof(struct cgit_repo));
53 ret->url = trim_end(url, '/');
54 ret->name = ret->url;
55 ret->path = NULL;
56 ret->desc = "[no description]";
57 ret->owner = NULL;
58 ret->section = ctx.cfg.section;
59 ret->defbranch = "master";
60 ret->snapshots = ctx.cfg.snapshots;
61 ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
62 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
63 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
64 ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
65 ret->enable_subject_links = ctx.cfg.enable_subject_links;
66 ret->max_stats = ctx.cfg.max_stats;
67 ret->module_link = ctx.cfg.module_link;
68 ret->readme = ctx.cfg.readme;
69 ret->mtime = -1;
70 ret->about_filter = ctx.cfg.about_filter;
71 ret->commit_filter = ctx.cfg.commit_filter;
72 ret->source_filter = ctx.cfg.source_filter;
73 ret->clone_url = ctx.cfg.clone_url;
74 ret->submodules.strdup_strings = 1;
75 return ret;
76 }
77
78 struct cgit_repo *cgit_get_repoinfo(const char *url)
79 {
80 int i;
81 struct cgit_repo *repo;
82
83 for (i=0; i<cgit_repolist.count; i++) {
84 repo = &cgit_repolist.repos[i];
85 if (!strcmp(repo->url, url))
86 return repo;
87 }
88 return NULL;
89 }
90
91 void *cgit_free_commitinfo(struct commitinfo *info)
92 {
93 free(info->author);
94 free(info->author_email);
95 free(info->committer);
96 free(info->committer_email);
97 free(info->subject);
98 free(info->msg);
99 free(info->msg_encoding);
100 free(info);
101 return NULL;
102 }
103
104 char *trim_end(const char *str, char c)
105 {
106 int len;
107
108 if (str == NULL)
109 return NULL;
110 len = strlen(str);
111 while(len > 0 && str[len - 1] == c)
112 len--;
113 if (len == 0)
114 return NULL;
115 return xstrndup(str, len);
116 }
117
118 char *strlpart(char *txt, int maxlen)
119 {
120 char *result;
121
122 if (!txt)
123 return txt;
124
125 if (strlen(txt) <= maxlen)
126 return txt;
127 result = xmalloc(maxlen + 1);
128 memcpy(result, txt, maxlen - 3);
129 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
130 result[maxlen] = '\0';
131 return result;
132 }
133
134 char *strrpart(char *txt, int maxlen)
135 {
136 char *result;
137
138 if (!txt)
139 return txt;
140
141 if (strlen(txt) <= maxlen)
142 return txt;
143 result = xmalloc(maxlen + 1);
144 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
145 result[0] = result[1] = result[2] = '.';
146 return result;
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 struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
162 {
163 struct refinfo *ref;
164
165 ref = xmalloc(sizeof (struct refinfo));
166 ref->refname = xstrdup(refname);
167 ref->object = parse_object(sha1);
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 int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
180 void *cb_data)
181 {
182 struct reflist *list = (struct reflist *)cb_data;
183 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
184
185 if (info)
186 cgit_add_ref(list, info);
187 return 0;
188 }
189
190 void cgit_diff_tree_cb(struct diff_queue_struct *q,
191 struct diff_options *options, void *data)
192 {
193 int i;
194
195 for (i = 0; i < q->nr; i++) {
196 if (q->queue[i]->status == 'U')
197 continue;
198 ((filepair_fn)data)(q->queue[i]);
199 }
200 }
201
202 static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
203 {
204 enum object_type type;
205
206 if (is_null_sha1(sha1)) {
207 file->ptr = (char *)"";
208 file->size = 0;
209 } else {
210 file->ptr = read_sha1_file(sha1, &type,
211 (unsigned long *)&file->size);
212 }
213 return 1;
214 }
215
216 /*
217 * Receive diff-buffers from xdiff and concatenate them as
218 * needed across multiple callbacks.
219 *
220 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
221 * ripped from git and modified to use globals instead of
222 * a special callback-struct.
223 */
224 char *diffbuf = NULL;
225 int buflen = 0;
226
227 int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
228 {
229 int i;
230
231 for (i = 0; i < nbuf; i++) {
232 if (mb[i].ptr[mb[i].size-1] != '\n') {
233 /* Incomplete line */
234 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
235 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
236 buflen += mb[i].size;
237 continue;
238 }
239
240 /* we have a complete line */
241 if (!diffbuf) {
242 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
243 continue;
244 }
245 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
246 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
247 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
248 free(diffbuf);
249 diffbuf = NULL;
250 buflen = 0;
251 }
252 if (diffbuf) {
253 ((linediff_fn)priv)(diffbuf, buflen);
254 free(diffbuf);
255 diffbuf = NULL;
256 buflen = 0;
257 }
258 return 0;
259 }
260
261 int cgit_diff_files(const unsigned char *old_sha1,
262 const unsigned char *new_sha1, unsigned long *old_size,
263 unsigned long *new_size, int *binary, int context,
264 int ignorews, linediff_fn fn)
265 {
266 mmfile_t file1, file2;
267 xpparam_t diff_params;
268 xdemitconf_t emit_params;
269 xdemitcb_t emit_cb;
270
271 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
272 return 1;
273
274 *old_size = file1.size;
275 *new_size = file2.size;
276
277 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
278 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
279 *binary = 1;
280 if (file1.size)
281 free(file1.ptr);
282 if (file2.size)
283 free(file2.ptr);
284 return 0;
285 }
286
287 memset(&diff_params, 0, sizeof(diff_params));
288 memset(&emit_params, 0, sizeof(emit_params));
289 memset(&emit_cb, 0, sizeof(emit_cb));
290 diff_params.flags = XDF_NEED_MINIMAL;
291 if (ignorews)
292 diff_params.flags |= XDF_IGNORE_WHITESPACE;
293 emit_params.ctxlen = context > 0 ? context : 3;
294 emit_params.flags = XDL_EMIT_FUNCNAMES;
295 emit_cb.outf = filediff_cb;
296 emit_cb.priv = fn;
297 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
298 if (file1.size)
299 free(file1.ptr);
300 if (file2.size)
301 free(file2.ptr);
302 return 0;
303 }
304
305 void cgit_diff_tree(const unsigned char *old_sha1,
306 const unsigned char *new_sha1,
307 filepair_fn fn, const char *prefix, int ignorews)
308 {
309 struct diff_options opt;
310 int prefixlen;
311
312 diff_setup(&opt);
313 opt.output_format = DIFF_FORMAT_CALLBACK;
314 opt.detect_rename = 1;
315 opt.rename_limit = ctx.cfg.renamelimit;
316 DIFF_OPT_SET(&opt, RECURSIVE);
317 if (ignorews)
318 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
319 opt.format_callback = cgit_diff_tree_cb;
320 opt.format_callback_data = fn;
321 if (prefix) {
322 opt.nr_paths = 1;
323 opt.paths = &prefix;
324 prefixlen = strlen(prefix);
325 opt.pathlens = &prefixlen;
326 }
327 diff_setup_done(&opt);
328
329 if (old_sha1 && !is_null_sha1(old_sha1))
330 diff_tree_sha1(old_sha1, new_sha1, "", &opt);
331 else
332 diff_root_tree_sha1(new_sha1, "", &opt);
333 diffcore_std(&opt);
334 diff_flush(&opt);
335 }
336
337 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
338 {
339 unsigned char *old_sha1 = NULL;
340
341 if (commit->parents)
342 old_sha1 = commit->parents->item->object.sha1;
343 cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
344 ctx.qry.ignorews);
345 }
346
347 int cgit_parse_snapshots_mask(const char *str)
348 {
349 const struct cgit_snapshot_format *f;
350 static const char *delim = " \t,:/|;";
351 int tl, sl, rv = 0;
352
353 /* favor legacy setting */
354 if(atoi(str))
355 return 1;
356 for(;;) {
357 str += strspn(str,delim);
358 tl = strcspn(str,delim);
359 if (!tl)
360 break;
361 for (f = cgit_snapshot_formats; f->suffix; f++) {
362 sl = strlen(f->suffix);
363 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
364 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
365 rv |= f->bit;
366 break;
367 }
368 }
369 str += tl;
370 }
371 return rv;
372 }
373
374 typedef struct {
375 char * name;
376 char * value;
377 } cgit_env_var;
378
379 void cgit_prepare_repo_env(struct cgit_repo * repo)
380 {
381 cgit_env_var env_vars[] = {
382 { .name = "CGIT_REPO_URL", .value = repo->url },
383 { .name = "CGIT_REPO_NAME", .value = repo->name },
384 { .name = "CGIT_REPO_PATH", .value = repo->path },
385 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
386 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
387 { .name = "CGIT_REPO_SECTION", .value = repo->section },
388 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
389 };
390 int env_var_count = ARRAY_SIZE(env_vars);
391 cgit_env_var *p, *q;
392 static char *warn = "cgit warning: failed to set env: %s=%s\n";
393
394 p = env_vars;
395 q = p + env_var_count;
396 for (; p < q; p++)
397 if (setenv(p->name, p->value, 1))
398 fprintf(stderr, warn, p->name, p->value);
399 }
400
401 int cgit_open_filter(struct cgit_filter *filter)
402 {
403
404 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
405 "Unable to duplicate STDOUT");
406 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
407 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
408 if (filter->pid == 0) {
409 close(filter->pipe_fh[1]);
410 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
411 "Unable to use pipe as STDIN");
412 execvp(filter->cmd, filter->argv);
413 die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
414 strerror(errno), errno);
415 }
416 close(filter->pipe_fh[0]);
417 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
418 "Unable to use pipe as STDOUT");
419 close(filter->pipe_fh[1]);
420 return 0;
421 }
422
423 int cgit_close_filter(struct cgit_filter *filter)
424 {
425 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
426 "Unable to restore STDOUT");
427 close(filter->old_stdout);
428 if (filter->pid < 0)
429 return 0;
430 waitpid(filter->pid, &filter->exitstatus, 0);
431 if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus))
432 return 0;
433 die("Subprocess %s exited abnormally", filter->cmd);
434 }
435
436 /* Read the content of the specified file into a newly allocated buffer,
437 * zeroterminate the buffer and return 0 on success, errno otherwise.
438 */
439 int readfile(const char *path, char **buf, size_t *size)
440 {
441 int fd, e;
442 struct stat st;
443
444 fd = open(path, O_RDONLY);
445 if (fd == -1)
446 return errno;
447 if (fstat(fd, &st)) {
448 e = errno;
449 close(fd);
450 return e;
451 }
452 if (!S_ISREG(st.st_mode)) {
453 close(fd);
454 return EISDIR;
455 }
456 *buf = xmalloc(st.st_size + 1);
457 *size = read_in_full(fd, *buf, st.st_size);
458 e = errno;
459 (*buf)[*size] = '\0';
460 close(fd);
461 return (*size == st.st_size ? 0 : e);
462 }
463
464 int is_token_char(char c)
465 {
466 return isalnum(c) || c == '_';
467 }
468
469 /* Replace name with getenv(name), return pointer to zero-terminating char
470 */
471 char *expand_macro(char *name, int maxlength)
472 {
473 char *value;
474 int len;
475
476 len = 0;
477 value = getenv(name);
478 if (value) {
479 len = strlen(value);
480 if (len > maxlength)
481 len = maxlength;
482 strncpy(name, value, len);
483 }
484 return name + len;
485 }
486
487 #define EXPBUFSIZE (1024 * 8)
488
489 /* Replace all tokens prefixed by '$' in the specified text with the
490 * value of the named environment variable.
491 * NB: the return value is a static buffer, i.e. it must be strdup'd
492 * by the caller.
493 */
494 char *expand_macros(const char *txt)
495 {
496 static char result[EXPBUFSIZE];
497 char *p, *start;
498 int len;
499
500 p = result;
501 start = NULL;
502 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
503 *p = *txt;
504 if (start) {
505 if (!is_token_char(*txt)) {
506 if (p - start > 0) {
507 *p = '\0';
508 len = result + EXPBUFSIZE - start - 1;
509 p = expand_macro(start, len) - 1;
510 }
511 start = NULL;
512 txt--;
513 }
514 p++;
515 txt++;
516 continue;
517 }
518 if (*txt == '$') {
519 start = p;
520 txt++;
521 continue;
522 }
523 p++;
524 txt++;
525 }
526 *p = '\0';
527 if (start && p - start > 0) {
528 len = result + EXPBUFSIZE - start - 1;
529 p = expand_macro(start, len);
530 *p = '\0';
531 }
532 return result;
533 }