Commit a9f51e430fef49b3299ec33c11a4e6623e3f58cc

Russell Belfer 2013-09-11T22:00:36

Merge git_buf and git_buffer This makes the git_buf struct that was used internally into an externally available structure and eliminates the git_buffer. As part of that, some of the special cases that arose with the externally used git_buffer were blended into the git_buf, such as being careful about git_buf objects that may have a NULL ptr and allowing for bufs with a valid ptr and size but zero asize as a way of referring to externally owned data.

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
diff --git a/include/git2/blob.h b/include/git2/blob.h
index dcb815b..dcab4fb 100644
--- a/include/git2/blob.h
+++ b/include/git2/blob.h
@@ -104,17 +104,17 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
  * CRLF filtering or other types of changes depending on the file
  * attributes set for the blob and the content detected in it.
  *
- * The output is written into a `git_buffer` which the caller must free
- * when done (via `git_buffer_free`).
+ * The output is written into a `git_buf` which the caller must free
+ * when done (via `git_buf_free`).
  *
  * If no filters need to be applied, then the `out` buffer will just be
  * populated with a pointer to the raw content of the blob.  In that case,
  * be careful to *not* free the blob until done with the buffer.  To keep
- * the data detached from the blob, call `git_buffer_resize` on the buffer
+ * the data detached from the blob, call `git_buf_grow` on the buffer
  * with a `want_size` of 0 and the buffer will be reallocated to be
  * detached from the blob.
  *
- * @param out The git_buffer to be filled in
+ * @param out The git_buf to be filled in
  * @param blob Pointer to the blob
  * @param as_path Path used for file attribute lookups, etc.
  * @param check_for_binary_data Should this test if blob content contains
@@ -122,7 +122,7 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
  * @return 0 on success or an error code
  */
 GIT_EXTERN(int) git_blob_filtered_content(
-	git_buffer *out,
+	git_buf *out,
 	git_blob *blob,
 	const char *as_path,
 	int check_for_binary_data);
diff --git a/include/git2/buffer.h b/include/git2/buffer.h
index cb80e48..ae8681f 100644
--- a/include/git2/buffer.h
+++ b/include/git2/buffer.h
@@ -4,8 +4,8 @@
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
  */
-#ifndef INCLUDE_git_buffer_h__
-#define INCLUDE_git_buffer_h__
+#ifndef INCLUDE_git_buf_h__
+#define INCLUDE_git_buf_h__
 
 #include "common.h"
 
@@ -25,59 +25,69 @@ GIT_BEGIN_DECL
  * caller and have the caller take responsibility for freeing that memory.
  * This can be awkward if the caller does not have easy access to the same
  * allocation functions that libgit2 is using.  In those cases, libgit2
- * will instead fill in a `git_buffer` and the caller can use
- * `git_buffer_free()` to release it when they are done.
- *
- * * `ptr` refers to the start of the allocated memory.
- * * `size` contains the size of the data in `ptr` that is actually used.
- * * `available` refers to the known total amount of allocated memory. It
- *   may be larger than the `size` actually in use.
- *
- * In a few cases, for uniformity and simplicity, an API may populate a
- * `git_buffer` with data that should *not* be freed (i.e. the lifetime of
- * the data buffer is actually tied to another libgit2 object).  These
- * cases will be clearly documented in the APIs that use the `git_buffer`.
- * In those cases, the `available` field will be set to zero even though
- * the `ptr` and `size` will be valid.
+ * will fill in a `git_buf` and the caller can use `git_buf_free()` to
+ * release it when they are done.
+ *
+ * A `git_buf` may also be used for the caller to pass in a reference to
+ * a block of memory they hold.  In this case, libgit2 will not resize or
+ * free the memory, but will read from it as needed.
+ *
+ * A `git_buf` is a public structure with three fields:
+ *
+ * - `ptr` points to the start of the allocated memory.  If it is NULL,
+ *   then the `git_buf` is considered empty and libgit2 will feel free
+ *   to overwrite it with new data.
+ *
+ * - `size` holds the size (in bytes) of the data that is actually used.
+ *
+ * - `asize` holds the known total amount of allocated memory if the `ptr`
+ *    was allocated by libgit2.  It may be larger than `size`.  If `ptr`
+ *    was not allocated by libgit2 and should not be resized and/or freed,
+ *    then `asize` will be set to zero.
+ *
+ * Some APIs may occasionally do something slightly unusual with a buffer,
+ * such as setting `ptr` to a value that was passed in by the user.  In
+ * those cases, the behavior will be clearly documented by the API.
  */
-typedef struct git_buffer {
+typedef struct {
 	char   *ptr;
-	size_t size;
-	size_t available;
-} git_buffer;
+	size_t asize, size;
+} git_buf;
 
 /**
- * Use to initialize buffer structure when git_buffer is on stack
- */
-#define GIT_BUFFER_INIT { NULL, 0, 0 }
-
-/**
- * Free the memory referred to by the git_buffer.
+ * Free the memory referred to by the git_buf.
  *
- * Note that this does not free the `git_buffer` itself, just the memory
- * pointed to by `buffer->ptr`.  If that memory was not allocated by
- * libgit2 itself, be careful with using this function because it could
- * cause problems.
+ * Note that this does not free the `git_buf` itself, just the memory
+ * pointed to by `buffer->ptr`.  This will not free the memory if it looks
+ * like it was not allocated internally, but it will clear the buffer back
+ * to the empty state.
  *
- * @param buffer The buffer with allocated memory
+ * @param buffer The buffer to deallocate
  */
