vendor/libgit2/src/diff_print.c in rugged-1.3.2.3 vs vendor/libgit2/src/diff_print.c in rugged-1.4.2

- old
+ new

@@ -5,10 +5,11 @@ * a Linking Exception. For full terms see the included COPYING file. */ #include "common.h" +#include "buf.h" #include "diff.h" #include "diff_file.h" #include "patch_generate.h" #include "futils.h" #include "zstream.h" @@ -19,11 +20,11 @@ typedef struct { git_diff_format_t format; git_diff_line_cb print_cb; void *payload; - git_buf *buf; + git_str *buf; git_diff_line line; const char *old_prefix; const char *new_prefix; uint32_t flags; @@ -32,11 +33,11 @@ int (*strcomp)(const char *, const char *); } diff_print_info; static int diff_print_info_init__common( diff_print_info *pi, - git_buf *out, + git_str *out, git_repository *repo, git_diff_format_t format, git_diff_line_cb cb, void *payload) { @@ -63,11 +64,11 @@ return 0; } static int diff_print_info_init_fromdiff( diff_print_info *pi, - git_buf *out, + git_str *out, git_diff *diff, git_diff_format_t format, git_diff_line_cb cb, void *payload) { @@ -87,11 +88,11 @@ return diff_print_info_init__common(pi, out, repo, format, cb, payload); } static int diff_print_info_init_frompatch( diff_print_info *pi, - git_buf *out, + git_str *out, git_patch *patch, git_diff_format_t format, git_diff_line_cb cb, void *payload) { @@ -140,36 +141,36 @@ static int diff_print_one_name_only( const git_diff_delta *delta, float progress, void *data) { diff_print_info *pi = data; - git_buf *out = pi->buf; + git_str *out = pi->buf; GIT_UNUSED(progress); if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && delta->status == GIT_DELTA_UNMODIFIED) return 0; - git_buf_clear(out); - git_buf_puts(out, delta->new_file.path); - git_buf_putc(out, '\n'); - if (git_buf_oom(out)) + git_str_clear(out); + git_str_puts(out, delta->new_file.path); + git_str_putc(out, '\n'); + if (git_str_oom(out)) return -1; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; - pi->line.content = git_buf_cstr(out); - pi->line.content_len = git_buf_len(out); + pi->line.content = git_str_cstr(out); + pi->line.content_len = git_str_len(out); return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_one_name_status( const git_diff_delta *delta, float progress, void *data) { diff_print_info *pi = data; - git_buf *out = pi->buf; + git_str *out = pi->buf; char old_suffix, new_suffix, code = git_diff_status_char(delta->status); int(*strcomp)(const char *, const char *) = pi->strcomp ? pi->strcomp : git__strcmp; GIT_UNUSED(progress); @@ -178,49 +179,49 @@ return 0; old_suffix = diff_pick_suffix(delta->old_file.mode); new_suffix = diff_pick_suffix(delta->new_file.mode); - git_buf_clear(out); + git_str_clear(out); if (delta->old_file.path != delta->new_file.path && strcomp(delta->old_file.path,delta->new_file.path) != 0) - git_buf_printf(out, "%c\t%s%c %s%c\n", code, + git_str_printf(out, "%c\t%s%c %s%c\n", code, delta->old_file.path, old_suffix, delta->new_file.path, new_suffix); else if (delta->old_file.mode != delta->new_file.mode && delta->old_file.mode != 0 && delta->new_file.mode != 0) - git_buf_printf(out, "%c\t%s%c %s%c\n", code, + git_str_printf(out, "%c\t%s%c %s%c\n", code, delta->old_file.path, old_suffix, delta->new_file.path, new_suffix); else if (old_suffix != ' ') - git_buf_printf(out, "%c\t%s%c\n", code, delta->old_file.path, old_suffix); + git_str_printf(out, "%c\t%s%c\n", code, delta->old_file.path, old_suffix); else - git_buf_printf(out, "%c\t%s\n", code, delta->old_file.path); - if (git_buf_oom(out)) + git_str_printf(out, "%c\t%s\n", code, delta->old_file.path); + if (git_str_oom(out)) return -1; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; - pi->line.content = git_buf_cstr(out); - pi->line.content_len = git_buf_len(out); + pi->line.content = git_str_cstr(out); + pi->line.content_len = git_str_len(out); return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_one_raw( const git_diff_delta *delta, float progress, void *data) { diff_print_info *pi = data; - git_buf *out = pi->buf; + git_str *out = pi->buf; int id_abbrev; char code = git_diff_status_char(delta->status); char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1]; GIT_UNUSED(progress); if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') return 0; - git_buf_clear(out); + git_str_clear(out); id_abbrev = delta->old_file.mode ? delta->old_file.id_abbrev : delta->new_file.id_abbrev; if (pi->id_strlen > id_abbrev) { @@ -231,47 +232,47 @@ } git_oid_tostr(start_oid, pi->id_strlen + 1, &delta->old_file.id); git_oid_tostr(end_oid, pi->id_strlen + 1, &delta->new_file.id); - git_buf_printf( + git_str_printf( out, (pi->id_strlen <= GIT_OID_HEXSZ) ? ":%06o %06o %s... %s... %c" : ":%06o %06o %s %s %c", delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code); if (delta->similarity > 0) - git_buf_printf(out, "%03u", delta->similarity); + git_str_printf(out, "%03u", delta->similarity); if (delta->old_file.path != delta->new_file.path) - git_buf_printf( + git_str_printf( out, "\t%s %s\n", delta->old_file.path, delta->new_file.path); else - git_buf_printf( + git_str_printf( out, "\t%s\n", delta->old_file.path ? delta->old_file.path : delta->new_file.path); - if (git_buf_oom(out)) + if (git_str_oom(out)) return -1; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; - pi->line.content = git_buf_cstr(out); - pi->line.content_len = git_buf_len(out); + pi->line.content = git_str_cstr(out); + pi->line.content_len = git_str_len(out); return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_modes( - git_buf *out, const git_diff_delta *delta) + git_str *out, const git_diff_delta *delta) { - git_buf_printf(out, "old mode %o\n", delta->old_file.mode); - git_buf_printf(out, "new mode %o\n", delta->new_file.mode); + git_str_printf(out, "old mode %o\n", delta->old_file.mode); + git_str_printf(out, "new mode %o\n", delta->new_file.mode); - return git_buf_oom(out) ? -1 : 0; + return git_str_oom(out) ? -1 : 0; } static int diff_print_oid_range( - git_buf *out, const git_diff_delta *delta, int id_strlen, + git_str *out, const git_diff_delta *delta, int id_strlen, bool print_index) { char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1]; if (delta->old_file.mode && @@ -293,38 +294,38 @@ git_oid_tostr(start_oid, id_strlen + 1, &delta->old_file.id); git_oid_tostr(end_oid, id_strlen + 1, &delta->new_file.id); if (delta->old_file.mode == delta->new_file.mode) { if (print_index) - git_buf_printf(out, "index %s..%s %o\n", + git_str_printf(out, "index %s..%s %o\n", start_oid, end_oid, delta->old_file.mode); } else { if (delta->old_file.mode == 0) - git_buf_printf(out, "new file mode %o\n", delta->new_file.mode); + git_str_printf(out, "new file mode %o\n", delta->new_file.mode); else if (delta->new_file.mode == 0) - git_buf_printf(out, "deleted file mode %o\n", delta->old_file.mode); + git_str_printf(out, "deleted file mode %o\n", delta->old_file.mode); else diff_print_modes(out, delta); if (print_index) - git_buf_printf(out, "index %s..%s\n", start_oid, end_oid); + git_str_printf(out, "index %s..%s\n", start_oid, end_oid); } - return git_buf_oom(out) ? -1 : 0; + return git_str_oom(out) ? -1 : 0; } static int diff_delta_format_path( - git_buf *out, const char *prefix, const char *filename) + git_str *out, const char *prefix, const char *filename) { - if (git_buf_joinpath(out, prefix, filename) < 0) + if (git_str_joinpath(out, prefix, filename) < 0) return -1; - return git_buf_quote(out); + return git_str_quote(out); } static int diff_delta_format_with_paths( - git_buf *out, + git_str *out, const git_diff_delta *delta, const char *template, const char *oldpath, const char *newpath) { @@ -332,18 +333,18 @@ oldpath = "/dev/null"; if (git_oid_is_zero(&delta->new_file.id)) newpath = "/dev/null"; - return git_buf_printf(out, template, oldpath, newpath); + return git_str_printf(out, template, oldpath, newpath); } static int diff_delta_format_similarity_header( - git_buf *out, + git_str *out, const git_diff_delta *delta) { - git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT; + git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT; const char *type; int error = 0; if (delta->similarity > 100) { git_error_set(GIT_ERROR_PATCH, "invalid similarity %d", delta->similarity); @@ -355,30 +356,30 @@ if (delta->status == GIT_DELTA_RENAMED) type = "rename"; else type = "copy"; - if ((error = git_buf_puts(&old_path, delta->old_file.path)) < 0 || - (error = git_buf_puts(&new_path, delta->new_file.path)) < 0 || - (error = git_buf_quote(&old_path)) < 0 || - (error = git_buf_quote(&new_path)) < 0) + if ((error = git_str_puts(&old_path, delta->old_file.path)) < 0 || + (error = git_str_puts(&new_path, delta->new_file.path)) < 0 || + (error = git_str_quote(&old_path)) < 0 || + (error = git_str_quote(&new_path)) < 0) goto done; - git_buf_printf(out, + git_str_printf(out, "similarity index %d%%\n" "%s from %s\n" "%s to %s\n", delta->similarity, type, old_path.ptr, type, new_path.ptr); - if (git_buf_oom(out)) + if (git_str_oom(out)) error = -1; done: - git_buf_dispose(&old_path); - git_buf_dispose(&new_path); + git_str_dispose(&old_path); + git_str_dispose(&new_path); return error; } static bool delta_is_unchanged(const git_diff_delta *delta) @@ -396,18 +397,18 @@ return false; } int git_diff_delta__format_file_header( - git_buf *out, + git_str *out, const git_diff_delta *delta, const char *oldpfx, const char *newpfx, int id_strlen, bool print_index) { - git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT; + git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT; bool unchanged = delta_is_unchanged(delta); int error = 0; if (!oldpfx) oldpfx = DIFF_OLD_PREFIX_DEFAULT; @@ -420,13 +421,13 @@ &old_path, oldpfx, delta->old_file.path)) < 0 || (error = diff_delta_format_path( &new_path, newpfx, delta->new_file.path)) < 0) goto done; - git_buf_clear(out); + git_str_clear(out); - git_buf_printf(out, "diff --git %s %s\n", + git_str_printf(out, "diff --git %s %s\n", old_path.ptr, new_path.ptr); if (unchanged && delta->old_file.mode != delta->new_file.mode) diff_print_modes(out, delta); @@ -444,16 +445,16 @@ if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) diff_delta_format_with_paths(out, delta, "--- %s\n+++ %s\n", old_path.ptr, new_path.ptr); } - if (git_buf_oom(out)) + if (git_str_oom(out)) error = -1; done: - git_buf_dispose(&old_path); - git_buf_dispose(&new_path); + git_str_dispose(&old_path); + git_str_dispose(&new_path); return error; } static int format_binary( @@ -465,45 +466,45 @@ { const char *typename = type == GIT_DIFF_BINARY_DELTA ? "delta" : "literal"; const char *scan, *end; - git_buf_printf(pi->buf, "%s %" PRIuZ "\n", typename, inflatedlen); + git_str_printf(pi->buf, "%s %" PRIuZ "\n", typename, inflatedlen); pi->line.num_lines++; for (scan = data, end = data + datalen; scan < end; ) { size_t chunk_len = end - scan; if (chunk_len > 52) chunk_len = 52; if (chunk_len <= 26) - git_buf_putc(pi->buf, (char)chunk_len + 'A' - 1); + git_str_putc(pi->buf, (char)chunk_len + 'A' - 1); else - git_buf_putc(pi->buf, (char)chunk_len - 26 + 'a' - 1); + git_str_putc(pi->buf, (char)chunk_len - 26 + 'a' - 1); - git_buf_encode_base85(pi->buf, scan, chunk_len); - git_buf_putc(pi->buf, '\n'); + git_str_encode_base85(pi->buf, scan, chunk_len); + git_str_putc(pi->buf, '\n'); - if (git_buf_oom(pi->buf)) + if (git_str_oom(pi->buf)) return -1; scan += chunk_len; pi->line.num_lines++; } - git_buf_putc(pi->buf, '\n'); + git_str_putc(pi->buf, '\n'); - if (git_buf_oom(pi->buf)) + if (git_str_oom(pi->buf)) return -1; return 0; } static int diff_print_patch_file_binary_noshow( diff_print_info *pi, git_diff_delta *delta, const char *old_pfx, const char *new_pfx) { - git_buf old_path = GIT_BUF_INIT, new_path = GIT_BUF_INIT; + git_str old_path = GIT_STR_INIT, new_path = GIT_STR_INIT; int error; if ((error = diff_delta_format_path(&old_path, old_pfx, delta->old_file.path)) < 0 || (error = diff_delta_format_path(&new_path, new_pfx, delta->new_file.path)) < 0 || (error = diff_delta_format_with_paths(pi->buf, delta, "Binary files %s and %s differ\n", @@ -511,12 +512,12 @@ goto done; pi->line.num_lines = 1; done: - git_buf_dispose(&old_path); - git_buf_dispose(&new_path); + git_str_dispose(&old_path); + git_str_dispose(&new_path); return error; } static int diff_print_patch_file_binary( diff_print_info *pi, git_diff_delta *delta, @@ -532,20 +533,20 @@ if ((pi->flags & GIT_DIFF_SHOW_BINARY) == 0 || !binary->contains_data) return diff_print_patch_file_binary_noshow( pi, delta, old_pfx, new_pfx); pre_binary_size = pi->buf->size; - git_buf_printf(pi->buf, "GIT binary patch\n"); + git_str_printf(pi->buf, "GIT binary patch\n"); pi->line.num_lines++; if ((error = format_binary(pi, binary->new_file.type, binary->new_file.data, binary->new_file.datalen, binary->new_file.inflatedlen)) < 0 || (error = format_binary(pi, binary->old_file.type, binary->old_file.data, binary->old_file.datalen, binary->old_file.inflatedlen)) < 0) { if (error == GIT_EBUFS) { git_error_clear(); - git_buf_truncate(pi->buf, pre_binary_size); + git_str_truncate(pi->buf, pre_binary_size); return diff_print_patch_file_binary_noshow( pi, delta, old_pfx, new_pfx); } } @@ -587,12 +588,12 @@ if ((error = git_diff_delta__format_file_header(pi->buf, delta, oldpfx, newpfx, id_strlen, print_index)) < 0) return error; pi->line.origin = GIT_DIFF_LINE_FILE_HDR; - pi->line.content = git_buf_cstr(pi->buf); - pi->line.content_len = git_buf_len(pi->buf); + pi->line.content = git_str_cstr(pi->buf); + pi->line.content_len = git_str_len(pi->buf); return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_patch_binary( @@ -605,19 +606,19 @@ pi->old_prefix ? pi->old_prefix : DIFF_OLD_PREFIX_DEFAULT; const char *new_pfx = pi->new_prefix ? pi->new_prefix : DIFF_NEW_PREFIX_DEFAULT; int error; - git_buf_clear(pi->buf); + git_str_clear(pi->buf); if ((error = diff_print_patch_file_binary( pi, (git_diff_delta *)delta, old_pfx, new_pfx, binary)) < 0) return error; pi->line.origin = GIT_DIFF_LINE_BINARY; - pi->line.content = git_buf_cstr(pi->buf); - pi->line.content_len = git_buf_len(pi->buf); + pi->line.content = git_str_cstr(pi->buf); + pi->line.content_len = git_str_len(pi->buf); return pi->print_cb(delta, NULL, &pi->line, pi->payload); } static int diff_print_patch_hunk( @@ -657,11 +658,11 @@ git_diff_format_t format, git_diff_line_cb print_cb, void *payload) { int error; - git_buf buf = GIT_BUF_INIT; + git_str buf = GIT_STR_INIT; diff_print_info pi; git_diff_file_cb print_file = NULL; git_diff_binary_cb print_binary = NULL; git_diff_hunk_cb print_hunk = NULL; git_diff_line_cb print_line = NULL; @@ -702,34 +703,34 @@ git_error_set_after_callback_function(error, "git_diff_print"); goto out; } out: - git_buf_dispose(&buf); + git_str_dispose(&buf); return error; } int git_diff_print_callback__to_buf( const git_diff_delta *delta, const git_diff_hunk *hunk, const git_diff_line *line, void *payload) { - git_buf *output = payload; + git_str *output = payload; GIT_UNUSED(delta); GIT_UNUSED(hunk); if (!output) { git_error_set(GIT_ERROR_INVALID, "buffer pointer must be provided"); return -1; } if (line->origin == GIT_DIFF_LINE_ADDITION || line->origin == GIT_DIFF_LINE_DELETION || line->origin == GIT_DIFF_LINE_CONTEXT) - git_buf_putc(output, line->origin); + git_str_putc(output, line->origin); - return git_buf_put(output, line->content, line->content_len); + return git_str_put(output, line->content, line->content_len); } int git_diff_print_callback__to_file_handle( const git_diff_delta *delta, const git_diff_hunk *hunk, @@ -759,31 +760,37 @@ } return 0; } -/* print a git_diff to a git_buf */ +/* print a git_diff to a git_str */ int git_diff_to_buf(git_buf *out, git_diff *diff, git_diff_format_t format) { + git_str str = GIT_STR_INIT; int error; GIT_ASSERT_ARG(out); GIT_ASSERT_ARG(diff); - if ((error = git_buf_sanitize(out)) < 0) - return error; + if ((error = git_buf_tostr(&str, out)) < 0 || + (error = git_diff_print(diff, format, git_diff_print_callback__to_buf, &str)) < 0) + goto done; - return git_diff_print(diff, format, git_diff_print_callback__to_buf, out); + error = git_buf_fromstr(out, &str); + +done: + git_str_dispose(&str); + return error; } /* print a git_patch to an output callback */ int git_patch_print( git_patch *patch, git_diff_line_cb print_cb, void *payload) { - git_buf temp = GIT_BUF_INIT; + git_str temp = GIT_STR_INIT; diff_print_info pi; int error; GIT_ASSERT_ARG(patch); GIT_ASSERT_ARG(print_cb); @@ -797,22 +804,22 @@ git_error_set_after_callback_function(error, "git_patch_print"); goto out; } out: - git_buf_dispose(&temp); + git_str_dispose(&temp); return error; } -/* print a git_patch to a git_buf */ +/* print a git_patch to a git_str */ int git_patch_to_buf(git_buf *out, git_patch *patch) { - int error; + GIT_BUF_WRAP_PRIVATE(out, git_patch__to_buf, patch); +} +int git_patch__to_buf(git_str *out, git_patch *patch) +{ GIT_ASSERT_ARG(out); GIT_ASSERT_ARG(patch); - - if ((error = git_buf_sanitize(out)) < 0) - return error; return git_patch_print(patch, git_diff_print_callback__to_buf, out); }