diff options
| author | Russell Belfer <rb@github.com> | 2013-07-23 15:45:58 -0700 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2013-07-23 15:45:58 -0700 |
| commit | eb1c1707ab6a399734d9083152c05516af052412 (patch) | |
| tree | 9609bee7a24220d476f2573606f6eb5d4f85fb2c /src | |
| parent | df40f3981c312b03415e388663176b2a8315221a (diff) | |
| download | libgit2-eb1c1707ab6a399734d9083152c05516af052412.tar.gz | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/diff_print.c | 65 |
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; } |
