tree: deprecate `git_treebuilder_write_with_buffer` The function `git_treebuilder_write_with_buffer` is unnecessary; it is used internally as part of treebuilder writing, but it has little use to external callers. For callers that repeatedly write a treebuilder, we can supply them with a buffer in the treebuilder struct instead of recreating it. For ourselves, when we want a single buffer in our write loop, we can use an internal function.
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
diff --git a/include/git2/deprecated.h b/include/git2/deprecated.h
index d18fffc..37857f8 100644
--- a/include/git2/deprecated.h
+++ b/include/git2/deprecated.h
@@ -119,6 +119,33 @@ GIT_EXTERN(int) git_blob_filtered_content(
/**@}*/
+/** @name Deprecated Tree Functions
+ *
+ * These functions are retained for backward compatibility. The
+ * newer versions of these functions and values should be preferred
+ * in all new code.
+ *
+ * There is no plan to remove these backward compatibility values at
+ * this time.
+ */
+/**@{*/
+
+/**
+ * Write the contents of the tree builder as a tree object.
+ * This is an alias of `git_treebuilder_write` and is preserved
+ * for backward compatibility.
+ *
+ * This function is deprecated, but there is no plan to remove this
+ * function at this time.
+ *
+ * @deprecated Use git_treebuilder_write
+ * @see git_treebuilder_write
+ */
+GIT_EXTERN(int) git_treebuilder_write_with_buffer(
+ git_oid *oid, git_treebuilder *bld, git_buf *tree);
+
+/**@}*/
+
/** @name Deprecated Buffer Functions
*
* These functions and enumeration values are retained for backward
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 1a8e155..d7545ac 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -378,20 +378,6 @@ GIT_EXTERN(int) git_treebuilder_filter(
GIT_EXTERN(int) git_treebuilder_write(
git_oid *id, git_treebuilder *bld);
-/**
- * Write the contents of the tree builder as a tree object
- * using a shared git_buf.
- *
- * @see git_treebuilder_write
- *
- * @param oid Pointer to store the OID of the newly written tree
- * @param bld Tree builder to write
- * @param tree Shared buffer for writing the tree. Will be grown as necessary.
- * @return 0 or an error code
- */
-GIT_EXTERN(int) git_treebuilder_write_with_buffer(
- git_oid *oid, git_treebuilder *bld, git_buf *tree);
-
/** Callback for the tree traversal method */
typedef int GIT_CALLBACK(git_treewalk_cb)(
const char *root, const git_tree_entry *entry, void *payload);
diff --git a/src/tree.c b/src/tree.c
index ba6383e..5abcc9d 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -492,6 +492,56 @@ static int check_entry(git_repository *repo, const char *filename, const git_oid
return 0;
}
+static int git_treebuilder__write_with_buffer(
+ git_oid *oid,
+ git_treebuilder *bld,
+ git_buf *buf)
+{
+ int error = 0;
+ size_t i, entrycount;
+ git_odb *odb;
+ git_tree_entry *entry;
+ git_vector entries = GIT_VECTOR_INIT;
+
+ git_buf_clear(buf);
+
+ entrycount = git_strmap_size(bld->map);
+ if ((error = git_vector_init(&entries, entrycount, entry_sort_cmp)) < 0)
+ goto out;
+
+ if (buf->asize == 0 &&
+ (error = git_buf_grow(buf, entrycount * 72)) < 0)
+ goto out;
+
+ git_strmap_foreach_value(bld->map, entry, {
+ if ((error = git_vector_insert(&entries, entry)) < 0)
+ goto out;
+ });
+
+ git_vector_sort(&entries);
+
+ for (i = 0; i < entries.length && !error; ++i) {
+ entry = git_vector_get(&entries, i);
+
+ git_buf_printf(buf, "%o ", entry->attr);
+ git_buf_put(buf, entry->filename, entry->filename_len + 1);
+ git_buf_put(buf, (char *)entry->oid->id, GIT_OID_RAWSZ);
+
+ if (git_buf_oom(buf)) {
+ error = -1;
+ goto out;
+ }
+ }
+
+ if ((error = git_repository_odb__weakptr(&odb, bld->repo)) == 0)
+ error = git_odb_write(oid, odb, buf->ptr, buf->size, GIT_OBJECT_TREE);
+
+out:
+ git_vector_free(&entries);
+
+ return error;
+}
+
static int append_entry(
git_treebuilder *bld,
const char *filename,
@@ -610,7 +660,7 @@ static int write_tree(
}
}
- if (git_treebuilder_write_with_buffer(oid, bld, shared_buf) < 0)
+ if (git_treebuilder__write_with_buffer(oid, bld, shared_buf) < 0)
goto on_error;
git_treebuilder_free(bld);
@@ -785,63 +835,10 @@ int git_treebuilder_remove(git_treebuilder *bld, const char *filename)
int git_treebuilder_write(git_oid *oid, git_treebuilder *bld)
{
- int error;
- git_buf buffer = GIT_BUF_INIT;
-
- error = git_treebuilder_write_with_buffer(oid, bld, &buffer);
-
- git_buf_dispose(&buffer);
- return error;
-}
-
-int git_treebuilder_write_with_buffer(git_oid *oid, git_treebuilder *bld, git_buf *tree)
-{
- int error = 0;
- size_t i, entrycount;
- git_odb *odb;
- git_tree_entry *entry;
- git_vector entries = GIT_VECTOR_INIT;
-
+ GIT_ASSERT_ARG(oid);
GIT_ASSERT_ARG(bld);
- GIT_ASSERT_ARG(tree);
-
- git_buf_clear(tree);
-
- entrycount = git_strmap_size(bld->map);
- if ((error = git_vector_init(&entries, entrycount, entry_sort_cmp)) < 0)
- goto out;
-
- if (tree->asize == 0 &&
- (error = git_buf_grow(tree, entrycount * 72)) < 0)
- goto out;
-
- git_strmap_foreach_value(bld->map, entry, {
- if ((error = git_vector_insert(&entries, entry)) < 0)
- goto out;
- });
-
- git_vector_sort(&entries);
- for (i = 0; i < entries.length && !error; ++i) {
- entry = git_vector_get(&entries, i);
-
- git_buf_printf(tree, "%o ", entry->attr);
- git_buf_put(tree, entry->filename, entry->filename_len + 1);
- git_buf_put(tree, (char *)entry->oid->id, GIT_OID_RAWSZ);
-
- if (git_buf_oom(tree)) {
- error = -1;
- goto out;
- }
- }
-
- if ((error = git_repository_odb__weakptr(&odb, bld->repo)) == 0)
- error = git_odb_write(oid, odb, tree->ptr, tree->size, GIT_OBJECT_TREE);
-
-out:
- git_vector_free(&entries);
-
- return error;
+ return git_treebuilder__write_with_buffer(oid, bld, &bld->write_cache);
}
int git_treebuilder_filter(
@@ -882,6 +879,7 @@ void git_treebuilder_free(git_treebuilder *bld)
if (bld == NULL)
return;
+ git_buf_dispose(&bld->write_cache);
git_treebuilder_clear(bld);
git_strmap_free(bld->map);
git__free(bld);
@@ -1306,3 +1304,16 @@ cleanup:
git_vector_free(&entries);
return error;
}
+
+/* Deprecated Functions */
+
+#ifndef GIT_DEPRECATE_HARD
+
+int git_treebuilder_write_with_buffer(git_oid *oid, git_treebuilder *bld, git_buf *buf)
+{
+ GIT_UNUSED(buf);
+
+ return git_treebuilder_write(oid, bld);
+}
+
+#endif
diff --git a/src/tree.h b/src/tree.h
index 973ba15..2f3027b 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -32,6 +32,7 @@ struct git_tree {
struct git_treebuilder {
git_repository *repo;
git_strmap *map;
+ git_buf write_cache;
};
GIT_INLINE(bool) git_tree_entry__is_tree(const struct git_tree_entry *e)