diff options
author | Nicolas Hake <nh@nosebud.de> | 2014-01-22 17:51:32 +0100 |
---|---|---|
committer | Nicolas Hake <nh@nosebud.de> | 2014-01-22 17:51:32 +0100 |
commit | c05cd7924d2409741bb4cb1eb3ba843bea7ec4a2 (patch) | |
tree | 548900c29909e116ec5c3c1fe41370bef3460ac5 /tests/diff/drivers.c | |
parent | 450e8e9e623b8c172ba4628c146838cbf4c56519 (diff) | |
download | libgit2-c05cd7924d2409741bb4cb1eb3ba843bea7ec4a2.tar.gz |
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.
Diffstat (limited to 'tests/diff/drivers.c')
-rw-r--r-- | tests/diff/drivers.c | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/tests/diff/drivers.c b/tests/diff/drivers.c index fbd1dff81..c80fad4c2 100644 --- a/tests/diff/drivers.c +++ b/tests/diff/drivers.c @@ -22,7 +22,7 @@ void test_diff_drivers__patterns(void) git_tree *one; git_diff *diff; 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 *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"; @@ -45,10 +45,10 @@ void test_diff_drivers__patterns(void) 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_to_str(&text, patch)); - cl_assert_equal_s(expected0, text); + cl_git_pass(git_patch_to_buf(&buf, patch)); + cl_assert_equal_s(expected0, buf.ptr); - git__free(text); + git_buf_free(&buf); git_patch_free(patch); git_diff_free(diff); @@ -60,10 +60,10 @@ void test_diff_drivers__patterns(void) 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_to_str(&text, patch)); - cl_assert_equal_s(expected1, text); + cl_git_pass(git_patch_to_buf(&buf, patch)); + cl_assert_equal_s(expected1, buf.ptr); - git__free(text); + git_buf_free(&buf); git_patch_free(patch); git_diff_free(diff); @@ -75,10 +75,10 @@ void test_diff_drivers__patterns(void) 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_to_str(&text, patch)); - cl_assert_equal_s(expected0, text); + cl_git_pass(git_patch_to_buf(&buf, patch)); + cl_assert_equal_s(expected0, buf.ptr); - git__free(text); + git_buf_free(&buf); git_patch_free(patch); git_diff_free(diff); @@ -92,10 +92,10 @@ void test_diff_drivers__patterns(void) 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_to_str(&text, patch)); - cl_assert_equal_s(expected1, text); + cl_git_pass(git_patch_to_buf(&buf, patch)); + cl_assert_equal_s(expected1, buf.ptr); - git__free(text); + git_buf_free(&buf); git_patch_free(patch); git_diff_free(diff); @@ -113,10 +113,10 @@ void test_diff_drivers__patterns(void) 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_to_str(&text, patch)); - cl_assert_equal_s(expected2, text); + cl_git_pass(git_patch_to_buf(&buf, patch)); + cl_assert_equal_s(expected2, buf.ptr); - git__free(text); + git_buf_free(&buf); git_patch_free(patch); git_diff_free(diff); @@ -129,7 +129,7 @@ void test_diff_drivers__long_lines(void) git_index *idx; git_diff *diff; 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"; g_repo = cl_git_sandbox_init("empty_standard_repo"); @@ -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_assert_equal_sz(1, git_diff_num_deltas(diff)); 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 (!cl_is_chmod_supported()) { - size_t actual_len = strlen(actual); - if (actual_len > 72 && memcmp(&actual[66], "100644", 6) != 0) - memcpy(&actual[66], "100644", 6); + if (buf.size > 72 && memcmp(&buf.ptr[66], "100644", 6) != 0) + memcpy(&buf.ptr[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_diff_free(diff); } |