-GIT_EXTERN(void) git_buffer_free(git_buffer *buffer);
+GIT_EXTERN(void) git_buf_free(git_buf *buffer);
 
 /**
  * Resize the buffer allocation to make more space.
  *
- * This will update `buffer->available` with the new size (which will be
- * at least `want_size` and may be larger).  This may or may not change
- * `buffer->ptr` depending on whether there is an existing allocation and
- * whether that allocation can be increased in place.
+ * This will attempt to grow the buffer to accomodate the target size.
+ *
+ * If the buffer refers to memory that was not allocated by libgit2 (i.e.
+ * the `asize` field is zero), then `ptr` will be replaced with a newly
+ * allocated block of data.  Be careful so that memory allocated by the
+ * caller is not lost.  As a special variant, if you pass `target_size` as
+ * 0 and the memory is not allocated by libgit2, this will allocate a new
+ * buffer of size `size` and copy the external data into it.
+ *
+ * Currently, this will never shrink a buffer, only expand it.
  *
- * Currently, this will never shrink the buffer, only expand it.
+ * If the allocation fails, this will return an error and the buffer will be
+ * marked as invalid for future operations, invaliding the contents.
  *
  * @param buffer The buffer to be resized; may or may not be allocated yet
- * @param want_size The desired available size
- * @return 0 on success, negative error code on allocation failure
+ * @param target_size The desired available size
+ * @return 0 on success, -1 on allocation failure
  */
-GIT_EXTERN(int) git_buffer_resize(git_buffer *buffer, size_t want_size);
+GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size);
 
 /**
  * Set buffer to a copy of some raw data.
@@ -85,10 +95,10 @@ GIT_EXTERN(int) git_buffer_resize(git_buffer *buffer, size_t want_size);
  * @param buffer The buffer to set
  * @param data The data to copy into the buffer
  * @param datalen The length of the data to copy into the buffer
- * @return 0 on success, negative error code on allocation failure
+ * @return 0 on success, -1 on allocation failure
  */
-GIT_EXTERN(int) git_buffer_copy(
-	git_buffer *buffer, const void *data, size_t datalen);
+GIT_EXTERN(int) git_buf_set(
+	git_buf *buffer, const void *data, size_t datalen);
 
 GIT_END_DECL
 
diff --git a/include/git2/filter.h b/include/git2/filter.h
index 649ed97..f96b676 100644
--- a/include/git2/filter.h
+++ b/include/git2/filter.h
@@ -88,16 +88,17 @@ GIT_EXTERN(int) git_filter_list_load(
 /**
  * Apply filter list to a data buffer.
  *
- * See `git2/buffer.h` for background on `git_buffer` objects.
+ * See `git2/buffer.h` for background on `git_buf` objects.
  *
- * If the `in` buffer refers to data managed by libgit2
- * (i.e. `in->available` is not zero), then it will be overwritten when
- * applying the filters.  If not, then it will be left untouched.
+ * If the `in` buffer holds data allocated by libgit2 (i.e. `in->asize` is
+ * not zero), then it will be overwritten when applying the filters.  If
+ * not, then it will be left untouched.
  *
  * If there are no filters to apply (or `filters` is NULL), then the `out`
- * buffer will reference the `in` buffer data (with `available` set to
- * zero) instead of allocating data.  This keeps allocations to a minimum,
- * but it means you have to be careful about freeing the `in` data.
+ * buffer will reference the `in` buffer data (with `asize` set to zero)
+ * instead of allocating data.  This keeps allocations to a minimum, but
+ * it means you have to be careful about freeing the `in` data since `out`
+ * may be pointing to it!
  *
  * @param out Buffer to store the result of the filtering
  * @param filters A loaded git_filter_list (or NULL)
@@ -105,15 +106,15 @@ GIT_EXTERN(int) git_filter_list_load(
  * @return 0 on success, an error code otherwise
  */
 GIT_EXTERN(int) git_filter_list_apply_to_data(
-	git_buffer *out,
+	git_buf *out,
 	git_filter_list *filters,
-	git_buffer *in);
+	git_buf *in);
 
 /**
  * Apply filter list to the contents of a file on disk
  */
 GIT_EXTERN(int) git_filter_list_apply_to_file(
-	git_buffer *out,
+	git_buf *out,
 	git_filter_list *filters,
 	git_repository *repo,
 	const char *path);
