summaryrefslogtreecommitdiff
path: root/src/diff_output.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-10-24 20:56:32 -0700
committerRussell Belfer <rb@github.com>2012-10-24 20:56:32 -0700
commit93cf7bb8e26a04d9bd4197c1b938cee352023f63 (patch)
tree34fc8c5fbf63e3962573d768c252197bcdf177e3 /src/diff_output.c
parent6f6b0c013c6eff2aca2a7ada1027044f2e20f578 (diff)
downloadlibgit2-93cf7bb8e26a04d9bd4197c1b938cee352023f63.tar.gz
Add git_diff_patch_to_str API
This adds an API to generate a complete single-file patch text from a git_diff_patch object.
Diffstat (limited to 'src/diff_output.c')
-rw-r--r--src/diff_output.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/diff_output.c b/src/diff_output.c
index 5f0d13c64..511e9318b 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -1502,3 +1502,57 @@ notfound:
return GIT_ENOTFOUND;
}
+static int print_to_buffer_cb(
+ void *cb_data,
+ const git_diff_delta *delta,
+ const git_diff_range *range,
+ char line_origin,
+ const char *content,
+ size_t content_len)
+{
+ git_buf *output = cb_data;
+ GIT_UNUSED(delta);
+ GIT_UNUSED(range);
+ GIT_UNUSED(line_origin);
+ git_buf_put(output, content, content_len);
+ return 0;
+}
+
+int git_diff_patch_to_str(
+ char **string,
+ git_diff_patch *patch)
+{
+ int error;
+ git_buf output = GIT_BUF_INIT, temp = GIT_BUF_INIT;
+ diff_print_info pi;
+ size_t h, l;
+
+ pi.diff = patch->diff;
+ pi.print_cb = print_to_buffer_cb;
+ pi.cb_data = &output;
+ pi.buf = &temp;
+
+ error = print_patch_file(&pi, patch->delta, 0);
+
+ for (h = 0; h < patch->hunks_size; ++h) {
+ diff_patch_hunk *hunk = &patch->hunks[h];
+
+ error = print_patch_hunk(&pi, patch->delta,
+ &hunk->range, hunk->header, hunk->header_len);
+
+ for (l = 0; l < hunk->line_count; ++l) {
+ diff_patch_line *line = &patch->lines[hunk->line_start + l];
+
+ error = print_patch_line(
+ &pi, patch->delta, &hunk->range,
+ line->origin, line->ptr, line->len);
+ }
+ }
+
+ git_buf_free(&temp);
+
+ *string = git_buf_detach(&output);
+
+ return error;
+}
+