aboutsummaryrefslogtreecommitdiffstats
path: root/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared.c')
-rw-r--r--shared.c93
1 files changed, 86 insertions, 7 deletions
diff --git a/shared.c b/shared.c
index 6adf2b6..7ec2e19 100644
--- a/shared.c
+++ b/shared.c
@@ -10,7 +10,6 @@
10 10
11struct cgit_repolist cgit_repolist; 11struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 12struct cgit_context ctx;
13int cgit_cmd;
14 13
15int chk_zero(int result, char *msg) 14int chk_zero(int result, char *msg)
16{ 15{
@@ -57,11 +56,14 @@ struct cgit_repo *cgit_add_repo(const char *url)
57 ret->section = ctx.cfg.section; 56 ret->section = ctx.cfg.section;
58 ret->defbranch = "master"; 57 ret->defbranch = "master";
59 ret->snapshots = ctx.cfg.snapshots; 58 ret->snapshots = ctx.cfg.snapshots;
59 ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
60 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 60 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
61 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 61 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
62 ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
63 ret->enable_subject_links = ctx.cfg.enable_subject_links;
62 ret->max_stats = ctx.cfg.max_stats; 64 ret->max_stats = ctx.cfg.max_stats;
63 ret->module_link = ctx.cfg.module_link; 65 ret->module_link = ctx.cfg.module_link;
64 ret->readme = NULL; 66 ret->readme = ctx.cfg.readme;
65 ret->mtime = -1; 67 ret->mtime = -1;
66 ret->about_filter = ctx.cfg.about_filter; 68 ret->about_filter = ctx.cfg.about_filter;
67 ret->commit_filter = ctx.cfg.commit_filter; 69 ret->commit_filter = ctx.cfg.commit_filter;
@@ -262,7 +264,8 @@ int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
262 264
263int cgit_diff_files(const unsigned char *old_sha1, 265int cgit_diff_files(const unsigned char *old_sha1,
264 const unsigned char *new_sha1, unsigned long *old_size, 266 const unsigned char *new_sha1, unsigned long *old_size,
265 unsigned long *new_size, int *binary, linediff_fn fn) 267 unsigned long *new_size, int *binary, int context,
268 int ignorews, linediff_fn fn)
266{ 269{
267 mmfile_t file1, file2; 270 mmfile_t file1, file2;
268 xpparam_t diff_params; 271 xpparam_t diff_params;
@@ -289,7 +292,9 @@ int cgit_diff_files(const unsigned char *old_sha1,
289 memset(&emit_params, 0, sizeof(emit_params)); 292 memset(&emit_params, 0, sizeof(emit_params));
290 memset(&emit_cb, 0, sizeof(emit_cb)); 293 memset(&emit_cb, 0, sizeof(emit_cb));
291 diff_params.flags = XDF_NEED_MINIMAL; 294 diff_params.flags = XDF_NEED_MINIMAL;
292 emit_params.ctxlen = 3; 295 if (ignorews)
296 diff_params.flags |= XDF_IGNORE_WHITESPACE;
297 emit_params.ctxlen = context > 0 ? context : 3;
293 emit_params.flags = XDL_EMIT_FUNCNAMES; 298 emit_params.flags = XDL_EMIT_FUNCNAMES;
294 emit_cb.outf = filediff_cb; 299 emit_cb.outf = filediff_cb;
295 emit_cb.priv = fn; 300 emit_cb.priv = fn;
@@ -303,7 +308,7 @@ int cgit_diff_files(const unsigned char *old_sha1,
303 308
304void cgit_diff_tree(const unsigned char *old_sha1, 309void cgit_diff_tree(const unsigned char *old_sha1,
305 const unsigned char *new_sha1, 310 const unsigned char *new_sha1,
306 filepair_fn fn, const char *prefix) 311 filepair_fn fn, const char *prefix, int ignorews)
307{ 312{
308 struct diff_options opt; 313 struct diff_options opt;
309 int ret; 314 int ret;
@@ -314,6 +319,8 @@ void cgit_diff_tree(const unsigned char *old_sha1,
314 opt.detect_rename = 1; 319 opt.detect_rename = 1;
315 opt.rename_limit = ctx.cfg.renamelimit; 320 opt.rename_limit = ctx.cfg.renamelimit;
316 DIFF_OPT_SET(&opt, RECURSIVE); 321 DIFF_OPT_SET(&opt, RECURSIVE);
322 if (ignorews)
323 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
317 opt.format_callback = cgit_diff_tree_cb; 324 opt.format_callback = cgit_diff_tree_cb;
318 opt.format_callback_data = fn; 325 opt.format_callback_data = fn;
319 if (prefix) { 326 if (prefix) {
@@ -332,13 +339,14 @@ void cgit_diff_tree(const unsigned char *old_sha1,
332 diff_flush(&opt); 339 diff_flush(&opt);
333} 340}
334 341
335void cgit_diff_commit(struct commit *commit, filepair_fn fn) 342void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
336{ 343{
337 unsigned char *old_sha1 = NULL; 344 unsigned char *old_sha1 = NULL;
338 345
339 if (commit->parents) 346 if (commit->parents)
340 old_sha1 = commit->parents->item->object.sha1; 347 old_sha1 = commit->parents->item->object.sha1;
341 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); 348 cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
349 ctx.qry.ignorews);
342} 350}
343 351
344int cgit_parse_snapshots_mask(const char *str) 352int cgit_parse_snapshots_mask(const char *str)
@@ -430,3 +438,74 @@ int readfile(const char *path, char **buf, size_t *size)
430 close(fd); 438 close(fd);
431 return (*size == st.st_size ? 0 : e); 439 return (*size == st.st_size ? 0 : e);
432} 440}
441
442int is_token_char(char c)
443{
444 return isalnum(c) || c == '_';
445}
446
447/* Replace name with getenv(name), return pointer to zero-terminating char
448 */
449char *expand_macro(char *name, int maxlength)
450{
451 char *value;
452 int len;
453
454 len = 0;
455 value = getenv(name);
456 if (value) {
457 len = strlen(value);
458 if (len > maxlength)
459 len = maxlength;
460 strncpy(name, value, len);
461 }
462 return name + len;
463}
464
465#define EXPBUFSIZE (1024 * 8)
466
467/* Replace all tokens prefixed by '$' in the specified text with the
468 * value of the named environment variable.
469 * NB: the return value is a static buffer, i.e. it must be strdup'd
470 * by the caller.
471 */
472char *expand_macros(const char *txt)
473{
474 static char result[EXPBUFSIZE];
475 char *p, *start;
476 int len;
477
478 p = result;
479 start = NULL;
480 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
481 *p = *txt;
482 if (start) {
483 if (!is_token_char(*txt)) {
484 if (p - start > 0) {
485 *p = '\0';
486 len = result + EXPBUFSIZE - start - 1;
487 p = expand_macro(start, len) - 1;
488 }
489 start = NULL;
490 txt--;
491 }
492 p++;
493 txt++;
494 continue;
495 }
496 if (*txt == '$') {
497 start = p;
498 txt++;
499 continue;
500 }
501 p++;
502 txt++;
503 }
504 *p = '\0';
505 if (start && p - start > 0) {
506 len = result + EXPBUFSIZE - start - 1;
507 p = expand_macro(start, len);
508 *p = '\0';
509 }
510 return result;
511}