Commit 46e7fc1853bb92a2deae93543477f1c1255352d0

Vicent Marti 2014-01-27T09:36:24

Merge pull request #2077 from libgit2/cmn/buf-out Buff up returning strings

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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
diff --git a/include/git2/branch.h b/include/git2/branch.h
index 44d6fd9..851de29 100644
--- a/include/git2/branch.h
+++ b/include/git2/branch.h
@@ -200,25 +200,20 @@ GIT_EXTERN(int) git_branch_set_upstream(git_reference *branch, const char *upstr
  * Return the name of the reference supporting the remote tracking branch,
  * given the name of a local branch reference.
  *
- * @param tracking_branch_name_out The user-allocated buffer which will be
- *     filled with the name of the reference. Pass NULL if you just want to
- *     get the needed size of the name of the reference as the output value.
- *
- * @param buffer_size Size of the `out` buffer in bytes.
+ * @param out Pointer to the user-allocated git_buf which will be
+ * filled with the name of the reference.
  *
  * @param repo the repository where the branches live
  *
- * @param canonical_branch_name name of the local branch.
+ * @param refname reference name of the local branch.
  *
- * @return number of characters in the reference name
- *     including the trailing NUL byte; GIT_ENOTFOUND when no remote tracking
- *     reference exists, otherwise an error code.
+ * @return 0, GIT_ENOTFOUND when no remote tracking reference exists,
+ *     otherwise an error code.
  */
 GIT_EXTERN(int) git_branch_upstream_name(
-	char *tracking_branch_name_out,
-	size_t buffer_size,
+	git_buf *out,
 	git_repository *repo,
-	const char *canonical_branch_name);
+	const char *refname);
 
 /**
  * Determine if the current local branch is pointed at by HEAD.
@@ -234,25 +229,19 @@ GIT_EXTERN(int) git_branch_is_head(
 /**
  * Return the name of remote that the remote tracking branch belongs to.
  *
- * @param remote_name_out The user-allocated buffer which will be
- *     filled with the name of the remote. Pass NULL if you just want to
- *     get the needed size of the name of the remote as the output value.
- *
- * @param buffer_size Size of the `out` buffer in bytes.
+ * @param out Pointer to the user-allocated git_buf which will be filled iwth the name of the remote.
  *
  * @param repo The repository where the branch lives.
  *
  * @param canonical_branch_name name of the remote tracking branch.
  *
- * @return Number of characters in the reference name
- *     including the trailing NUL byte; GIT_ENOTFOUND
+ * @return 0, GIT_ENOTFOUND
  *     when no remote matching remote was found,
  *     GIT_EAMBIGUOUS when the branch maps to several remotes,
  *     otherwise an error code.
  */
 GIT_EXTERN(int) git_branch_remote_name(
-	char *remote_name_out,
-	size_t buffer_size,
+	git_buf *out,
 	git_repository *repo,
 	const char *canonical_branch_name);
 
diff --git a/include/git2/config.h b/include/git2/config.h
index f650f1b..663b4f6 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -9,6 +9,7 @@
 
 #include "common.h"
 #include "types.h"
+#include "buffer.h"
 
 /**
  * @file git2/config.h
@@ -90,11 +91,10 @@ typedef struct {
  * This method will not guess the path to the xdg compatible
  * config file (.config/git/config).
  *
- * @param out Buffer to store the path in
- * @param length size of the buffer in bytes
- * @return 0 if a global configuration file has been found. Its path will be stored in `buffer`.
+ * @param out Pointer to a user-allocated git_buf in which to store the path
+ * @return 0 if a global configuration file has been found. Its path will be stored in `out`.
  */
-GIT_EXTERN(int) git_config_find_global(char *out, size_t length);
+GIT_EXTERN(int) git_config_find_global(git_buf *out);
 
 /**
  * Locate the path to the global xdg compatible configuration file
@@ -107,25 +107,23 @@ GIT_EXTERN(int) git_config_find_global(char *out, size_t length);
  * may be used on any `git_config` call to load the
  * xdg compatible configuration file.
  *
- * @param out Buffer to store the path in
- * @param length size of the buffer in bytes
+ * @param out Pointer to a user-allocated git_buf in which to store the path
  * @return 0 if a xdg compatible configuration file has been
- *	found. Its path will be stored in `buffer`.
+ *	found. Its path will be stored in `out`.
  */
-GIT_EXTERN(int) git_config_find_xdg(char *out, size_t length);
+GIT_EXTERN(int) git_config_find_xdg(git_buf *out);
 
 /**
  * Locate the path to the system configuration file
  *
  * If /etc/gitconfig doesn't exist, it will look for
  * %PROGRAMFILES%\Git\etc\gitconfig.
-
- * @param out Buffer to store the path in
- * @param length size of the buffer in bytes
+ *
+ * @param out Pointer to a user-allocated git_buf in which to store the path
  * @return 0 if a system configuration file has been
- *	found. Its path will be stored in `buffer`.
+ *	found. Its path will be stored in `out`.
  */
-GIT_EXTERN(int) git_config_find_system(char *out, size_t length);
+GIT_EXTERN(int) git_config_find_system(git_buf *out);
 
 /**
  * Open the global, XDG and system configuration files
diff --git a/include/git2/message.h b/include/git2/message.h
index 395c886..bcdb72f 100644
--- a/include/git2/message.h
+++ b/include/git2/message.h
@@ -8,6 +8,7 @@
 #define INCLUDE_git_message_h__
 
 #include "common.h"
+#include "buffer.h"
 
 /**
  * @file git2/message.h
@@ -23,25 +24,17 @@ GIT_BEGIN_DECL
  *
  * Optionally, can remove lines starting with a "#".
  *
- * @param out The user-allocated buffer which will be filled with the
- *     cleaned up message. Pass NULL if you just want to get the needed
- *     size of the prettified message as the output value.
- *
- * @param out_size Size of the `out` buffer in bytes.
+ * @param out The user-allocated git_buf which will be filled with the
+ *     cleaned up message.
  *
  * @param message The message to be prettified.
  *
  * @param strip_comments Non-zero to remove lines starting with "#", 0 to
  *     leave them in.
  *
- * @return -1 on error, else number of characters in prettified message
- *     including the trailing NUL byte
+ * @return 0 or an error code.
  */