@@ -122,7 +123,7 @@ GIT_EXTERN(int) git_filter_list_apply_to_file(
  * Apply filter list to the contents of a blob
  */
 GIT_EXTERN(int) git_filter_list_apply_to_blob(
-	git_buffer *out,
+	git_buf *out,
 	git_filter_list *filters,
 	git_blob *blob);
 
diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h
index c35fe55..36e97fe 100644
--- a/include/git2/sys/filter.h
+++ b/include/git2/sys/filter.h
@@ -4,8 +4,8 @@
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
  */
-#ifndef INCLUDE_sys_git_config_backend_h__
-#define INCLUDE_sys_git_config_backend_h__
+#ifndef INCLUDE_sys_git_filter_h__
+#define INCLUDE_sys_git_filter_h__
 
 #include "git2/filter.h"
 
@@ -117,19 +117,19 @@ typedef void (*git_filter_shutdown_fn)(git_filter *self);
  * Callback to decide if a given source needs this filter
  */
 typedef int (*git_filter_check_fn)(
-	git_filter        *self,
-	void              **payload, /* points to NULL ptr on entry, may be set */
+	git_filter  *self,
+	void       **payload, /* points to NULL ptr on entry, may be set */
 	const git_filter_source *src,
-	const char        **attr_values);
+	const char **attr_values);
 
 /**
  * Callback to actually perform the data filtering
  */
 typedef int (*git_filter_apply_fn)(
-	git_filter        *self,
-	void              **payload, /* may be read and/or set */
-	git_buffer        *to,
-	const git_buffer  *from,
+	git_filter    *self,
+	void         **payload, /* may be read and/or set */
+	git_buf       *to,
+	const git_buf *from,
 	const git_filter_source *src);
 
 /**
diff --git a/src/blob.c b/src/blob.c
index 97fd6f7..e18db4d 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -111,7 +111,7 @@ static int write_file_filtered(
 	git_filter_list *fl)
 {
 	int error;
-	git_buffer tgt = GIT_BUFFER_INIT;
+	git_buf tgt = GIT_BUF_INIT;
 
 	error = git_filter_list_apply_to_file(&tgt, fl, NULL, full_path);
 
@@ -122,7 +122,7 @@ static int write_file_filtered(
 		error = git_odb_write(oid, odb, tgt.ptr, tgt.size, GIT_OBJ_BLOB);
 	}
 
-	git_buffer_free(&tgt);
+	git_buf_free(&tgt);
 	return error;
 }
 
@@ -329,7 +329,7 @@ int git_blob_is_binary(git_blob *blob)
 }
 
 int git_blob_filtered_content(
-	git_buffer *out,
+	git_buf *out,
 	git_blob *blob,
 	const char *path,
 	int check_for_binary_data)
diff --git a/src/buffer.c b/src/buffer.c
index 07725b9..f8d47d9 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -32,7 +32,8 @@ void git_buf_init(git_buf *buf, size_t initial_size)
 		git_buf_grow(buf, initial_size);
 }
 
-int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
+int git_buf_try_grow(
+	git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external)
 {
 	char *new_ptr;
 	size_t new_size;
@@ -40,6 +41,9 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
 	if (buf->ptr == git_buf__oom)
 		return -1;
 
+	if (!target_size)
+		target_size = buf->size;
+
 	if (target_size <= buf->asize)
 		return 0;
 
@@ -67,6 +71,9 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
 		return -1;
 	}
 
+	if (preserve_external && !buf->asize && buf->ptr != NULL && buf->size > 0)
+		memcpy(new_ptr, buf->ptr, min(buf->size, new_size));
+
 	buf->asize = new_size;
 	buf->ptr   = new_ptr;
 
@@ -78,11 +85,16 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
 	return 0;
 }
 
+int git_buf_grow(git_buf *buffer, size_t target_size)
+{
+	return git_buf_try_grow(buffer, target_size, true, true);
+}
+
 void git_buf_free(git_buf *buf)
 {
 	if (!buf) return;
 
-	if (buf->ptr != git_buf__initbuf && buf->ptr != git_buf__oom)
+	if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_buf__oom)
 		git__free(buf->ptr);
 
 	git_buf_init(buf, 0);
@@ -91,11 +103,15 @@ void git_buf_free(git_buf *buf)
 void git_buf_clear(git_buf *buf)
 {
 	buf->size = 0;
+
+	if (!buf->ptr)
+		buf->ptr = git_buf__initbuf;
+
 	if (buf->asize > 0)
 		buf->ptr[0] = '\0';
 }
 
-int git_buf_set(git_buf *buf, const char *data, size_t len)
+int git_buf_set(git_buf *buf, const void *data, size_t len)
 {
 	if (len == 0 || data == NULL) {
 		git_buf_clear(buf);
@@ -485,74 +501,3 @@ int git_buf_splice(
 	buf->ptr[buf->size] = '\0';
 	return 0;
 }
-
-/*
- * Public buffers API
- */
-
-void git_buffer_free(git_buffer *buffer)
-{
-	if (!buffer)
-		return;
-
-	if (buffer->ptr != NULL && buffer->available > 0)
-		git__free(buffer->ptr);
-
-	git__memzero(buffer, sizeof(*buffer));
-}
-
-static int git_buffer__resize(
-	git_buffer *buffer, size_t want_size, int preserve_data)
-{
-	int non_allocated_buffer = 0;
-	char *new_ptr;
-
-	assert(buffer);
-
-	/* check if buffer->ptr points to memory owned elsewhere */
-	non_allocated_buffer = (buffer->ptr != NULL && buffer->available == 0);
-
-	if (non_allocated_buffer && !want_size)
-		want_size = buffer->size;
-
-	if (buffer->available >= want_size)
-		return 0;
-
-	if (non_allocated_buffer) {
-		new_ptr = NULL;
-		if (want_size < buffer->size)
-			want_size = buffer->size;
-	} else {
-		new_ptr = buffer->ptr;
-	}
-
-	want_size = (want_size + 7) & ~7; /* round up to multiple of 8 */
-
-	new_ptr = git__realloc(new_ptr, want_size);
-	GITERR_CHECK_ALLOC(new_ptr);
-
-	if (non_allocated_buffer && preserve_data)
-		memcpy(new_ptr, buffer->ptr, buffer->size);
-
-	buffer->ptr       = new_ptr;
-	buffer->available = want_size;
-
-	return 0;
-}
-
-int git_buffer_resize(git_buffer *buffer, size_t want_size)
-{
-	return git_buffer__resize(buffer, want_size, true);
-}
-
-int git_buffer_copy(
-	git_buffer *buffer, const void *data, size_t datalen)
-{
-	if (git_buffer__resize(buffer, datalen + 1, false) < 0)
-		return -1;
-	memcpy(buffer->ptr, data, datalen);
-	buffer->ptr[datalen] = '\0';
-	buffer->size = datalen;
-	return 0;
-}
-
diff --git a/src/buffer.h b/src/buffer.h
index e07f291..4ca9d4d 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -12,16 +12,23 @@
 #include "git2/buffer.h"
 #include <stdarg.h>
 
