+#include <stdio.h>
+#include <linux/limits.h>
+
+struct cgit_repolist cgit_repolist;
+struct cgit_context ctx;
+
+int chk_zero(int result, char *msg)
+{
+ if (result != 0)
+ die("%s: %s", msg, strerror(errno));
+ return result;
+}
+
+int chk_positive(int result, char *msg)
+{
+ if (result <= 0)
+ die("%s: %s", msg, strerror(errno));
+ return result;
+}
+
+int chk_non_negative(int result, char *msg)
+{
+ if (result < 0)
+ die("%s: %s",msg, strerror(errno));
+ return result;
+}
+
+struct cgit_repo *cgit_add_repo(const char *url)
+{
+ struct cgit_repo *ret;
+
+ if (++cgit_repolist.count > cgit_repolist.length) {
+ if (cgit_repolist.length == 0)
+ cgit_repolist.length = 8;
+ else
+ cgit_repolist.length *= 2;
+ cgit_repolist.repos = xrealloc(cgit_repolist.repos,
+ cgit_repolist.length *
+ sizeof(struct cgit_repo));
+ }
+
+ ret = &cgit_repolist.repos[cgit_repolist.count-1];
+ memset(ret, 0, sizeof(struct cgit_repo));
+ ret->url = trim_end(url, '/');
+ ret->name = ret->url;
+ ret->path = NULL;
+ ret->desc = "[no description]";
+ ret->owner = NULL;
+ ret->section = ctx.cfg.section;
+ ret->defbranch = "master";
+ ret->snapshots = ctx.cfg.snapshots;
+ ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
+ ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
+ ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
+ ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
+ ret->enable_subject_links = ctx.cfg.enable_subject_links;
+ ret->max_stats = ctx.cfg.max_stats;
+ ret->module_link = ctx.cfg.module_link;
+ ret->readme = ctx.cfg.readme;
+ ret->mtime = -1;
+ ret->about_filter = ctx.cfg.about_filter;
+ ret->commit_filter = ctx.cfg.commit_filter;
+ ret->source_filter = ctx.cfg.source_filter;
+ ret->clone_url = ctx.cfg.clone_url;
+ return ret;
+}
+
+struct cgit_repo *cgit_get_repoinfo(const char *url)
+{
+ int i;
+ struct cgit_repo *repo;
+
+ for (i=0; i<cgit_repolist.count; i++) {
+ repo = &cgit_repolist.repos[i];
+ if (!strcmp(repo->url, url))
+ return repo;
+ }
+ return NULL;
+}
+
+void *cgit_free_commitinfo(struct commitinfo *info)
+{
+ free(info->author);
+ free(info->author_email);
+ free(info->committer);
+ free(info->committer_email);
+ free(info->subject);
+ free(info->msg);
+ free(info->msg_encoding);
+ free(info);
+ return NULL;
+}
+
+char *trim_end(const char *str, char c)
+{
+ int len;
+
+ if (str == NULL)
+ return NULL;
+ len = strlen(str);
+ while(len > 0 && str[len - 1] == c)
+ len--;
+ if (len == 0)
+ return NULL;
+ return xstrndup(str, len);
+}
+
+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;
+}
+
+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;
+}
+
+struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
+{
+ struct refinfo *ref;
+
+ ref = xmalloc(sizeof (struct refinfo));
+ ref->refname = xstrdup(refname);
+ ref->object = parse_object(sha1);
+ 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;
+}
+
+int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
+ void *cb_data)
+{
+ struct reflist *list = (struct reflist *)cb_data;
+ struct refinfo *info = cgit_mk_refinfo(refname, sha1);
+
+ if (info)
+ cgit_add_ref(list, info);
+ return 0;
+}
+
+void cgit_diff_tree_cb(struct diff_queue_struct *q,
+ struct diff_options *options, void *data)
+{
+ int i;