-GIT_EXTERN(int) git_message_prettify(
-	char *out,
-	size_t out_size,
-	const char *message,
-	int strip_comments);
+GIT_EXTERN(int) git_message_prettify(git_buf *out, const char *message, int strip_comments);
 
 /** @} */
 GIT_END_DECL
diff --git a/include/git2/refspec.h b/include/git2/refspec.h
index d96b83c..9acdc72 100644
--- a/include/git2/refspec.h
+++ b/include/git2/refspec.h
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "types.h"
 #include "net.h"
+#include "buffer.h"
 
 /**
  * @file git2/refspec.h
@@ -82,23 +83,21 @@ GIT_EXTERN(int) git_refspec_dst_matches(const git_refspec *refspec, const char *
  * Transform a reference to its target following the refspec's rules
  *
  * @param out where to store the target name
- * @param outlen the size of the `out` buffer
  * @param spec the refspec
  * @param name the name of the reference to transform
  * @return 0, GIT_EBUFS or another error
  */
-GIT_EXTERN(int) git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name);
+GIT_EXTERN(int) git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name);
 
 /**
  * Transform a target reference to its source reference following the refspec's rules
  *
  * @param out where to store the source reference name
- * @param outlen the size of the `out` buffer
  * @param spec the refspec
  * @param name the name of the reference to transform
  * @return 0, GIT_EBUFS or another error
  */
-GIT_EXTERN(int) git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name);
+GIT_EXTERN(int) git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name);
 
 GIT_END_DECL
 
diff --git a/include/git2/repository.h b/include/git2/repository.h
index 9f71d29..a0453da 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "types.h"
 #include "oid.h"
+#include "buffer.h"
 
 /**
  * @file git2/repository.h
@@ -58,10 +59,8 @@ GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb);
  * The method will automatically detect if the repository is bare
  * (if there is a repository).
  *
- * @param path_out The user allocated buffer which will
- * contain the found path.
- *
- * @param path_size repository_path size
+ * @param out A pointer to a user-allocated git_buf which will contain
+ * the found path.
  *
  * @param start_path The base path where the lookup starts.
  *
@@ -77,8 +76,7 @@ GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb);
  * @return 0 or an error code
  */
 GIT_EXTERN(int) git_repository_discover(
-		char *path_out,
-		size_t path_size,
+		git_buf *out,
 		const char *start_path,
 		int across_fs,
 		const char *ceiling_dirs);
@@ -464,21 +462,11 @@ GIT_EXTERN(int) git_repository_index(git_index **out, git_repository *repo);
  * Use this function to get the contents of this file. Don't forget to
  * remove the file after you create the commit.
  *
- * If the repository message exists and there are no errors reading it, this
- * returns the bytes needed to store the message in memory (i.e. message
- * file size plus one terminating NUL byte).  That value is returned even if
- * `out` is NULL or `len` is shorter than the necessary size.
- *
- * The `out` buffer will *always* be NUL terminated, even if truncation
- * occurs.
- *
- * @param out Buffer to write data into or NULL to just read required size
- * @param len Length of `out` buffer in bytes
+ * @param out git_buf to write data into
  * @param repo Repository to read prepared message from
- * @return GIT_ENOTFOUND if no message exists, other value < 0 for other
- *         errors, or total bytes in message (may be > `len`) on success
+ * @return 0, GIT_ENOTFOUND if no message exists or an error code
  */
-GIT_EXTERN(int) git_repository_message(char *out, size_t len, git_repository *repo);
+GIT_EXTERN(int) git_repository_message(git_buf *out, git_repository *repo);
 
 /**
  * Remove git's prepared message.
diff --git a/src/branch.c b/src/branch.c
index 3b9aa0d..a1a04b2 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -286,10 +286,10 @@ static int retrieve_upstream_configuration(
 	return error;
 }
 
-int git_branch_upstream__name(
-	git_buf *tracking_name,
+int git_branch_upstream_name(
+	git_buf *out,
 	git_repository *repo,
-	const char *canonical_branch_name)
+	const char *refname)
 {
 	const char *remote_name, *merge_name;
 	git_buf buf = GIT_BUF_INIT;
@@ -297,22 +297,24 @@ int git_branch_upstream__name(
 	git_remote *remote = NULL;
 	const git_refspec *refspec;
 
-	assert(tracking_name && canonical_branch_name);
+	assert(out && refname);
+
+	git_buf_sanitize(out);
 
-	if (!git_reference__is_branch(canonical_branch_name))
-		return not_a_local_branch(canonical_branch_name);
+	if (!git_reference__is_branch(refname))
+		return not_a_local_branch(refname);
 
 	if ((error = retrieve_upstream_configuration(
-		&remote_name, repo, canonical_branch_name, "branch.%s.remote")) < 0)
+		&remote_name, repo, refname, "branch.%s.remote")) < 0)
 			goto cleanup;
 
 	if ((error = retrieve_upstream_configuration(
-		&merge_name, repo, canonical_branch_name, "branch.%s.merge")) < 0)
+		&merge_name, repo, refname, "branch.%s.merge")) < 0)
 			goto cleanup;
 
 	if (!*remote_name || !*merge_name) {
 		giterr_set(GITERR_REFERENCE,
-			"branch '%s' does not have an upstream", canonical_branch_name);
+			"branch '%s' does not have an upstream", refname);
 		error = GIT_ENOTFOUND;
 		goto cleanup;
 	}
@@ -327,13 +329,13 @@ int git_branch_upstream__name(
 			goto cleanup;
 		}
 
-		if (git_refspec_transform_r(&buf, refspec, merge_name) < 0)
+		if (git_refspec_transform(&buf, refspec, merge_name) < 0)
 			goto cleanup;
 	} else
 		if (git_buf_sets(&buf, merge_name) < 0)
 			goto cleanup;
 
-	error = git_buf_set(tracking_name, git_buf_cstr(&buf), git_buf_len(&buf));
+	error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf));
 
 cleanup:
 	git_remote_free(remote);
@@ -341,7 +343,7 @@ cleanup:
 	return error;
 }
 
-static int remote_name(git_buf *buf, git_repository *repo, const char *canonical_branch_name)
+int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refname)
 {
 	git_strarray remote_list = {0};
 	size_t i;
@@ -350,12 +352,14 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical
 	int error = 0;
 	char *remote_name = NULL;
 
-	assert(buf && repo && canonical_branch_name);
+	assert(buf && repo && refname);
+
+	git_buf_sanitize(buf);
 
 	/* Verify that this is a remote branch */
