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