blob: 8dd2b0097de6229b7bf5a9989e47ef969dfcb5db [file] [log] [blame]
Lars Hjemlia1a79992006-12-16 01:14:01 +01001/* shared.c: global vars + some callback functions
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
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 Hjemlice1c7332007-02-03 15:02:55 +010011struct repolist cgit_repolist;
12struct repoinfo *cgit_repo;
Lars Hjemlid14d77f2008-02-16 11:53:40 +010013struct cgit_context ctx;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020014int cgit_cmd;
Lars Hjemlice1c7332007-02-03 15:02:55 +010015
Lars Hjemlif6925032007-06-18 09:42:10 +020016const char *cgit_version = CGIT_VERSION;
17
Lars Hjemli44923f82006-12-11 17:25:41 +010018int htmlfd = 0;
19
Lars Hjemlib228d4f2008-02-16 13:07:13 +010020void cgit_prepare_context(struct cgit_context *ctx)
21{
22 memset(ctx, 0, sizeof(ctx));
23 ctx->cfg.agefile = "info/web/last-modified";
24 ctx->cfg.cache_dynamic_ttl = 5;
25 ctx->cfg.cache_max_create_time = 5;
26 ctx->cfg.cache_repo_ttl = 5;
27 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
28 ctx->cfg.cache_root_ttl = 5;
29 ctx->cfg.cache_static_ttl = -1;
30 ctx->cfg.css = "/cgit.css";
31 ctx->cfg.logo = "/git-logo.png";
32 ctx->cfg.max_commit_count = 50;
33 ctx->cfg.max_lock_attempts = 5;
34 ctx->cfg.max_msg_len = 60;
35 ctx->cfg.max_repodesc_len = 60;
36 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
37 ctx->cfg.renamelimit = -1;
38 ctx->cfg.robots = "index, nofollow";
39 ctx->cfg.root_title = "Git repository browser";
40 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
41}
42
Lars Hjemli43d40f22007-05-18 00:50:46 +020043int cgit_get_cmd_index(const char *cmd)
44{
Lars Hjemliffc69732007-06-16 20:20:42 +020045 static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
Lars Hjemli620bb3e2007-12-10 21:47:29 +010046 "snapshot", "tag", "refs", "patch", NULL};
Lars Hjemli43d40f22007-05-18 00:50:46 +020047 int i;
48
49 for(i = 0; cmds[i]; i++)
50 if (!strcmp(cmd, cmds[i]))
51 return i + 1;
52 return 0;
53}
54
Lars Hjemliab2ab952007-02-08 13:53:13 +010055int chk_zero(int result, char *msg)
56{
57 if (result != 0)
58 die("%s: %s", msg, strerror(errno));
59 return result;
60}
61
62int chk_positive(int result, char *msg)
63{
64 if (result <= 0)
65 die("%s: %s", msg, strerror(errno));
66 return result;
67}
68
Michael Krelin127f43d2007-07-20 20:56:43 +020069int chk_non_negative(int result, char *msg)
70{
71 if (result < 0)
72 die("%s: %s",msg, strerror(errno));
73 return result;
74}
75
Lars Hjemlice1c7332007-02-03 15:02:55 +010076struct repoinfo *add_repo(const char *url)
77{
78 struct repoinfo *ret;
79
80 if (++cgit_repolist.count > cgit_repolist.length) {
81 if (cgit_repolist.length == 0)
82 cgit_repolist.length = 8;
83 else
84 cgit_repolist.length *= 2;
Lars Hjemli1b49de32007-05-13 11:24:23 +020085 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
86 cgit_repolist.length *
Lars Hjemlice1c7332007-02-03 15:02:55 +010087 sizeof(struct repoinfo));
88 }
89
90 ret = &cgit_repolist.repos[cgit_repolist.count-1];
Lars Hjemli4e40d852007-09-20 00:56:53 +020091 ret->url = trim_end(url, '/');
Lars Hjemlice1c7332007-02-03 15:02:55 +010092 ret->name = ret->url;
93 ret->path = NULL;
Evan Martin7b346642007-12-02 14:39:30 -080094 ret->desc = "[no description]";
Lars Hjemlice1c7332007-02-03 15:02:55 +010095 ret->owner = NULL;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010096 ret->group = ctx.cfg.repo_group;
Lars Hjemlib28b1052007-05-16 00:14:51 +020097 ret->defbranch = "master";
Lars Hjemlib228d4f2008-02-16 13:07:13 +010098 ret->snapshots = ctx.cfg.snapshots;
99 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
100 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
101 ret->module_link = ctx.cfg.module_link;
Lars Hjemlibbcdc292007-05-23 22:46:54 +0200102 ret->readme = NULL;
Lars Hjemlice1c7332007-02-03 15:02:55 +0100103 return ret;
104}
105
Lars Hjemli305414d2007-05-18 00:47:47 +0200106struct repoinfo *cgit_get_repoinfo(const char *url)
107{
108 int i;
109 struct repoinfo *repo;
110
111 for (i=0; i<cgit_repolist.count; i++) {
112 repo = &cgit_repolist.repos[i];
113 if (!strcmp(repo->url, url))
114 return repo;
115 }
116 return NULL;
117}
118
Lars Hjemli44923f82006-12-11 17:25:41 +0100119void cgit_global_config_cb(const char *name, const char *value)
120{
Lars Hjemlice1c7332007-02-03 15:02:55 +0100121 if (!strcmp(name, "root-title"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100122 ctx.cfg.root_title = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100123 else if (!strcmp(name, "css"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100124 ctx.cfg.css = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100125 else if (!strcmp(name, "logo"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100126 ctx.cfg.logo = xstrdup(value);
Lars Hjemlide69ce02007-05-19 00:00:25 +0200127 else if (!strcmp(name, "index-header"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100128 ctx.cfg.index_header = xstrdup(value);
Lars Hjemli10ac7ad2007-10-30 10:39:59 +0100129 else if (!strcmp(name, "index-info"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100130 ctx.cfg.index_info = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100131 else if (!strcmp(name, "logo-link"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100132 ctx.cfg.logo_link = xstrdup(value);
Lars Hjemlided93932007-05-11 12:12:48 +0200133 else if (!strcmp(name, "module-link"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100134 ctx.cfg.module_link = xstrdup(value);
Lars Hjemlic188c482007-11-08 12:20:05 +0100135 else if (!strcmp(name, "virtual-root")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100136 ctx.cfg.virtual_root = trim_end(value, '/');
137 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
138 ctx.cfg.virtual_root = "";
Lars Hjemlic188c482007-11-08 12:20:05 +0100139 } else if (!strcmp(name, "nocache"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100140 ctx.cfg.nocache = atoi(value);
Lars Hjemliac70cb42007-02-08 14:47:56 +0100141 else if (!strcmp(name, "snapshots"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100142 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200143 else if (!strcmp(name, "enable-index-links"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100144 ctx.cfg.enable_index_links = atoi(value);
Lars Hjemlie1893442007-05-18 13:55:52 +0200145 else if (!strcmp(name, "enable-log-filecount"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100146 ctx.cfg.enable_log_filecount = atoi(value);
Lars Hjemlie1893442007-05-18 13:55:52 +0200147 else if (!strcmp(name, "enable-log-linecount"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100148 ctx.cfg.enable_log_linecount = atoi(value);
Lars Hjemli61245ad2006-12-16 13:43:01 +0100149 else if (!strcmp(name, "cache-root"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100150 ctx.cfg.cache_root = xstrdup(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100151 else if (!strcmp(name, "cache-root-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100152 ctx.cfg.cache_root_ttl = atoi(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100153 else if (!strcmp(name, "cache-repo-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100154 ctx.cfg.cache_repo_ttl = atoi(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100155 else if (!strcmp(name, "cache-static-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100156 ctx.cfg.cache_static_ttl = atoi(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100157 else if (!strcmp(name, "cache-dynamic-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100158 ctx.cfg.cache_dynamic_ttl = atoi(value);
Lars Hjemli7dd50c92007-01-28 12:17:03 +0100159 else if (!strcmp(name, "max-message-length"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100160 ctx.cfg.max_msg_len = atoi(value);
Lars Hjemlic1ad6cb2007-05-16 10:45:45 +0200161 else if (!strcmp(name, "max-repodesc-length"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100162 ctx.cfg.max_repodesc_len = atoi(value);
Lars Hjemlic6cf3a42007-05-13 17:15:06 +0200163 else if (!strcmp(name, "max-commit-count"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100164 ctx.cfg.max_commit_count = atoi(value);
Ondrej Jirman51a960a2007-05-26 03:33:41 +0200165 else if (!strcmp(name, "summary-log"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100166 ctx.cfg.summary_log = atoi(value);
Lars Hjemli763a6a02007-10-27 10:13:42 +0200167 else if (!strcmp(name, "summary-branches"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100168 ctx.cfg.summary_branches = atoi(value);
Lars Hjemlife211c72007-10-25 10:40:16 +0200169 else if (!strcmp(name, "summary-tags"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100170 ctx.cfg.summary_tags = atoi(value);
Lars Hjemli57f6a8b2007-05-22 23:25:25 +0200171 else if (!strcmp(name, "agefile"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100172 ctx.cfg.agefile = xstrdup(value);
Lars Hjemli98fcf722007-09-24 23:52:30 +0200173 else if (!strcmp(name, "renamelimit"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100174 ctx.cfg.renamelimit = atoi(value);
Lars Hjemlid267d882007-11-11 21:57:21 +0100175 else if (!strcmp(name, "robots"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100176 ctx.cfg.robots = xstrdup(value);
Lars Hjemliafcdd082007-12-03 01:49:38 +0100177 else if (!strcmp(name, "clone-prefix"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100178 ctx.cfg.clone_prefix = xstrdup(value);
Lars Hjemli5877c492007-05-18 22:48:22 +0200179 else if (!strcmp(name, "repo.group"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100180 ctx.cfg.repo_group = xstrdup(value);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100181 else if (!strcmp(name, "repo.url"))
182 cgit_repo = add_repo(value);
183 else if (!strcmp(name, "repo.name"))
184 cgit_repo->name = xstrdup(value);
185 else if (cgit_repo && !strcmp(name, "repo.path"))
Lars Hjemli4e40d852007-09-20 00:56:53 +0200186 cgit_repo->path = trim_end(value, '/');
Lars Hjemliafcdd082007-12-03 01:49:38 +0100187 else if (cgit_repo && !strcmp(name, "repo.clone-url"))
188 cgit_repo->clone_url = xstrdup(value);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100189 else if (cgit_repo && !strcmp(name, "repo.desc"))
190 cgit_repo->desc = xstrdup(value);
191 else if (cgit_repo && !strcmp(name, "repo.owner"))
192 cgit_repo->owner = xstrdup(value);
Lars Hjemlib28b1052007-05-16 00:14:51 +0200193 else if (cgit_repo && !strcmp(name, "repo.defbranch"))
194 cgit_repo->defbranch = xstrdup(value);
Lars Hjemliac70cb42007-02-08 14:47:56 +0100195 else if (cgit_repo && !strcmp(name, "repo.snapshots"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100196 cgit_repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
Lars Hjemlie1893442007-05-18 13:55:52 +0200197 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100198 cgit_repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
Lars Hjemlie1893442007-05-18 13:55:52 +0200199 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100200 cgit_repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
Lars Hjemlided93932007-05-11 12:12:48 +0200201 else if (cgit_repo && !strcmp(name, "repo.module-link"))
202 cgit_repo->module_link= xstrdup(value);
Lars Hjemlibbcdc292007-05-23 22:46:54 +0200203 else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) {
204 if (*value == '/')
205 cgit_repo->readme = xstrdup(value);
206 else
207 cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value));
208 } else if (!strcmp(name, "include"))
Lars Hjemli5ec6e022007-05-14 23:40:33 +0200209 cgit_read_config(value, cgit_global_config_cb);
Lars Hjemli44923f82006-12-11 17:25:41 +0100210}
211
Lars Hjemli44923f82006-12-11 17:25:41 +0100212void cgit_querystring_cb(const char *name, const char *value)
213{
Lars Hjemli420712a2006-12-14 00:40:34 +0100214 if (!strcmp(name,"r")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100215 ctx.qry.repo = xstrdup(value);
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200216 cgit_repo = cgit_get_repoinfo(value);
Lars Hjemli420712a2006-12-14 00:40:34 +0100217 } else if (!strcmp(name, "p")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100218 ctx.qry.page = xstrdup(value);
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200219 cgit_cmd = cgit_get_cmd_index(value);
220 } else if (!strcmp(name, "url")) {
221 cgit_parse_url(value);
Lars Hjemli68ca0322007-10-28 15:23:00 +0100222 } else if (!strcmp(name, "qt")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100223 ctx.qry.grep = xstrdup(value);
Lars Hjemlie39d7382006-12-28 02:01:49 +0100224 } else if (!strcmp(name, "q")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100225 ctx.qry.search = xstrdup(value);
Lars Hjemli420712a2006-12-14 00:40:34 +0100226 } else if (!strcmp(name, "h")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100227 ctx.qry.head = xstrdup(value);
228 ctx.qry.has_symref = 1;
Lars Hjemli44923f82006-12-11 17:25:41 +0100229 } else if (!strcmp(name, "id")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100230 ctx.qry.sha1 = xstrdup(value);
231 ctx.qry.has_sha1 = 1;
Lars Hjemli36aba002006-12-20 22:48:27 +0100232 } else if (!strcmp(name, "id2")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100233 ctx.qry.sha2 = xstrdup(value);
234 ctx.qry.has_sha1 = 1;
Lars Hjemli420712a2006-12-14 00:40:34 +0100235 } else if (!strcmp(name, "ofs")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100236 ctx.qry.ofs = atoi(value);
Lars Hjemli5cd2bf72007-01-12 00:46:17 +0100237 } else if (!strcmp(name, "path")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100238 ctx.qry.path = trim_end(value, '/');
Lars Hjemliab2ab952007-02-08 13:53:13 +0100239 } else if (!strcmp(name, "name")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100240 ctx.qry.name = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100241 }
242}
243
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100244void *cgit_free_commitinfo(struct commitinfo *info)
245{
246 free(info->author);
247 free(info->author_email);
248 free(info->committer);
249 free(info->committer_email);
250 free(info->subject);
Jonathan Bastien-Filiatrault3845e172007-10-26 18:09:06 -0400251 free(info->msg);
252 free(info->msg_encoding);
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100253 free(info);
254 return NULL;
255}
Lars Hjemli52e605c2007-01-04 16:53:03 +0100256
257int hextoint(char c)
258{
259 if (c >= 'a' && c <= 'f')
260 return 10 + c - 'a';
261 else if (c >= 'A' && c <= 'F')
262 return 10 + c - 'A';
263 else if (c >= '0' && c <= '9')
264 return c - '0';
265 else
266 return -1;
267}
268
Lars Hjemli382805e2007-06-26 18:04:31 +0200269char *trim_end(const char *str, char c)
270{
271 int len;
272 char *s, *t;
273
274 if (str == NULL)
275 return NULL;
276 t = (char *)str;
277 len = strlen(t);
278 while(len > 0 && t[len - 1] == c)
279 len--;
280
281 if (len == 0)
282 return NULL;
283
284 c = t[len];
285 t[len] = '\0';
286 s = xstrdup(t);
287 t[len] = c;
288 return s;
289}
290
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100291char *strlpart(char *txt, int maxlen)
292{
293 char *result;
294
295 if (!txt)
296 return txt;
297
298 if (strlen(txt) <= maxlen)
299 return txt;
300 result = xmalloc(maxlen + 1);
301 memcpy(result, txt, maxlen - 3);
302 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
303 result[maxlen] = '\0';
304 return result;
305}
306
307char *strrpart(char *txt, int maxlen)
308{
309 char *result;
310
311 if (!txt)
312 return txt;
313
314 if (strlen(txt) <= maxlen)
315 return txt;
316 result = xmalloc(maxlen + 1);
317 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
318 result[0] = result[1] = result[2] = '.';
319 return result;
320}
321
Lars Hjemlie397ff72007-10-25 09:30:06 +0200322void cgit_add_ref(struct reflist *list, struct refinfo *ref)
323{
324 size_t size;
325
326 if (list->count >= list->alloc) {
327 list->alloc += (list->alloc ? list->alloc : 4);
328 size = list->alloc * sizeof(struct refinfo *);
329 list->refs = xrealloc(list->refs, size);
330 }
331 list->refs[list->count++] = ref;
332}
333
334struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
335{
336 struct refinfo *ref;
337
338 ref = xmalloc(sizeof (struct refinfo));
339 ref->refname = xstrdup(refname);
340 ref->object = parse_object(sha1);
341 switch (ref->object->type) {
342 case OBJ_TAG:
343 ref->tag = cgit_parse_tag((struct tag *)ref->object);
344 break;
345 case OBJ_COMMIT:
346 ref->commit = cgit_parse_commit((struct commit *)ref->object);
347 break;
348 }
349 return ref;
350}
351
352int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
353 void *cb_data)
354{
355 struct reflist *list = (struct reflist *)cb_data;
356 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
357
358 if (info)
359 cgit_add_ref(list, info);
360 return 0;
361}
362
Lars Hjemli1b49de32007-05-13 11:24:23 +0200363void cgit_diff_tree_cb(struct diff_queue_struct *q,
364 struct diff_options *options, void *data)
365{
366 int i;
367
368 for (i = 0; i < q->nr; i++) {
369 if (q->queue[i]->status == 'U')
370 continue;
371 ((filepair_fn)data)(q->queue[i]);
372 }
373}
374
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200375static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
376{
377 enum object_type type;
378
379 if (is_null_sha1(sha1)) {
380 file->ptr = (char *)"";
381 file->size = 0;
382 } else {
Lars Hjemli0835ffe2007-09-20 00:21:47 +0200383 file->ptr = read_sha1_file(sha1, &type,
384 (unsigned long *)&file->size);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200385 }
386 return 1;
387}
388
389/*
390 * Receive diff-buffers from xdiff and concatenate them as
391 * needed across multiple callbacks.
392 *
393 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
394 * ripped from git and modified to use globals instead of
395 * a special callback-struct.
396 */
397char *diffbuf = NULL;
398int buflen = 0;
399
400int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
401{
402 int i;
403
404 for (i = 0; i < nbuf; i++) {
405 if (mb[i].ptr[mb[i].size-1] != '\n') {
406 /* Incomplete line */
407 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
408 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
409 buflen += mb[i].size;
410 continue;
411 }
412
413 /* we have a complete line */
414 if (!diffbuf) {
415 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
416 continue;
417 }
418 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
419 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
420 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
421 free(diffbuf);
422 diffbuf = NULL;
423 buflen = 0;
424 }
425 if (diffbuf) {
426 ((linediff_fn)priv)(diffbuf, buflen);
427 free(diffbuf);
428 diffbuf = NULL;
429 buflen = 0;
430 }
431 return 0;
432}
433
434int cgit_diff_files(const unsigned char *old_sha1,
435 const unsigned char *new_sha1,
436 linediff_fn fn)
437{
438 mmfile_t file1, file2;
439 xpparam_t diff_params;
440 xdemitconf_t emit_params;
441 xdemitcb_t emit_cb;
442
443 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
444 return 1;
445
446 diff_params.flags = XDF_NEED_MINIMAL;
447 emit_params.ctxlen = 3;
448 emit_params.flags = XDL_EMIT_FUNCNAMES;
Lars Hjemli0de21a82007-09-04 11:04:47 +0200449 emit_params.find_func = NULL;
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200450 emit_cb.outf = filediff_cb;
451 emit_cb.priv = fn;
452 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
453 return 0;
454}
455
Lars Hjemli1b49de32007-05-13 11:24:23 +0200456void cgit_diff_tree(const unsigned char *old_sha1,
457 const unsigned char *new_sha1,
Lars Hjemlif527a572007-10-01 11:42:19 +0200458 filepair_fn fn, const char *prefix)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200459{
460 struct diff_options opt;
461 int ret;
Lars Hjemlif527a572007-10-01 11:42:19 +0200462 int prefixlen;
Lars Hjemli1b49de32007-05-13 11:24:23 +0200463
464 diff_setup(&opt);
465 opt.output_format = DIFF_FORMAT_CALLBACK;
466 opt.detect_rename = 1;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100467 opt.rename_limit = ctx.cfg.renamelimit;
Lars Hjemli776200b2008-01-13 19:16:23 +0100468 DIFF_OPT_SET(&opt, RECURSIVE);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200469 opt.format_callback = cgit_diff_tree_cb;
470 opt.format_callback_data = fn;
Lars Hjemlif527a572007-10-01 11:42:19 +0200471 if (prefix) {
472 opt.nr_paths = 1;
473 opt.paths = &prefix;
474 prefixlen = strlen(prefix);
475 opt.pathlens = &prefixlen;
476 }
Lars Hjemli1b49de32007-05-13 11:24:23 +0200477 diff_setup_done(&opt);
478
Lars Hjemli4a0be582007-06-17 18:12:03 +0200479 if (old_sha1 && !is_null_sha1(old_sha1))
Lars Hjemli1b49de32007-05-13 11:24:23 +0200480 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
481 else
482 ret = diff_root_tree_sha1(new_sha1, "", &opt);
483 diffcore_std(&opt);
484 diff_flush(&opt);
485}
486
487void cgit_diff_commit(struct commit *commit, filepair_fn fn)
488{
489 unsigned char *old_sha1 = NULL;
490
491 if (commit->parents)
492 old_sha1 = commit->parents->item->object.sha1;
Lars Hjemlif527a572007-10-01 11:42:19 +0200493 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200494}