-	if (!git_reference__is_remote(canonical_branch_name)) {
+	if (!git_reference__is_remote(refname)) {
 		giterr_set(GITERR_INVALID, "Reference '%s' is not a remote branch.",
-			canonical_branch_name);
+			refname);
 		error = GIT_ERROR;
 		goto cleanup;
 	}
@@ -369,7 +373,7 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical
 		if ((error = git_remote_load(&remote, repo, remote_list.strings[i])) < 0)
 			continue;
 
-		fetchspec = git_remote__matching_dst_refspec(remote, canonical_branch_name);
+		fetchspec = git_remote__matching_dst_refspec(remote, refname);
 		if (fetchspec) {
 			/* If we have not already set out yet, then set
 			 * it to the matching remote name. Otherwise
@@ -381,7 +385,7 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical
 				git_remote_free(remote);
 
 				giterr_set(GITERR_REFERENCE,
-					"Reference '%s' is ambiguous", canonical_branch_name);
+					"Reference '%s' is ambiguous", refname);
 				error = GIT_EAMBIGUOUS;
 				goto cleanup;
 			}
@@ -395,68 +399,18 @@ static int remote_name(git_buf *buf, git_repository *repo, const char *canonical
 		error = git_buf_puts(buf, remote_name);
 	} else {
 		giterr_set(GITERR_REFERENCE,
-			"Could not determine remote for '%s'", canonical_branch_name);
+			"Could not determine remote for '%s'", refname);
 		error = GIT_ENOTFOUND;
 	}
 
 cleanup:
+	if (error < 0)
+		git_buf_free(buf);
+
 	git_strarray_free(&remote_list);
 	return error;
 }
 
-int git_branch_remote_name(char *buffer, size_t buffer_len, git_repository *repo, const char *refname)
-{
-	int ret;
-	git_buf buf = GIT_BUF_INIT;
-
-	if ((ret = remote_name(&buf, repo, refname)) < 0)
-		return ret;
-
-	if (buffer)
-		git_buf_copy_cstr(buffer, buffer_len, &buf);
-
-	ret = (int)git_buf_len(&buf) + 1;
-	git_buf_free(&buf);
-
-	return ret;
-}
-
-int git_branch_upstream_name(
-	char *tracking_branch_name_out,
-	size_t buffer_size,
-	git_repository *repo,
-	const char *canonical_branch_name)
-{
-	git_buf buf = GIT_BUF_INIT;
-	int error;
-
-	assert(canonical_branch_name);
-
-	if (tracking_branch_name_out && buffer_size)
-		*tracking_branch_name_out = '\0';
-
-	if ((error = git_branch_upstream__name(
-		&buf, repo, canonical_branch_name)) < 0)
-			goto cleanup;
-
-	if (tracking_branch_name_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */
-		giterr_set(
-			GITERR_INVALID,
-			"Buffer too short to hold the tracked reference name.");
-		error = -1;
-		goto cleanup;
-	}
-
-	if (tracking_branch_name_out)
-		git_buf_copy_cstr(tracking_branch_name_out, buffer_size, &buf);
-
-	error = (int)buf.size + 1;
-
-cleanup:
-	git_buf_free(&buf);
-	return (int)error;
-}
-
 int git_branch_upstream(
 		git_reference **tracking_out,
 		git_reference *branch)
