2006-12-16 01:14:01 +01:00
|
|
|
/* shared.c: global vars + some callback functions
|
|
|
|
*
|
2014-01-08 15:10:49 +01:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2006-12-16 01:14:01 +01:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2006-12-11 17:25:41 +01:00
|
|
|
#include "cgit.h"
|
|
|
|
|
2008-02-16 13:56:09 +01:00
|
|
|
struct cgit_repolist cgit_repolist;
|
2008-02-16 11:53:40 +01:00
|
|
|
struct cgit_context ctx;
|
2007-02-03 15:02:55 +01:00
|
|
|
|
2007-02-08 13:53:13 +01:00
|
|
|
int chk_zero(int result, char *msg)
|
|
|
|
{
|
|
|
|
if (result != 0)
|
2013-05-18 17:21:36 +02:00
|
|
|
die_errno("%s", msg);
|
2007-02-08 13:53:13 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int chk_positive(int result, char *msg)
|
|
|
|
{
|
|
|
|
if (result <= 0)
|
2013-05-18 17:21:36 +02:00
|
|
|
die_errno("%s", msg);
|
2007-02-08 13:53:13 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-07-20 20:56:43 +02:00
|
|
|
int chk_non_negative(int result, char *msg)
|
|
|
|
{
|
2013-03-03 16:04:29 +01:00
|
|
|
if (result < 0)
|
2013-05-18 17:21:36 +02:00
|
|
|
die_errno("%s", msg);
|
2007-07-20 20:56:43 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-10-09 12:56:14 +02:00
|
|
|
char *cgit_default_repo_desc = "[no description]";
|
2008-03-24 17:26:08 +01:00
|
|
|
struct cgit_repo *cgit_add_repo(const char *url)
|
2007-02-03 15:02:55 +01:00
|
|
|
{
|
2008-02-16 13:56:09 +01:00
|
|
|
struct cgit_repo *ret;
|
2007-02-03 15:02:55 +01:00
|
|
|
|
|
|
|
if (++cgit_repolist.count > cgit_repolist.length) {
|
|
|
|
if (cgit_repolist.length == 0)
|
|
|
|
cgit_repolist.length = 8;
|
|
|
|
else
|
|
|
|
cgit_repolist.length *= 2;
|
2007-05-13 11:24:23 +02:00
|
|
|
cgit_repolist.repos = xrealloc(cgit_repolist.repos,
|
|
|
|
cgit_repolist.length *
|
2008-02-16 13:56:09 +01:00
|
|
|
sizeof(struct cgit_repo));
|
2007-02-03 15:02:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = &cgit_repolist.repos[cgit_repolist.count-1];
|
2009-08-24 10:14:02 +02:00
|
|
|
memset(ret, 0, sizeof(struct cgit_repo));
|
2007-09-20 00:56:53 +02:00
|
|
|
ret->url = trim_end(url, '/');
|
2007-02-03 15:02:55 +01:00
|
|
|
ret->name = ret->url;
|
|
|
|
ret->path = NULL;
|
2012-10-09 12:56:14 +02:00
|
|
|
ret->desc = cgit_default_repo_desc;
|
2018-02-12 23:10:06 +01:00
|
|
|
ret->extra_head_content = NULL;
|
2007-02-03 15:02:55 +01:00
|
|
|
ret->owner = NULL;
|
2016-02-22 16:04:15 +01:00
|
|
|
ret->homepage = NULL;
|
2009-08-23 22:58:39 +02:00
|
|
|
ret->section = ctx.cfg.section;
|
2008-02-16 13:07:13 +01:00
|
|
|
ret->snapshots = ctx.cfg.snapshots;
|
2010-11-15 18:39:50 +01:00
|
|
|
ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
|
2008-02-16 13:07:13 +01:00
|
|
|
ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
|
|
|
|
ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
|
2009-11-07 19:10:58 +01:00
|
|
|
ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
|
2010-02-27 13:12:55 +01:00
|
|
|
ret->enable_subject_links = ctx.cfg.enable_subject_links;
|
2016-01-14 14:53:28 +01:00
|
|
|
ret->enable_html_serving = ctx.cfg.enable_html_serving;
|
2008-12-07 13:17:21 +01:00
|
|
|
ret->max_stats = ctx.cfg.max_stats;
|
2013-04-08 16:57:12 +02:00
|
|
|
ret->branch_sort = ctx.cfg.branch_sort;
|
2012-10-13 16:10:30 +02:00
|
|
|
ret->commit_sort = ctx.cfg.commit_sort;
|
2008-02-16 13:07:13 +01:00
|
|
|
ret->module_link = ctx.cfg.module_link;
|
2010-08-21 15:08:01 +02:00
|
|
|
ret->readme = ctx.cfg.readme;
|
2008-11-29 16:46:37 +01:00
|
|
|
ret->mtime = -1;
|
2009-08-09 13:27:21 +02:00
|
|
|
ret->about_filter = ctx.cfg.about_filter;
|
2009-08-09 13:22:00 +02:00
|
|
|
ret->commit_filter = ctx.cfg.commit_filter;
|
|
|
|
ret->source_filter = ctx.cfg.source_filter;
|
2014-01-13 04:04:52 +01:00
|
|
|
ret->email_filter = ctx.cfg.email_filter;
|
2014-08-04 15:23:08 +02:00
|
|
|
ret->owner_filter = ctx.cfg.owner_filter;
|
2011-06-06 22:49:13 +02:00
|
|
|
ret->clone_url = ctx.cfg.clone_url;
|
2011-06-15 10:04:13 +02:00
|
|
|
ret->submodules.strdup_strings = 1;
|
2015-01-29 12:52:49 +01:00
|
|
|
ret->hide = ret->ignore = 0;
|
2007-02-03 15:02:55 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-16 13:56:09 +01:00
|
|
|
struct cgit_repo *cgit_get_repoinfo(const char *url)
|
2007-05-18 00:47:47 +02:00
|
|
|
{
|
|
|
|
int i;
|
2008-02-16 13:56:09 +01:00
|
|
|
struct cgit_repo *repo;
|
2007-05-18 00:47:47 +02:00
|
|
|
|
2013-03-03 16:04:29 +01:00
|
|
|
for (i = 0; i < cgit_repolist.count; i++) {
|
2007-05-18 00:47:47 +02:00
|
|
|
repo = &cgit_repolist.repos[i];
|
2015-01-29 12:52:49 +01:00
|
|
|
if (repo->ignore)
|
|
|
|
continue;
|
2007-05-18 00:47:47 +02:00
|
|
|
if (!strcmp(repo->url, url))
|
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:51:58 +02:00
|
|
|
void cgit_free_commitinfo(struct commitinfo *info)
|
2006-12-16 14:58:20 +01:00
|
|
|
{
|
|
|
|
free(info->author);
|
|
|
|
free(info->author_email);
|
|
|
|
free(info->committer);
|
|
|
|
free(info->committer_email);
|
|
|
|
free(info->subject);
|
2007-10-27 00:09:06 +02:00
|
|
|
free(info->msg);
|
|
|
|
free(info->msg_encoding);
|
2006-12-16 14:58:20 +01:00
|
|
|
free(info);
|
|
|
|
}
|
2007-01-04 16:53:03 +01:00
|
|
|
|
2007-06-26 18:04:31 +02:00
|
|
|
char *trim_end(const char *str, char c)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
2011-05-22 12:45:32 +02:00
|
|
|
len = strlen(str);
|
2013-03-04 05:21:33 +01:00
|
|
|
while (len > 0 && str[len - 1] == c)
|
2007-06-26 18:04:31 +02:00
|
|
|
len--;
|
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
2011-05-22 12:45:32 +02:00
|
|
|
return xstrndup(str, len);
|
2007-06-26 18:04:31 +02:00
|
|
|
}
|
|
|
|
|
2013-04-01 20:03:34 +02:00
|
|
|
char *ensure_end(const char *str, char c)
|
|
|
|
{
|
|
|
|
size_t len = strlen(str);
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (len && str[len - 1] == c)
|
|
|
|
return xstrndup(str, len);
|
|
|
|
|
|
|
|
result = xmalloc(len + 2);
|
|
|
|
memcpy(result, str, len);
|
|
|
|
result[len] = '/';
|
|
|
|
result[len + 1] = '\0';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-04-07 15:03:47 +02:00
|
|
|
void strbuf_ensure_end(struct strbuf *sb, char c)
|
|
|
|
{
|
|
|
|
if (!sb->len || sb->buf[sb->len - 1] != c)
|
|
|
|
strbuf_addch(sb, c);
|
|
|
|
}
|
|
|
|
|
2007-10-25 09:30:06 +02:00
|
|
|
void cgit_add_ref(struct reflist *list, struct refinfo *ref)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (list->count >= list->alloc) {
|
|
|
|
list->alloc += (list->alloc ? list->alloc : 4);
|
|
|
|
size = list->alloc * sizeof(struct refinfo *);
|
|
|
|
list->refs = xrealloc(list->refs, size);
|
|
|
|
}
|
|
|
|
list->refs[list->count++] = ref;
|
|
|
|
}
|
|
|
|
|
2015-07-28 10:42:01 +02:00
|
|
|
static struct refinfo *cgit_mk_refinfo(const char *refname, const struct object_id *oid)
|
2007-10-25 09:30:06 +02:00
|
|
|
{
|
|
|
|
struct refinfo *ref;
|
|
|
|
|
|
|
|
ref = xmalloc(sizeof (struct refinfo));
|
|
|
|
ref->refname = xstrdup(refname);
|
2017-08-10 02:02:56 +02:00
|
|
|
ref->object = parse_object(oid);
|
2007-10-25 09:30:06 +02:00
|
|
|
switch (ref->object->type) {
|
|
|
|
case OBJ_TAG:
|
|
|
|
ref->tag = cgit_parse_tag((struct tag *)ref->object);
|
|
|
|
break;
|
|
|
|
case OBJ_COMMIT:
|
|
|
|
ref->commit = cgit_parse_commit((struct commit *)ref->object);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:52:51 +02:00
|
|
|
void cgit_free_taginfo(struct taginfo *tag)
|
2013-03-04 13:25:33 +01:00
|
|
|
{
|
|
|
|
if (tag->tagger)
|
|
|
|
free(tag->tagger);
|
|
|
|
if (tag->tagger_email)
|
|
|
|
free(tag->tagger_email);
|
|
|
|
if (tag->msg)
|
|
|
|
free(tag->msg);
|
|
|
|
free(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cgit_free_refinfo(struct refinfo *ref)
|
|
|
|
{
|
|
|
|
if (ref->refname)
|
|
|
|
free((char *)ref->refname);
|
|
|
|
switch (ref->object->type) {
|
|
|
|
case OBJ_TAG:
|
|
|
|
cgit_free_taginfo(ref->tag);
|
|
|
|
break;
|
|
|
|
case OBJ_COMMIT:
|
|
|
|
cgit_free_commitinfo(ref->commit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cgit_free_reflist_inner(struct reflist *list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
cgit_free_refinfo(list->refs[i]);
|
|
|
|
}
|
|
|
|
free(list->refs);
|
|
|
|
}
|
|
|
|
|
2015-07-28 10:42:01 +02:00
|
|
|
int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
|
2007-10-25 09:30:06 +02:00
|
|
|
void *cb_data)
|
|
|
|
{
|
|
|
|
struct reflist *list = (struct reflist *)cb_data;
|
2015-07-28 10:42:01 +02:00
|
|
|
struct refinfo *info = cgit_mk_refinfo(refname, oid);
|
2007-10-25 09:30:06 +02:00
|
|
|
|
|
|
|
if (info)
|
|
|
|
cgit_add_ref(list, info);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-12 16:34:48 +02:00
|
|
|
void cgit_diff_tree_cb(struct diff_queue_struct *q,
|
|
|
|
struct diff_options *options, void *data)
|
2007-05-13 11:24:23 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < q->nr; i++) {
|
|
|
|
if (q->queue[i]->status == 'U')
|
|
|
|
continue;
|
|
|
|
((filepair_fn)data)(q->queue[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:38:18 +02:00
|
|
|
static int load_mmfile(mmfile_t *file, const struct object_id *oid)
|
2007-05-13 14:21:19 +02:00
|
|
|
{
|
|
|
|
enum object_type type;
|
|
|
|
|
2016-09-04 12:38:18 +02:00
|
|
|
if (is_null_oid(oid)) {
|
2007-05-13 14:21:19 +02:00
|
|
|
file->ptr = (char *)"";
|
|
|
|
file->size = 0;
|
|
|
|
} else {
|
2018-06-04 18:49:28 +02:00
|
|
|
file->ptr = read_object_file(oid, &type,
|
2007-09-20 00:21:47 +02:00
|
|
|
(unsigned long *)&file->size);
|
2007-05-13 14:21:19 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive diff-buffers from xdiff and concatenate them as
|
|
|
|
* needed across multiple callbacks.
|
|
|
|
*
|
|
|
|
* This is basically a copy of xdiff-interface.c/xdiff_outf(),
|
|
|
|
* ripped from git and modified to use globals instead of
|
|
|
|
* a special callback-struct.
|
|
|
|
*/
|
2015-03-08 17:32:19 +01:00
|
|
|
static char *diffbuf = NULL;
|
|
|
|
static int buflen = 0;
|
2007-05-13 14:21:19 +02:00
|
|
|
|
2013-03-04 08:52:33 +01:00
|
|
|
static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
|
2007-05-13 14:21:19 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nbuf; i++) {
|
|
|
|
if (mb[i].ptr[mb[i].size-1] != '\n') {
|
|
|
|
/* Incomplete line */
|
|
|
|
diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
|
|
|
|
memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
|
|
|
|
buflen += mb[i].size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have a complete line */
|
|
|
|
if (!diffbuf) {
|
|
|
|
((linediff_fn)priv)(mb[i].ptr, mb[i].size);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
|
|
|
|
memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
|
|
|
|
((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
|
|
|
|
free(diffbuf);
|
|
|
|
diffbuf = NULL;
|
|
|
|
buflen = 0;
|
|
|
|
}
|
|
|
|
if (diffbuf) {
|
|
|
|
((linediff_fn)priv)(diffbuf, buflen);
|
|
|
|
free(diffbuf);
|
|
|
|
diffbuf = NULL;
|
|
|
|
buflen = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:38:18 +02:00
|
|
|
int cgit_diff_files(const struct object_id *old_oid,
|
|
|
|
const struct object_id *new_oid, unsigned long *old_size,
|
2010-06-10 20:15:27 +02:00
|
|
|
unsigned long *new_size, int *binary, int context,
|
2010-06-24 17:52:57 +02:00
|
|
|
int ignorews, linediff_fn fn)
|
2007-05-13 14:21:19 +02:00
|
|
|
{
|
|
|
|
mmfile_t file1, file2;
|
|
|
|
xpparam_t diff_params;
|
|
|
|
xdemitconf_t emit_params;
|
|
|
|
xdemitcb_t emit_cb;
|
|
|
|
|
2016-09-04 12:38:18 +02:00
|
|
|
if (!load_mmfile(&file1, old_oid) || !load_mmfile(&file2, new_oid))
|
2007-05-13 14:21:19 +02:00
|
|
|
return 1;
|
|
|
|
|
2009-01-31 10:40:40 +01:00
|
|
|
*old_size = file1.size;
|
|
|
|
*new_size = file2.size;
|
|
|
|
|
2009-02-01 19:29:24 +01:00
|
|
|
if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
|
|
|
|
(file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
|
2009-01-31 10:40:40 +01:00
|
|
|
*binary = 1;
|
2010-04-08 00:48:36 +02:00
|
|
|
if (file1.size)
|
|
|
|
free(file1.ptr);
|
|
|
|
if (file2.size)
|
|
|
|
free(file2.ptr);
|
2009-01-31 10:40:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-26 11:02:02 +01:00
|
|
|
memset(&diff_params, 0, sizeof(diff_params));
|
|
|
|
memset(&emit_params, 0, sizeof(emit_params));
|
|
|
|
memset(&emit_cb, 0, sizeof(emit_cb));
|
2007-05-13 14:21:19 +02:00
|
|
|
diff_params.flags = XDF_NEED_MINIMAL;
|
2010-06-24 17:52:57 +02:00
|
|
|
if (ignorews)
|
|
|
|
diff_params.flags |= XDF_IGNORE_WHITESPACE;
|
2010-06-10 20:15:27 +02:00
|
|
|
emit_params.ctxlen = context > 0 ? context : 3;
|
2007-05-13 14:21:19 +02:00
|
|
|
emit_params.flags = XDL_EMIT_FUNCNAMES;
|
|
|
|
emit_cb.outf = filediff_cb;
|
|
|
|
emit_cb.priv = fn;
|
|
|
|
xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
|
2010-04-08 00:48:36 +02:00
|
|
|
if (file1.size)
|
|
|
|
free(file1.ptr);
|
|
|
|
if (file2.size)
|
|
|
|
free(file2.ptr);
|
2007-05-13 14:21:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:38:18 +02:00
|
|
|
void cgit_diff_tree(const struct object_id *old_oid,
|
|
|
|
const struct object_id *new_oid,
|
2010-06-24 17:52:57 +02:00
|
|
|
filepair_fn fn, const char *prefix, int ignorews)
|
2007-05-13 11:24:23 +02:00
|
|
|
{
|
|
|
|
struct diff_options opt;
|
2013-03-02 13:32:10 +01:00
|
|
|
struct pathspec_item item;
|
2007-05-13 11:24:23 +02:00
|
|
|
|
2014-02-20 19:48:24 +01:00
|
|
|
memset(&item, 0, sizeof(item));
|
2007-05-13 11:24:23 +02:00
|
|
|
diff_setup(&opt);
|
|
|
|
opt.output_format = DIFF_FORMAT_CALLBACK;
|
|
|
|
opt.detect_rename = 1;
|
2008-02-16 13:07:13 +01:00
|
|
|
opt.rename_limit = ctx.cfg.renamelimit;
|
2018-01-18 09:19:31 +01:00
|
|
|
opt.flags.recursive = 1;
|
2010-06-24 17:52:57 +02:00
|
|
|
if (ignorews)
|
|
|
|
DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
|
2007-05-13 11:24:23 +02:00
|
|
|
opt.format_callback = cgit_diff_tree_cb;
|
|
|
|
opt.format_callback_data = fn;
|
2007-10-01 11:42:19 +02:00
|
|
|
if (prefix) {
|
2017-07-24 17:22:52 +02:00
|
|
|
item.match = xstrdup(prefix);
|
2013-03-02 13:32:10 +01:00
|
|
|
item.len = strlen(prefix);
|
|
|
|
opt.pathspec.nr = 1;
|
|
|
|
opt.pathspec.items = &item;
|
2007-10-01 11:42:19 +02:00
|
|
|
}
|
2007-05-13 11:24:23 +02:00
|
|
|
diff_setup_done(&opt);
|
|
|
|
|
2016-09-04 12:38:18 +02:00
|
|
|
if (old_oid && !is_null_oid(old_oid))
|
2017-08-10 02:02:56 +02:00
|
|
|
diff_tree_oid(old_oid, new_oid, "", &opt);
|
2007-05-13 11:24:23 +02:00
|
|
|
else
|
2017-08-10 02:02:56 +02:00
|
|
|
diff_root_tree_oid(new_oid, "", &opt);
|
2007-05-13 11:24:23 +02:00
|
|
|
diffcore_std(&opt);
|
|
|
|
diff_flush(&opt);
|
2017-07-24 17:22:52 +02:00
|
|
|
|
|
|
|
free(item.match);
|
2007-05-13 11:24:23 +02:00
|
|
|
}
|
|
|
|
|
2010-09-30 20:15:14 +02:00
|
|
|
void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
|
2007-05-13 11:24:23 +02:00
|
|
|
{
|
2016-09-04 12:38:18 +02:00
|
|
|
const struct object_id *old_oid = NULL;
|
2007-05-13 11:24:23 +02:00
|
|
|
|
|
|
|
if (commit->parents)
|
2016-09-04 12:38:18 +02:00
|
|
|
old_oid = &commit->parents->item->object.oid;
|
|
|
|
cgit_diff_tree(old_oid, &commit->object.oid, fn, prefix,
|
2010-06-24 17:52:57 +02:00
|
|
|
ctx.qry.ignorews);
|
2007-05-13 11:24:23 +02:00
|
|
|
}
|
2008-03-24 16:00:27 +01:00
|
|
|
|
|
|
|
int cgit_parse_snapshots_mask(const char *str)
|
|
|
|
{
|
2014-01-10 12:44:37 +01:00
|
|
|
struct string_list tokens = STRING_LIST_INIT_DUP;
|
|
|
|
struct string_list_item *item;
|
2008-03-24 16:00:27 +01:00
|
|
|
const struct cgit_snapshot_format *f;
|
2014-01-10 12:44:37 +01:00
|
|
|
int rv = 0;
|
2008-03-24 16:00:27 +01:00
|
|
|
|
|
|
|
/* favor legacy setting */
|
2013-03-04 05:21:33 +01:00
|
|
|
if (atoi(str))
|
2008-03-24 16:00:27 +01:00
|
|
|
return 1;
|
2014-01-10 12:44:37 +01:00
|
|
|
|
2018-06-07 22:01:50 +02:00
|
|
|
if (strcmp(str, "all") == 0)
|
|
|
|
return INT_MAX;
|
|
|
|
|
2014-01-10 12:44:37 +01:00
|
|
|
string_list_split(&tokens, str, ' ', -1);
|
|
|
|
string_list_remove_empty_items(&tokens, 0);
|
|
|
|
|
|
|
|
for_each_string_list_item(item, &tokens) {
|
2008-03-24 16:00:27 +01:00
|
|
|
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
2014-01-10 12:44:37 +01:00
|
|
|
if (!strcmp(item->string, f->suffix) ||
|
|
|
|
!strcmp(item->string, f->suffix + 1)) {
|
2018-06-11 08:26:59 +02:00
|
|
|
rv |= cgit_snapshot_format_bit(f);
|
2008-03-24 16:00:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 12:44:37 +01:00
|
|
|
|
|
|
|
string_list_clear(&tokens, 0);
|
2008-03-24 16:00:27 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2009-07-31 17:38:38 +02:00
|
|
|
|
2011-03-23 11:57:43 +01:00
|
|
|
typedef struct {
|
|
|
|
char * name;
|
|
|
|
char * value;
|
|
|
|
} cgit_env_var;
|
|
|
|
|
2011-06-06 21:29:58 +02:00
|
|
|
void cgit_prepare_repo_env(struct cgit_repo * repo)
|
|
|
|
{
|
2011-03-23 11:57:43 +01:00
|
|
|
cgit_env_var env_vars[] = {
|
|
|
|
{ .name = "CGIT_REPO_URL", .value = repo->url },
|
|
|
|
{ .name = "CGIT_REPO_NAME", .value = repo->name },
|
|
|
|
{ .name = "CGIT_REPO_PATH", .value = repo->path },
|
|
|
|
{ .name = "CGIT_REPO_OWNER", .value = repo->owner },
|
|
|
|
{ .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
|
|
|
|
{ .name = "CGIT_REPO_SECTION", .value = repo->section },
|
|
|
|
{ .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
|
|
|
|
};
|
|
|
|
int env_var_count = ARRAY_SIZE(env_vars);
|
2011-03-26 11:22:35 +01:00
|
|
|
cgit_env_var *p, *q;
|
|
|
|
static char *warn = "cgit warning: failed to set env: %s=%s\n";
|
|
|
|
|
|
|
|
p = env_vars;
|
|
|
|
q = p + env_var_count;
|
|
|
|
for (; p < q; p++)
|
2011-09-14 11:52:43 +02:00
|
|
|
if (p->value && setenv(p->name, p->value, 1))
|
2011-03-26 11:22:35 +01:00
|
|
|
fprintf(stderr, warn, p->name, p->value);
|
2011-03-23 11:57:43 +01:00
|
|
|
}
|
|
|
|
|
2009-08-18 17:17:41 +02:00
|
|
|
/* Read the content of the specified file into a newly allocated buffer,
|
|
|
|
* zeroterminate the buffer and return 0 on success, errno otherwise.
|
|
|
|
*/
|
|
|
|
int readfile(const char *path, char **buf, size_t *size)
|
|
|
|
{
|
2009-11-07 18:08:30 +01:00
|
|
|
int fd, e;
|
2009-08-18 17:17:41 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
return errno;
|
2009-11-07 15:24:45 +01:00
|
|
|
if (fstat(fd, &st)) {
|
2009-11-07 18:08:30 +01:00
|
|
|
e = errno;
|
2009-11-07 15:24:45 +01:00
|
|
|
close(fd);
|
2009-11-07 18:08:30 +01:00
|
|
|
return e;
|
2009-11-07 15:24:45 +01:00
|
|
|
}
|
|
|
|
if (!S_ISREG(st.st_mode)) {
|
|
|
|
close(fd);
|
2009-08-18 17:17:41 +02:00
|
|
|
return EISDIR;
|
2009-11-07 15:24:45 +01:00
|
|
|
}
|
2009-08-18 17:17:41 +02:00
|
|
|
*buf = xmalloc(st.st_size + 1);
|
|
|
|
*size = read_in_full(fd, *buf, st.st_size);
|
2009-11-07 18:08:30 +01:00
|
|
|
e = errno;
|
2009-08-18 17:17:41 +02:00
|
|
|
(*buf)[*size] = '\0';
|
2009-11-07 15:24:45 +01:00
|
|
|
close(fd);
|
2009-11-07 18:08:30 +01:00
|
|
|
return (*size == st.st_size ? 0 : e);
|
2009-08-18 17:17:41 +02:00
|
|
|
}
|
2010-03-22 00:09:43 +01:00
|
|
|
|
2013-03-04 08:52:33 +01:00
|
|
|
static int is_token_char(char c)
|
2010-03-22 00:09:43 +01:00
|
|
|
{
|
|
|
|
return isalnum(c) || c == '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace name with getenv(name), return pointer to zero-terminating char
|
|
|
|
*/
|
2013-03-04 08:52:33 +01:00
|
|
|
static char *expand_macro(char *name, int maxlength)
|
2010-03-22 00:09:43 +01:00
|
|
|
{
|
|
|
|
char *value;
|
2018-07-04 03:13:31 +02:00
|
|
|
size_t len;
|
2010-03-22 00:09:43 +01:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
value = getenv(name);
|
|
|
|
if (value) {
|
2018-07-04 03:13:31 +02:00
|
|
|
len = strlen(value) + 1;
|
2010-03-22 00:09:43 +01:00
|
|
|
if (len > maxlength)
|
|
|
|
len = maxlength;
|
2018-07-04 03:13:31 +02:00
|
|
|
strlcpy(name, value, len);
|
|
|
|
--len;
|
2010-03-22 00:09:43 +01:00
|
|
|
}
|
|
|
|
return name + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EXPBUFSIZE (1024 * 8)
|
|
|
|
|
|
|
|
/* Replace all tokens prefixed by '$' in the specified text with the
|
|
|
|
* value of the named environment variable.
|
|
|
|
* NB: the return value is a static buffer, i.e. it must be strdup'd
|
|
|
|
* by the caller.
|
|
|
|
*/
|
|
|
|
char *expand_macros(const char *txt)
|
|
|
|
{
|
|
|
|
static char result[EXPBUFSIZE];
|
|
|
|
char *p, *start;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
p = result;
|
|
|
|
start = NULL;
|
|
|
|
while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
|
|
|
|
*p = *txt;
|
|
|
|
if (start) {
|
|
|
|
if (!is_token_char(*txt)) {
|
|
|
|
if (p - start > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
len = result + EXPBUFSIZE - start - 1;
|
|
|
|
p = expand_macro(start, len) - 1;
|
|
|
|
}
|
|
|
|
start = NULL;
|
|
|
|
txt--;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
txt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*txt == '$') {
|
|
|
|
start = p;
|
|
|
|
txt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
txt++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
if (start && p - start > 0) {
|
|
|
|
len = result + EXPBUFSIZE - start - 1;
|
|
|
|
p = expand_macro(start, len);
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2015-08-14 16:50:56 +02:00
|
|
|
|
2015-08-16 14:53:52 +02:00
|
|
|
char *get_mimetype_for_filename(const char *filename)
|
2015-08-14 16:50:56 +02:00
|
|
|
{
|
2015-10-09 11:01:04 +02:00
|
|
|
char *ext, *mimetype, *token, line[1024], *saveptr;
|
2015-08-17 14:35:20 +02:00
|
|
|
FILE *file;
|
2015-08-16 14:53:52 +02:00
|
|
|
struct string_list_item *mime;
|
2015-08-14 16:50:56 +02:00
|
|
|
|
2015-08-17 14:35:20 +02:00
|
|
|
if (!filename)
|
2015-08-14 16:50:56 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-08-16 14:53:52 +02:00
|
|
|
ext = strrchr(filename, '.');
|
2015-08-17 14:35:20 +02:00
|
|
|
if (!ext)
|
|
|
|
return NULL;
|
|
|
|
++ext;
|
|
|
|
if (!ext[0])
|
|
|
|
return NULL;
|
|
|
|
mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
|
|
|
|
if (mime)
|
|
|
|
return xstrdup(mime->util);
|
2015-08-16 14:53:52 +02:00
|
|
|
|
2015-08-17 14:35:20 +02:00
|
|
|
if (!ctx.cfg.mimetype_file)
|
|
|
|
return NULL;
|
|
|
|
file = fopen(ctx.cfg.mimetype_file, "r");
|
|
|
|
if (!file)
|
|
|
|
return NULL;
|
|
|
|
while (fgets(line, sizeof(line), file)) {
|
|
|
|
if (!line[0] || line[0] == '#')
|
|
|
|
continue;
|
2015-10-09 11:01:04 +02:00
|
|
|
mimetype = strtok_r(line, " \t\r\n", &saveptr);
|
|
|
|
while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) {
|
2015-08-17 14:35:20 +02:00
|
|
|
if (!strcasecmp(ext, token)) {
|
|
|
|
fclose(file);
|
|
|
|
return xstrdup(mimetype);
|
2015-08-14 16:50:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 14:35:20 +02:00
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
2015-08-14 16:50:56 +02:00
|
|
|
}
|