mirror of
https://git.proxmox.com/git/libgit2
synced 2025-12-09 14:42:38 +00:00
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).
This commit is contained in:
parent
3943dc78a5
commit
cb7180a6e2
@ -603,11 +603,28 @@ GIT_EXTERN(int) git_diff_patch_get_line_in_hunk(
|
|||||||
size_t hunk_idx,
|
size_t hunk_idx,
|
||||||
size_t line_of_hunk);
|
size_t line_of_hunk);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize the patch to text via callback.
|
||||||
|
*
|
||||||
|
* Returning a non-zero value from the callback will terminate the iteration
|
||||||
|
* and cause this return `GIT_EUSER`.
|
||||||
|
*
|
||||||
|
* @param patch A git_diff_patch representing changes to one file
|
||||||
|
* @param cb_data Reference pointer that will be passed to your callbacks.
|
||||||
|
* @param print_cb Callback function to output lines of the patch. Will be
|
||||||
|
* called for file headers, hunk headers, and diff lines.
|
||||||
|
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
|
||||||
|
*/
|
||||||
|
GIT_EXTERN(int) git_diff_patch_print(
|
||||||
|
git_diff_patch *patch,
|
||||||
|
void *cb_data,
|
||||||
|
git_diff_data_fn print_cb);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the content of a patch as a single diff text.
|
* Get the content of a patch as a single diff text.
|
||||||
*
|
*
|
||||||
* @param string Allocated string; caller must free.
|
* @param string Allocated string; caller must free.
|
||||||
* @param patch The patch to generate a string from.
|
* @param patch A git_diff_patch representing changes to one file
|
||||||
* @return 0 on success, <0 on failure.
|
* @return 0 on success, <0 on failure.
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(int) git_diff_patch_to_str(
|
GIT_EXTERN(int) git_diff_patch_to_str(
|
||||||
|
|||||||
@ -1511,26 +1511,25 @@ static int print_to_buffer_cb(
|
|||||||
size_t content_len)
|
size_t content_len)
|
||||||
{
|
{
|
||||||
git_buf *output = cb_data;
|
git_buf *output = cb_data;
|
||||||
GIT_UNUSED(delta);
|
GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin);
|
||||||
GIT_UNUSED(range);
|
|
||||||
GIT_UNUSED(line_origin);
|
|
||||||
return git_buf_put(output, content, content_len);
|
return git_buf_put(output, content, content_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_diff_patch_to_str(
|
int git_diff_patch_print(
|
||||||
char **string,
|
git_diff_patch *patch,
|
||||||
git_diff_patch *patch)
|
void *cb_data,
|
||||||
|
git_diff_data_fn print_cb)
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
git_buf output = GIT_BUF_INIT, temp = GIT_BUF_INIT;
|
git_buf temp = GIT_BUF_INIT;
|
||||||
diff_print_info pi;
|
diff_print_info pi;
|
||||||
size_t h, l;
|
size_t h, l;
|
||||||
|
|
||||||
assert(string && patch);
|
assert(patch && print_cb);
|
||||||
|
|
||||||
pi.diff = patch->diff;
|
pi.diff = patch->diff;
|
||||||
pi.print_cb = print_to_buffer_cb;
|
pi.print_cb = print_cb;
|
||||||
pi.cb_data = &output;
|
pi.cb_data = cb_data;
|
||||||
pi.buf = &temp;
|
pi.buf = &temp;
|
||||||
|
|
||||||
error = print_patch_file(&pi, patch->delta, 0);
|
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,
|
/* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1,
|
||||||
* meaning a memory allocation failure, so just map to -1...
|
* meaning a memory allocation failure, so just map to -1...
|
||||||
*/
|
*/
|
||||||
if (error == GIT_EUSER)
|
if (error == GIT_EUSER)
|
||||||
error = -1;
|
error = -1;
|
||||||
|
|
||||||
git_buf_free(&temp);
|
|
||||||
|
|
||||||
*string = git_buf_detach(&output);
|
*string = git_buf_detach(&output);
|
||||||
|
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user