-rw-r--r-- | cgit.h | 4 | ||||
-rw-r--r-- | shared.c | 13 | ||||
-rw-r--r-- | ui-diff.c | 26 | ||||
-rw-r--r-- | ui-log.c | 7 | ||||
-rw-r--r-- | ui-patch.c | 9 |
5 files changed, 52 insertions, 7 deletions
@@ -12,12 +12,13 @@ #include <diff.h> #include <diffcore.h> #include <refs.h> #include <revision.h> #include <log-tree.h> #include <archive.h> +#include <xdiff-interface.h> #include <xdiff/xdiff.h> #include <utf8.h> /* * Dateformats used on misc. pages @@ -218,13 +219,14 @@ extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, void *cb_data); extern void *cgit_free_commitinfo(struct commitinfo *info); extern int cgit_diff_files(const unsigned char *old_sha1, const unsigned char *new_sha1, - linediff_fn fn); + unsigned long *old_size, unsigned long *new_size, + int *binary, linediff_fn fn); extern void cgit_diff_tree(const unsigned char *old_sha1, const unsigned char *new_sha1, filepair_fn fn, const char *prefix); extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); @@ -254,23 +254,32 @@ int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) buflen = 0; } return 0; } int cgit_diff_files(const unsigned char *old_sha1, - const unsigned char *new_sha1, - linediff_fn fn) + const unsigned char *new_sha1, unsigned long *old_size, + unsigned long *new_size, int *binary, linediff_fn fn) { 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; + *old_size = file1.size; + *new_size = file2.size; + + if (buffer_is_binary(file1.ptr, file1.size) || + buffer_is_binary(file2.ptr, file2.size)) { + *binary = 1; + return 0; + } + memset(&diff_params, 0, sizeof(diff_params)); memset(&emit_params, 0, sizeof(emit_params)); memset(&emit_cb, 0, sizeof(emit_cb)); diff_params.flags = XDF_NEED_MINIMAL; emit_params.ctxlen = 3; emit_params.flags = XDL_EMIT_FUNCNAMES; @@ -24,12 +24,15 @@ static struct fileinfo { unsigned short old_mode; unsigned short new_mode; char *old_path; char *new_path; unsigned int added; unsigned int removed; + unsigned long old_size; + unsigned long new_size; + int binary:1; } *items; static void print_fileinfo(struct fileinfo *info) { char *class; @@ -83,12 +86,17 @@ static void print_fileinfo(struct fileinfo *info) ctx.qry.sha2, info->new_path); if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) htmlf(" (%s from %s)", info->status == DIFF_STATUS_COPIED ? "copied" : "renamed", info->old_path); html("</td><td class='right'>"); + if (info->binary) { + htmlf("bin</td><td class='graph'>%d -> %d bytes", + info->old_size, info->new_size); + return; + } htmlf("%d", info->added + info->removed); html("</td><td class='graph'>"); htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes)); htmlf("<td class='add' style='width: %.1f%%;'/>", info->added * 100.0 / max_changes); htmlf("<td class='rem' style='width: %.1f%%;'/>", @@ -107,16 +115,20 @@ static void count_diff_lines(char *line, int len) lines_removed++; } } static void inspect_filepair(struct diff_filepair *pair) { + int binary = 0; + unsigned long old_size = 0; + unsigned long new_size = 0; files++; lines_added = 0; lines_removed = 0; - cgit_diff_files(pair->one->sha1, pair->two->sha1, count_diff_lines); + cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, &new_size, + &binary, count_diff_lines); if (files >= slots) { if (slots == 0) slots = 4; else slots = slots * 2; items = xrealloc(items, slots * sizeof(struct fileinfo)); @@ -127,12 +139,15 @@ static void inspect_filepair(struct diff_filepair *pair) items[files-1].old_mode = pair->one->mode; items[files-1].new_mode = pair->two->mode; items[files-1].old_path = xstrdup(pair->one->path); items[files-1].new_path = xstrdup(pair->two->path); items[files-1].added = lines_added; items[files-1].removed = lines_removed; + items[files-1].old_size = old_size; + items[files-1].new_size = new_size; + items[files-1].binary = binary; if (lines_added + lines_removed > max_changes) max_changes = lines_added + lines_removed; total_adds += lines_added; total_rems += lines_removed; } @@ -230,23 +245,30 @@ static void header(unsigned char *sha1, char *path1, int mode1, } html("</div>"); } static void filepair_cb(struct diff_filepair *pair) { + unsigned long old_size = 0; + unsigned long new_size = 0; + int binary = 0; + header(pair->one->sha1, pair->one->path, pair->one->mode, pair->two->sha1, pair->two->path, pair->two->mode); if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { if (S_ISGITLINK(pair->one->mode)) print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); if (S_ISGITLINK(pair->two->mode)) print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); return; } - if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) + if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, + &new_size, &binary, print_line)) cgit_print_error("Error running diff"); + if (binary) + html("Binary files differ"); } void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) { enum object_type type; unsigned long size; @@ -23,15 +23,20 @@ void count_lines(char *line, int size) else if (line[0] == '-') rem_lines++; } void inspect_files(struct diff_filepair *pair) { + unsigned long old_size = 0; + unsigned long new_size = 0; + int binary = 0; + files++; if (ctx.repo->enable_log_linecount) - cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines); + cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, + &new_size, &binary, count_lines); } void show_commit_decorations(struct commit *commit) { struct name_decoration *deco; static char buf[1024]; @@ -54,23 +54,30 @@ static void header(unsigned char *sha1, char *path1, int mode1, htmlf("+++ b/%s\n", path2); } } static void filepair_cb(struct diff_filepair *pair) { + unsigned long old_size = 0; + unsigned long new_size = 0; + int binary = 0; + header(pair->one->sha1, pair->one->path, pair->one->mode, pair->two->sha1, pair->two->path, pair->two->mode); if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { if (S_ISGITLINK(pair->one->mode)) print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); if (S_ISGITLINK(pair->two->mode)) print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); return; } - if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) + if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, + &new_size, &binary, print_line)) html("Error running diff"); + if (binary) + html("Binary files differ\n"); } void cgit_print_patch(char *hex) { struct commit *commit; struct commitinfo *info; |