@@ -464,7 +418,7 @@ int git_branch_upstream(
 	int error;
 	git_buf tracking_name = GIT_BUF_INIT;
 
-	if ((error = git_branch_upstream__name(&tracking_name,
+	if ((error = git_branch_upstream_name(&tracking_name,
 		git_reference_owner(branch), git_reference_name(branch))) < 0)
 			return error;
 
@@ -547,7 +501,7 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
 	if (local)
 		git_buf_puts(&value, ".");
 	else
-		remote_name(&value, repo, git_reference_name(upstream));
+		git_branch_remote_name(&value, repo, git_reference_name(upstream));
 
 	if (git_buf_printf(&key, "branch.%s.remote", shortname) < 0)
 		goto on_error;
@@ -566,7 +520,7 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
 
 		fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream));
 		git_buf_clear(&value);
-		if (!fetchspec || git_refspec_transform_l(&value, fetchspec, git_reference_name(upstream)) < 0)
+		if (!fetchspec || git_refspec_rtransform(&value, fetchspec, git_reference_name(upstream)) < 0)
 			goto on_error;
 
 		git_remote_free(remote);
diff --git a/src/clone.c b/src/clone.c
index 828c47f..2e9d72a 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -131,7 +131,7 @@ static int reference_matches_remote_head(
 
 	if (!error && !git_oid__cmp(&head_info->remote_head_oid, &oid)) {
 		/* Determine the local reference name from the remote tracking one */
-		error = git_refspec_transform_l(
+		error = git_refspec_rtransform(
 			&head_info->branchname, head_info->refspec, reference_name);
 
 		if (!error &&
@@ -199,7 +199,7 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
 	}
 
 	/* Determine the remote tracking reference name from the local master */
-	if ((error = git_refspec_transform_r(
+	if ((error = git_refspec_transform(
 		&remote_master_name,
 		head_info.refspec,
 		GIT_REFS_HEADS_MASTER_FILE)) < 0)
diff --git a/src/config.c b/src/config.c
index fa1dd81..6aa7146 100644
--- a/src/config.c
+++ b/src/config.c
@@ -935,61 +935,21 @@ void git_config_iterator_free(git_config_iterator *iter)
 	iter->free(iter);
 }
 
-static int git_config__find_file_to_path(
-	char *out, size_t outlen, int (*find)(git_buf *buf))
-{
-	int error = 0;
-	git_buf path = GIT_BUF_INIT;
-
-	if ((error = find(&path)) < 0)
-		goto done;
-
-	if (path.size >= outlen) {
-		giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
-		error = GIT_EBUFS;
-		goto done;
-	}
-
-	git_buf_copy_cstr(out, outlen, &path);
-
-done:
-	git_buf_free(&path);
-	return error;
-}
-
-int git_config_find_global_r(git_buf *path)
+int git_config_find_global(git_buf *path)
 {
 	return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
 }
 
-int git_config_find_global(char *global_config_path, size_t length)
-{
-	return git_config__find_file_to_path(
-		global_config_path, length, git_config_find_global_r);
-}
-
-int git_config_find_xdg_r(git_buf *path)
+int git_config_find_xdg(git_buf *path)
 {
 	return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
 }
 
-int git_config_find_xdg(char *xdg_config_path, size_t length)
-{
-	return git_config__find_file_to_path(
-		xdg_config_path, length, git_config_find_xdg_r);
-}
-
-int git_config_find_system_r(git_buf *path)
+int git_config_find_system(git_buf *path)
 {
 	return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
 }
 
-int git_config_find_system(char *system_config_path, size_t length)
-{
-	return git_config__find_file_to_path(
-		system_config_path, length, git_config_find_system_r);
-}
-
 int git_config__global_location(git_buf *buf)
 {
 	const git_buf *paths;
@@ -1026,16 +986,16 @@ int git_config_open_default(git_config **out)
 	if ((error = git_config_new(&cfg)) < 0)
 		return error;
 
-	if (!git_config_find_global_r(&buf) || !git_config__global_location(&buf)) {
+	if (!git_config_find_global(&buf) || !git_config__global_location(&buf)) {
 		error = git_config_add_file_ondisk(cfg, buf.ptr,
 			GIT_CONFIG_LEVEL_GLOBAL, 0);
 	}
 
-	if (!error && !git_config_find_xdg_r(&buf))
+	if (!error && !git_config_find_xdg(&buf))
 		error = git_config_add_file_ondisk(cfg, buf.ptr,
 			GIT_CONFIG_LEVEL_XDG, 0);
 
-	if (!error && !git_config_find_system_r(&buf))
+	if (!error && !git_config_find_system(&buf))
 		error = git_config_add_file_ondisk(cfg, buf.ptr,
 			GIT_CONFIG_LEVEL_SYSTEM, 0);
 
diff --git a/src/config.h b/src/config.h
index 3cd888c..03d9106 100644
--- a/src/config.h
+++ b/src/config.h
@@ -24,11 +24,6 @@ struct git_config {
 	git_vector files;
 };
 
-extern int git_config_find_global_r(git_buf *global_config_path);
-extern int git_config_find_xdg_r(git_buf *system_config_path);
-extern int git_config_find_system_r(git_buf *system_config_path);
-
-
 extern int git_config__global_location(git_buf *buf);
 
 extern int git_config_rename_section(
diff --git a/src/message.c b/src/message.c
index 0eff426..07b2569 100644
--- a/src/message.c
+++ b/src/message.c
@@ -21,7 +21,7 @@ static size_t line_length_without_trailing_spaces(const char *line, size_t len)
 
 /* Greatly inspired from git.git "stripspace" */
 /* see https://github.com/git/git/blob/497215d8811ac7b8955693ceaad0899ecd894ed2/builtin/stripspace.c#L4-67 */
-int git_message__prettify(git_buf *message_out, const char *message, int strip_comments)
+int git_message_prettify(git_buf *message_out, const char *message, int strip_comments)
 {
 	const size_t message_len = strlen(message);
 
@@ -29,6 +29,8 @@ int git_message__prettify(git_buf *message_out, const char *message, int strip_c
 	size_t i, line_length, rtrimmed_line_length;
 	char *next_newline;
 
+	git_buf_sanitize(message_out);
+
 	for (i = 0; i < strlen(message); i += line_length) {
 		next_newline = memchr(message + i, '\n', message_len - i);
 
@@ -58,29 +60,3 @@ int git_message__prettify(git_buf *message_out, const char *message, int strip_c
 
 	return git_buf_oom(message_out) ? -1 : 0;
 }
-
-int git_message_prettify(char *message_out, size_t buffer_size, const char *message, int strip_comments)
-{
-	git_buf buf = GIT_BUF_INIT;
-	ssize_t out_size = -1;
-
-	if (message_out && buffer_size)
-		*message_out = '\0';
-
-	if (git_message__prettify(&buf, message, strip_comments) < 0)
-		goto done;
-
-	if (message_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */
-		giterr_set(GITERR_INVALID, "Buffer too short to hold the cleaned message");
-		goto done;
-	}
-
-	if (message_out)
-		git_buf_copy_cstr(message_out, buffer_size, &buf);
-
-	out_size = buf.size + 1;
-
-done:
-	git_buf_free(&buf);
-	return (int)out_size;
-}
diff --git a/src/push.c b/src/push.c
index 0be82f3..d39a271 100644
--- a/src/push.c
+++ b/src/push.c
@@ -214,7 +214,7 @@ int git_push_update_tips(git_push *push)
 		if (!fetch_spec)
 			continue;
 
-		if ((error = git_refspec_transform_r(&remote_ref_name, fetch_spec, status->ref)) < 0)
+		if ((error = git_refspec_transform(&remote_ref_name, fetch_spec, status->ref)) < 0)
 			goto on_error;
 
 		/* Find matching  push ref spec */
diff --git a/src/refspec.c b/src/refspec.c
index a973400..fa60aa7 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -178,54 +178,6 @@ int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
 	return (p_fnmatch(refspec->dst, refname, 0) == 0);
 }
 
-static int refspec_transform_internal(char *out, size_t outlen, const char *from, const char *to, const char *name)
-{
-	size_t baselen, namelen;
-
-	baselen = strlen(to);
-	if (outlen <= baselen) {
-		giterr_set(GITERR_INVALID, "Reference name too long");
-		return GIT_EBUFS;
-	}
-
-	/*
-	 * No '*' at the end means that it's mapped to one specific local
-	 * branch, so no actual transformation is needed.
-	 */
-	if (to[baselen - 1] != '*') {
-		memcpy(out, to, baselen + 1); /* include '\0' */
-		return 0;
-	}
-
-	/* There's a '*' at the end, so remove its length */
-	baselen--;
-
-	/* skip the prefix, -1 is for the '*' */
-	name += strlen(from) - 1;
-
-	namelen = strlen(name);
-
-	if (outlen <= baselen + namelen) {
-		giterr_set(GITERR_INVALID, "Reference name too long");
-		return GIT_EBUFS;
-	}
-
-	memcpy(out, to, baselen);
-	memcpy(out + baselen, name, namelen + 1);
-
-	return 0;
-}
-
-int git_refspec_transform(char *out, size_t outlen, const git_refspec *spec, const char *name)
-{
-	return refspec_transform_internal(out, outlen, spec->src, spec->dst, name);
-}
-
-int git_refspec_rtransform(char *out, size_t outlen, const git_refspec *spec, const char *name)
-{
-	return refspec_transform_internal(out, outlen, spec->dst, spec->src, name);
-}
-
 static int refspec_transform(
 	git_buf *out, const char *from, const char *to, const char *name)
 {
@@ -233,6 +185,8 @@ static int refspec_transform(
 	size_t from_len = from ? strlen(from) : 0;
 	size_t name_len = name ? strlen(name) : 0;
 
+	git_buf_sanitize(out);
+
 	if (git_buf_set(out, to, to_len) < 0)
 		return -1;
 
@@ -253,12 +207,12 @@ static int refspec_transform(
 	return git_buf_put(out, name + from_len, name_len - from_len);
 }
 
-int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name)
+int git_refspec_transform(git_buf *out, const git_refspec *spec, const char *name)
 {
 	return refspec_transform(out, spec->src, spec->dst, name);
 }
 
-int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name)
+int git_refspec_rtransform(git_buf *out, const git_refspec *spec, const char *name)
 {
 	return refspec_transform(out, spec->dst, spec->src, name);
 }
diff --git a/src/refspec.h b/src/refspec.h
index 51b7bfe..375465f 100644
--- a/src/refspec.h
+++ b/src/refspec.h
@@ -31,28 +31,6 @@ int git_refspec__parse(
 
 void git_refspec__free(git_refspec *refspec);
 
-/**
- * Transform a reference to its target following the refspec's rules,
- * and writes the results into a git_buf.
- *
- * @param out where to store the target name
- * @param spec the refspec
- * @param name the name of the reference to transform
- * @return 0 or error if buffer allocation fails
- */
-int git_refspec_transform_r(git_buf *out, const git_refspec *spec, const char *name);
-
-/**
- * Transform a reference from its target following the refspec's rules,
- * and writes the results into a git_buf.
- *
- * @param out where to store the source name
- * @param spec the refspec
- * @param name the name of the reference to transform
- * @return 0 or error if buffer allocation fails
- */
-int git_refspec_transform_l(git_buf *out, const git_refspec *spec, const char *name);
-
 int git_refspec__serialize(git_buf *out, const git_refspec *refspec);
 
 /**
diff --git a/src/remote.c b/src/remote.c
index 5b3656a..5d35aff 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -896,7 +896,7 @@ static int remote_head_for_ref(git_remote_head **out, git_refspec *spec, git_vec
 	if ((error = git_reference_resolve(&resolved_ref, ref)) < 0 ||
 		(!git_reference_is_branch(resolved_ref)) ||
 		(error = git_branch_upstream(&tracking_ref, resolved_ref)) < 0 ||
-		(error = git_refspec_transform_l(&remote_name, spec, git_reference_name(tracking_ref))) < 0) {
+		(error = git_refspec_rtransform(&remote_name, spec, git_reference_name(tracking_ref))) < 0) {
 		/* Not an error if HEAD is unborn or no tracking branch */
 		if (error == GIT_ENOTFOUND)
 			error = 0;
@@ -1011,7 +1011,7 @@ static int update_tips_for_spec(git_remote *remote, git_refspec *spec, git_vecto
 			continue;
 
 		if (git_refspec_src_matches(spec, head->name) && spec->dst) {
-			if (git_refspec_transform_r(&refname, spec, head->name) < 0)
+			if (git_refspec_transform(&refname, spec, head->name) < 0)
 				goto on_error;
 		} else if (remote->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_NONE) {
 
diff --git a/src/repository.c b/src/repository.c
index 8645357..285d889 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -495,34 +495,18 @@ int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb)
 }
 
 int git_repository_discover(
-	char *repository_path,
-	size_t size,
+	git_buf *out,
 	const char *start_path,
 	int across_fs,
 	const char *ceiling_dirs)
 {
-	git_buf path = GIT_BUF_INIT;
 	uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0;
-	int error;
-
-	assert(start_path && repository_path && size > 0);
-
-	*repository_path = '\0';
 
-	if ((error = find_repo(&path, NULL, start_path, flags, ceiling_dirs)) < 0)
-		return error != GIT_ENOTFOUND ? -1 : error;
+	assert(start_path);
 
-	if (size < (size_t)(path.size + 1)) {
-		giterr_set(GITERR_REPOSITORY,
-			"The given buffer is too small to store the discovered path");
-		git_buf_free(&path);
-		return -1;
-	}
+	git_buf_sanitize(out);
 
-	/* success: we discovered a repository */
-	git_buf_copy_cstr(repository_path, size, &path);
-	git_buf_free(&path);
-	return 0;
+	return find_repo(out, NULL, start_path, flags, ceiling_dirs);
 }
 
 static int load_config(
@@ -598,9 +582,9 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
 		git_buf system_buf = GIT_BUF_INIT;
 		git_config *config;
 
-		git_config_find_global_r(&global_buf);
-		git_config_find_xdg_r(&xdg_buf);
-		git_config_find_system_r(&system_buf);
+		git_config_find_global(&global_buf);
+		git_config_find_xdg(&xdg_buf);
+		git_config_find_system(&system_buf);
 
 		/* If there is no global file, open a backend for it anyway */
 		if (git_buf_len(&global_buf) == 0)
@@ -1732,14 +1716,13 @@ cleanup:
 	return error;
 }
 
-int git_repository_message(char *buffer, size_t len, git_repository *repo)
+int git_repository_message(git_buf *out,  git_repository *repo)
 {
-	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
+	git_buf path = GIT_BUF_INIT;
 	struct stat st;
 	int error;
 
-	if (buffer != NULL)
-		*buffer = '\0';
+	git_buf_sanitize(out);
 
 	if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0)
 		return -1;
@@ -1749,16 +1732,10 @@ int git_repository_message(char *buffer, size_t len, git_repository *repo)
 			error = GIT_ENOTFOUND;
 		giterr_set(GITERR_OS, "Could not access message file");
 	}
-	else if (buffer != NULL) {
-		error = git_futils_readbuffer(&buf, git_buf_cstr(&path));
-		git_buf_copy_cstr(buffer, len, &buf);
-	}
 
-	git_buf_free(&path);
-	git_buf_free(&buf);
+	error = git_futils_readbuffer(out, git_buf_cstr(&path));
 
-	if (!error)
-		error = (int)st.st_size + 1; /* add 1 for NUL byte */
+	git_buf_free(&path);
 
 	return error;
 }
diff --git a/tests/clone/empty.c b/tests/clone/empty.c
index 6d19244..78aef7d 100644
--- a/tests/clone/empty.c
+++ b/tests/clone/empty.c
@@ -38,7 +38,7 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void)
 	char *local_name = "refs/heads/master";
 	const char *expected_tracked_branch_name = "refs/remotes/origin/master";
 	const char *expected_remote_name = "origin";
-	char buffer[1024];
+	git_buf buf = GIT_BUF_INIT;
 	git_reference *ref;
 
 	cl_set_cleanup(&cleanup_repository, "./empty");
@@ -50,16 +50,14 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void)
 	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, local_name));
 
 	/* ...one can still retrieve the name of the remote tracking reference */
-	cl_assert_equal_i((int)strlen(expected_tracked_branch_name) + 1,
-		git_branch_upstream_name(buffer, 1024, g_repo_cloned, local_name));
+	cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, local_name));
 
-	cl_assert_equal_s(expected_tracked_branch_name, buffer);
+	cl_assert_equal_s(expected_tracked_branch_name, buf.ptr);
 
 	/* ...and the name of the remote... */
-	cl_assert_equal_i((int)strlen(expected_remote_name) + 1,
-		git_branch_remote_name(buffer, 1024, g_repo_cloned, expected_tracked_branch_name));
+	cl_git_pass(git_branch_remote_name(&buf, g_repo_cloned, expected_tracked_branch_name));
 
-	cl_assert_equal_s(expected_remote_name, buffer);
+	cl_assert_equal_s(expected_remote_name, buf.ptr);
 
 	/* ...even when the remote HEAD is unborn as well */
 	cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned,
diff --git a/tests/network/remote/remotes.c b/tests/network/remote/remotes.c
index 44a98d3..09963e8 100644
--- a/tests/network/remote/remotes.c
+++ b/tests/network/remote/remotes.c
@@ -230,27 +230,20 @@ void test_network_remote_remotes__fnmatch(void)
 
 void test_network_remote_remotes__transform(void)
 {
-	char ref[1024] = {0};
+	git_buf ref = GIT_BUF_INIT;
 
-	cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master"));
+	cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master"));
 	cl_assert_equal_s(ref, "refs/remotes/test/master");
+	git_buf_free(&ref);
 }
 
 void test_network_remote_remotes__transform_destination_to_source(void)
 {
-	char ref[1024] = {0};
+	git_buf ref = GIT_BUF_INIT;
 
-	cl_git_pass(git_refspec_rtransform(ref, sizeof(ref), _refspec, "refs/remotes/test/master"));
-	cl_assert_equal_s(ref, "refs/heads/master");
-}
-
-void test_network_remote_remotes__transform_r(void)
-{
-	git_buf buf = GIT_BUF_INIT;
-
-	cl_git_pass(git_refspec_transform_r(&buf,  _refspec, "refs/heads/master"));
-	cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master");
-	git_buf_free(&buf);
+	cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master"));
+	cl_assert_equal_s(ref.ptr, "refs/heads/master");
+	git_buf_free(&ref);
 }
 
 void test_network_remote_remotes__missing_refspecs(void)
diff --git a/tests/object/commit/commitstagedfile.c b/tests/object/commit/commitstagedfile.c
index 9867ab4..1509277 100644
--- a/tests/object/commit/commitstagedfile.c
+++ b/tests/object/commit/commitstagedfile.c
@@ -25,7 +25,7 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
 	git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid;
 	git_signature *signature;
 	git_tree *tree;
-	char buffer[128];
+	git_buf buffer;
 
 	/*
 	 * The test below replicates the following git scenario
@@ -111,7 +111,8 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
 	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
 	cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
 
-	cl_assert_equal_i(16, git_message_prettify(buffer, 128, "Initial commit", 0));
+	memset(&buffer, 0, sizeof(git_buf));
+	cl_git_pass(git_message_prettify(&buffer, "Initial commit", 0));
 
 	cl_git_pass(git_commit_create_v(
 		&commit_oid,
@@ -120,12 +121,13 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
 		signature,
 		signature,
 		NULL,
-		buffer,
+		buffer.ptr,
 		tree,
 		0));
 
 	cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0);
 
+	git_buf_free(&buffer);
 	git_signature_free(signature);
 	git_tree_free(tree);
 	git_index_free(index);
diff --git a/tests/object/message.c b/tests/object/message.c
index 7ef6374..ab55653 100644
--- a/tests/object/message.c
+++ b/tests/object/message.c
@@ -6,7 +6,7 @@ static void assert_message_prettifying(char *expected_output, char *input, int s
 {
 	git_buf prettified_message = GIT_BUF_INIT;
 
-	git_message__prettify(&prettified_message, input, strip_comments);
+	git_message_prettify(&prettified_message, input, strip_comments);
 	cl_assert_equal_s(expected_output, git_buf_cstr(&prettified_message));
 
 	git_buf_free(&prettified_message);
@@ -172,65 +172,28 @@ void test_object_message__keep_comments(void)
 
 void test_object_message__message_prettify(void)
 {
-	char buffer[100];
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 0) == 1);
-	cl_assert_equal_s(buffer, "");
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 1) == 1);
-	cl_assert_equal_s(buffer, "");
-
-	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 0));
-	cl_assert_equal_s("Short\n", buffer);
-	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 1));
-	cl_assert_equal_s("Short\n", buffer);
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 0) > 0);
-	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n# with some comments still in\n");
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 1) > 0);
-	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n");
-
-	/* try out overflow */
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678",
-		0) > 0);
-	cl_assert_equal_s(buffer,
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n",
-		0) > 0);
-	cl_assert_equal_s(buffer,
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
-
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "123456789",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "123456789\n",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890""x",
-		0));
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
-		"# 1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
-		"1234567890",
-		1) > 0);
-
-	cl_assert(git_message_prettify(NULL, 0, "", 0) == 1);
-	cl_assert(git_message_prettify(NULL, 0, "Short test", 0) == 12);
-	cl_assert(git_message_prettify(NULL, 0, "Test\n# with\nComments", 1) == 15);
+	git_buf buffer;
+
+	memset(&buffer, 0, sizeof(buffer));
+	cl_git_pass(git_message_prettify(&buffer, "", 0));
+	cl_assert_equal_s(buffer.ptr, "");
+	git_buf_free(&buffer);
+	cl_git_pass(git_message_prettify(&buffer, "", 1));
+	cl_assert_equal_s(buffer.ptr, "");
+	git_buf_free(&buffer);
+
+	cl_git_pass(git_message_prettify(&buffer, "Short", 0));
+	cl_assert_equal_s("Short\n", buffer.ptr);
+	git_buf_free(&buffer);
+	cl_git_pass(git_message_prettify(&buffer, "Short", 1));
+	cl_assert_equal_s("Short\n", buffer.ptr);
+	git_buf_free(&buffer);
+
+	cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 0));
+	cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n# with some comments still in\n");
+	git_buf_free(&buffer);
+
+	cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 1));
+	cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n");
+	git_buf_free(&buffer);
 }
