Commit c05cd7924d2409741bb4cb1eb3ba843bea7ec4a2

Nicolas Hake 2014-01-22T17:51:32

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
diff --git a/include/git2/patch.h b/include/git2/patch.h
index e5dd5d8..1eca29d 100644
--- a/include/git2/patch.h
+++ b/include/git2/patch.h
@@ -234,17 +234,6 @@ GIT_EXTERN(int) git_patch_print(
 /**
  * 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 patch A git_patch representing changes to one file
  * @return 0 on success, <0 on failure.
diff --git a/src/diff_print.c b/src/diff_print.c
index 0ab21d0..dd31d1f 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -459,21 +459,3 @@ int git_patch_to_buf(
 {
 	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;
-}
diff --git a/tests/diff/blob.c b/tests/diff/blob.c
index 93f2071..63e3c5b 100644
--- a/tests/diff/blob.c
+++ b/tests/diff/blob.c
@@ -853,7 +853,7 @@ void test_diff_blob__using_path_and_attributes(void)
 	size_t bin_len = 33;
 	const char *changed;
 	git_patch *p;
-	char *pout;
+	git_buf buf = GIT_BUF_INIT;
 
 	/* set up custom diff drivers and 'diff' attribute mappings for them */
 
@@ -951,7 +951,7 @@ void test_diff_blob__using_path_and_attributes(void)
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.normal b/zzz.normal\n"
 		"index 45141a7..75b0dbb 100644\n"
@@ -960,23 +960,23 @@ void test_diff_blob__using_path_and_attributes(void)
 		"@@ -1,0 +2,3 @@ Hello from the root\n"
 		"+More lines\n"
 		"+And more\n"
-		"+Go here\n", pout);
-	git__free(pout);
+		"+Go here\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.binary b/zzz.binary\n"
 		"index 45141a7..75b0dbb 100644\n"
-		"Binary files a/zzz.binary and b/zzz.binary differ\n", pout);
-	git__free(pout);
+		"Binary files a/zzz.binary and b/zzz.binary differ\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.alphary b/zzz.alphary\n"
 		"index 45141a7..75b0dbb 100644\n"
@@ -985,13 +985,13 @@ void test_diff_blob__using_path_and_attributes(void)
 		"@@ -1,0 +2,3 @@ Hello from the root\n"
 		"+More lines\n"
 		"+And more\n"
-		"+Go here\n", pout);
-	git__free(pout);
+		"+Go here\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.numary b/zzz.numary\n"
 		"index 45141a7..75b0dbb 100644\n"
@@ -1000,8 +1000,8 @@ void test_diff_blob__using_path_and_attributes(void)
 		"@@ -1,0 +2,3 @@\n"
 		"+More lines\n"
 		"+And more\n"
-		"+Go here\n", pout);
-	git__free(pout);
+		"+Go here\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
 	/* "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)
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.normal b/zzz.normal\n"
 		"index b435cd5..1604519 100644\n"
-		"Binary files a/zzz.normal and b/zzz.normal differ\n", pout);
-	git__free(pout);
+		"Binary files a/zzz.normal and b/zzz.normal differ\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.textary b/zzz.textary\n"
 		"index b435cd5..1604519 100644\n"
@@ -1030,13 +1030,13 @@ void test_diff_blob__using_path_and_attributes(void)
 		"+++ b/zzz.textary\n"
 		"@@ -3 +3 @@\n"
 		"-0123456789\n"
-		"+replace a line\n", pout);
-	git__free(pout);
+		"+replace a line\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.textalphary b/zzz.textalphary\n"
 		"index b435cd5..1604519 100644\n"
@@ -1044,13 +1044,13 @@ void test_diff_blob__using_path_and_attributes(void)
 		"+++ b/zzz.textalphary\n"
 		"@@ -3 +3 @@\n"
 		"-0123456789\n"
-		"+replace a line\n", pout);
-	git__free(pout);
+		"+replace a line\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
 	cl_git_pass(git_patch_from_blob_and_buffer(
 		&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(
 		"diff --git a/zzz.textnumary b/zzz.textnumary\n"
 		"index b435cd5..1604519 100644\n"
@@ -1058,10 +1058,11 @@ void test_diff_blob__using_path_and_attributes(void)
 		"+++ b/zzz.textnumary\n"
 		"@@ -3 +3 @@ 0123456789\n"
 		"-0123456789\n"
-		"+replace a line\n", pout);
-	git__free(pout);
+		"+replace a line\n", buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(p);
 
+	git_buf_free(&buf);
 	git_blob_free(nonbin);
 	git_blob_free(bin);
 }
diff --git a/tests/diff/diffiter.c b/tests/diff/diffiter.c
index f886e1b..c976e30 100644
--- a/tests/diff/diffiter.c
+++ b/tests/diff/diffiter.c
@@ -414,16 +414,16 @@ void test_diff_diffiter__iterate_and_generate_patch_text(void)
 
 	for (d = 0; d < num_d; ++d) {
 		git_patch *patch;
-		char *text;
+		git_buf buf = GIT_BUF_INIT;
 
 		cl_git_pass(git_patch_from_diff(&patch, diff, d));
 		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);
 	}
 
diff --git a/tests/diff/drivers.c b/tests/diff/drivers.c
index fbd1dff..c80fad4 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);
 }
diff --git a/tests/diff/patch.c b/tests/diff/patch.c
index 0cef3bd..76d7fcd 100644
--- a/tests/diff/patch.c
+++ b/tests/diff/patch.c
@@ -141,7 +141,7 @@ void test_diff_patch__to_string(void)
 	git_tree *one, *another;
 	git_diff *diff;
 	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";
 
 	g_repo = cl_git_sandbox_init("status");
@@ -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_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, 1, 0, 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));
 
-	git__free(text);
+	git_buf_free(&buf);
 	git_patch_free(patch);
 	git_diff_free(diff);
 	git_tree_free(another);
@@ -178,7 +178,7 @@ void test_diff_patch__config_options(void)
 	git_config *cfg;
 	git_diff *diff;
 	git_patch *patch;
-	char *text;
+	git_buf buf = GIT_BUF_INIT;
 	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
 	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";
@@ -199,10 +199,10 @@ void test_diff_patch__config_options(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_clear(&buf);
 	git_patch_free(patch);
 	git_diff_free(diff);
 
@@ -210,10 +210,10 @@ void test_diff_patch__config_options(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_clear(&buf);
 	git_patch_free(patch);
 	git_diff_free(diff);
 
@@ -224,10 +224,10 @@ void test_diff_patch__config_options(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(expected3, text);
+	cl_git_pass(git_patch_to_buf(&buf, patch));
+	cl_assert_equal_s(expected3, buf.ptr);
 
-	git__free(text);
+	git_buf_clear(&buf);
 	git_patch_free(patch);
 	git_diff_free(diff);
 
@@ -238,13 +238,14 @@ void test_diff_patch__config_options(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(expected4, text);
+	cl_git_pass(git_patch_to_buf(&buf, patch));
+	cl_assert_equal_s(expected4, buf.ptr);
 
-	git__free(text);
+	git_buf_clear(&buf);
 	git_patch_free(patch);
 	git_diff_free(diff);
 
+	git_buf_free(&buf);
 	git_tree_free(one);
 	git_config_free(cfg);
 }
@@ -465,10 +466,10 @@ static void check_single_patch_stats(
 	cl_assert_equal_sz(dels, actual_dels);
 
 	if (expected != NULL) {
-		char *text;
-		cl_git_pass(git_patch_to_str(&text, patch));
-		cl_assert_equal_s(expected, text);
-		git__free(text);
+		git_buf buf = GIT_BUF_INIT;
+		cl_git_pass(git_patch_to_buf(&buf, patch));
+		cl_assert_equal_s(expected, buf.ptr);
+		git_buf_free(&buf);
 
 		cl_assert_equal_sz(
 			strlen(expected), git_patch_size(patch, 1, 1, 1));
diff --git a/tests/diff/rename.c b/tests/diff/rename.c
index 4d1f764..ca08d15 100644
--- a/tests/diff/rename.c
+++ b/tests/diff/rename.c
@@ -584,7 +584,7 @@ void test_diff_rename__patch(void)
 	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
 	git_patch *patch;
 	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";
 
 	old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
@@ -610,9 +610,9 @@ void test_diff_rename__patch(void)
 	cl_assert((delta = git_patch_get_delta(patch)) != NULL);
 	cl_assert_equal_i(GIT_DELTA_COPIED, (int)delta->status);
 
-	cl_git_pass(git_patch_to_str(&text, patch));
-	cl_assert_equal_s(expected, text);
-	git__free(text);
+	cl_git_pass(git_patch_to_buf(&buf, patch));
+	cl_assert_equal_s(expected, buf.ptr);
+	git_buf_free(&buf);
 
 	git_patch_free(patch);
 
diff --git a/tests/diff/submodules.c b/tests/diff/submodules.c
index 24545b2..da96ba9 100644
--- a/tests/diff/submodules.c
+++ b/tests/diff/submodules.c
@@ -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(
 	git_diff *diff, const char **expected, const char *file, int line)
 {
 	const git_diff_delta *delta;
 	git_patch *patch = NULL;
 	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)) {
 		cl_git_pass(git_patch_from_diff(&patch, diff, d));
@@ -33,16 +35,16 @@ static void check_diff_patches_at_line(
 		if (expected[d] && !strcmp(expected[d], "<SKIP>"))
 			continue;
 		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_git_pass(git_patch_to_str(&patch_text, patch));
+		cl_git_pass(git_patch_to_buf(&buf, patch));
 
 		clar__assert_equal(
 			file, line, "expected diff did not match actual diff", 1,
-			"%s", expected[d], patch_text);
-		git__free(patch_text);
+			"%s", expected[d], get_buf_ptr(&buf));
+		git_buf_free(&buf);
 	}
 
 	cl_assert_at_line(expected[d] && !strcmp(expected[d], "<END>"), file, line);
diff --git a/tests/diff/workdir.c b/tests/diff/workdir.c
index 7cc0322..449dc63 100644
--- a/tests/diff/workdir.c
+++ b/tests/diff/workdir.c
@@ -1375,7 +1375,7 @@ void test_diff_workdir__patience_diff(void)
 	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
 	git_diff *diff = 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_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)
 	cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
 	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_to_str(&as_str, patch));
+	cl_git_pass(git_patch_to_buf(&buf, patch));
 
-	cl_assert_equal_s(expected_normal, as_str);
-	git__free(as_str);
+	cl_assert_equal_s(expected_normal, buf.ptr);
+	git_buf_clear(&buf);
 	git_patch_free(patch);
 	git_diff_free(diff);
 
@@ -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_assert_equal_i(1, git_diff_num_deltas(diff));
 	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);
-	git__free(as_str);
+	cl_assert_equal_s(expected_patience, buf.ptr);
+	git_buf_clear(&buf);
+
+	git_buf_free(&buf);
 	git_patch_free(patch);
 	git_diff_free(diff);
 }