2006-12-16 00:14:01 +00:00
|
|
|
/* shared.c: global vars + some callback functions
|
|
|
|
*
|
2014-01-08 14:10:49 +00:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2006-12-16 00:14:01 +00:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2006-12-11 16:25:41 +00:00
|
|
|
#include "cgit.h"
|
|
|
|
|
2008-02-16 12:56:09 +00:00
|
|
|
struct cgit_repolist cgit_repolist;
|
2008-02-16 10:53:40 +00:00
|
|
|
struct cgit_context ctx;
|
2007-02-03 14:02:55 +00:00
|
|
|
|
2007-02-08 12:53:13 +00:00
|
|
|
int chk_zero(int result, char *msg)
|
|
|
|
{
|
|
|
|
if (result != 0)
|
2013-05-18 15:21:36 +00:00
|
|
|
die_errno("%s", msg);
|
2007-02-08 12:53:13 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int chk_positive(int result, char *msg)
|
|
|
|
{
|
|
|
|
if (result <= 0)
|
2013-05-18 15:21:36 +00:00
|
|
|
die_errno("%s", msg);
|
2007-02-08 12:53:13 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-07-20 18:56:43 +00:00
|
|
|
int chk_non_negative(int result, char *msg)
|
|
|
|
{
|
2013-03-03 15:04:29 +00:00
|
|
|
if (result < 0)
|
2013-05-18 15:21:36 +00:00
|
|
|
die_errno("%s", msg);
|
2007-07-20 18:56:43 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:56:14 +00:00
|
|
|
char *cgit_default_repo_desc = "[no description]";
|
2008-03-24 16:26:08 +00:00
|
|
|
struct cgit_repo *cgit_add_repo(const char *url)
|
2007-02-03 14:02:55 +00:00
|
|
|
{
|
2008-02-16 12:56:09 +00:00
|
|
|
struct cgit_repo *ret;
|
2007-02-03 14:02:55 +00: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 09:24:23 +00:00
|
|
|
cgit_repolist.repos = xrealloc(cgit_repolist.repos,
|
|
|
|
cgit_repolist.length *
|
2008-02-16 12:56:09 +00:00
|
|
|
sizeof(struct cgit_repo));
|
2007-02-03 14:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = &cgit_repolist.repos[cgit_repolist.count-1];
|
2009-08-24 08:14:02 +00:00
|
|
|
memset(ret, 0, sizeof(struct cgit_repo));
|
2007-09-19 22:56:53 +00:00
|
|
|
ret->url = trim_end(url, '/');
|
2007-02-03 14:02:55 +00:00
|
|
|
ret->name = ret->url;
|
|
|
|
ret->path = NULL;
|
2012-10-09 10:56:14 +00:00
|
|
|
ret->desc = cgit_default_repo_desc;
|
2007-02-03 14:02:55 +00:00
|
|
|
ret->owner = NULL;
|
2016-02-22 15:04:15 +00:00
|
|
|
ret->homepage = NULL;
|
2009-08-23 20:58:39 +00:00
|
|
|
ret->section = ctx.cfg.section;
|
2008-02-16 12:07:13 +00:00
|
|
|
ret->snapshots = ctx.cfg.snapshots;
|
2010-11-15 17:39:50 +00:00
|
|
|
ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
|
2008-02-16 12:07:13 +00:00
|
|
|
ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
|
|
|
|
ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
|
2009-11-07 18:10:58 +00:00
|
|
|
ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
|
2010-02-27 12:12:55 +00:00
|
|
|
ret->enable_subject_links = ctx.cfg.enable_subject_links;
|
2016-01-14 13:53:28 +00:00
|
|
|
ret->enable_html_serving = ctx.cfg.enable_html_serving;
|
2008-12-07 12:17:21 +00:00
|
|
|
ret->max_stats = ctx.cfg.max_stats;
|
2013-04-08 14:57:12 +00:00
|
|
|
ret->branch_sort = ctx.cfg.branch_sort;
|
2012-10-13 14:10:30 +00:00
|
|
|
ret->commit_sort = ctx.cfg.commit_sort;
|
2008-02-16 12:07:13 +00:00
|
|
|
ret->module_link = ctx.cfg.module_link;
|
2010-08-21 13:08:01 +00:00
|
|
|
ret->readme = ctx.cfg.readme;
|
2008-11-29 15:46:37 +00:00
|
|
|
ret->mtime = -1;
|
2009-08-09 11:27:21 +00:00
|
|
|
ret->about_filter = ctx.cfg.about_filter;
|
2009-08-09 11:22:00 +00:00
|
|
|
ret->commit_filter = ctx.cfg.commit_filter;
|
|
|
|
ret->source_filter = ctx.cfg.source_filter;
|
2014-01-13 03:04:52 +00:00
|
|
|
ret->email_filter = ctx.cfg.email_filter;
|
2014-08-04 13:23:08 +00:00
|
|
|
ret->owner_filter = ctx.cfg.owner_filter;
|
2011-06-06 20:49:13 +00:00
|
|
|
ret->clone_url = ctx.cfg.clone_url;
|
2011-06-15 08:04:13 +00:00
|
|
|
ret->submodules.strdup_strings = 1;
|
2015-01-29 11:52:49 +00:00
|
|
|
ret->hide = ret->ignore = 0;
|
2007-02-03 14:02:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-16 12:56:09 +00:00
|
|
|
struct cgit_repo *cgit_get_repoinfo(const char *url)
|
2007-05-17 22:47:47 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-02-16 12:56:09 +00:00
|
|
|
struct cgit_repo *repo;
|
2007-05-17 22:47:47 +00:00
|
|
|
|
2013-03-03 15:04:29 +00:00
|
|
|
for (i = 0; i < cgit_repolist.count; i++) {
|
2007-05-17 22:47:47 +00:00
|
|
|
repo = &cgit_repolist.repos[i];
|
2015-01-29 11:52:49 +00:00
|
|
|
if (repo->ignore)
|
|
|
|
continue;
|
2007-05-17 22:47:47 +00:00
|
|
|
if (!strcmp(repo->url, url))
|
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-12-16 13:58:20 +00:00
|
|
|
void *cgit_free_commitinfo(struct commitinfo *info)
|
|
|
|
{
|
|
|
|
free(info->author);
|
|
|
|
free(info->author_email);
|
|
|
|
free(info->committer);
|
|
|
|
free(info->committer_email);
|
|
|
|
free(info->subject);
|
2007-10-26 22:09:06 +00:00
|
|
|
free(info->msg);
|
|
|
|
free(info->msg_encoding);
|
2006-12-16 13:58:20 +00:00
|
|
|
free(info);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-04 15:53:03 +00:00
|
|
|
|
2007-06-26 16:04:31 +00:00
|
|
|
char *trim_end(const char *str, char c)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
2011-05-22 10:45:32 +00:00
|
|
|
len = strlen(str);
|
2013-03-04 04:21:33 +00:00
|
|
|
while (len > 0 && str[len - 1] == c)
|
2007-06-26 16:04:31 +00:00
|
|
|
len--;
|
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
2011-05-22 10:45:32 +00:00
|
|
|
return xstrndup(str, len);
|
2007-06-26 16:04:31 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 18:03:34 +00: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 13:03:47 +00: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-30 09:47:38 +00:00
|
|
|
char *strlpart(char *txt, int maxlen)
|
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (!txt)
|
|
|
|
return txt;
|
|
|
|
|
|
|
|
if (strlen(txt) <= maxlen)
|
|
|
|
return txt;
|
|
|
|
result = xmalloc(maxlen + 1);
|
|
|
|
memcpy(result, txt, maxlen - 3);
|
|
|
|
result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
|
|
|
|
result[maxlen] = '\0';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strrpart(char *txt, int maxlen)
|
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (!txt)
|
|
|
|
return txt;
|
|
|
|
|
|
|
|
if (strlen(txt) <= maxlen)
|
|
|
|
return txt;
|
|
|
|
result = xmalloc(maxlen + 1);
|
|
|
|
memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
|
|
|
|
result[0] = result[1] = result[2] = '.';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-10-25 07:30:06 +00: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 08:42:01 +00:00
|
|
|
static struct refinfo *cgit_mk_refinfo(const char *refname, const struct object_id *oid)
|
2007-10-25 07:30:06 +00:00
|
|
|
{
|
|
|
|
struct refinfo *ref;
|
|
|
|
|
|
|
|
ref = xmalloc(sizeof (struct refinfo));
|
|
|
|
ref->refname = xstrdup(refname);
|
2015-07-28 08:42:01 +00:00
|
|
|
ref->object = parse_object(oid->hash);
|
2007-10-25 07:30:06 +00: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;
|
|
|
|
}
|
|
|
|
|
2013-03-04 12:25:33 +00:00
|
|
|
static void cgit_free_taginfo(struct taginfo *tag)
|
|
|
|
{
|
|
|
|
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 08:42:01 +00:00
|
|
|
int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
|
2007-10-25 07:30:06 +00:00
|
|
|
void *cb_data)
|
|
|
|
{
|
|
|
|
struct reflist *list = (struct reflist *)cb_data;
|
2015-07-28 08:42:01 +00:00
|
|
|
struct refinfo *info = cgit_mk_refinfo(refname, oid);
|
2007-10-25 07:30:06 +00:00
|
|
|
|
|
|
|
if (info)
|
|
|
|
cgit_add_ref(list, info);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:34:48 +00:00
|
|
|
void cgit_diff_tree_cb(struct diff_queue_struct *q,
|
|
|
|
struct diff_options *options, void *data)
|
2007-05-13 09:24:23 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < q->nr; i++) {
|
|
|
|
if (q->queue[i]->status == 'U')
|
|
|
|
continue;
|
|
|
|
((filepair_fn)data)(q->queue[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-13 12:21:19 +00:00
|
|
|
static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
|
|
|
|
{
|
|
|
|
enum object_type type;
|
|
|
|
|
|
|
|
if (is_null_sha1(sha1)) {
|
|
|
|
file->ptr = (char *)"";
|
|
|
|
file->size = 0;
|
|
|
|
} else {
|
2013-03-03 15:04:29 +00:00
|
|
|
file->ptr = read_sha1_file(sha1, &type,
|
2007-09-19 22:21:47 +00:00
|
|
|
(unsigned long *)&file->size);
|
2007-05-13 12:21:19 +00: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 16:32:19 +00:00
|
|
|
static char *diffbuf = NULL;
|
|
|
|
static int buflen = 0;
|
2007-05-13 12:21:19 +00:00
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
|
2007-05-13 12:21:19 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cgit_diff_files(const unsigned char *old_sha1,
|
2009-01-31 09:40:40 +00:00
|
|
|
const unsigned char *new_sha1, unsigned long *old_size,
|
2010-06-10 18:15:27 +00:00
|
|
|
unsigned long *new_size, int *binary, int context,
|
2010-06-24 15:52:57 +00:00
|
|
|
int ignorews, linediff_fn fn)
|
2007-05-13 12:21:19 +00:00
|
|
|
{
|
|
|
|
mmfile_t file1, file2;
|
|
|
|
xpparam_t diff_params;
|
|
|
|
xdemitconf_t emit_params;
|
|
|
|
xdemitcb_t emit_cb;
|
|
|
|
|
|
|
|
if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
|
|
|
|
return 1;
|
|
|
|
|
2009-01-31 09:40:40 +00:00
|
|
|
*old_size = file1.size;
|
|
|
|
*new_size = file2.size;
|
|
|
|
|
2009-02-01 18:29:24 +00:00
|
|
|
if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
|
|
|
|
(file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
|
2009-01-31 09:40:40 +00:00
|
|
|
*binary = 1;
|
2010-04-07 22:48:36 +00:00
|
|
|
if (file1.size)
|
|
|
|
free(file1.ptr);
|
|
|
|
if (file2.size)
|
|
|
|
free(file2.ptr);
|
2009-01-31 09:40:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-26 10:02:02 +00: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 12:21:19 +00:00
|
|
|
diff_params.flags = XDF_NEED_MINIMAL;
|
2010-06-24 15:52:57 +00:00
|
|
|
if (ignorews)
|
|
|
|
diff_params.flags |= XDF_IGNORE_WHITESPACE;
|
2010-06-10 18:15:27 +00:00
|
|
|
emit_params.ctxlen = context > 0 ? context : 3;
|
2007-05-13 12:21:19 +00: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-07 22:48:36 +00:00
|
|
|
if (file1.size)
|
|
|
|
free(file1.ptr);
|
|
|
|
if (file2.size)
|
|
|
|
free(file2.ptr);
|
2007-05-13 12:21:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-13 09:24:23 +00:00
|
|
|
void cgit_diff_tree(const unsigned char *old_sha1,
|
|
|
|
const unsigned char *new_sha1,
|
2010-06-24 15:52:57 +00:00
|
|
|
filepair_fn fn, const char *prefix, int ignorews)
|
2007-05-13 09:24:23 +00:00
|
|
|
{
|
|
|
|
struct diff_options opt;
|
2013-03-02 12:32:10 +00:00
|
|
|
struct pathspec_item item;
|
2007-05-13 09:24:23 +00:00
|
|
|
|
2014-02-20 18:48:24 +00:00
|
|
|
memset(&item, 0, sizeof(item));
|
2007-05-13 09:24:23 +00:00
|
|
|
diff_setup(&opt);
|
|
|
|
opt.output_format = DIFF_FORMAT_CALLBACK;
|
|
|
|
opt.detect_rename = 1;
|
2008-02-16 12:07:13 +00:00
|
|
|
opt.rename_limit = ctx.cfg.renamelimit;
|
2008-01-13 18:16:23 +00:00
|
|
|
DIFF_OPT_SET(&opt, RECURSIVE);
|
2010-06-24 15:52:57 +00:00
|
|
|
if (ignorews)
|
|
|
|
DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
|
2007-05-13 09:24:23 +00:00
|
|
|
opt.format_callback = cgit_diff_tree_cb;
|
|
|
|
opt.format_callback_data = fn;
|
2007-10-01 09:42:19 +00:00
|
|
|
if (prefix) {
|
2013-03-02 12:32:10 +00:00
|
|
|
item.match = prefix;
|
|
|
|
item.len = strlen(prefix);
|
|
|
|
opt.pathspec.nr = 1;
|
|
|
|
opt.pathspec.items = &item;
|
2007-10-01 09:42:19 +00:00
|
|
|
}
|
2007-05-13 09:24:23 +00:00
|
|
|
diff_setup_done(&opt);
|
|
|
|
|
2007-06-17 16:12:03 +00:00
|
|
|
if (old_sha1 && !is_null_sha1(old_sha1))
|
2011-03-30 17:17:58 +00:00
|
|
|
diff_tree_sha1(old_sha1, new_sha1, "", &opt);
|
2007-05-13 09:24:23 +00:00
|
|
|
else
|
2011-03-30 17:17:58 +00:00
|
|
|
diff_root_tree_sha1(new_sha1, "", &opt);
|
2007-05-13 09:24:23 +00:00
|
|
|
diffcore_std(&opt);
|
|
|
|
diff_flush(&opt);
|
|
|
|
}
|
|
|
|
|
2010-09-30 18:15:14 +00:00
|
|
|
void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
|
2007-05-13 09:24:23 +00:00
|
|
|
{
|
|
|
|
unsigned char *old_sha1 = NULL;
|
|
|
|
|
|
|
|
if (commit->parents)
|
2016-01-05 06:38:53 +00:00
|
|
|
old_sha1 = commit->parents->item->object.oid.hash;
|
|
|
|
cgit_diff_tree(old_sha1, commit->object.oid.hash, fn, prefix,
|
2010-06-24 15:52:57 +00:00
|
|
|
ctx.qry.ignorews);
|
2007-05-13 09:24:23 +00:00
|
|
|
}
|
2008-03-24 15:00:27 +00:00
|
|
|
|
|
|
|
int cgit_parse_snapshots_mask(const char *str)
|
|
|
|
{
|
2014-01-10 11:44:37 +00:00
|
|
|
struct string_list tokens = STRING_LIST_INIT_DUP;
|
|
|
|
struct string_list_item *item;
|
2008-03-24 15:00:27 +00:00
|
|
|
const struct cgit_snapshot_format *f;
|
2014-01-10 11:44:37 +00:00
|
|
|
int rv = 0;
|
2008-03-24 15:00:27 +00:00
|
|
|
|
|
|
|
/* favor legacy setting */
|
2013-03-04 04:21:33 +00:00
|
|
|
if (atoi(str))
|
2008-03-24 15:00:27 +00:00
|
|
|
return 1;
|
2014-01-10 11:44:37 +00:00
|
|
|
|
|
|
|
string_list_split(&tokens, str, ' ', -1);
|
|
|
|
string_list_remove_empty_items(&tokens, 0);
|
|
|
|
|
|
|
|
for_each_string_list_item(item, &tokens) {
|
2008-03-24 15:00:27 +00:00
|
|
|
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
2014-01-10 11:44:37 +00:00
|
|
|
if (!strcmp(item->string, f->suffix) ||
|
|
|
|
!strcmp(item->string, f->suffix + 1)) {
|
2008-03-24 15:00:27 +00:00
|
|
|
rv |= f->bit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 11:44:37 +00:00
|
|
|
|
|
|
|
string_list_clear(&tokens, 0);
|
2008-03-24 15:00:27 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2009-07-31 15:38:38 +00:00
|
|
|
|
2011-03-23 10:57:43 +00:00
|
|
|
typedef struct {
|
|
|
|
char * name;
|
|
|
|
char * value;
|
|
|
|
} cgit_env_var;
|
|
|
|
|
2011-06-06 19:29:58 +00:00
|
|
|
void cgit_prepare_repo_env(struct cgit_repo * repo)
|
|
|
|
{
|
2011-03-23 10:57:43 +00: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 10:22:35 +00: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 09:52:43 +00:00
|
|
|
if (p->value && setenv(p->name, p->value, 1))
|
2011-03-26 10:22:35 +00:00
|
|
|
fprintf(stderr, warn, p->name, p->value);
|
2011-03-23 10:57:43 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 15:17:41 +00: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 17:08:30 +00:00
|
|
|
int fd, e;
|
2009-08-18 15:17:41 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
return errno;
|
2009-11-07 14:24:45 +00:00
|
|
|
if (fstat(fd, &st)) {
|
2009-11-07 17:08:30 +00:00
|
|
|
e = errno;
|
2009-11-07 14:24:45 +00:00
|
|
|
close(fd);
|
2009-11-07 17:08:30 +00:00
|
|
|
return e;
|
2009-11-07 14:24:45 +00:00
|
|
|
}
|
|
|
|
if (!S_ISREG(st.st_mode)) {
|
|
|
|
close(fd);
|
2009-08-18 15:17:41 +00:00
|
|
|
return EISDIR;
|
2009-11-07 14:24:45 +00:00
|
|
|
}
|
2009-08-18 15:17:41 +00:00
|
|
|
*buf = xmalloc(st.st_size + 1);
|
|
|
|
*size = read_in_full(fd, *buf, st.st_size);
|
2009-11-07 17:08:30 +00:00
|
|
|
e = errno;
|
2009-08-18 15:17:41 +00:00
|
|
|
(*buf)[*size] = '\0';
|
2009-11-07 14:24:45 +00:00
|
|
|
close(fd);
|
2009-11-07 17:08:30 +00:00
|
|
|
return (*size == st.st_size ? 0 : e);
|
2009-08-18 15:17:41 +00:00
|
|
|
}
|
2010-03-21 23:09:43 +00:00
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static int is_token_char(char c)
|
2010-03-21 23:09:43 +00:00
|
|
|
{
|
|
|
|
return isalnum(c) || c == '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace name with getenv(name), return pointer to zero-terminating char
|
|
|
|
*/
|
2013-03-04 07:52:33 +00:00
|
|
|
static char *expand_macro(char *name, int maxlength)
|
2010-03-21 23:09:43 +00:00
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
value = getenv(name);
|
|
|
|
if (value) {
|
|
|
|
len = strlen(value);
|
|
|
|
if (len > maxlength)
|
|
|
|
len = maxlength;
|
|
|
|
strncpy(name, value, len);
|
|
|
|
}
|
|
|
|
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 14:50:56 +00:00
|
|
|
|
2015-08-16 12:53:52 +00:00
|
|
|
char *get_mimetype_for_filename(const char *filename)
|
2015-08-14 14:50:56 +00:00
|
|
|
{
|
2015-10-09 09:01:04 +00:00
|
|
|
char *ext, *mimetype, *token, line[1024], *saveptr;
|
2015-08-17 12:35:20 +00:00
|
|
|
FILE *file;
|
2015-08-16 12:53:52 +00:00
|
|
|
struct string_list_item *mime;
|
2015-08-14 14:50:56 +00:00
|
|
|
|
2015-08-17 12:35:20 +00:00
|
|
|
if (!filename)
|
2015-08-14 14:50:56 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-08-16 12:53:52 +00:00
|
|
|
ext = strrchr(filename, '.');
|
2015-08-17 12:35:20 +00: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 12:53:52 +00:00
|
|
|
|
2015-08-17 12:35:20 +00: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 09:01:04 +00:00
|
|
|
mimetype = strtok_r(line, " \t\r\n", &saveptr);
|
|
|
|
while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) {
|
2015-08-17 12:35:20 +00:00
|
|
|
if (!strcasecmp(ext, token)) {
|
|
|
|
fclose(file);
|
|
|
|
return xstrdup(mimetype);
|
2015-08-14 14:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 12:35:20 +00:00
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
2015-08-14 14:50:56 +00:00
|
|
|
}
|
2016-06-25 19:23:51 +00:00
|
|
|
|
2016-06-25 21:29:24 +00:00
|
|
|
static int namespaced_dwim_ref_get_sha1(const char *name, unsigned char *sha1)
|
|
|
|
{
|
|
|
|
/* The standard git name disambiguation order is:
|
|
|
|
$name
|
|
|
|
refs/$name
|
|
|
|
refs/tags/$name
|
|
|
|
refs/heads/$name
|
|
|
|
refs/remotes/$name (not sure why)
|
|
|
|
refs/remotes/$name/HEAD
|
|
|
|
we don't care about remotes, so we can skip those,
|
|
|
|
and we can't specify a prefix for dwm_ref,
|
|
|
|
so we have to do this ourselves */
|
|
|
|
static const char *namespaced_ref_patterns[] = {
|
|
|
|
"%s%s",
|
|
|
|
"%srefs/%s",
|
|
|
|
"%srefs/tags/%s",
|
|
|
|
"%srefs/heads/%s",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
const char **p;
|
|
|
|
|
|
|
|
for (p = namespaced_ref_patterns; *p; p++) {
|
|
|
|
char *fullref = NULL;
|
|
|
|
const char *r;
|
|
|
|
fullref = mkpathdup(*p, get_git_namespace(), name);
|
|
|
|
r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING, sha1, NULL);
|
|
|
|
free(fullref);
|
|
|
|
if (r)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-25 19:23:51 +00:00
|
|
|
int cgit_get_sha1(const char *name, unsigned char *sha1)
|
|
|
|
{
|
2016-06-25 21:29:24 +00:00
|
|
|
if (ctx.repo->namespace) {
|
|
|
|
/* If we have a namespace, we can get either a sha1,
|
|
|
|
or a possibly abbreviated ref.
|
|
|
|
Advanced ref forms are not supported at this time
|
|
|
|
as this would require reimplementing all of ref parsing.
|
|
|
|
If get_sha1_with_context grows support for a namespaced flag
|
|
|
|
then this code may go away. */
|
|
|
|
if (get_sha1_hex(name, sha1) == 0)
|
|
|
|
return 0;
|
|
|
|
return namespaced_dwim_ref_get_sha1(name, sha1);
|
|
|
|
} else {
|
|
|
|
return get_sha1(name, sha1);
|
|
|
|
}
|
2016-06-25 19:23:51 +00:00
|
|
|
}
|