]> granicus.if.org Git - cgit/commitdiff
Handle binary files in diffs
authorLars Hjemli <hjemli@gmail.com>
Sat, 31 Jan 2009 09:40:40 +0000 (10:40 +0100)
committerLars Hjemli <hjemli@gmail.com>
Sat, 31 Jan 2009 09:40:40 +0000 (10:40 +0100)
This teaches all diff-related operations (i.e. ui-log, ui-diff and ui-patch)
how to handle binary files.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
cgit.h
shared.c
ui-diff.c
ui-log.c
ui-patch.c

diff --git a/cgit.h b/cgit.h
index 4fe94c69e6939d969572a6c1b4601d5267f390c2..8b0defa988d062a24fa40dbcd7a39d2b68de61cb 100644 (file)
--- a/cgit.h
+++ b/cgit.h
@@ -15,6 +15,7 @@
 #include <revision.h>
 #include <log-tree.h>
 #include <archive.h>
+#include <xdiff-interface.h>
 #include <xdiff/xdiff.h>
 #include <utf8.h>
 
@@ -221,7 +222,8 @@ 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,
index 578a54430a38d89036c2ae747e41a48d0f9666f1..dbb84d84cae97790426df67f61cd4cb305a39dce 100644 (file)
--- a/shared.c
+++ b/shared.c
@@ -257,8 +257,8 @@ int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
 }
 
 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;
@@ -268,6 +268,15 @@ int cgit_diff_files(const unsigned char *old_sha1,
        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));
index 878496d43430fc2a9770f6cb43431a7e1b5d7d90..21967451e3fdb5adc462210616b8df6065fe817b 100644 (file)
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -27,6 +27,9 @@ static struct fileinfo {
        char *new_path;
        unsigned int added;
        unsigned int removed;
+       unsigned long old_size;
+       unsigned long new_size;
+       int binary:1;
 } *items;
 
 
@@ -86,6 +89,11 @@ static void print_fileinfo(struct fileinfo *info)
                      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));
@@ -110,10 +118,14 @@ static void count_diff_lines(char *line, int len)
 
 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;
@@ -130,6 +142,9 @@ static void inspect_filepair(struct diff_filepair *pair)
        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;
@@ -233,6 +248,10 @@ static void header(unsigned char *sha1, char *path1, int mode1,
 
 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)) {
@@ -242,8 +261,11 @@ static void filepair_cb(struct diff_filepair *pair)
                        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)
index 32028484b7a531898aa4c3a00e51ebad4ec53da6..ba2ab0314186fbbed847907f113227d879767cea 100644 (file)
--- a/ui-log.c
+++ b/ui-log.c
@@ -26,9 +26,14 @@ void count_lines(char *line, int size)
 
 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)
index 1d7733645d68efb41e682fa8e24c80dc9f8958df..5d665d3edbe826c01705ef7f009d045860b6e243 100644 (file)
@@ -57,6 +57,10 @@ static void header(unsigned char *sha1, char *path1, int mode1,
 
 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)) {
@@ -66,8 +70,11 @@ static void filepair_cb(struct diff_filepair *pair)
                        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)