-typedef struct {
-	char *ptr;
-	size_t asize, size;
-} git_buf;
+/* typedef struct {
+ *  	char   *ptr;
+ *  	size_t asize, size;
+ * } git_buf;
+ */
 
 extern char git_buf__initbuf[];
 extern char git_buf__oom[];
 
+/* Use to initialize buffer structure when git_buf is on stack */
 #define GIT_BUF_INIT { git_buf__initbuf, 0, 0 }
 
+GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
+{
+	return (buf->ptr != NULL && buf->asize > 0);
+}
+
 /**
  * Initialize a git_buf structure.
  *
@@ -33,27 +40,16 @@ extern void git_buf_init(git_buf *buf, size_t initial_size);
 /**
  * Attempt to grow the buffer to hold at least `target_size` bytes.
  *
- * If the allocation fails, this will return an error.  If mark_oom is true,
+ * If the allocation fails, this will return an error.  If `mark_oom` is true,
  * this will mark the buffer as invalid for future operations; if false,
  * existing buffer content will be preserved, but calling code must handle
- * that buffer was not expanded.
- */
-extern int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom);
-
-/**
- * Grow the buffer to hold at least `target_size` bytes.
- *
- * If the allocation fails, this will return an error and the buffer will be
- * marked as invalid for future operations, invaliding contents.
- *
- * @return 0 on success or -1 on failure
+ * that buffer was not expanded.  If `preserve_external` is true, then any
+ * existing data pointed to be `ptr` even if `asize` is zero will be copied
+ * into the newly allocated buffer.
  */
-GIT_INLINE(int) git_buf_grow(git_buf *buf, size_t target_size)
-{
-	return git_buf_try_grow(buf, target_size, true);
-}
+extern int git_buf_try_grow(
+	git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external);
 
-extern void git_buf_free(git_buf *buf);
 extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
 extern char *git_buf_detach(git_buf *buf);
 extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
@@ -82,7 +78,6 @@ GIT_INLINE(bool) git_buf_oom(const git_buf *buf)
  * return code of these functions and call them in a series then just call
  * git_buf_oom at the end.
  */
-int git_buf_set(git_buf *buf, const char *data, size_t len);
 int git_buf_sets(git_buf *buf, const char *string);
 int git_buf_putc(git_buf *buf, char c);
 int git_buf_put(git_buf *buf, const char *data, size_t len);
@@ -175,30 +170,4 @@ int git_buf_splice(
 	const char *data,
 	size_t nb_to_insert);
 
-
-GIT_INLINE(bool) git_buffer_is_allocated(const git_buffer *buffer)
-{
-	return (buffer->ptr != NULL && buffer->available > 0);
-}
-
-#define GIT_BUF_FROM_BUFFER(buffer) \
-	{ (buffer)->ptr, (buffer)->available, (buffer)->size }
-
-GIT_INLINE(void) git_buf_from_buffer(git_buf *buf, const git_buffer *buffer)
-{
-	buf->ptr   = buffer->ptr;
-	buf->size  = buffer->size;
-	buf->asize = buffer->available;
-}
-
-#define GIT_BUFFER_FROM_BUF(buf) \
-	{ (buf)->ptr, (buf)->size, (buf)->asize }
-
-GIT_INLINE(void) git_buffer_from_buf(git_buffer *buffer, const git_buf *buf)
-{
-	buffer->ptr  = buf->ptr;
-	buffer->size = buf->size;
-	buffer->available = buf->asize;
-}
-
 #endif