diff --git a/tests/online/push.c b/tests/online/push.c
index 33f1746..8efe21e 100644
--- a/tests/online/push.c
+++ b/tests/online/push.c
@@ -219,7 +219,7 @@ static void verify_tracking_branches(git_remote *remote, expected_ref expected_r
 		if (!fetch_spec)
 			continue;
 
-		cl_git_pass(git_refspec_transform_r(&ref_name, fetch_spec, expected_refs[i].name));
+		cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));
 
 		/* Find matching remote branch */
 		git_vector_foreach(&actual_refs, j, actual_ref) {
diff --git a/tests/refs/branches/remote.c b/tests/refs/branches/remote.c
index c110adb..bac0884 100644
--- a/tests/refs/branches/remote.c
+++ b/tests/refs/branches/remote.c
@@ -21,53 +21,40 @@ void test_refs_branches_remote__cleanup(void)
 
 void test_refs_branches_remote__can_get_remote_for_branch(void)
 {
-	char remotename[1024] = {0};
+	git_buf remotename = {0};
 
-	cl_assert_equal_i(expected_remote_name_length,
-		git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name));
+	cl_git_pass(git_branch_remote_name(&remotename, g_repo, remote_tracking_branch_name));
 
-	cl_assert_equal_i(expected_remote_name_length,
-		git_branch_remote_name(remotename, expected_remote_name_length, g_repo,
-			remote_tracking_branch_name));
-
-	cl_assert_equal_s("test", remotename);
-}
-
-void test_refs_branches_remote__insufficient_buffer_returns_error(void)
-{
-	char remotename[1024] = {0};
-
-	cl_assert_equal_i(expected_remote_name_length,
-		git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name));
-
-	cl_git_fail_with(git_branch_remote_name(remotename,
-		expected_remote_name_length - 1, g_repo, remote_tracking_branch_name),
-			expected_remote_name_length);
+	cl_assert_equal_s("test", remotename.ptr);
+	git_buf_free(&remotename);
 }
 
 void test_refs_branches_remote__no_matching_remote_returns_error(void)
 {
 	const char *unknown = "refs/remotes/nonexistent/master";
+	git_buf buf;
 
 	giterr_clear();
-	cl_git_fail_with(git_branch_remote_name(
-		NULL, 0, g_repo, unknown), GIT_ENOTFOUND);
+	memset(&buf, 0, sizeof(git_buf));
+	cl_git_fail_with(git_branch_remote_name(&buf, g_repo, unknown), GIT_ENOTFOUND);
 	cl_assert(giterr_last() != NULL);
 }
 
 void test_refs_branches_remote__local_remote_returns_error(void)
 {
 	const char *local = "refs/heads/master";
+	git_buf buf;
 
 	giterr_clear();
-	cl_git_fail_with(git_branch_remote_name(
-		NULL, 0, g_repo, local), GIT_ERROR);
+	memset(&buf, 0, sizeof(git_buf));
+	cl_git_fail_with(git_branch_remote_name(&buf, g_repo, local), GIT_ERROR);
 	cl_assert(giterr_last() != NULL);
 }
 
 void test_refs_branches_remote__ambiguous_remote_returns_error(void)
 {
 	git_remote *remote;
+	git_buf buf;
 
 	/* Create the remote */
 	cl_git_pass(git_remote_create(&remote, g_repo, "addtest", "http://github.com/libgit2/libgit2"));
@@ -80,7 +67,7 @@ void test_refs_branches_remote__ambiguous_remote_returns_error(void)
 	git_remote_free(remote);
 
 	giterr_clear();
-	cl_git_fail_with(git_branch_remote_name(NULL, 0, g_repo,
-		remote_tracking_branch_name), GIT_EAMBIGUOUS);
+	memset(&buf, 0, sizeof(git_buf));
+	cl_git_fail_with(git_branch_remote_name(&buf, g_repo, remote_tracking_branch_name), GIT_EAMBIGUOUS);
 	cl_assert(giterr_last() != NULL);
 }
