blob: a48eea6a4d62e5cbbcc14201f062c5e080af0cda [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;
Lars Hjemlice1c7332007-02-03 15:02:55 +010080 return ret;
81}
82
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010083struct cgit_repo *cgit_get_repoinfo(const char *url)
Lars Hjemli305414d2007-05-18 00:47:47 +020084{
85 int i;
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010086 struct cgit_repo *repo;
Lars Hjemli305414d2007-05-18 00:47:47 +020087
Lukas Fleischer53bc7472013-03-03 16:04:29 +010088 for (i = 0; i < cgit_repolist.count; i++) {
Lars Hjemli305414d2007-05-18 00:47:47 +020089 repo = &cgit_repolist.repos[i];
Lukas Fleischerc58cec92015-01-29 12:52:49 +010090 if (repo->ignore)
91 continue;
Lars Hjemli305414d2007-05-18 00:47:47 +020092 if (!strcmp(repo->url, url))
93 return repo;
94 }
95 return NULL;
96}
97
Lars Hjemliaaa24bd2006-12-16 14:58:20 +010098void *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);
Jonathan Bastien-Filiatrault3845e172007-10-26 18:09:06 -0400105 free(info->msg);
106 free(info->msg_encoding);
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100107 free(info);
108 return NULL;
109}
Lars Hjemli52e605c2007-01-04 16:53:03 +0100110
Lars Hjemli382805e2007-06-26 18:04:31 +0200111char *trim_end(const char *str, char c)
112{
113 int len;
Lars Hjemli382805e2007-06-26 18:04:31 +0200114
115 if (str == NULL)
116 return NULL;
Lars Hjemlidc1a8ea2011-05-22 12:45:32 +0200117 len = strlen(str);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500118 while (len > 0 && str[len - 1] == c)
Lars Hjemli382805e2007-06-26 18:04:31 +0200119 len--;
Lars Hjemli382805e2007-06-26 18:04:31 +0200120 if (len == 0)
121 return NULL;
Lars Hjemlidc1a8ea2011-05-22 12:45:32 +0200122 return xstrndup(str, len);
Lars Hjemli382805e2007-06-26 18:04:31 +0200123}
124
John Keepingb1f17f12013-04-01 19:03:34 +0100125char *ensure_end(const char *str, char c)
126{
127 size_t len = strlen(str);
128 char *result;
129
130 if (len && str[len - 1] == c)
131 return xstrndup(str, len);
132
133 result = xmalloc(len + 2);
134 memcpy(result, str, len);
135 result[len] = '/';
136 result[len + 1] = '\0';
137 return result;
138}
139
John Keepingd2e20e32013-04-07 14:03:47 +0100140void strbuf_ensure_end(struct strbuf *sb, char c)
141{
142 if (!sb->len || sb->buf[sb->len - 1] != c)
143 strbuf_addch(sb, c);
144}
145
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100146char *strlpart(char *txt, int maxlen)
147{
148 char *result;
149
150 if (!txt)
151 return txt;
152
153 if (strlen(txt) <= maxlen)
154 return txt;
155 result = xmalloc(maxlen + 1);
156 memcpy(result, txt, maxlen - 3);
157 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
158 result[maxlen] = '\0';
159 return result;
160}
161
162char *strrpart(char *txt, int maxlen)
163{
164 char *result;
165
166 if (!txt)
167 return txt;
168
169 if (strlen(txt) <= maxlen)
170 return txt;
171 result = xmalloc(maxlen + 1);
172 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
173 result[0] = result[1] = result[2] = '.';
174 return result;
175}
176
Lars Hjemlie397ff72007-10-25 09:30:06 +0200177void cgit_add_ref(struct reflist *list, struct refinfo *ref)
178{
179 size_t size;
180
181 if (list->count >= list->alloc) {
182 list->alloc += (list->alloc ? list->alloc : 4);
183 size = list->alloc * sizeof(struct refinfo *);
184 list->refs = xrealloc(list->refs, size);
185 }
186 list->refs[list->count++] = ref;
187}
188
Christian Hessede83de22015-07-28 10:42:01 +0200189static struct refinfo *cgit_mk_refinfo(const char *refname, const struct object_id *oid)
Lars Hjemlie397ff72007-10-25 09:30:06 +0200190{
191 struct refinfo *ref;
192
193 ref = xmalloc(sizeof (struct refinfo));
194 ref->refname = xstrdup(refname);
Christian Hessede83de22015-07-28 10:42:01 +0200195 ref->object = parse_object(oid->hash);
Lars Hjemlie397ff72007-10-25 09:30:06 +0200196 switch (ref->object->type) {
197 case OBJ_TAG:
198 ref->tag = cgit_parse_tag((struct tag *)ref->object);
199 break;
200 case OBJ_COMMIT:
201 ref->commit = cgit_parse_commit((struct commit *)ref->object);
202 break;
203 }
204 return ref;
205}
206
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100207static void cgit_free_taginfo(struct taginfo *tag)
208{
209 if (tag->tagger)
210 free(tag->tagger);
211 if (tag->tagger_email)
212 free(tag->tagger_email);
213 if (tag->msg)
214 free(tag->msg);
215 free(tag);
216}
217
218static void cgit_free_refinfo(struct refinfo *ref)
219{
220 if (ref->refname)
221 free((char *)ref->refname);
222 switch (ref->object->type) {
223 case OBJ_TAG:
224 cgit_free_taginfo(ref->tag);
225 break;
226 case OBJ_COMMIT:
227 cgit_free_commitinfo(ref->commit);
228 break;
229 }
230 free(ref);
231}
232
233void cgit_free_reflist_inner(struct reflist *list)
234{
235 int i;
236
237 for (i = 0; i < list->count; i++) {
238 cgit_free_refinfo(list->refs[i]);
239 }
240 free(list->refs);
241}
242
Christian Hessede83de22015-07-28 10:42:01 +0200243int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
Lars Hjemlie397ff72007-10-25 09:30:06 +0200244 void *cb_data)
245{
246 struct reflist *list = (struct reflist *)cb_data;
Christian Hessede83de22015-07-28 10:42:01 +0200247 struct refinfo *info = cgit_mk_refinfo(refname, oid);
Lars Hjemlie397ff72007-10-25 09:30:06 +0200248
249 if (info)
250 cgit_add_ref(list, info);
251 return 0;
252}
253
John Keeping044e2d22015-08-12 15:34:48 +0100254void cgit_diff_tree_cb(struct diff_queue_struct *q,
255 struct diff_options *options, void *data)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200256{
257 int i;
258
259 for (i = 0; i < q->nr; i++) {
260 if (q->queue[i]->status == 'U')
261 continue;
262 ((filepair_fn)data)(q->queue[i]);
263 }
264}
265
Christian Hesse11695a52016-09-04 12:38:18 +0200266static int load_mmfile(mmfile_t *file, const struct object_id *oid)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200267{
268 enum object_type type;
269
Christian Hesse11695a52016-09-04 12:38:18 +0200270 if (is_null_oid(oid)) {
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200271 file->ptr = (char *)"";
272 file->size = 0;
273 } else {
Christian Hesse11695a52016-09-04 12:38:18 +0200274 file->ptr = read_sha1_file(oid->hash, &type,
Lars Hjemli0835ffe2007-09-20 00:21:47 +0200275 (unsigned long *)&file->size);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200276 }
277 return 1;
278}
279
280/*
281 * Receive diff-buffers from xdiff and concatenate them as
282 * needed across multiple callbacks.
283 *
284 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
285 * ripped from git and modified to use globals instead of
286 * a special callback-struct.
287 */
John Keepingeefd5e02015-03-08 16:32:19 +0000288static char *diffbuf = NULL;
289static int buflen = 0;
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200290
Lukas Fleischerbafab422013-03-04 08:52:33 +0100291static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200292{
293 int i;
294
295 for (i = 0; i < nbuf; i++) {
296 if (mb[i].ptr[mb[i].size-1] != '\n') {
297 /* Incomplete line */
298 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
299 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
300 buflen += mb[i].size;
301 continue;
302 }
303
304 /* we have a complete line */
305 if (!diffbuf) {
306 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
307 continue;
308 }
309 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
310 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
311 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
312 free(diffbuf);
313 diffbuf = NULL;
314 buflen = 0;
315 }
316 if (diffbuf) {
317 ((linediff_fn)priv)(diffbuf, buflen);
318 free(diffbuf);
319 diffbuf = NULL;
320 buflen = 0;
321 }
322 return 0;
323}
324
Christian Hesse11695a52016-09-04 12:38:18 +0200325int cgit_diff_files(const struct object_id *old_oid,
326 const struct object_id *new_oid, unsigned long *old_size,
Johan Herland6180e612010-06-10 20:15:27 +0200327 unsigned long *new_size, int *binary, int context,
Johan Herland2cc8b992010-06-24 17:52:57 +0200328 int ignorews, linediff_fn fn)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200329{
330 mmfile_t file1, file2;
331 xpparam_t diff_params;
332 xdemitconf_t emit_params;
333 xdemitcb_t emit_cb;
334
Christian Hesse11695a52016-09-04 12:38:18 +0200335 if (!load_mmfile(&file1, old_oid) || !load_mmfile(&file2, new_oid))
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200336 return 1;
337
Lars Hjemlic495cf02009-01-31 10:40:40 +0100338 *old_size = file1.size;
339 *new_size = file2.size;
340
Lars Hjemli481ce5e2009-02-01 19:29:24 +0100341 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
342 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
Lars Hjemlic495cf02009-01-31 10:40:40 +0100343 *binary = 1;
Lars Hjemlice761fd2010-04-08 00:48:36 +0200344 if (file1.size)
345 free(file1.ptr);
346 if (file2.size)
347 free(file2.ptr);
Lars Hjemlic495cf02009-01-31 10:40:40 +0100348 return 0;
349 }
350
Lars Hjemli0edf7602008-12-26 11:02:02 +0100351 memset(&diff_params, 0, sizeof(diff_params));
352 memset(&emit_params, 0, sizeof(emit_params));
353 memset(&emit_cb, 0, sizeof(emit_cb));
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200354 diff_params.flags = XDF_NEED_MINIMAL;
Johan Herland2cc8b992010-06-24 17:52:57 +0200355 if (ignorews)
356 diff_params.flags |= XDF_IGNORE_WHITESPACE;
Johan Herland6180e612010-06-10 20:15:27 +0200357 emit_params.ctxlen = context > 0 ? context : 3;
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200358 emit_params.flags = XDL_EMIT_FUNCNAMES;
359 emit_cb.outf = filediff_cb;
360 emit_cb.priv = fn;
361 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
Lars Hjemlice761fd2010-04-08 00:48:36 +0200362 if (file1.size)
363 free(file1.ptr);
364 if (file2.size)
365 free(file2.ptr);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200366 return 0;
367}
368
Christian Hesse11695a52016-09-04 12:38:18 +0200369void cgit_diff_tree(const struct object_id *old_oid,
370 const struct object_id *new_oid,
Johan Herland2cc8b992010-06-24 17:52:57 +0200371 filepair_fn fn, const char *prefix, int ignorews)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200372{
373 struct diff_options opt;
John Keepingbfc14d02013-03-02 12:32:10 +0000374 struct pathspec_item item;
Lars Hjemli1b49de32007-05-13 11:24:23 +0200375
Jason A. Donenfeld7e1c0ed2014-02-20 19:48:24 +0100376 memset(&item, 0, sizeof(item));
Lars Hjemli1b49de32007-05-13 11:24:23 +0200377 diff_setup(&opt);
378 opt.output_format = DIFF_FORMAT_CALLBACK;
379 opt.detect_rename = 1;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100380 opt.rename_limit = ctx.cfg.renamelimit;
Lars Hjemli776200b2008-01-13 19:16:23 +0100381 DIFF_OPT_SET(&opt, RECURSIVE);
Johan Herland2cc8b992010-06-24 17:52:57 +0200382 if (ignorews)
383 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200384 opt.format_callback = cgit_diff_tree_cb;
385 opt.format_callback_data = fn;
Lars Hjemlif527a572007-10-01 11:42:19 +0200386 if (prefix) {
John Keepingbfc14d02013-03-02 12:32:10 +0000387 item.match = prefix;
388 item.len = strlen(prefix);
389 opt.pathspec.nr = 1;
390 opt.pathspec.items = &item;
Lars Hjemlif527a572007-10-01 11:42:19 +0200391 }
Lars Hjemli1b49de32007-05-13 11:24:23 +0200392 diff_setup_done(&opt);
393
Christian Hesse11695a52016-09-04 12:38:18 +0200394 if (old_oid && !is_null_oid(old_oid))
395 diff_tree_sha1(old_oid->hash, new_oid->hash, "", &opt);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200396 else
Christian Hesse11695a52016-09-04 12:38:18 +0200397 diff_root_tree_sha1(new_oid->hash, "", &opt);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200398 diffcore_std(&opt);
399 diff_flush(&opt);
400}
401
Johan Herland1415f3f2010-09-30 20:15:14 +0200402void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200403{
Christian Hesse11695a52016-09-04 12:38:18 +0200404 const struct object_id *old_oid = NULL;
Lars Hjemli1b49de32007-05-13 11:24:23 +0200405
406 if (commit->parents)
Christian Hesse11695a52016-09-04 12:38:18 +0200407 old_oid = &commit->parents->item->object.oid;
408 cgit_diff_tree(old_oid, &commit->object.oid, fn, prefix,
Johan Herland2cc8b992010-06-24 17:52:57 +0200409 ctx.qry.ignorews);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200410}
Lars Hjemlif34478c2008-03-24 16:00:27 +0100411
412int cgit_parse_snapshots_mask(const char *str)
413{
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100414 struct string_list tokens = STRING_LIST_INIT_DUP;
415 struct string_list_item *item;
Lars Hjemlif34478c2008-03-24 16:00:27 +0100416 const struct cgit_snapshot_format *f;
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100417 int rv = 0;
Lars Hjemlif34478c2008-03-24 16:00:27 +0100418
419 /* favor legacy setting */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500420 if (atoi(str))
Lars Hjemlif34478c2008-03-24 16:00:27 +0100421 return 1;
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100422
423 string_list_split(&tokens, str, ' ', -1);
424 string_list_remove_empty_items(&tokens, 0);
425
426 for_each_string_list_item(item, &tokens) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100427 for (f = cgit_snapshot_formats; f->suffix; f++) {
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100428 if (!strcmp(item->string, f->suffix) ||
429 !strcmp(item->string, f->suffix + 1)) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100430 rv |= f->bit;
431 break;
432 }
433 }
Lars Hjemlif34478c2008-03-24 16:00:27 +0100434 }
Lukas Fleischerf04b8d52014-01-10 12:44:37 +0100435
436 string_list_clear(&tokens, 0);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100437 return rv;
438}
Lars Hjemlid6f60722009-07-31 17:38:38 +0200439
Ferry Huberts14f28922011-03-23 11:57:43 +0100440typedef struct {
441 char * name;
442 char * value;
443} cgit_env_var;
444
Lars Hjemli3ec6b302011-06-06 19:29:58 +0000445void cgit_prepare_repo_env(struct cgit_repo * repo)
446{
Ferry Huberts14f28922011-03-23 11:57:43 +0100447 cgit_env_var env_vars[] = {
448 { .name = "CGIT_REPO_URL", .value = repo->url },
449 { .name = "CGIT_REPO_NAME", .value = repo->name },
450 { .name = "CGIT_REPO_PATH", .value = repo->path },
451 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
452 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
453 { .name = "CGIT_REPO_SECTION", .value = repo->section },
454 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
455 };
456 int env_var_count = ARRAY_SIZE(env_vars);
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100457 cgit_env_var *p, *q;
458 static char *warn = "cgit warning: failed to set env: %s=%s\n";
Ferry Huberts14f28922011-03-23 11:57:43 +0100459
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100460 p = env_vars;
461 q = p + env_var_count;
462 for (; p < q; p++)
Lukas Fleischerd96d2c92011-09-14 11:52:43 +0200463 if (p->value && setenv(p->name, p->value, 1))
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100464 fprintf(stderr, warn, p->name, p->value);
Ferry Huberts14f28922011-03-23 11:57:43 +0100465}
466
Lars Hjemlie16f1782009-08-18 17:17:41 +0200467/* Read the content of the specified file into a newly allocated buffer,
468 * zeroterminate the buffer and return 0 on success, errno otherwise.
469 */
470int readfile(const char *path, char **buf, size_t *size)
471{
Lars Hjemli21f67e72009-11-07 18:08:30 +0100472 int fd, e;
Lars Hjemlie16f1782009-08-18 17:17:41 +0200473 struct stat st;
474
475 fd = open(path, O_RDONLY);
476 if (fd == -1)
477 return errno;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100478 if (fstat(fd, &st)) {
Lars Hjemli21f67e72009-11-07 18:08:30 +0100479 e = errno;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100480 close(fd);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100481 return e;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100482 }
483 if (!S_ISREG(st.st_mode)) {
484 close(fd);
Lars Hjemlie16f1782009-08-18 17:17:41 +0200485 return EISDIR;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100486 }
Lars Hjemlie16f1782009-08-18 17:17:41 +0200487 *buf = xmalloc(st.st_size + 1);
488 *size = read_in_full(fd, *buf, st.st_size);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100489 e = errno;
Lars Hjemlie16f1782009-08-18 17:17:41 +0200490 (*buf)[*size] = '\0';
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100491 close(fd);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100492 return (*size == st.st_size ? 0 : e);
Lars Hjemlie16f1782009-08-18 17:17:41 +0200493}
Lars Hjemliba56a372010-03-22 00:09:43 +0100494
Lukas Fleischerbafab422013-03-04 08:52:33 +0100495static int is_token_char(char c)
Lars Hjemliba56a372010-03-22 00:09:43 +0100496{
497 return isalnum(c) || c == '_';
498}
499
500/* Replace name with getenv(name), return pointer to zero-terminating char
501 */
Lukas Fleischerbafab422013-03-04 08:52:33 +0100502static char *expand_macro(char *name, int maxlength)
Lars Hjemliba56a372010-03-22 00:09:43 +0100503{
504 char *value;
505 int len;
506
507 len = 0;
508 value = getenv(name);
509 if (value) {
510 len = strlen(value);
511 if (len > maxlength)
512 len = maxlength;
513 strncpy(name, value, len);
514 }
515 return name + len;
516}
517
518#define EXPBUFSIZE (1024 * 8)
519
520/* Replace all tokens prefixed by '$' in the specified text with the
521 * value of the named environment variable.
522 * NB: the return value is a static buffer, i.e. it must be strdup'd
523 * by the caller.
524 */
525char *expand_macros(const char *txt)
526{
527 static char result[EXPBUFSIZE];
528 char *p, *start;
529 int len;
530
531 p = result;
532 start = NULL;
533 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
534 *p = *txt;
535 if (start) {
536 if (!is_token_char(*txt)) {
537 if (p - start > 0) {
538 *p = '\0';
539 len = result + EXPBUFSIZE - start - 1;
540 p = expand_macro(start, len) - 1;
541 }
542 start = NULL;
543 txt--;
544 }
545 p++;
546 txt++;
547 continue;
548 }
549 if (*txt == '$') {
550 start = p;
551 txt++;
552 continue;
553 }
554 p++;
555 txt++;
556 }
557 *p = '\0';
558 if (start && p - start > 0) {
559 len = result + EXPBUFSIZE - start - 1;
560 p = expand_macro(start, len);
561 *p = '\0';
562 }
563 return result;
564}
Christian Hessef5c83d72015-08-14 16:50:56 +0200565
Christian Hesseaa943bc2015-08-16 14:53:52 +0200566char *get_mimetype_for_filename(const char *filename)
Christian Hessef5c83d72015-08-14 16:50:56 +0200567{
Jason A. Donenfeldad006912015-10-09 11:01:04 +0200568 char *ext, *mimetype, *token, line[1024], *saveptr;
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200569 FILE *file;
Christian Hesseaa943bc2015-08-16 14:53:52 +0200570 struct string_list_item *mime;
Christian Hessef5c83d72015-08-14 16:50:56 +0200571
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200572 if (!filename)
Christian Hessef5c83d72015-08-14 16:50:56 +0200573 return NULL;
574
Christian Hesseaa943bc2015-08-16 14:53:52 +0200575 ext = strrchr(filename, '.');
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200576 if (!ext)
577 return NULL;
578 ++ext;
579 if (!ext[0])
580 return NULL;
581 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
582 if (mime)
583 return xstrdup(mime->util);
Christian Hessef5c83d72015-08-14 16:50:56 +0200584
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200585 if (!ctx.cfg.mimetype_file)
586 return NULL;
587 file = fopen(ctx.cfg.mimetype_file, "r");
588 if (!file)
589 return NULL;
590 while (fgets(line, sizeof(line), file)) {
591 if (!line[0] || line[0] == '#')
592 continue;
Jason A. Donenfeldad006912015-10-09 11:01:04 +0200593 mimetype = strtok_r(line, " \t\r\n", &saveptr);
594 while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) {
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200595 if (!strcasecmp(ext, token)) {
596 fclose(file);
597 return xstrdup(mimetype);
Christian Hessef5c83d72015-08-14 16:50:56 +0200598 }
599 }
600 }
Jason A. Donenfeld73f199b2015-08-17 14:35:20 +0200601 fclose(file);
602 return NULL;
Christian Hessef5c83d72015-08-14 16:50:56 +0200603}