diff --git a/src/checkout.c b/src/checkout.c
index 7e79c9b..140544c 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -678,20 +678,19 @@ fail:
 
 static int buffer_to_file(
 	struct stat *st,
-	git_buffer *buffer,
+	git_buf *buf,
 	const char *path,
 	mode_t dir_mode,
 	int file_open_flags,
 	mode_t file_mode)
 {
 	int error;
-	git_buf buf = GIT_BUF_FROM_BUFFER(buffer);
 
 	if ((error = git_futils_mkpath2file(path, dir_mode)) < 0)
 		return error;
 
 	if ((error = git_futils_writebuffer(
-			&buf, path, file_open_flags, file_mode)) < 0)
+			buf, path, file_open_flags, file_mode)) < 0)
 		return error;
 
 	if (st != NULL && (error = p_stat(path, st)) < 0)
@@ -713,7 +712,7 @@ static int blob_content_to_file(
 {
 	int error = 0;
 	mode_t file_mode = opts->file_mode ? opts->file_mode : entry_filemode;
-	git_buffer out = GIT_BUFFER_INIT;
+	git_buf out = GIT_BUF_INIT;
 	git_filter_list *fl = NULL;
 
 	if (!opts->disable_filters && !git_blob_is_binary(blob))
@@ -731,7 +730,7 @@ static int blob_content_to_file(
 
 		st->st_mode = entry_filemode;
 
-		git_buffer_free(&out);
+		git_buf_free(&out);
 	}
 
 	return error;
diff --git a/src/config_file.c b/src/config_file.c
index bd4fa74..d0910a2 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -293,6 +293,8 @@ static int config_iterator_new(
 	diskfile_backend *b = (diskfile_backend *)backend;
 	git_config_file_iter *it = git__calloc(1, sizeof(git_config_file_iter));
 
+	GIT_UNUSED(b);
+
 	GITERR_CHECK_ALLOC(it);
 
 	it->parent.backend = backend;
diff --git a/src/crlf.c b/src/crlf.c
index f61a870..bde85ca 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -119,15 +119,12 @@ static int has_cr_in_index(const git_filter_source *src)
 
 static int crlf_apply_to_odb(
 	struct crlf_attrs *ca,
-	git_buffer *to,
-	const git_buffer *from,
+	git_buf *to,
+	const git_buf *from,
 	const git_filter_source *src)
 {
-	const git_buf from_buf = GIT_BUF_FROM_BUFFER(from);
-	git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
-
 	/* Empty file? Nothing to do */
-	if (!git_buf_len(&from_buf))
+	if (!git_buf_len(from))
 		return 0;
 
 	/* Heuristics to see if we can skip the conversion.
@@ -137,7 +134,7 @@ static int crlf_apply_to_odb(
 		git_buf_text_stats stats;
 
 		/* Check heuristics for binary vs text... */
-		if (git_buf_text_gather_stats(&stats, &from_buf, false))
+		if (git_buf_text_gather_stats(&stats, from, false))
 			return -1;
 
 		/*
@@ -162,13 +159,7 @@ static int crlf_apply_to_odb(
 	}
 
 	/* Actually drop the carriage returns */
-	if (git_buf_text_crlf_to_lf(&to_buf, &from_buf) < 0)
-		return -1;
-
-	/* Overwrite "to" buffer in case data was resized */
-	git_buffer_from_buf(to, &to_buf);
-
-	return 0;
+	return git_buf_text_crlf_to_lf(to, from);
 }
 
 static const char *line_ending(struct crlf_attrs *ca)
@@ -210,14 +201,12 @@ line_ending_error:
 }
 
 static int crlf_apply_to_workdir(
-	struct crlf_attrs *ca, git_buffer *to, const git_buffer *from)
+	struct crlf_attrs *ca, git_buf *to, const git_buf *from)
 {
-	const git_buf from_buf = GIT_BUF_FROM_BUFFER(from);
-	git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
 	const char *workdir_ending = NULL;
 
 	/* Empty file? Nothing to do. */
-	if (git_buf_len(&from_buf) == 0)
+	if (git_buf_len(from) == 0)
 		return 0;
 
 	/* Determine proper line ending */
@@ -229,22 +218,19 @@ static int crlf_apply_to_workdir(
 		if (ca->crlf_action == GIT_CRLF_GUESS && ca->auto_crlf)
 			return GIT_ENOTFOUND;
 
-		if (git_buf_find(&from_buf, '\r') < 0)
+		if (git_buf_find(from, '\r') < 0)
 			return GIT_ENOTFOUND;
 
-		if (git_buf_text_crlf_to_lf(&to_buf, &from_buf) < 0)
+		if (git_buf_text_crlf_to_lf(to, from) < 0)
 			return -1;
 	} else {
 		/* only other supported option is lf->crlf conversion */
 		assert(!strcmp("\r\n", workdir_ending));
 
-		if (git_buf_text_lf_to_crlf(&to_buf, &from_buf) < 0)
+		if (git_buf_text_lf_to_crlf(to, from) < 0)
 			return -1;
 	}
 
-	/* Overwrite "to" buffer in case data was resized */
-	git_buffer_from_buf(to, &to_buf);
-
 	return 0;
 }
 
@@ -297,10 +283,10 @@ static int crlf_check(
 }
 
 static int crlf_apply(
-	git_filter        *self,
-	void              **payload, /* may be read and/or set */
-	git_buffer        *to,
-	const git_buffer  *from,
+	git_filter    *self,
+	void         **payload, /* may be read and/or set */
+	git_buf       *to,
+	const git_buf *from,
 	const git_filter_source *src)
 {
 	/* initialize payload in case `check` was bypassed */
diff --git a/src/diff_file.c b/src/diff_file.c
index d02787c..5939ee8 100644
--- a/src/diff_file.c
+++ b/src/diff_file.c
@@ -327,11 +327,11 @@ static int diff_file_content_load_workdir_file(
 	}
 
 	if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
-		git_buffer in = GIT_BUFFER_FROM_BUF(&raw), out = GIT_BUFFER_INIT;
+		git_buf out = GIT_BUF_INIT;
 
-		error = git_filter_list_apply_to_data(&out, fl, &in);
+		error = git_filter_list_apply_to_data(&out, fl, &raw);
 
-		git_buffer_free(&in);
+		git_buf_free(&raw);
 
 		if (!error) {
 			fc->map.len  = out.size;
diff --git a/src/filter.c b/src/filter.c
index f206114..08467a6 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -549,23 +549,28 @@ int git_filter_list_push(
 }
 
 static int filter_list_out_buffer_from_raw(
-	git_buffer *out, const void *ptr, size_t size)
+	git_buf *out, const void *ptr, size_t size)
 {
-	if (git_buffer_is_allocated(out))
-		git_buffer_free(out);
+	if (git_buf_is_allocated(out))
+		git_buf_free(out);
+
+	if (!size) {
+		git_buf_init(out, 0);
+	} else {
+		out->ptr   = (char *)ptr;
+		out->asize = 0;
+		out->size  = size;
+	}
 
-	out->ptr  = (char *)ptr;
-	out->size = size;
-	out->available = 0;
 	return 0;
 }
 
 int git_filter_list_apply_to_data(
-	git_buffer *tgt, git_filter_list *fl, git_buffer *src)
+	git_buf *tgt, git_filter_list *fl, git_buf *src)
 {
 	int error = 0;
 	uint32_t i;
-	git_buffer *dbuffer[2], local = GIT_BUFFER_INIT;
+	git_buf *dbuffer[2], local = GIT_BUF_INIT;
 	unsigned int si = 0;
 
 	if (!fl)
@@ -575,8 +580,8 @@ int git_filter_list_apply_to_data(
 	dbuffer[1] = tgt;
 
 	/* if `src` buffer is reallocable, then use it, otherwise copy it */
-	if (!git_buffer_is_allocated(src)) {
-		if (git_buffer_copy(&local, src->ptr, src->size) < 0)
+	if (!git_buf_is_allocated(src)) {
+		if (git_buf_set(&local, src->ptr, src->size) < 0)
 			return -1;
 		dbuffer[0] = &local;
 	}
@@ -610,19 +615,16 @@ int git_filter_list_apply_to_data(
 	}
 
 	/* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */
-	if (si != 1) {
-		git_buffer sw = *dbuffer[1];
-		*dbuffer[1] = *dbuffer[0];
-		*dbuffer[0] = sw;
-	}
+	if (si != 1)
+		git_buf_swap(dbuffer[0], dbuffer[1]);
 
-	git_buffer_free(&local); /* don't leak if we allocated locally */
+	git_buf_free(&local); /* don't leak if we allocated locally */
 
 	return 0;
 }
 
 int git_filter_list_apply_to_file(
-	git_buffer *out,
+	git_buf *out,
 	git_filter_list *filters,
 	git_repository *repo,
 	const char *path)
@@ -634,11 +636,9 @@ int git_filter_list_apply_to_file(
 	if (!(error = git_path_join_unrooted(&abspath, path, base, NULL)) &&
 		!(error = git_futils_readbuffer(&raw, abspath.ptr)))
 	{
-		git_buffer in = GIT_BUFFER_FROM_BUF(&raw);
-
-		error = git_filter_list_apply_to_data(out, filters, &in);
+		error = git_filter_list_apply_to_data(out, filters, &raw);
 
-		git_buffer_free(&in);
+		git_buf_free(&raw);
 	}
 
 	git_buf_free(&abspath);
@@ -646,12 +646,12 @@ int git_filter_list_apply_to_file(
 }
 
 int git_filter_list_apply_to_blob(
-	git_buffer *out,
+	git_buf *out,
 	git_filter_list *filters,
 	git_blob *blob)
 {
-	git_buffer in = {
-		(char *)git_blob_rawcontent(blob), git_blob_rawsize(blob), 0
+	git_buf in = {
+		(char *)git_blob_rawcontent(blob), 0, git_blob_rawsize(blob)
 	};
 
 	if (filters)
diff --git a/src/ident.c b/src/ident.c
index aedb973..3ea9498 100644
--- a/src/ident.c
+++ b/src/ident.c
@@ -9,7 +9,7 @@
 #include "filter.h"
 #include "buffer.h"
 
-int ident_find_id(
+static int ident_find_id(
 	const char **id_start, const char **id_end, const char *start, size_t len)
 {
 	const char *found;
@@ -36,12 +36,11 @@ int ident_find_id(
 }
 
 static int ident_insert_id(
-	git_buffer *to, const git_buffer *from, const git_filter_source *src)
+	git_buf *to, const git_buf *from, const git_filter_source *src)
 {
 	char oid[GIT_OID_HEXSZ+1];
 	const char *id_start, *id_end, *from_end = from->ptr + from->size;
 	size_t need_size;
-	git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
 
 	/* replace $Id$ with blob id */
 
@@ -57,28 +56,23 @@ static int ident_insert_id(
 		5 /* "$Id: " */ + GIT_OID_HEXSZ + 1 /* "$" */ +
 		(size_t)(from_end - id_end);
 
-	if (git_buf_grow(&to_buf, need_size) < 0)
+	if (git_buf_grow(to, need_size) < 0)
 		return -1;
 
-	git_buf_set(&to_buf, from->ptr, (size_t)(id_start - from->ptr));
-	git_buf_put(&to_buf, "$Id: ", 5);
-	git_buf_put(&to_buf, oid, GIT_OID_HEXSZ);
-	git_buf_putc(&to_buf, '$');
-	git_buf_put(&to_buf, id_end, (size_t)(from_end - id_end));
+	git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr));
+	git_buf_put(to, "$Id: ", 5);
+	git_buf_put(to, oid, GIT_OID_HEXSZ);
+	git_buf_putc(to, '$');
+	git_buf_put(to, id_end, (size_t)(from_end - id_end));
 
-	if (git_buf_oom(&to_buf))
-		return -1;
-
-	git_buffer_from_buf(to, &to_buf);
-	return 0;
+	return git_buf_oom(to) ? -1 : 0;
 }
 
 static int ident_remove_id(
-	git_buffer *to, const git_buffer *from)
+	git_buf *to, const git_buf *from)
 {
 	const char *id_start, *id_end, *from_end = from->ptr + from->size;
 	size_t need_size;
-	git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
 
 	if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0)
 		return GIT_ENOTFOUND;
@@ -86,25 +80,21 @@ static int ident_remove_id(
 	need_size = (size_t)(id_start - from->ptr) +
 		4 /* "$Id$" */ + (size_t)(from_end - id_end);
 
-	if (git_buf_grow(&to_buf, need_size) < 0)
+	if (git_buf_grow(to, need_size) < 0)
 		return -1;
 
-	git_buf_set(&to_buf, from->ptr, (size_t)(id_start - from->ptr));
-	git_buf_put(&to_buf, "$Id$", 4);
-	git_buf_put(&to_buf, id_end, (size_t)(from_end - id_end));
+	git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr));
+	git_buf_put(to, "$Id$", 4);
+	git_buf_put(to, id_end, (size_t)(from_end - id_end));
 
-	if (git_buf_oom(&to_buf))
-		return -1;
-
-	git_buffer_from_buf(to, &to_buf);
-	return 0;
+	return git_buf_oom(to) ? -1 : 0;
 }
 
 static int ident_apply(
-	git_filter        *self,
-	void              **payload,
-	git_buffer        *to,
-	const git_buffer  *from,
+	git_filter     *self,
+	void          **payload,
+	git_buf        *to,
+	const git_buf  *from,
 	const git_filter_source *src)
 {
 	GIT_UNUSED(self); GIT_UNUSED(payload);
diff --git a/src/odb.c b/src/odb.c
index b71b038..eef9748 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -192,16 +192,16 @@ int git_odb__hashfd_filtered(
 	 */
 
 	if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
-		git_buffer pre = GIT_BUFFER_FROM_BUF(&raw), post = GIT_BUFFER_INIT;
+		git_buf post = GIT_BUF_INIT;
 
-		error = git_filter_list_apply_to_data(&post, fl, &pre);
+		error = git_filter_list_apply_to_data(&post, fl, &raw);
 
-		git_buffer_free(&pre);
+		git_buf_free(&raw);
 
 		if (!error)
 			error = git_odb_hash(out, post.ptr, post.size, type);
 
-		git_buffer_free(&post);
+		git_buf_free(&post);
 	}
 
 	return error;
diff --git a/src/path.c b/src/path.c
index 56b0b49..42b3d6f 100644
--- a/src/path.c
+++ b/src/path.c
@@ -565,7 +565,7 @@ static bool _check_dir_contents(
 	size_t sub_size = strlen(sub);
 
 	/* leave base valid even if we could not make space for subdir */
-	if (git_buf_try_grow(dir, dir_size + sub_size + 2, false) < 0)
+	if (git_buf_try_grow(dir, dir_size + sub_size + 2, false, false) < 0)
 		return false;
 
 	/* save excursion */
diff --git a/src/util.c b/src/util.c
index d0c326a..1517823 100644
--- a/src/util.c
+++ b/src/util.c
@@ -679,6 +679,9 @@ size_t git__unescape(char *str)
 {
 	char *scan, *pos = str;
 
+	if (!str)
+		return 0;
+
 	for (scan = str; *scan; pos++, scan++) {
 		if (*scan == '\\' && *(scan + 1) != '\0')
 			scan++; /* skip '\' but include next char */
diff --git a/tests-clar/filter/blob.c b/tests-clar/filter/blob.c
index c265ed6..916721e 100644
--- a/tests-clar/filter/blob.c
+++ b/tests-clar/filter/blob.c
@@ -23,7 +23,7 @@ void test_filter_blob__cleanup(void)
 void test_filter_blob__all_crlf(void)
 {
 	git_blob *blob;
-	git_buffer buf = GIT_BUFFER_INIT;
+	git_buf buf = { 0 };
 
 	cl_git_pass(git_revparse_single(
 		(git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */
@@ -43,7 +43,7 @@ void test_filter_blob__all_crlf(void)
 
 	cl_assert_equal_s(ALL_CRLF_TEXT_AS_LF, buf.ptr);
 
-	git_buffer_free(&buf);
+	git_buf_free(&buf);
 	git_blob_free(blob);
 }
 
@@ -51,7 +51,7 @@ void test_filter_blob__ident(void)
 {
 	git_oid id;
 	git_blob *blob;
-	git_buffer buf = GIT_BUFFER_INIT;
+	git_buf buf = { 0 };
 
 	cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
 	cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident"));
@@ -78,4 +78,7 @@ void test_filter_blob__ident(void)
 	cl_assert_equal_s(
 		"Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845$\nGoes there\n", buf.ptr);
 
+	git_buf_free(&buf);
+	git_blob_free(blob);
+
 }
diff --git a/tests-clar/filter/crlf.c b/tests-clar/filter/crlf.c
index 098a85d..ccd7ef4 100644
--- a/tests-clar/filter/crlf.c
+++ b/tests-clar/filter/crlf.c
@@ -20,7 +20,7 @@ void test_filter_crlf__to_worktree(void)
 {
 	git_filter_list *fl;
 	git_filter *crlf;
-	git_buffer in = GIT_BUFFER_INIT, out = GIT_BUFFER_INIT;
+	git_buf in = { 0 }, out = { 0 };
 
 	{
 		git_config *cfg;
@@ -48,14 +48,14 @@ void test_filter_crlf__to_worktree(void)
 #endif
 
 	git_filter_list_free(fl);
-	git_buffer_free(&out);
+	git_buf_free(&out);
 }
 
 void test_filter_crlf__to_odb(void)
 {
 	git_filter_list *fl;
 	git_filter *crlf;
-	git_buffer in = GIT_BUFFER_INIT, out = GIT_BUFFER_INIT;
+	git_buf in = { 0 }, out = { 0 };
 
 	{
 		git_config *cfg;
@@ -79,5 +79,5 @@ void test_filter_crlf__to_odb(void)
 	cl_assert_equal_s("Some text\nRight here\n", out.ptr);
 
 	git_filter_list_free(fl);
-	git_buffer_free(&out);
+	git_buf_free(&out);
 }
diff --git a/tests-clar/filter/ident.c b/tests-clar/filter/ident.c
index 55774fb..2c8e6ab 100644
--- a/tests-clar/filter/ident.c
+++ b/tests-clar/filter/ident.c
@@ -20,7 +20,7 @@ static void add_blob_and_filter(
 {
 	git_oid id;
 	git_blob *blob;
-	git_buffer out = GIT_BUFFER_INIT;
+	git_buf out = { 0 };
 
 	cl_git_mkfile("crlf/identtest", data);
 	cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "identtest"));
@@ -31,7 +31,7 @@ static void add_blob_and_filter(
 	cl_assert_equal_s(expected, out.ptr);
 
 	git_blob_free(blob);
-	git_buffer_free(&out);
+	git_buf_free(&out);
 }
 
 void test_filter_ident__to_worktree(void)
diff --git a/tests-clar/object/blob/filter.c b/tests-clar/object/blob/filter.c
index 1e82b69..6dc7800 100644
--- a/tests-clar/object/blob/filter.c
+++ b/tests-clar/object/blob/filter.c
@@ -104,7 +104,7 @@ void test_object_blob_filter__to_odb(void)
 	git_config *cfg;
 	int i;
 	git_blob *blob;
-	git_buffer out = GIT_BUFFER_INIT;
+	git_buf out = GIT_BUF_INIT;
 
 	cl_git_pass(git_repository_config(&cfg, g_repo));
 	cl_assert(cfg);
@@ -129,7 +129,7 @@ void test_object_blob_filter__to_odb(void)
 	}
 
 	git_filter_list_free(fl);
-	git_buffer_free(&out);
+	git_buf_free(&out);
 	git_config_free(cfg);
 }