Restore GIT_DIFF_LINE_BINARY usage This restores the usage of GIT_DIFF_LINE_BINARY for the diff output line that reads "Binary files x and y differ" so that it can be optionally colorized independently of the file header.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 269eb77..c989ba4 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -391,7 +391,7 @@ typedef enum {
*/
GIT_DIFF_LINE_FILE_HDR = 'F',
GIT_DIFF_LINE_HUNK_HDR = 'H',
- GIT_DIFF_LINE_BINARY = 'B' /**< Deprecated, will not be returned */
+ GIT_DIFF_LINE_BINARY = 'B' /**< For "Binary files x and y differ" */
} git_diff_line_t;
/**
diff --git a/src/diff_print.c b/src/diff_print.c
index f427baa..4ddd724 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -228,16 +228,35 @@ static int diff_print_oid_range(
return 0;
}
-int git_diff_delta__format_file_header(
+static int diff_delta_format_with_paths(
git_buf *out,
const git_diff_delta *delta,
const char *oldpfx,
const char *newpfx,
- int oid_strlen)
+ const char *template)
{
const char *oldpath = delta->old_file.path;
const char *newpath = delta->new_file.path;
+ if (git_oid_iszero(&delta->old_file.oid)) {
+ oldpfx = "";
+ oldpath = "/dev/null";
+ }
+ if (git_oid_iszero(&delta->new_file.oid)) {
+ newpfx = "";
+ newpath = "/dev/null";
+ }
+
+ return git_buf_printf(out, template, oldpfx, oldpath, newpfx, newpath);
+}
+
+int git_diff_delta__format_file_header(
+ git_buf *out,
+ const git_diff_delta *delta,
+ const char *oldpfx,
+ const char *newpfx,
+ int oid_strlen)
+{
if (!oldpfx)
oldpfx = DIFF_OLD_PREFIX_DEFAULT;
if (!newpfx)
@@ -248,28 +267,14 @@ int git_diff_delta__format_file_header(
git_buf_clear(out);
git_buf_printf(out, "diff --git %s%s %s%s\n",
- oldpfx, oldpath, newpfx, newpath);
+ oldpfx, delta->old_file.path, newpfx, delta->new_file.path);
if (diff_print_oid_range(out, delta, oid_strlen) < 0)
return -1;
- if (git_oid_iszero(&delta->old_file.oid)) {
- oldpfx = "";
- oldpath = "/dev/null";
- }
- if (git_oid_iszero(&delta->new_file.oid)) {
- newpfx = "";
- newpath = "/dev/null";
- }
-
- if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) {
- git_buf_printf(out, "--- %s%s\n", oldpfx, oldpath);
- git_buf_printf(out, "+++ %s%s\n", newpfx, newpath);
- } else {
- git_buf_printf(
- out, "Binary files %s%s and %s%s differ\n",
- oldpfx, oldpath, newpfx, newpath);
- }
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
+ diff_delta_format_with_paths(
+ out, delta, oldpfx, newpfx, "--- %s%s\n+++ %s%s\n");
return git_buf_oom(out) ? -1 : 0;
}
@@ -278,8 +283,10 @@ static int diff_print_patch_file(
const git_diff_delta *delta, float progress, void *data)
{
diff_print_info *pi = data;
- const char *oldpfx = pi->diff ? pi->diff->opts.old_prefix : NULL;
- const char *newpfx = pi->diff ? pi->diff->opts.new_prefix : NULL;
+ const char *oldpfx =
+ pi->diff ? pi->diff->opts.old_prefix : DIFF_OLD_PREFIX_DEFAULT;
+ const char *newpfx =
+ pi->diff ? pi->diff->opts.new_prefix : DIFF_NEW_PREFIX_DEFAULT;
uint32_t opts_flags = pi->diff ? pi->diff->opts.flags : GIT_DIFF_NORMAL;
GIT_UNUSED(progress);
@@ -299,6 +306,20 @@ static int diff_print_patch_file(
git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
return callback_error();
+ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
+ return 0;
+
+ git_buf_clear(pi->buf);
+
+ if (diff_delta_format_with_paths(
+ pi->buf, delta, oldpfx, newpfx,
+ "Binary files %s%s and %s%s differ\n") < 0)
+ return -1;
+
+ if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_BINARY,
+ git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+ return callback_error();
+
return 0;
}