blob: 67eb67bdb64e9b27d570e9baba1a28d79f9c233c [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 Hjemlid1f3bbe2008-02-16 13:56:09 +010011struct cgit_repolist cgit_repolist;
Lars Hjemlid14d77f2008-02-16 11:53:40 +010012struct cgit_context ctx;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020013int cgit_cmd;
Lars Hjemlice1c7332007-02-03 15:02:55 +010014
Lars Hjemlif6925032007-06-18 09:42:10 +020015const char *cgit_version = CGIT_VERSION;
16
Lars Hjemlib228d4f2008-02-16 13:07:13 +010017void cgit_prepare_context(struct cgit_context *ctx)
18{
19 memset(ctx, 0, sizeof(ctx));
20 ctx->cfg.agefile = "info/web/last-modified";
21 ctx->cfg.cache_dynamic_ttl = 5;
22 ctx->cfg.cache_max_create_time = 5;
23 ctx->cfg.cache_repo_ttl = 5;
24 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
25 ctx->cfg.cache_root_ttl = 5;
26 ctx->cfg.cache_static_ttl = -1;
27 ctx->cfg.css = "/cgit.css";
28 ctx->cfg.logo = "/git-logo.png";
29 ctx->cfg.max_commit_count = 50;
30 ctx->cfg.max_lock_attempts = 5;
31 ctx->cfg.max_msg_len = 60;
32 ctx->cfg.max_repodesc_len = 60;
33 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
34 ctx->cfg.renamelimit = -1;
35 ctx->cfg.robots = "index, nofollow";
36 ctx->cfg.root_title = "Git repository browser";
37 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
Lars Hjemlif3c1a182008-03-24 00:51:19 +010038 ctx->page.mimetype = "text/html";
39 ctx->page.charset = PAGE_ENCODING;
40 ctx->page.filename = NULL;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010041}
42
Lars Hjemliab2ab952007-02-08 13:53:13 +010043int chk_zero(int result, char *msg)
44{
45 if (result != 0)
46 die("%s: %s", msg, strerror(errno));
47 return result;
48}
49
50int chk_positive(int result, char *msg)
51{
52 if (result <= 0)
53 die("%s: %s", msg, strerror(errno));
54 return result;
55}
56
Michael Krelin127f43d2007-07-20 20:56:43 +020057int chk_non_negative(int result, char *msg)
58{
59 if (result < 0)
60 die("%s: %s",msg, strerror(errno));
61 return result;
62}
63
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010064struct cgit_repo *add_repo(const char *url)
Lars Hjemlice1c7332007-02-03 15:02:55 +010065{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010066 struct cgit_repo *ret;
Lars Hjemlice1c7332007-02-03 15:02:55 +010067
68 if (++cgit_repolist.count > cgit_repolist.length) {
69 if (cgit_repolist.length == 0)
70 cgit_repolist.length = 8;
71 else
72 cgit_repolist.length *= 2;
Lars Hjemli1b49de32007-05-13 11:24:23 +020073 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
74 cgit_repolist.length *
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010075 sizeof(struct cgit_repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +010076 }
77
78 ret = &cgit_repolist.repos[cgit_repolist.count-1];
Lars Hjemli4e40d852007-09-20 00:56:53 +020079 ret->url = trim_end(url, '/');
Lars Hjemlice1c7332007-02-03 15:02:55 +010080 ret->name = ret->url;
81 ret->path = NULL;
Evan Martin7b346642007-12-02 14:39:30 -080082 ret->desc = "[no description]";
Lars Hjemlice1c7332007-02-03 15:02:55 +010083 ret->owner = NULL;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010084 ret->group = ctx.cfg.repo_group;
Lars Hjemlib28b1052007-05-16 00:14:51 +020085 ret->defbranch = "master";
Lars Hjemlib228d4f2008-02-16 13:07:13 +010086 ret->snapshots = ctx.cfg.snapshots;
87 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
88 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
89 ret->module_link = ctx.cfg.module_link;
Lars Hjemlibbcdc292007-05-23 22:46:54 +020090 ret->readme = NULL;
Lars Hjemlice1c7332007-02-03 15:02:55 +010091 return ret;
92}
93
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010094struct cgit_repo *cgit_get_repoinfo(const char *url)
Lars Hjemli305414d2007-05-18 00:47:47 +020095{
96 int i;
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010097 struct cgit_repo *repo;
Lars Hjemli305414d2007-05-18 00:47:47 +020098
99 for (i=0; i<cgit_repolist.count; i++) {
100 repo = &cgit_repolist.repos[i];
101 if (!strcmp(repo->url, url))
102 return repo;
103 }
104 return NULL;
105}
106
Lars Hjemli44923f82006-12-11 17:25:41 +0100107void cgit_global_config_cb(const char *name, const char *value)
108{
Lars Hjemlice1c7332007-02-03 15:02:55 +0100109 if (!strcmp(name, "root-title"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100110 ctx.cfg.root_title = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100111 else if (!strcmp(name, "css"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100112 ctx.cfg.css = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100113 else if (!strcmp(name, "logo"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100114 ctx.cfg.logo = xstrdup(value);
Lars Hjemlide69ce02007-05-19 00:00:25 +0200115 else if (!strcmp(name, "index-header"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100116 ctx.cfg.index_header = xstrdup(value);
Lars Hjemli10ac7ad2007-10-30 10:39:59 +0100117 else if (!strcmp(name, "index-info"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100118 ctx.cfg.index_info = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100119 else if (!strcmp(name, "logo-link"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100120 ctx.cfg.logo_link = xstrdup(value);
Lars Hjemlided93932007-05-11 12:12:48 +0200121 else if (!strcmp(name, "module-link"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100122 ctx.cfg.module_link = xstrdup(value);
Lars Hjemlic188c482007-11-08 12:20:05 +0100123 else if (!strcmp(name, "virtual-root")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100124 ctx.cfg.virtual_root = trim_end(value, '/');
125 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
126 ctx.cfg.virtual_root = "";
Lars Hjemlic188c482007-11-08 12:20:05 +0100127 } else if (!strcmp(name, "nocache"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100128 ctx.cfg.nocache = atoi(value);
Lars Hjemliac70cb42007-02-08 14:47:56 +0100129 else if (!strcmp(name, "snapshots"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100130 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200131 else if (!strcmp(name, "enable-index-links"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100132 ctx.cfg.enable_index_links = atoi(value);
Lars Hjemlie1893442007-05-18 13:55:52 +0200133 else if (!strcmp(name, "enable-log-filecount"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100134 ctx.cfg.enable_log_filecount = atoi(value);
Lars Hjemlie1893442007-05-18 13:55:52 +0200135 else if (!strcmp(name, "enable-log-linecount"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100136 ctx.cfg.enable_log_linecount = atoi(value);
Lars Hjemli61245ad2006-12-16 13:43:01 +0100137 else if (!strcmp(name, "cache-root"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100138 ctx.cfg.cache_root = xstrdup(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100139 else if (!strcmp(name, "cache-root-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100140 ctx.cfg.cache_root_ttl = atoi(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100141 else if (!strcmp(name, "cache-repo-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100142 ctx.cfg.cache_repo_ttl = atoi(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100143 else if (!strcmp(name, "cache-static-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100144 ctx.cfg.cache_static_ttl = atoi(value);
Lars Hjemli378cae62006-12-22 00:56:02 +0100145 else if (!strcmp(name, "cache-dynamic-ttl"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100146 ctx.cfg.cache_dynamic_ttl = atoi(value);
Lars Hjemli7dd50c92007-01-28 12:17:03 +0100147 else if (!strcmp(name, "max-message-length"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100148 ctx.cfg.max_msg_len = atoi(value);
Lars Hjemlic1ad6cb2007-05-16 10:45:45 +0200149 else if (!strcmp(name, "max-repodesc-length"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100150 ctx.cfg.max_repodesc_len = atoi(value);
Lars Hjemlic6cf3a42007-05-13 17:15:06 +0200151 else if (!strcmp(name, "max-commit-count"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100152 ctx.cfg.max_commit_count = atoi(value);
Ondrej Jirman51a960a2007-05-26 03:33:41 +0200153 else if (!strcmp(name, "summary-log"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100154 ctx.cfg.summary_log = atoi(value);
Lars Hjemli763a6a02007-10-27 10:13:42 +0200155 else if (!strcmp(name, "summary-branches"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100156 ctx.cfg.summary_branches = atoi(value);
Lars Hjemlife211c72007-10-25 10:40:16 +0200157 else if (!strcmp(name, "summary-tags"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100158 ctx.cfg.summary_tags = atoi(value);
Lars Hjemli57f6a8b2007-05-22 23:25:25 +0200159 else if (!strcmp(name, "agefile"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100160 ctx.cfg.agefile = xstrdup(value);
Lars Hjemli98fcf722007-09-24 23:52:30 +0200161 else if (!strcmp(name, "renamelimit"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100162 ctx.cfg.renamelimit = atoi(value);
Lars Hjemlid267d882007-11-11 21:57:21 +0100163 else if (!strcmp(name, "robots"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100164 ctx.cfg.robots = xstrdup(value);
Lars Hjemliafcdd082007-12-03 01:49:38 +0100165 else if (!strcmp(name, "clone-prefix"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100166 ctx.cfg.clone_prefix = xstrdup(value);
Lars Hjemli5877c492007-05-18 22:48:22 +0200167 else if (!strcmp(name, "repo.group"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100168 ctx.cfg.repo_group = xstrdup(value);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100169 else if (!strcmp(name, "repo.url"))
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100170 ctx.repo = add_repo(value);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100171 else if (!strcmp(name, "repo.name"))
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100172 ctx.repo->name = xstrdup(value);
173 else if (ctx.repo && !strcmp(name, "repo.path"))
174 ctx.repo->path = trim_end(value, '/');
175 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
176 ctx.repo->clone_url = xstrdup(value);
177 else if (ctx.repo && !strcmp(name, "repo.desc"))
178 ctx.repo->desc = xstrdup(value);
179 else if (ctx.repo && !strcmp(name, "repo.owner"))
180 ctx.repo->owner = xstrdup(value);
181 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
182 ctx.repo->defbranch = xstrdup(value);
183 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
184 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
185 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
186 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
187 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
188 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
189 else if (ctx.repo && !strcmp(name, "repo.module-link"))
190 ctx.repo->module_link= xstrdup(value);
191 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
Lars Hjemlibbcdc292007-05-23 22:46:54 +0200192 if (*value == '/')
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100193 ctx.repo->readme = xstrdup(value);
Lars Hjemlibbcdc292007-05-23 22:46:54 +0200194 else
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100195 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
Lars Hjemlibbcdc292007-05-23 22:46:54 +0200196 } else if (!strcmp(name, "include"))
Lars Hjemli5ec6e022007-05-14 23:40:33 +0200197 cgit_read_config(value, cgit_global_config_cb);
Lars Hjemli44923f82006-12-11 17:25:41 +0100198}
199
Lars Hjemli44923f82006-12-11 17:25:41 +0100200void cgit_querystring_cb(const char *name, const char *value)
201{
Lars Hjemli420712a2006-12-14 00:40:34 +0100202 if (!strcmp(name,"r")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100203 ctx.qry.repo = xstrdup(value);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100204 ctx.repo = cgit_get_repoinfo(value);
Lars Hjemli420712a2006-12-14 00:40:34 +0100205 } else if (!strcmp(name, "p")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100206 ctx.qry.page = xstrdup(value);
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200207 } else if (!strcmp(name, "url")) {
208 cgit_parse_url(value);
Lars Hjemli68ca0322007-10-28 15:23:00 +0100209 } else if (!strcmp(name, "qt")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100210 ctx.qry.grep = xstrdup(value);
Lars Hjemlie39d7382006-12-28 02:01:49 +0100211 } else if (!strcmp(name, "q")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100212 ctx.qry.search = xstrdup(value);
Lars Hjemli420712a2006-12-14 00:40:34 +0100213 } else if (!strcmp(name, "h")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100214 ctx.qry.head = xstrdup(value);
215 ctx.qry.has_symref = 1;
Lars Hjemli44923f82006-12-11 17:25:41 +0100216 } else if (!strcmp(name, "id")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100217 ctx.qry.sha1 = xstrdup(value);
218 ctx.qry.has_sha1 = 1;
Lars Hjemli36aba002006-12-20 22:48:27 +0100219 } else if (!strcmp(name, "id2")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100220 ctx.qry.sha2 = xstrdup(value);
221 ctx.qry.has_sha1 = 1;
Lars Hjemli420712a2006-12-14 00:40:34 +0100222 } else if (!strcmp(name, "ofs")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100223 ctx.qry.ofs = atoi(value);
Lars Hjemli5cd2bf72007-01-12 00:46:17 +0100224 } else if (!strcmp(name, "path")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100225 ctx.qry.path = trim_end(value, '/');
Lars Hjemliab2ab952007-02-08 13:53:13 +0100226 } else if (!strcmp(name, "name")) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100227 ctx.qry.name = xstrdup(value);
Lars Hjemli44923f82006-12-11 17:25:41 +0100228 }
229}
230
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100231void *cgit_free_commitinfo(struct commitinfo *info)
232{
233 free(info->author);
234 free(info->author_email);
235 free(info->committer);
236 free(info->committer_email);
237 free(info->subject);
Jonathan Bastien-Filiatrault3845e172007-10-26 18:09:06 -0400238 free(info->msg);
239 free(info->msg_encoding);
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100240 free(info);
241 return NULL;
242}
Lars Hjemli52e605c2007-01-04 16:53:03 +0100243
244int hextoint(char c)
245{
246 if (c >= 'a' && c <= 'f')
247 return 10 + c - 'a';
248 else if (c >= 'A' && c <= 'F')
249 return 10 + c - 'A';
250 else if (c >= '0' && c <= '9')
251 return c - '0';
252 else
253 return -1;
254}
255
Lars Hjemli382805e2007-06-26 18:04:31 +0200256char *trim_end(const char *str, char c)
257{
258 int len;
259 char *s, *t;
260
261 if (str == NULL)
262 return NULL;
263 t = (char *)str;
264 len = strlen(t);
265 while(len > 0 && t[len - 1] == c)
266 len--;
267
268 if (len == 0)
269 return NULL;
270
271 c = t[len];
272 t[len] = '\0';
273 s = xstrdup(t);
274 t[len] = c;
275 return s;
276}
277
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100278char *strlpart(char *txt, int maxlen)
279{
280 char *result;
281
282 if (!txt)
283 return txt;
284
285 if (strlen(txt) <= maxlen)
286 return txt;
287 result = xmalloc(maxlen + 1);
288 memcpy(result, txt, maxlen - 3);
289 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
290 result[maxlen] = '\0';
291 return result;
292}
293
294char *strrpart(char *txt, int maxlen)
295{
296 char *result;
297
298 if (!txt)
299 return txt;
300
301 if (strlen(txt) <= maxlen)
302 return txt;
303 result = xmalloc(maxlen + 1);
304 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
305 result[0] = result[1] = result[2] = '.';
306 return result;
307}
308
Lars Hjemlie397ff72007-10-25 09:30:06 +0200309void cgit_add_ref(struct reflist *list, struct refinfo *ref)
310{
311 size_t size;
312
313 if (list->count >= list->alloc) {
314 list->alloc += (list->alloc ? list->alloc : 4);
315 size = list->alloc * sizeof(struct refinfo *);
316 list->refs = xrealloc(list->refs, size);
317 }
318 list->refs[list->count++] = ref;
319}
320
321struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
322{
323 struct refinfo *ref;
324
325 ref = xmalloc(sizeof (struct refinfo));
326 ref->refname = xstrdup(refname);
327 ref->object = parse_object(sha1);
328 switch (ref->object->type) {
329 case OBJ_TAG:
330 ref->tag = cgit_parse_tag((struct tag *)ref->object);
331 break;
332 case OBJ_COMMIT:
333 ref->commit = cgit_parse_commit((struct commit *)ref->object);
334 break;
335 }
336 return ref;
337}
338
339int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
340 void *cb_data)
341{
342 struct reflist *list = (struct reflist *)cb_data;
343 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
344
345 if (info)
346 cgit_add_ref(list, info);
347 return 0;
348}
349
Lars Hjemli1b49de32007-05-13 11:24:23 +0200350void cgit_diff_tree_cb(struct diff_queue_struct *q,
351 struct diff_options *options, void *data)
352{
353 int i;
354
355 for (i = 0; i < q->nr; i++) {
356 if (q->queue[i]->status == 'U')
357 continue;
358 ((filepair_fn)data)(q->queue[i]);
359 }
360}
361
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200362static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
363{
364 enum object_type type;
365
366 if (is_null_sha1(sha1)) {
367 file->ptr = (char *)"";
368 file->size = 0;
369 } else {
Lars Hjemli0835ffe2007-09-20 00:21:47 +0200370 file->ptr = read_sha1_file(sha1, &type,
371 (unsigned long *)&file->size);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200372 }
373 return 1;
374}
375
376/*
377 * Receive diff-buffers from xdiff and concatenate them as
378 * needed across multiple callbacks.
379 *
380 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
381 * ripped from git and modified to use globals instead of
382 * a special callback-struct.
383 */
384char *diffbuf = NULL;
385int buflen = 0;
386
387int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
388{
389 int i;
390
391 for (i = 0; i < nbuf; i++) {
392 if (mb[i].ptr[mb[i].size-1] != '\n') {
393 /* Incomplete line */
394 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
395 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
396 buflen += mb[i].size;
397 continue;
398 }
399
400 /* we have a complete line */
401 if (!diffbuf) {
402 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
403 continue;
404 }
405 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
406 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
407 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
408 free(diffbuf);
409 diffbuf = NULL;
410 buflen = 0;
411 }
412 if (diffbuf) {
413 ((linediff_fn)priv)(diffbuf, buflen);
414 free(diffbuf);
415 diffbuf = NULL;
416 buflen = 0;
417 }
418 return 0;
419}
420
421int cgit_diff_files(const unsigned char *old_sha1,
422 const unsigned char *new_sha1,
423 linediff_fn fn)
424{
425 mmfile_t file1, file2;
426 xpparam_t diff_params;
427 xdemitconf_t emit_params;
428 xdemitcb_t emit_cb;
429
430 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
431 return 1;
432
433 diff_params.flags = XDF_NEED_MINIMAL;
434 emit_params.ctxlen = 3;
435 emit_params.flags = XDL_EMIT_FUNCNAMES;
Lars Hjemli0de21a82007-09-04 11:04:47 +0200436 emit_params.find_func = NULL;
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200437 emit_cb.outf = filediff_cb;
438 emit_cb.priv = fn;
439 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
440 return 0;
441}
442
Lars Hjemli1b49de32007-05-13 11:24:23 +0200443void cgit_diff_tree(const unsigned char *old_sha1,
444 const unsigned char *new_sha1,
Lars Hjemlif527a572007-10-01 11:42:19 +0200445 filepair_fn fn, const char *prefix)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200446{
447 struct diff_options opt;
448 int ret;
Lars Hjemlif527a572007-10-01 11:42:19 +0200449 int prefixlen;
Lars Hjemli1b49de32007-05-13 11:24:23 +0200450
451 diff_setup(&opt);
452 opt.output_format = DIFF_FORMAT_CALLBACK;
453 opt.detect_rename = 1;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100454 opt.rename_limit = ctx.cfg.renamelimit;
Lars Hjemli776200b2008-01-13 19:16:23 +0100455 DIFF_OPT_SET(&opt, RECURSIVE);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200456 opt.format_callback = cgit_diff_tree_cb;
457 opt.format_callback_data = fn;
Lars Hjemlif527a572007-10-01 11:42:19 +0200458 if (prefix) {
459 opt.nr_paths = 1;
460 opt.paths = &prefix;
461 prefixlen = strlen(prefix);
462 opt.pathlens = &prefixlen;
463 }
Lars Hjemli1b49de32007-05-13 11:24:23 +0200464 diff_setup_done(&opt);
465
Lars Hjemli4a0be582007-06-17 18:12:03 +0200466 if (old_sha1 && !is_null_sha1(old_sha1))
Lars Hjemli1b49de32007-05-13 11:24:23 +0200467 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
468 else
469 ret = diff_root_tree_sha1(new_sha1, "", &opt);
470 diffcore_std(&opt);
471 diff_flush(&opt);
472}
473
474void cgit_diff_commit(struct commit *commit, filepair_fn fn)
475{
476 unsigned char *old_sha1 = NULL;
477
478 if (commit->parents)
479 old_sha1 = commit->parents->item->object.sha1;
Lars Hjemlif527a572007-10-01 11:42:19 +0200480 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200481}