summaryrefslogtreecommitdiff
path: root/src/diff_output.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-10-25 11:48:39 -0700
committerRussell Belfer <rb@github.com>2012-10-25 11:48:39 -0700
commitcb7180a6e2bb7e5912c16d2109f273c75731a607 (patch)
tree24ab45a03ba348cce9ba237856b462d6e1b09dec /src/diff_output.c
parent3943dc78a5c67c1db367c4e13a79884ef72f7f3b (diff)
downloadlibgit2-cb7180a6e2bb7e5912c16d2109f273c75731a607.tar.gz
Add git_diff_patch_print
This adds a `git_diff_patch_print()` API which is more like the existing API to "print" a patch from an entire `git_diff_list` but operates on a single `git_diff_patch` object. Also, it rewrites the `git_diff_patch_to_str()` API to use that function (making it very small).
Diffstat (limited to 'src/diff_output.c')
-rw-r--r--src/diff_output.c36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/diff_output.c b/src/diff_output.c
index 495de251b..e678ec857 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -1511,26 +1511,25 @@ static int print_to_buffer_cb(
size_t content_len)
{
git_buf *output = cb_data;
- GIT_UNUSED(delta);
- GIT_UNUSED(range);
- GIT_UNUSED(line_origin);
+ GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin);
return git_buf_put(output, content, content_len);
}
-int git_diff_patch_to_str(
- char **string,
- git_diff_patch *patch)
+int git_diff_patch_print(
+ git_diff_patch *patch,
+ void *cb_data,
+ git_diff_data_fn print_cb)
{
int error;
- git_buf output = GIT_BUF_INIT, temp = GIT_BUF_INIT;
+ git_buf temp = GIT_BUF_INIT;
diff_print_info pi;
size_t h, l;
- assert(string && patch);
+ assert(patch && print_cb);
pi.diff = patch->diff;
- pi.print_cb = print_to_buffer_cb;
- pi.cb_data = &output;
+ pi.print_cb = print_cb;
+ pi.cb_data = cb_data;
pi.buf = &temp;
error = print_patch_file(&pi, patch->delta, 0);
@@ -1550,16 +1549,27 @@ int git_diff_patch_to_str(
}
}
+ git_buf_free(&temp);
+
+ return error;
+}
+
+int git_diff_patch_to_str(
+ char **string,
+ git_diff_patch *patch)
+{
+ int error;
+ git_buf output = GIT_BUF_INIT;
+
+ error = git_diff_patch_print(patch, &output, print_to_buffer_cb);
+
/* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1,
* meaning a memory allocation failure, so just map to -1...
*/
if (error == GIT_EUSER)
error = -1;
- git_buf_free(&temp);
-
*string = git_buf_detach(&output);
return error;
}
-