diff --git a/tests/refs/branches/upstreamname.c b/tests/refs/branches/upstreamname.c
index f05607d..d30002e 100644
--- a/tests/refs/branches/upstreamname.c
+++ b/tests/refs/branches/upstreamname.c
@@ -21,7 +21,7 @@ void test_refs_branches_upstreamname__cleanup(void)
 
 void test_refs_branches_upstreamname__can_retrieve_the_remote_tracking_reference_name_of_a_local_branch(void)
 {
-	cl_git_pass(git_branch_upstream__name(
+	cl_git_pass(git_branch_upstream_name(
 		&upstream_name, repo, "refs/heads/master"));
 
 	cl_assert_equal_s("refs/remotes/test/master", git_buf_cstr(&upstream_name));
@@ -29,14 +29,8 @@ void test_refs_branches_upstreamname__can_retrieve_the_remote_tracking_reference
 
 void test_refs_branches_upstreamname__can_retrieve_the_local_upstream_reference_name_of_a_local_branch(void)
 {
-	cl_git_pass(git_branch_upstream__name(
+	cl_git_pass(git_branch_upstream_name(
 		&upstream_name, repo, "refs/heads/track-local"));
 
 	cl_assert_equal_s("refs/heads/master", git_buf_cstr(&upstream_name));
 }
-
-void test_refs_branches_upstreamname__can_return_the_size_of_thelocal_upstream_reference_name_of_a_local_branch(void)
-{
-	cl_assert_equal_i((int)strlen("refs/heads/master") + 1,
-		git_branch_upstream_name(NULL, 0, repo, "refs/heads/track-local"));
-}
diff --git a/tests/repo/discover.c b/tests/repo/discover.c
index f93ff24..7904b64 100644
--- a/tests/repo/discover.c
+++ b/tests/repo/discover.c
@@ -25,12 +25,13 @@
 
 static void ensure_repository_discover(const char *start_path,
                                        const char *ceiling_dirs,
-                                       const char *expected_path)
+				       git_buf *expected_path)
 {
-	char found_path[GIT_PATH_MAX];
-	cl_git_pass(git_repository_discover(found_path, sizeof(found_path), start_path, 0, ceiling_dirs));
+	git_buf found_path = GIT_BUF_INIT;
+	cl_git_pass(git_repository_discover(&found_path, start_path, 0, ceiling_dirs));
 	//across_fs is always 0 as we can't automate the filesystem change tests
-	cl_assert_equal_s(found_path, expected_path);
+	cl_assert_equal_s(found_path.ptr, expected_path->ptr);
+	git_buf_free(&found_path);
 }
 
 static void write_file(const char *path, const char *content)
@@ -69,42 +70,40 @@ static void append_ceiling_dir(git_buf *ceiling_dirs, const char *path)
 
 void test_repo_discover__0(void)
 {
-   // test discover
+	// test discover
 	git_repository *repo;
-	git_buf ceiling_dirs_buf = GIT_BUF_INIT;
+	git_buf ceiling_dirs_buf = GIT_BUF_INIT, repository_path = GIT_BUF_INIT,
+		sub_repository_path = GIT_BUF_INIT, found_path = GIT_BUF_INIT;
 	const char *ceiling_dirs;
-	char repository_path[GIT_PATH_MAX];
-	char sub_repository_path[GIT_PATH_MAX];
-	char found_path[GIT_PATH_MAX];
 	const mode_t mode = 0777;
 
 	git_futils_mkdir_r(DISCOVER_FOLDER, NULL, mode);
 	append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER);
 	ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
 
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs));
 
 	cl_git_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1));
