Commit c05cd792 by Nicolas Hake

Drop git_patch_to_str

It's hard or even impossible to correctly free the string buffer
allocated by git_patch_to_str in some circumstances. Drop the function
so people have to use git_patch_to_buf instead - git_buf has a dedicated
destructor.
parent 450e8e9e
...@@ -234,17 +234,6 @@ GIT_EXTERN(int) git_patch_print( ...@@ -234,17 +234,6 @@ GIT_EXTERN(int) git_patch_print(
/** /**
* 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 patch A git_patch representing changes to one file
* @return 0 on success, <0 on failure.
*/
GIT_EXTERN(int) git_patch_to_str(
char **string,
git_patch *patch);
/**
* Get the content of a patch as a single diff text.
*
* @param out The git_buf to be filled in * @param out The git_buf to be filled in
* @param patch A git_patch representing changes to one file * @param patch A git_patch representing changes to one file
* @return 0 on success, <0 on failure. * @return 0 on success, <0 on failure.
......
...@@ -459,21 +459,3 @@ int git_patch_to_buf( ...@@ -459,21 +459,3 @@ int git_patch_to_buf(
{ {
return git_patch_print(patch, diff_print_to_buffer_cb, out); return git_patch_print(patch, diff_print_to_buffer_cb, out);
} }
/* print a git_patch to a char* */
int git_patch_to_str(
char **string,
git_patch *patch)
{
int error;
git_buf output = GIT_BUF_INIT;
if (!(error = git_patch_to_buf(&output, patch)))
*string = git_buf_detach(&output);
else {
git_buf_free(&output);
*string = NULL;
}
return error;
}
...@@ -853,7 +853,7 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -853,7 +853,7 @@ void test_diff_blob__using_path_and_attributes(void)
size_t bin_len = 33; size_t bin_len = 33;
const char *changed; const char *changed;
git_patch *p; git_patch *p;
char *pout; git_buf buf = GIT_BUF_INIT;
/* set up custom diff drivers and 'diff' attribute mappings for them */ /* set up custom diff drivers and 'diff' attribute mappings for them */
...@@ -951,7 +951,7 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -951,7 +951,7 @@ void test_diff_blob__using_path_and_attributes(void)
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts)); &p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.normal b/zzz.normal\n" "diff --git a/zzz.normal b/zzz.normal\n"
"index 45141a7..75b0dbb 100644\n" "index 45141a7..75b0dbb 100644\n"
...@@ -960,23 +960,23 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -960,23 +960,23 @@ void test_diff_blob__using_path_and_attributes(void)
"@@ -1,0 +2,3 @@ Hello from the root\n" "@@ -1,0 +2,3 @@ Hello from the root\n"
"+More lines\n" "+More lines\n"
"+And more\n" "+And more\n"
"+Go here\n", pout); "+Go here\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.binary", changed, strlen(changed), NULL, &opts)); &p, nonbin, "zzz.binary", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.binary b/zzz.binary\n" "diff --git a/zzz.binary b/zzz.binary\n"
"index 45141a7..75b0dbb 100644\n" "index 45141a7..75b0dbb 100644\n"
"Binary files a/zzz.binary and b/zzz.binary differ\n", pout); "Binary files a/zzz.binary and b/zzz.binary differ\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.alphary", changed, strlen(changed), NULL, &opts)); &p, nonbin, "zzz.alphary", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.alphary b/zzz.alphary\n" "diff --git a/zzz.alphary b/zzz.alphary\n"
"index 45141a7..75b0dbb 100644\n" "index 45141a7..75b0dbb 100644\n"
...@@ -985,13 +985,13 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -985,13 +985,13 @@ void test_diff_blob__using_path_and_attributes(void)
"@@ -1,0 +2,3 @@ Hello from the root\n" "@@ -1,0 +2,3 @@ Hello from the root\n"
"+More lines\n" "+More lines\n"
"+And more\n" "+And more\n"
"+Go here\n", pout); "+Go here\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.numary", changed, strlen(changed), NULL, &opts)); &p, nonbin, "zzz.numary", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.numary b/zzz.numary\n" "diff --git a/zzz.numary b/zzz.numary\n"
"index 45141a7..75b0dbb 100644\n" "index 45141a7..75b0dbb 100644\n"
...@@ -1000,8 +1000,8 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -1000,8 +1000,8 @@ void test_diff_blob__using_path_and_attributes(void)
"@@ -1,0 +2,3 @@\n" "@@ -1,0 +2,3 @@\n"
"+More lines\n" "+More lines\n"
"+And more\n" "+And more\n"
"+Go here\n", pout); "+Go here\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
/* "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n" /* "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n"
...@@ -1012,17 +1012,17 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -1012,17 +1012,17 @@ void test_diff_blob__using_path_and_attributes(void)
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.normal", changed, 37, NULL, &opts)); &p, bin, "zzz.normal", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.normal b/zzz.normal\n" "diff --git a/zzz.normal b/zzz.normal\n"
"index b435cd5..1604519 100644\n" "index b435cd5..1604519 100644\n"
"Binary files a/zzz.normal and b/zzz.normal differ\n", pout); "Binary files a/zzz.normal and b/zzz.normal differ\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.textary", changed, 37, NULL, &opts)); &p, bin, "zzz.textary", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.textary b/zzz.textary\n" "diff --git a/zzz.textary b/zzz.textary\n"
"index b435cd5..1604519 100644\n" "index b435cd5..1604519 100644\n"
...@@ -1030,13 +1030,13 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -1030,13 +1030,13 @@ void test_diff_blob__using_path_and_attributes(void)
"+++ b/zzz.textary\n" "+++ b/zzz.textary\n"
"@@ -3 +3 @@\n" "@@ -3 +3 @@\n"
"-0123456789\n" "-0123456789\n"
"+replace a line\n", pout); "+replace a line\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.textalphary", changed, 37, NULL, &opts)); &p, bin, "zzz.textalphary", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.textalphary b/zzz.textalphary\n" "diff --git a/zzz.textalphary b/zzz.textalphary\n"
"index b435cd5..1604519 100644\n" "index b435cd5..1604519 100644\n"
...@@ -1044,13 +1044,13 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -1044,13 +1044,13 @@ void test_diff_blob__using_path_and_attributes(void)
"+++ b/zzz.textalphary\n" "+++ b/zzz.textalphary\n"
"@@ -3 +3 @@\n" "@@ -3 +3 @@\n"
"-0123456789\n" "-0123456789\n"
"+replace a line\n", pout); "+replace a line\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer( cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.textnumary", changed, 37, NULL, &opts)); &p, bin, "zzz.textnumary", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_str(&pout, p)); cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s( cl_assert_equal_s(
"diff --git a/zzz.textnumary b/zzz.textnumary\n" "diff --git a/zzz.textnumary b/zzz.textnumary\n"
"index b435cd5..1604519 100644\n" "index b435cd5..1604519 100644\n"
...@@ -1058,10 +1058,11 @@ void test_diff_blob__using_path_and_attributes(void) ...@@ -1058,10 +1058,11 @@ void test_diff_blob__using_path_and_attributes(void)
"+++ b/zzz.textnumary\n" "+++ b/zzz.textnumary\n"
"@@ -3 +3 @@ 0123456789\n" "@@ -3 +3 @@ 0123456789\n"
"-0123456789\n" "-0123456789\n"
"+replace a line\n", pout); "+replace a line\n", buf.ptr);
git__free(pout); git_buf_clear(&buf);
git_patch_free(p); git_patch_free(p);
git_buf_free(&buf);
git_blob_free(nonbin); git_blob_free(nonbin);
git_blob_free(bin); git_blob_free(bin);
} }
...@@ -414,16 +414,16 @@ void test_diff_diffiter__iterate_and_generate_patch_text(void) ...@@ -414,16 +414,16 @@ void test_diff_diffiter__iterate_and_generate_patch_text(void)
for (d = 0; d < num_d; ++d) { for (d = 0; d < num_d; ++d) {
git_patch *patch; git_patch *patch;
char *text; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_patch_from_diff(&patch, diff, d)); cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert(patch != NULL); cl_assert(patch != NULL);
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected_patch_text[d], text); cl_assert_equal_s(expected_patch_text[d], buf.ptr);
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
} }
......
...@@ -22,7 +22,7 @@ void test_diff_drivers__patterns(void) ...@@ -22,7 +22,7 @@ void test_diff_drivers__patterns(void)
git_tree *one; git_tree *one;
git_diff *diff; git_diff *diff;
git_patch *patch; git_patch *patch;
char *text; git_buf buf = GIT_BUF_INIT;
const char *expected0 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Comes through the blood of the vanguards who\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n"; const char *expected0 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Comes through the blood of the vanguards who\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n";
const char *expected1 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\nBinary files a/untimely.txt and b/untimely.txt differ\n"; const char *expected1 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\nBinary files a/untimely.txt and b/untimely.txt differ\n";
const char *expected2 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Heaven delivers on earth the Hour that cannot be\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n"; const char *expected2 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Heaven delivers on earth the Hour that cannot be\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n";
...@@ -45,10 +45,10 @@ void test_diff_drivers__patterns(void) ...@@ -45,10 +45,10 @@ void test_diff_drivers__patterns(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected0, text); cl_assert_equal_s(expected0, buf.ptr);
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -60,10 +60,10 @@ void test_diff_drivers__patterns(void) ...@@ -60,10 +60,10 @@ void test_diff_drivers__patterns(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected1, text); cl_assert_equal_s(expected1, buf.ptr);
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -75,10 +75,10 @@ void test_diff_drivers__patterns(void) ...@@ -75,10 +75,10 @@ void test_diff_drivers__patterns(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected0, text); cl_assert_equal_s(expected0, buf.ptr);
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -92,10 +92,10 @@ void test_diff_drivers__patterns(void) ...@@ -92,10 +92,10 @@ void test_diff_drivers__patterns(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected1, text); cl_assert_equal_s(expected1, buf.ptr);
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -113,10 +113,10 @@ void test_diff_drivers__patterns(void) ...@@ -113,10 +113,10 @@ void test_diff_drivers__patterns(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected2, text); cl_assert_equal_s(expected2, buf.ptr);
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -129,7 +129,7 @@ void test_diff_drivers__long_lines(void) ...@@ -129,7 +129,7 @@ void test_diff_drivers__long_lines(void)
git_index *idx; git_index *idx;
git_diff *diff; git_diff *diff;
git_patch *patch; git_patch *patch;
char *actual; git_buf buf = GIT_BUF_INIT;
const char *expected = "diff --git a/longlines.txt b/longlines.txt\nindex c1ce6ef..0134431 100644\n--- a/longlines.txt\n+++ b/longlines.txt\n@@ -3,3 +3,5 @@ Phasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissi\n Nam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\n Mauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\n Aliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n+newline\n+newline\n"; const char *expected = "diff --git a/longlines.txt b/longlines.txt\nindex c1ce6ef..0134431 100644\n--- a/longlines.txt\n+++ b/longlines.txt\n@@ -3,3 +3,5 @@ Phasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissi\n Nam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\n Mauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\n Aliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n+newline\n+newline\n";
g_repo = cl_git_sandbox_init("empty_standard_repo"); g_repo = cl_git_sandbox_init("empty_standard_repo");
...@@ -145,18 +145,17 @@ void test_diff_drivers__long_lines(void) ...@@ -145,18 +145,17 @@ void test_diff_drivers__long_lines(void)
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
cl_assert_equal_sz(1, git_diff_num_deltas(diff)); cl_assert_equal_sz(1, git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&actual, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
/* if chmod not supported, overwrite mode bits since anything is possible */ /* if chmod not supported, overwrite mode bits since anything is possible */
if (!cl_is_chmod_supported()) { if (!cl_is_chmod_supported()) {
size_t actual_len = strlen(actual); if (buf.size > 72 && memcmp(&buf.ptr[66], "100644", 6) != 0)
if (actual_len > 72 && memcmp(&actual[66], "100644", 6) != 0) memcpy(&buf.ptr[66], "100644", 6);
memcpy(&actual[66], "100644", 6);
} }
cl_assert_equal_s(expected, actual); cl_assert_equal_s(expected, buf.ptr);
free(actual); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
} }
......
...@@ -141,7 +141,7 @@ void test_diff_patch__to_string(void) ...@@ -141,7 +141,7 @@ void test_diff_patch__to_string(void)
git_tree *one, *another; git_tree *one, *another;
git_diff *diff; git_diff *diff;
git_patch *patch; git_patch *patch;
char *text; git_buf buf = GIT_BUF_INIT;
const char *expected = "diff --git a/subdir.txt b/subdir.txt\ndeleted file mode 100644\nindex e8ee89e..0000000\n--- a/subdir.txt\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-Is it a bird?\n-Is it a plane?\n"; const char *expected = "diff --git a/subdir.txt b/subdir.txt\ndeleted file mode 100644\nindex e8ee89e..0000000\n--- a/subdir.txt\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-Is it a bird?\n-Is it a plane?\n";
g_repo = cl_git_sandbox_init("status"); g_repo = cl_git_sandbox_init("status");
...@@ -155,16 +155,16 @@ void test_diff_patch__to_string(void) ...@@ -155,16 +155,16 @@ void test_diff_patch__to_string(void)
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected, text); cl_assert_equal_s(expected, buf.ptr);
cl_assert_equal_sz(31, git_patch_size(patch, 0, 0, 0)); cl_assert_equal_sz(31, git_patch_size(patch, 0, 0, 0));
cl_assert_equal_sz(31, git_patch_size(patch, 1, 0, 0)); cl_assert_equal_sz(31, git_patch_size(patch, 1, 0, 0));
cl_assert_equal_sz(31 + 16, git_patch_size(patch, 1, 1, 0)); cl_assert_equal_sz(31 + 16, git_patch_size(patch, 1, 1, 0));
cl_assert_equal_sz(strlen(expected), git_patch_size(patch, 1, 1, 1)); cl_assert_equal_sz(strlen(expected), git_patch_size(patch, 1, 1, 1));
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
git_tree_free(another); git_tree_free(another);
...@@ -178,7 +178,7 @@ void test_diff_patch__config_options(void) ...@@ -178,7 +178,7 @@ void test_diff_patch__config_options(void)
git_config *cfg; git_config *cfg;
git_diff *diff; git_diff *diff;
git_patch *patch; git_patch *patch;
char *text; git_buf buf = GIT_BUF_INIT;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
char *onefile = "staged_changes_modified_file"; char *onefile = "staged_changes_modified_file";
const char *expected1 = "diff --git c/staged_changes_modified_file i/staged_changes_modified_file\nindex 70bd944..906ee77 100644\n--- c/staged_changes_modified_file\n+++ i/staged_changes_modified_file\n@@ -1 +1,2 @@\n staged_changes_modified_file\n+staged_changes_modified_file\n"; const char *expected1 = "diff --git c/staged_changes_modified_file i/staged_changes_modified_file\nindex 70bd944..906ee77 100644\n--- c/staged_changes_modified_file\n+++ i/staged_changes_modified_file\n@@ -1 +1,2 @@\n staged_changes_modified_file\n+staged_changes_modified_file\n";
...@@ -199,10 +199,10 @@ void test_diff_patch__config_options(void) ...@@ -199,10 +199,10 @@ void test_diff_patch__config_options(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected1, text); cl_assert_equal_s(expected1, buf.ptr);
git__free(text); git_buf_clear(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -210,10 +210,10 @@ void test_diff_patch__config_options(void) ...@@ -210,10 +210,10 @@ void test_diff_patch__config_options(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected2, text); cl_assert_equal_s(expected2, buf.ptr);
git__free(text); git_buf_clear(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -224,10 +224,10 @@ void test_diff_patch__config_options(void) ...@@ -224,10 +224,10 @@ void test_diff_patch__config_options(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected3, text); cl_assert_equal_s(expected3, buf.ptr);
git__free(text); git_buf_clear(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -238,13 +238,14 @@ void test_diff_patch__config_options(void) ...@@ -238,13 +238,14 @@ void test_diff_patch__config_options(void)
cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected4, text); cl_assert_equal_s(expected4, buf.ptr);
git__free(text); git_buf_clear(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
git_buf_free(&buf);
git_tree_free(one); git_tree_free(one);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -465,10 +466,10 @@ static void check_single_patch_stats( ...@@ -465,10 +466,10 @@ static void check_single_patch_stats(
cl_assert_equal_sz(dels, actual_dels); cl_assert_equal_sz(dels, actual_dels);
if (expected != NULL) { if (expected != NULL) {
char *text; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected, text); cl_assert_equal_s(expected, buf.ptr);
git__free(text); git_buf_free(&buf);
cl_assert_equal_sz( cl_assert_equal_sz(
strlen(expected), git_patch_size(patch, 1, 1, 1)); strlen(expected), git_patch_size(patch, 1, 1, 1));
......
...@@ -584,7 +584,7 @@ void test_diff_rename__patch(void) ...@@ -584,7 +584,7 @@ void test_diff_rename__patch(void)
git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
git_patch *patch; git_patch *patch;
const git_diff_delta *delta; const git_diff_delta *delta;
char *text; git_buf buf = GIT_BUF_INIT;
const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n"; const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n";
old_tree = resolve_commit_oid_to_tree(g_repo, sha0); old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
...@@ -610,9 +610,9 @@ void test_diff_rename__patch(void) ...@@ -610,9 +610,9 @@ void test_diff_rename__patch(void)
cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert((delta = git_patch_get_delta(patch)) != NULL);
cl_assert_equal_i(GIT_DELTA_COPIED, (int)delta->status); cl_assert_equal_i(GIT_DELTA_COPIED, (int)delta->status);
cl_git_pass(git_patch_to_str(&text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected, text); cl_assert_equal_s(expected, buf.ptr);
git__free(text); git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
......
...@@ -13,13 +13,15 @@ void test_diff_submodules__cleanup(void) ...@@ -13,13 +13,15 @@ void test_diff_submodules__cleanup(void)
{ {
} }
#define get_buf_ptr(buf) ((buf)->asize ? (buf)->ptr : NULL)
static void check_diff_patches_at_line( static void check_diff_patches_at_line(
git_diff *diff, const char **expected, const char *file, int line) git_diff *diff, const char **expected, const char *file, int line)
{ {
const git_diff_delta *delta; const git_diff_delta *delta;
git_patch *patch = NULL; git_patch *patch = NULL;
size_t d, num_d = git_diff_num_deltas(diff); size_t d, num_d = git_diff_num_deltas(diff);
char *patch_text; git_buf buf = GIT_BUF_INIT;
for (d = 0; d < num_d; ++d, git_patch_free(patch)) { for (d = 0; d < num_d; ++d, git_patch_free(patch)) {
cl_git_pass(git_patch_from_diff(&patch, diff, d)); cl_git_pass(git_patch_from_diff(&patch, diff, d));
...@@ -33,16 +35,16 @@ static void check_diff_patches_at_line( ...@@ -33,16 +35,16 @@ static void check_diff_patches_at_line(
if (expected[d] && !strcmp(expected[d], "<SKIP>")) if (expected[d] && !strcmp(expected[d], "<SKIP>"))
continue; continue;
if (expected[d] && !strcmp(expected[d], "<END>")) { if (expected[d] && !strcmp(expected[d], "<END>")) {
cl_git_pass(git_patch_to_str(&patch_text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_at_line(!strcmp(expected[d], "<END>"), file, line); cl_assert_at_line(!strcmp(expected[d], "<END>"), file, line);
} }
cl_git_pass(git_patch_to_str(&patch_text, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
clar__assert_equal( clar__assert_equal(
file, line, "expected diff did not match actual diff", 1, file, line, "expected diff did not match actual diff", 1,
"%s", expected[d], patch_text); "%s", expected[d], get_buf_ptr(&buf));
git__free(patch_text); git_buf_free(&buf);
} }
cl_assert_at_line(expected[d] && !strcmp(expected[d], "<END>"), file, line); cl_assert_at_line(expected[d] && !strcmp(expected[d], "<END>"), file, line);
......
...@@ -1375,7 +1375,7 @@ void test_diff_workdir__patience_diff(void) ...@@ -1375,7 +1375,7 @@ void test_diff_workdir__patience_diff(void)
git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL; git_diff *diff = NULL;
git_patch *patch = NULL; git_patch *patch = NULL;
char *as_str = NULL; git_buf buf = GIT_BUF_INIT;
const char *expected_normal = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n-how to create\n-a patience diff\n I did not know\n how to create\n+a patience diff\n another problem\n-I did not know\n-how to create\n a minimal diff\n"; const char *expected_normal = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n-how to create\n-a patience diff\n I did not know\n how to create\n+a patience diff\n another problem\n-I did not know\n-how to create\n a minimal diff\n";
const char *expected_patience = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n+I did not know\n how to create\n a patience diff\n-I did not know\n-how to create\n another problem\n-I did not know\n-how to create\n a minimal diff\n"; const char *expected_patience = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n+I did not know\n how to create\n a patience diff\n-I did not know\n-how to create\n another problem\n-I did not know\n-how to create\n a minimal diff\n";
...@@ -1397,10 +1397,10 @@ void test_diff_workdir__patience_diff(void) ...@@ -1397,10 +1397,10 @@ void test_diff_workdir__patience_diff(void)
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_assert_equal_i(1, git_diff_num_deltas(diff)); cl_assert_equal_i(1, git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&as_str, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected_normal, as_str); cl_assert_equal_s(expected_normal, buf.ptr);
git__free(as_str); git_buf_clear(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
...@@ -1409,10 +1409,12 @@ void test_diff_workdir__patience_diff(void) ...@@ -1409,10 +1409,12 @@ void test_diff_workdir__patience_diff(void)
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_assert_equal_i(1, git_diff_num_deltas(diff)); cl_assert_equal_i(1, git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_str(&as_str, patch)); cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected_patience, as_str); cl_assert_equal_s(expected_patience, buf.ptr);
git__free(as_str); git_buf_clear(&buf);
git_buf_free(&buf);
git_patch_free(patch); git_patch_free(patch);
git_diff_free(diff); git_diff_free(diff);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment