blob: 3d7296d4245f7015e22ab40311231d66ded47092 [file] [log] [blame]
Lars Hjemlia1a79992006-12-16 01:14:01 +01001/* shared.c: global vars + some callback functions
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemlia1a79992006-12-16 01:14:01 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli44923f82006-12-11 17:25:41 +01009#include "cgit.h"
10
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010011struct cgit_repolist cgit_repolist;
Lars Hjemlid14d77f2008-02-16 11:53:40 +010012struct cgit_context ctx;
Lars Hjemlice1c7332007-02-03 15:02:55 +010013
Lars Hjemliab2ab952007-02-08 13:53:13 +010014int chk_zero(int result, char *msg)
15{
16 if (result != 0)
John Keeping1e9f1ee2013-05-18 16:21:36 +010017 die_errno("%s", msg);
Lars Hjemliab2ab952007-02-08 13:53:13 +010018 return result;
19}
20
21int chk_positive(int result, char *msg)
22{
23 if (result <= 0)
John Keeping1e9f1ee2013-05-18 16:21:36 +010024 die_errno("%s", msg);
Lars Hjemliab2ab952007-02-08 13:53:13 +010025 return result;
26}
27
Michael Krelin127f43d2007-07-20 20:56:43 +020028int chk_non_negative(int result, char *msg)
29{
Lukas Fleischer53bc7472013-03-03 16:04:29 +010030 if (result < 0)
John Keeping1e9f1ee2013-05-18 16:21:36 +010031 die_errno("%s", msg);
Michael Krelin127f43d2007-07-20 20:56:43 +020032 return result;
33}
34
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040035char *cgit_default_repo_desc = "[no description]";
Lars Hjemli163037e2008-03-24 17:26:08 +010036struct cgit_repo *cgit_add_repo(const char *url)
Lars Hjemlice1c7332007-02-03 15:02:55 +010037{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010038 struct cgit_repo *ret;
Lars Hjemlice1c7332007-02-03 15:02:55 +010039
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;
Lars Hjemli1b49de32007-05-13 11:24:23 +020045 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
46 cgit_repolist.length *
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010047 sizeof(struct cgit_repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +010048 }
49
50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
Lars Hjemli42e7a162009-08-24 10:14:02 +020051 memset(ret, 0, sizeof(struct cgit_repo));
Lars Hjemli4e40d852007-09-20 00:56:53 +020052 ret->url = trim_end(url, '/');
Lars Hjemlice1c7332007-02-03 15:02:55 +010053 ret->name = ret->url;
54 ret->path = NULL;
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040055 ret->desc = cgit_default_repo_desc;
Lars Hjemlice1c7332007-02-03 15:02:55 +010056 ret->owner = NULL;
Jason A. Donenfeld5f2664f2016-02-22 16:04:15 +010057 ret->homepage = NULL;
Lars Hjemlie7af0022009-08-23 22:58:39 +020058 ret->section = ctx.cfg.section;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010059 ret->snapshots = ctx.cfg.snapshots;
Johan Herland9a8d39c2010-11-15 18:39:50 +010060 ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010061 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
62 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
Lars Hjemli41934a32009-11-07 19:10:58 +010063 ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
Lars Hjemli581a0c22010-02-27 13:12:55 +010064 ret->enable_subject_links = ctx.cfg.enable_subject_links;
Jason A. Donenfeldc326f3e2016-01-14 14:53:28 +010065 ret->enable_html_serving = ctx.cfg.enable_html_serving;
Lars Hjemlifb2f3f62008-12-07 13:17:21 +010066 ret->max_stats = ctx.cfg.max_stats;
Jason A. Donenfeld389cc172013-04-08 16:57:12 +020067 ret->branch_sort = ctx.cfg.branch_sort;
Tobias Bieniek792f8132012-10-13 16:10:30 +020068 ret->commit_sort = ctx.cfg.commit_sort;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010069 ret->module_link = ctx.cfg.module_link;
Lars Hjemli515edb02010-08-21 15:08:01 +020070 ret->readme = ctx.cfg.readme;
Lars Hjemli88131702008-11-29 16:46:37 +010071 ret->mtime = -1;
Lars Hjemli537c05f2009-08-09 13:27:21 +020072 ret->about_filter = ctx.cfg.about_filter;
Lars Hjemlie976df22009-08-09 13:22:00 +020073 ret->commit_filter = ctx.cfg.commit_filter;
74 ret->source_filter = ctx.cfg.source_filter;
Jason A. Donenfelda5e15532014-01-13 04:04:52 +010075 ret->email_filter = ctx.cfg.email_filter;
Chris Burroughs96ceb9a2014-08-04 09:23:08 -040076 ret->owner_filter = ctx.cfg.owner_filter;
Lars Hjemlia1429db2011-06-06 20:49:13 +000077 ret->clone_url = ctx.cfg.clone_url;
Lars Hjemli6857bec2011-06-15 10:04:13 +020078 ret->submodules.strdup_strings = 1;
Lukas Fleischerc58cec92015-01-29 12:52:49 +010079 ret->hide = ret->ignore = 0;
Andy Green7d7ec482018-06-18 14:24:57 +080080 ret->inline_readme = ctx.cfg.inline_readme;
81
Lars Hjemlice1c7332007-02-03 15:02:55 +010082 return ret;
83}
84
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010085struct cgit_repo *cgit_get_repoinfo(const char *url)
Lars Hjemli305414d2007-05-18 00:47:47 +020086{
87 int i;
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010088 struct cgit_repo *repo;
Lars Hjemli305414d2007-05-18 00:47:47 +020089
Lukas Fleischer53bc7472013-03-03 16:04:29 +010090 for (i = 0; i < cgit_repolist.count; i++) {
Lars Hjemli305414d2007-05-18 00:47:47 +020091 repo = &cgit_repolist.repos[i];
Lukas Fleischerc58cec92015-01-29 12:52:49 +010092 if (repo->ignore)
93 continue;
Lars Hjemli305414d2007-05-18 00:47:47 +020094 if (!strcmp(repo->url, url))
95 return repo;
96 }
97 return NULL;
98}
99
John Keepingd9dff372016-08-13 11:51:58 +0100100void cgit_free_commitinfo(struct commitinfo *info)
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100101{
102 free(info->author);
103 free(info->author_email);
104 free(info->committer);
105 free(info->committer_email);
106 free(info->subject);
Jonathan Bastien-Filiatrault3845e172007-10-26 18:09:06 -0400107 free(info->msg);
108 free(info->msg_encoding);
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100109 free(info);
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100110}
Lars Hjemli52e605c2007-01-04 16:53:03 +0100111
Lars Hjemli382805e2007-06-26 18:04:31 +0200112char *trim_end(const char *str, char c)
113{
114 int len;
Lars Hjemli382805e2007-06-26 18:04:31 +0200115
116 if (str == NULL)
117 return NULL;
Lars Hjemlidc1a8ea2011-05-22 12:45:32 +0200118 len = strlen(str);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500119 while (len > 0 && str[len - 1] == c)
Lars Hjemli382805e2007-06-26 18:04:31 +0200120 len--;
Lars Hjemli382805e2007-06-26 18:04:31 +0200121 if (len == 0)
122 return NULL;
Lars Hjemlidc1a8ea2011-05-22 12:45:32 +0200123 return xstrndup(str, len);
Lars Hjemli382805e2007-06-26 18:04:31 +0200124}
125
John Keepingb1f17f12013-04-01 19:03:34 +0100126char *ensure_end(const char *str, char c)
127{
128 size_t len = strlen(str);
129 char *result;
130
131 if (len && str[len - 1] == c)
132 return xstrndup(str, len);
133
134 result = xmalloc(len + 2);
135 memcpy(result, str, len);
136 result[len] = '/';
137 result[len + 1] = '\0';
138 return result;
139}
140
John Keepingd2e20e32013-04-07 14:03:47 +0100141void strbuf_ensure_end(struct strbuf *sb, char c)
142{
143 if (!sb->len || sb->buf[sb->len - 1] != c)
144 strbuf_addch(sb, c);
145}
146
Lars Hjemlie397ff72007-10-25 09:30:06 +0200147void cgit_add_ref(struct reflist *list, struct refinfo *ref)
148{
149 size_t size;
150
151 if (list->count >= list->alloc) {
152 list->alloc += (list->alloc ? list->alloc : 4);
153 size = list->alloc * sizeof(struct refinfo *);
154 list->refs = xrealloc(list->refs, size);
155 }
156 list->refs[list->count++] = ref;
157}
158
Christian Hessede83de22015-07-28 10:42:01 +0200159static struct refinfo *cgit_mk_refinfo(const char *refname, const struct object_id *oid)
Lars Hjemlie397ff72007-10-25 09:30:06 +0200160{
161 struct refinfo *ref;
162
163 ref = xmalloc(sizeof (struct refinfo));
164 ref->refname = xstrdup(refname);
Jeff Smith86a6d352017-08-09 19:02:56 -0500165 ref->object = parse_object(oid);
Lars Hjemlie397ff72007-10-25 09:30:06 +0200166 switch (ref->object->type) {
167 case OBJ_TAG:
168 ref->tag = cgit_parse_tag((struct tag *)ref->object);
169 break;
170 case OBJ_COMMIT:
171 ref->commit = cgit_parse_commit((struct commit *)ref->object);
172 break;
173 }
174 return ref;
175}
176
John Keepingb19d8892016-08-13 11:52:51 +0100177void cgit_free_taginfo(struct taginfo *tag)
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100178{
179 if (tag->tagger)
180 free(tag->tagger);
181 if (tag->tagger_email)
182 free(tag->tagger_email);
183 if (tag->msg)
184 free(tag->msg);
185 free(tag);
186}
187
188static void cgit_free_refinfo(struct refinfo *ref)
189{
190 if (ref->refname)
191 free((char *)ref->refname);
192 switch (ref->object->type) {
193 case OBJ_TAG:
194 cgit_free_taginfo(ref->tag);
195 break;
196 case OBJ_COMMIT:
197 cgit_free_commitinfo(ref->commit);
198 break;
199 }
200 free(ref);
201}
202
203void cgit_free_reflist_inner(struct reflist *list)
204{
205 int i;
206
207 for (i = 0; i < list->count; i++) {
208 cgit_free_refinfo(list->refs[i]);
209 }
210 free(list->refs);
211}
212
Christian Hessede83de22015-07-28 10:42:01 +0200213int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
Lars Hjemlie397ff72007-10-25 09:30:06 +0200214 void *cb_data)
215{
216 struct reflist *list = (struct reflist *)cb_data;
Christian Hessede83de22015-07-28 10:42:01 +0200217 struct refinfo *info = cgit_mk_refinfo(refname, oid);
Lars Hjemlie397ff72007-10-25 09:30:06 +0200218
219 if (info)
220 cgit_add_ref(list, info);
221 return 0;
222}
223
John Keeping044e2d22015-08-12 15:34:48 +0100224void cgit_diff_tree_cb(struct diff_queue_struct *q,
225 struct diff_options *options, void *data)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200226{
227 int i;
228
229 for (i = 0; i < q->nr; i++) {
230 if (q->queue[i]->status == 'U')
231 continue;
232 ((filepair_fn)data)(q->queue[i]);
233 }
234}
235
Christian Hesse11695a52016-09-04 12:38:18 +0200236static int load_mmfile(mmfile_t *file, const struct object_id *oid)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200237{
238 enum object_type type;
239
Christian Hesse11695a52016-09-04 12:38:18 +0200240 if (is_null_oid(oid)) {
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200241 file->ptr = (char *)"";
242 file->size = 0;
243 } else {
Christian Hesse255b78f2018-06-04 18:49:28 +0200244 file->ptr = read_object_file(oid, &type,
Lars Hjemli0835ffe2007-09-20 00:21:47 +0200245 (unsigned long *)&file->size);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200246 }
247 return 1;
248}
249
250/*
251 * Receive diff-buffers from xdiff and concatenate them as
252 * needed across multiple callbacks.
253 *
254 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
255 * ripped from git and modified to use globals instead of
256 * a special callback-struct.
257 */
John Keepingeefd5e02015-03-08 16:32:19 +0000258static char *diffbuf = NULL;
259static int buflen = 0;
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200260
Lukas Fleischerbafab422013-03-04 08:52:33 +0100261static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200262{
263 int i;
264
265 for (i = 0; i < nbuf; i++) {
266 if (mb[i].ptr[mb[i].size-1] != '\n') {
267 /* Incomplete line */
268 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
269 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
270 buflen += mb[i].size;
271 continue;
272 }
273
274 /* we have a complete line */
275 if (!diffbuf) {
276 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
277 continue;
278 }
279 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
280 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
281 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
282 free(diffbuf);
283 diffbuf = NULL;
284 buflen = 0;
285 }
286 if (diffbuf) {
287 ((linediff_fn)priv)(diffbuf, buflen);
288 free(diffbuf);
289 diffbuf = NULL;
290 buflen = 0;
291 }
292 return 0;
293}
294
Christian Hesse11695a52016-09-04 12:38:18 +0200295int cgit_diff_files(const struct object_id *old_oid,
296 const struct object_id *new_oid, unsigned long *old_size,
Johan Herland6180e612010-06-10 20:15:27 +0200297 unsigned long *new_size, int *binary, int context,
Johan Herland2cc8b992010-06-24 17:52:57 +0200298 int ignorews, linediff_fn fn)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200299{
300 mmfile_t file1, file2;
301 xpparam_t diff_params;
302 xdemitconf_t emit_params;
303 xdemitcb_t emit_cb;
304
Christian Hesse11695a52016-09-04 12:38:18 +0200305 if (!load_mmfile(&file1, old_oid) || !load_mmfile(&file2, new_oid))
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200306 return 1;
307
Lars Hjemlic495cf02009-01-31 10:40:40 +0100308 *old_size = file1.size;
309 *new_size = file2.size;
310
Lars Hjemli481ce5e2009-02-01 19:29:24 +0100311 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
312 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
Lars Hjemlic495cf02009-01-31 10:40:40 +0100313 *binary = 1;
Lars Hjemlice761fd2010-04-08 00:48:36 +0200314 if (file1.size)
315 free(file1.ptr);
316 if (file2.size)
317 free(file2.ptr);
Lars Hjemlic495cf02009-01-31 10:40:40 +0100318 return 0;
319 }
320
Lars Hjemli0edf7602008-12-26 11:02:02 +0100321 memset(&diff_params, 0, sizeof(diff_params));
322 memset(&emit_params, 0, sizeof(emit_params));
323 memset(&emit_cb, 0, sizeof(emit_cb));
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200324 diff_params.flags = XDF_NEED_MINIMAL;
Johan Herland2cc8b992010-06-24 17:52:57 +0200325 if (ignorews)
326 diff_params.flags |= XDF_IGNORE_WHITESPACE;
Johan Herland6180e612010-06-10 20:15:27 +0200327 emit_params.ctxlen = context > 0 ? context : 3;
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200328 emit_params.flags = XDL_EMIT_FUNCNAMES;
329 emit_cb.outf = filediff_cb;
330 emit_cb.priv = fn;
331 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
Lars Hjemlice761fd2010-04-08 00:48:36 +0200332 if (file1.size)
333 free(file1.ptr);
334 if (file2.size)
335 free(file2.ptr);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200336 return 0;
337}
338
Christian Hesse11695a52016-09-04 12:38:18 +0200339void cgit_diff_tree(const struct object_id *old_oid,
340 const struct object_id *new_oid,
Johan Herland2cc8b992010-06-24 17:52:57 +0200341 filepair_fn fn, const char *prefix, int ignorews)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200342{
343 struct diff_options opt;
John Keepingbfc14d02013-03-02 12:32:10 +0000344 struct pathspec_item item;
Lars Hjemli1b49de32007-05-13 11:24:23 +0200345
Jason A. Donenfeld7e1c0ed2014-02-20 19:48:24 +0100346 memset(&item, 0, sizeof(item));
Lars Hjemli1b49de32007-05-13 11:24:23 +0200347 diff_setup(&opt);
348 opt.output_format = DIFF_FORMAT_CALLBACK;
349 opt.detect_rename = 1;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100350 opt.rename_limit = ctx.cfg.renamelimit;
Christian Hesse1dd53e32018-01-18 09:19:31 +0100351 opt.flags.recursive = 1;
Johan Herland2cc8b992010-06-24 17:52:57 +0200352 if (ignorews)
353 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200354 opt.format_callback = cgit_diff_tree_cb;
355 opt.format_callback_data = fn;
Lars Hjemlif527a572007-10-01 11:42:19 +0200356 if (prefix) {
Christian Hesse3d33b462017-07-24 17:22:52 +0200357 item.match = xstrdup(prefix);
John Keepingbfc14d02013-03-02 12:32:10 +0000358 item.len = strlen(prefix);
359 opt.pathspec.nr = 1;
360 opt.pathspec.items = &item;
Lars Hjemlif527a572007-10-01 11:42:19 +0200361 }
Lars Hjemli1b49de32007-05-13 11:24:23 +0200362 diff_setup_done(&opt);
363
Christian Hesse11695a52016-09-04 12:38:18 +0200364 if (old_oid && !is_null_oid(old_oid))
Jeff Smith86a6d352017-08-09 19:02:56 -0500365 diff_tree_oid(old_oid, new_oid, "", &opt);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200366 else
Jeff Smith86a6d352017-08-09 19:02:56 -0500367 diff_root_tree_oid(new_oid, "", &opt);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200368 diffcore_std(&opt);
369 diff_flush(&opt);
Christian Hesse3d33b462017-07-24 17:22:52 +0200370
371 free(item.match);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200372}
373
Johan Herland1415f3f2010-09-30 20:15:14 +0200374void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200375{
Christian Hesse11695a52016-09-04 12:38:18 +0200376 const struct object_id *old_oid = NULL;
Lars Hjemli1b49de32007-05-13 11:24:23 +0200377
378 if (commit->parents)
Christian Hesse11695a52016-09-04 12:38:18 +0200379 old_oid = &commit->parents->item->object.oid;
380 cgit_diff_tree(old_oid, &commit->object.oid, fn, prefix,
Johan Herland2cc8b992010-06-24 17:52:57 +0200381 ctx.qry.ignorews);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200382}
Lars Hjemlif34478c2008-03-24 16:00:27 +0100383
384int cgit_parse_snapshots_mask(const char *str)
385{
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100386 struct string_list tokens = STRING_LIST_INIT_DUP;
387 struct string_list_item *item;
Lars Hjemlif34478c2008-03-24 16:00:27 +0100388 const struct cgit_snapshot_format *f;
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100389 int rv = 0;
Lars Hjemlif34478c2008-03-24 16:00:27 +0100390
391 /* favor legacy setting */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500392 if (atoi(str))
Lars Hjemlif34478c2008-03-24 16:00:27 +0100393 return 1;
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100394
Christian Hesse30a378b2018-06-07 22:01:50 +0200395 if (strcmp(str, "all") == 0)
396 return INT_MAX;
397
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100398 string_list_split(&tokens, str, ' ', -1);
399 string_list_remove_empty_items(&tokens, 0);
400
401 for_each_string_list_item(item, &tokens) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100402 for (f = cgit_snapshot_formats; f->suffix; f++) {
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100403 if (!strcmp(item->string, f->suffix) ||
404 !strcmp(item->string, f->suffix + 1)) {
Christian Hesse2f8648f2018-06-11 08:26:59 +0200405 rv |= cgit_snapshot_format_bit(f);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100406 break;
407 }
408 }
Lars Hjemlif34478c2008-03-24 16:00:27 +0100409 }
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100410
411 string_list_clear(&tokens, 0);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100412 return rv;
413}
Lars Hjemlid6f60722009-07-31 17:38:38 +0200414
Ferry Huberts14f28922011-03-23 11:57:43 +0100415typedef struct {
416 char * name;
417 char * value;
418} cgit_env_var;
419
Lars Hjemli3ec6b302011-06-06 19:29:58 +0000420void cgit_prepare_repo_env(struct cgit_repo * repo)
421{
Ferry Huberts14f28922011-03-23 11:57:43 +0100422 cgit_env_var env_vars[] = {
423 { .name = "CGIT_REPO_URL", .value = repo->url },
424 { .name = "CGIT_REPO_NAME", .value = repo->name },
425 { .name = "CGIT_REPO_PATH", .value = repo->path },
426 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
427 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
428 { .name = "CGIT_REPO_SECTION", .value = repo->section },
429 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
430 };
431 int env_var_count = ARRAY_SIZE(env_vars);
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100432 cgit_env_var *p, *q;
433 static char *warn = "cgit warning: failed to set env: %s=%s\n";
Ferry Huberts14f28922011-03-23 11:57:43 +0100434
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100435 p = env_vars;
436 q = p + env_var_count;
437 for (; p < q; p++)
Lukas Fleischerd96d2c92011-09-14 11:52:43 +0200438 if (p->value && setenv(p->name, p->value, 1))
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100439 fprintf(stderr, warn, p->name, p->value);
Ferry Huberts14f28922011-03-23 11:57:43 +0100440}
441
Lars Hjemlie16f1782009-08-18 17:17:41 +0200442/* Read the content of the specified file into a newly allocated buffer,
443 * zeroterminate the buffer and return 0 on success, errno otherwise.
444 */
445int readfile(const char *path, char **buf, size_t *size)
446{
Lars Hjemli21f67e72009-11-07 18:08:30 +0100447 int fd, e;
Lars Hjemlie16f1782009-08-18 17:17:41 +0200448 struct stat st;
449
450 fd = open(path, O_RDONLY);
451 if (fd == -1)
452 return errno;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100453 if (fstat(fd, &st)) {
Lars Hjemli21f67e72009-11-07 18:08:30 +0100454 e = errno;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100455 close(fd);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100456 return e;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100457 }
458 if (!S_ISREG(st.st_mode)) {
459 close(fd);
Lars Hjemlie16f1782009-08-18 17:17:41 +0200460 return EISDIR;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100461 }
Lars Hjemlie16f1782009-08-18 17:17:41 +0200462 *buf = xmalloc(st.st_size + 1);
463 *size = read_in_full(fd, *buf, st.st_size);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100464 e = errno;
Lars Hjemlie16f1782009-08-18 17:17:41 +0200465 (*buf)[*size] = '\0';
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100466 close(fd);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100467 return (*size == st.st_size ? 0 : e);
Lars Hjemlie16f1782009-08-18 17:17:41 +0200468}
Lars Hjemliba56a372010-03-22 00:09:43 +0100469
Lukas Fleischerbafab422013-03-04 08:52:33 +0100470static int is_token_char(char c)
Lars Hjemliba56a372010-03-22 00:09:43 +0100471{
472 return isalnum(c) || c == '_';
473}
474
475/* Replace name with getenv(name), return pointer to zero-terminating char
476 */
Lukas Fleischerbafab422013-03-04 08:52:33 +0100477static char *expand_macro(char *name, int maxlength)
Lars Hjemliba56a372010-03-22 00:09:43 +0100478{
479 char *value;
480 int len;
481
482 len = 0;
483 value = getenv(name);
484 if (value) {
485 len = strlen(value);
486 if (len > maxlength)
487 len = maxlength;
Andy Green2d789a82018-06-26 18:29:56 +0800488 memcpy(name, value, len);
Lars Hjemliba56a372010-03-22 00:09:43 +0100489 }
490 return name + len;
491}
492
493#define EXPBUFSIZE (1024 * 8)
494
495/* Replace all tokens prefixed by '$' in the specified text with the
496 * value of the named environment variable.
497 * NB: the return value is a static buffer, i.e. it must be strdup'd
498 * by the caller.
499 */
500char *expand_macros(const char *txt)
501{
502 static char result[EXPBUFSIZE];
503 char *p, *start;
504 int len;
505
506 p = result;
507 start = NULL;
508 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
509 *p = *txt;
510 if (start) {
511 if (!is_token_char(*txt)) {
512 if (p - start > 0) {
513 *p = '\0';
514 len = result + EXPBUFSIZE - start - 1;
515 p = expand_macro(start, len) - 1;
516 }
517 start = NULL;
518 txt--;
519 }
520 p++;
521 txt++;
522 continue;
523 }
524 if (*txt == '$') {
525 start = p;
526 txt++;
527 continue;
528 }
529 p++;
530 txt++;
531 }
532 *p = '\0';
533 if (start && p - start > 0) {
534 len = result + EXPBUFSIZE - start - 1;
535 p = expand_macro(start, len);
536 *p = '\0';
537 }
538 return result;
539}
Christian Hessef5c83d72015-08-14 16:50:56 +0200540
Christian Hesseaa943bc2015-08-16 14:53:52 +0200541char *get_mimetype_for_filename(const char *filename)
Christian Hessef5c83d72015-08-14 16:50:56 +0200542{
Jason A. Donenfeldad006912015-10-09 11:01:04 +0200543 char *ext, *mimetype, *token, line[1024], *saveptr;
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200544 FILE *file;
Christian Hesseaa943bc2015-08-16 14:53:52 +0200545 struct string_list_item *mime;
Christian Hessef5c83d72015-08-14 16:50:56 +0200546
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200547 if (!filename)
Christian Hessef5c83d72015-08-14 16:50:56 +0200548 return NULL;
549
Christian Hesseaa943bc2015-08-16 14:53:52 +0200550 ext = strrchr(filename, '.');
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200551 if (!ext)
552 return NULL;
553 ++ext;
554 if (!ext[0])
555 return NULL;
556 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
557 if (mime)
558 return xstrdup(mime->util);
Christian Hessef5c83d72015-08-14 16:50:56 +0200559
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200560 if (!ctx.cfg.mimetype_file)
561 return NULL;
562 file = fopen(ctx.cfg.mimetype_file, "r");
563 if (!file)
564 return NULL;
565 while (fgets(line, sizeof(line), file)) {
566 if (!line[0] || line[0] == '#')
567 continue;
Jason A. Donenfeldad006912015-10-09 11:01:04 +0200568 mimetype = strtok_r(line, " \t\r\n", &saveptr);
569 while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) {
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200570 if (!strcasecmp(ext, token)) {
571 fclose(file);
572 return xstrdup(mimetype);
Christian Hessef5c83d72015-08-14 16:50:56 +0200573 }
574 }
575 }
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200576 fclose(file);
577 return NULL;
Christian Hessef5c83d72015-08-14 16:50:56 +0200578}
John Keeping653da152018-06-18 14:22:12 +0800579
580struct cgit_filter *get_render_for_filename(const char *filename)
581{
582 char *ext;
583 struct string_list_item *item;
584
585 if (!filename)
586 return NULL;
587
588 ext = strrchr(filename, '.');
589 if (!ext)
590 ext = ".";
591 ++ext;
592 item = string_list_lookup(&ctx.cfg.render_filters, ext);
593 if (item)
594 return item->util;
595
596 return NULL;
597}