-	cl_git_pass(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs));
+	cl_git_pass(git_repository_discover(&repository_path, DISCOVER_FOLDER, 0, ceiling_dirs));
 	git_repository_free(repo);
 
 	cl_git_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0));
 	cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
-	cl_git_pass(git_repository_discover(sub_repository_path, sizeof(sub_repository_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
+	cl_git_pass(git_repository_discover(&sub_repository_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
 
 	cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
-	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, sub_repository_path);
+	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, &sub_repository_path);
 
 	cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, NULL, mode));
 	write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT);
 	write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT);
 	write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../");
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path);
 
 	cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, NULL, mode));
 	write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:");
@@ -114,29 +113,31 @@ void test_repo_discover__0(void)
 	write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n");
 	cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, NULL, mode));
 	write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist");
-	cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs));
-	cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs));
-	cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs));
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs));
+	cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs));
+	cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs));
+	cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs));
 
 	append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER);
 	ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
 
 	//this must pass as ceiling_directories cannot predent the current
 	//working directory to be checked
-	cl_git_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs));
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs));
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs));
+	cl_git_pass(git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_discover(&found_path, SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs));
 
 	//.gitfile redirection should not be affected by ceiling directories
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path);
-	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path);
+	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path);
 
 	cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, NULL, GIT_RMDIR_REMOVE_FILES));
 	git_repository_free(repo);
 	git_buf_free(&ceiling_dirs_buf);
