summaryrefslogtreecommitdiff
path: root/src/diff_print.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/diff_print.c')
-rw-r--r--src/diff_print.c65
1 files changed, 43 insertions, 22 deletions
diff --git a/src/diff_print.c b/src/diff_print.c
index f427baa36..4ddd72443 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;
}