+	git_buf_free(&repository_path);
+	git_buf_free(&sub_repository_path);
 }
 
diff --git a/tests/repo/message.c b/tests/repo/message.c
index 629d40c..57e8e5f 100644
--- a/tests/repo/message.c
+++ b/tests/repo/message.c
@@ -5,48 +5,37 @@
 
 static git_repository *_repo;
 static git_buf _path;
-static char *_actual;
+static git_buf _actual;
 
 void test_repo_message__initialize(void)
 {
         _repo = cl_git_sandbox_init("testrepo.git");
+	git_buf_init(&_actual, 0);
 }
 
 void test_repo_message__cleanup(void)
 {
         cl_git_sandbox_cleanup();
 	git_buf_free(&_path);
-	git__free(_actual);
-	_actual = NULL;
+	git_buf_free(&_actual);
 }
 
 void test_repo_message__none(void)
 {
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo));
 }
 
 void test_repo_message__message(void)
 {
 	const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n";
-	ssize_t len;
 
 	cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG"));
 	cl_git_mkfile(git_buf_cstr(&_path), expected);
 
-	len = git_repository_message(NULL, 0, _repo);
-	cl_assert(len > 0);
-
-	_actual = git__malloc(len + 1);
-	cl_assert(_actual != NULL);
-
-	/* Test non truncation */
-	cl_assert(git_repository_message(_actual, len, _repo) > 0);
+	cl_git_pass(git_repository_message(&_actual, _repo));
 	cl_assert_equal_s(expected, _actual);
-
-	/* Test truncation and that trailing NUL is inserted */
-	cl_assert(git_repository_message(_actual, 6, _repo) > 0);
-	cl_assert_equal_s("Test\n", _actual);
+	git_buf_free(&_actual);
 
 	cl_git_pass(p_unlink(git_buf_cstr(&_path)));
-	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(NULL, 0, _repo));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo));
 }