Skip to content

Commit 4f4b113

Browse files
authored
Merge pull request libgit2#5815 from libgit2/ethomson/treebuilder_write
tree: deprecate `git_treebuilder_write_with_buffer`
2 parents a0fca80 + 85af7f2 commit 4f4b113

File tree

5 files changed

+99
-70
lines changed

5 files changed

+99
-70
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ jobs:
123123
SKIP_SSH_TESTS: true
124124
SKIP_NEGOTIATE_TESTS: true
125125
ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10
126+
UBSAN_OPTIONS: print_stacktrace=1
126127
os: ubuntu-latest
127128
- # Focal, Clang 10, OpenSSL, UndefinedBehaviorSanitizer
128129
container:
@@ -135,6 +136,7 @@ jobs:
135136
SKIP_SSH_TESTS: true
136137
SKIP_NEGOTIATE_TESTS: true
137138
ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10
139+
UBSAN_OPTIONS: print_stacktrace=1
138140
os: ubuntu-latest
139141
- # Focal, Clang 10, OpenSSL, ThreadSanitizer
140142
container:
@@ -147,6 +149,7 @@ jobs:
147149
SKIP_SSH_TESTS: true
148150
SKIP_NEGOTIATE_TESTS: true
149151
ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10
152+
UBSAN_OPTIONS: print_stacktrace=1
150153
TSAN_OPTIONS: suppressions=/home/libgit2/source/script/thread-sanitizer.supp second_deadlock_stack=1
151154
os: ubuntu-latest
152155
- # macOS
@@ -242,6 +245,7 @@ jobs:
242245
-e SKIP_NEGOTIATE_TESTS \
243246
-e SKIP_SSH_TESTS \
244247
-e TSAN_OPTIONS \
248+
-e UBSAN_OPTIONS \
245249
${{ env.docker-registry-container-sha }} \
246250
/bin/bash -c "mkdir build && cd build && ../ci/build.sh && ../ci/test.sh"
247251
else

include/git2/deprecated.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,33 @@ GIT_EXTERN(int) git_blob_filtered_content(
119119

120120
/**@}*/
121121

122+
/** @name Deprecated Tree Functions
123+
*
124+
* These functions are retained for backward compatibility. The
125+
* newer versions of these functions and values should be preferred
126+
* in all new code.
127+
*
128+
* There is no plan to remove these backward compatibility values at
129+
* this time.
130+
*/
131+
/**@{*/
132+
133+
/**
134+
* Write the contents of the tree builder as a tree object.
135+
* This is an alias of `git_treebuilder_write` and is preserved
136+
* for backward compatibility.
137+
*
138+
* This function is deprecated, but there is no plan to remove this
139+
* function at this time.
140+
*
141+
* @deprecated Use git_treebuilder_write
142+
* @see git_treebuilder_write
143+
*/
144+
GIT_EXTERN(int) git_treebuilder_write_with_buffer(
145+
git_oid *oid, git_treebuilder *bld, git_buf *tree);
146+
147+
/**@}*/
148+
122149
/** @name Deprecated Buffer Functions
123150
*
124151
* These functions and enumeration values are retained for backward

include/git2/tree.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -378,20 +378,6 @@ GIT_EXTERN(int) git_treebuilder_filter(
378378
GIT_EXTERN(int) git_treebuilder_write(
379379
git_oid *id, git_treebuilder *bld);
380380

381-
/**
382-
* Write the contents of the tree builder as a tree object
383-
* using a shared git_buf.
384-
*
385-
* @see git_treebuilder_write
386-
*
387-
* @param oid Pointer to store the OID of the newly written tree
388-
* @param bld Tree builder to write
389-
* @param tree Shared buffer for writing the tree. Will be grown as necessary.
390-
* @return 0 or an error code
391-
*/
392-
GIT_EXTERN(int) git_treebuilder_write_with_buffer(
393-
git_oid *oid, git_treebuilder *bld, git_buf *tree);
394-
395381
/** Callback for the tree traversal method */
396382
typedef int GIT_CALLBACK(git_treewalk_cb)(
397383
const char *root, const git_tree_entry *entry, void *payload);

src/tree.c

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,56 @@ static int check_entry(git_repository *repo, const char *filename, const git_oid
492492
return 0;
493493
}
494494

495+
static int git_treebuilder__write_with_buffer(
496+
git_oid *oid,
497+
git_treebuilder *bld,
498+
git_buf *buf)
499+
{
500+
int error = 0;
501+
size_t i, entrycount;
502+
git_odb *odb;
503+
git_tree_entry *entry;
504+
git_vector entries = GIT_VECTOR_INIT;
505+
506+
git_buf_clear(buf);
507+
508+
entrycount = git_strmap_size(bld->map);
509+
if ((error = git_vector_init(&entries, entrycount, entry_sort_cmp)) < 0)
510+
goto out;
511+
512+
if (buf->asize == 0 &&
513+
(error = git_buf_grow(buf, entrycount * 72)) < 0)
514+
goto out;
515+
516+
git_strmap_foreach_value(bld->map, entry, {
517+
if ((error = git_vector_insert(&entries, entry)) < 0)
518+
goto out;
519+
});
520+
521+
git_vector_sort(&entries);
522+
523+
for (i = 0; i < entries.length && !error; ++i) {
524+
entry = git_vector_get(&entries, i);
525+
526+
git_buf_printf(buf, "%o ", entry->attr);
527+
git_buf_put(buf, entry->filename, entry->filename_len + 1);
528+
git_buf_put(buf, (char *)entry->oid->id, GIT_OID_RAWSZ);
529+
530+
if (git_buf_oom(buf)) {
531+
error = -1;
532+
goto out;
533+
}
534+
}
535+
536+
if ((error = git_repository_odb__weakptr(&odb, bld->repo)) == 0)
537+
error = git_odb_write(oid, odb, buf->ptr, buf->size, GIT_OBJECT_TREE);
538+
539+
out:
540+
git_vector_free(&entries);
541+
542+
return error;
543+
}
544+
495545
static int append_entry(
496546
git_treebuilder *bld,
497547
const char *filename,
@@ -610,7 +660,7 @@ static int write_tree(
610660
}
611661
}
612662

613-
if (git_treebuilder_write_with_buffer(oid, bld, shared_buf) < 0)
663+
if (git_treebuilder__write_with_buffer(oid, bld, shared_buf) < 0)
614664
goto on_error;
615665

616666
git_treebuilder_free(bld);
@@ -785,63 +835,10 @@ int git_treebuilder_remove(git_treebuilder *bld, const char *filename)
785835

786836
int git_treebuilder_write(git_oid *oid, git_treebuilder *bld)
787837
{
788-
int error;
789-
git_buf buffer = GIT_BUF_INIT;
790-
791-
error = git_treebuilder_write_with_buffer(oid, bld, &buffer);
792-
793-
git_buf_dispose(&buffer);
794-
return error;
795-
}
796-
797-
int git_treebuilder_write_with_buffer(git_oid *oid, git_treebuilder *bld, git_buf *tree)
798-
{
799-
int error = 0;
800-
size_t i, entrycount;
801-
git_odb *odb;
802-
git_tree_entry *entry;
803-
git_vector entries = GIT_VECTOR_INIT;
804-
838+
GIT_ASSERT_ARG(oid);
805839
GIT_ASSERT_ARG(bld);
806-
GIT_ASSERT_ARG(tree);
807-
808-
git_buf_clear(tree);
809-
810-
entrycount = git_strmap_size(bld->map);
811-
if ((error = git_vector_init(&entries, entrycount, entry_sort_cmp)) < 0)
812-
goto out;
813-
814-
if (tree->asize == 0 &&
815-
(error = git_buf_grow(tree, entrycount * 72)) < 0)
816-
goto out;
817-
818-
git_strmap_foreach_value(bld->map, entry, {
819-
if ((error = git_vector_insert(&entries, entry)) < 0)
820-
goto out;
821-
});
822-
823-
git_vector_sort(&entries);
824840

825-
for (i = 0; i < entries.length && !error; ++i) {
826-
entry = git_vector_get(&entries, i);
827-
828-
git_buf_printf(tree, "%o ", entry->attr);
829-
git_buf_put(tree, entry->filename, entry->filename_len + 1);
830-
git_buf_put(tree, (char *)entry->oid->id, GIT_OID_RAWSZ);
831-
832-
if (git_buf_oom(tree)) {
833-
error = -1;
834-
goto out;
835-
}
836-
}
837-
838-
if ((error = git_repository_odb__weakptr(&odb, bld->repo)) == 0)
839-
error = git_odb_write(oid, odb, tree->ptr, tree->size, GIT_OBJECT_TREE);
840-
841-
out:
842-
git_vector_free(&entries);
843-
844-
return error;
841+
return git_treebuilder__write_with_buffer(oid, bld, &bld->write_cache);
845842
}
846843

847844
int git_treebuilder_filter(
@@ -882,6 +879,7 @@ void git_treebuilder_free(git_treebuilder *bld)
882879
if (bld == NULL)
883880
return;
884881

882+
git_buf_dispose(&bld->write_cache);
885883
git_treebuilder_clear(bld);
886884
git_strmap_free(bld->map);
887885
git__free(bld);
@@ -1306,3 +1304,16 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli
13061304
git_vector_free(&entries);
13071305
return error;
13081306
}
1307+
1308+
/* Deprecated Functions */
1309+
1310+
#ifndef GIT_DEPRECATE_HARD
1311+
1312+
int git_treebuilder_write_with_buffer(git_oid *oid, git_treebuilder *bld, git_buf *buf)
1313+
{
1314+
GIT_UNUSED(buf);
1315+
1316+
return git_treebuilder_write(oid, bld);
1317+
}
1318+
1319+
#endif

src/tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct git_tree {
3232
struct git_treebuilder {
3333
git_repository *repo;
3434
git_strmap *map;
35+
git_buf write_cache;
3536
};
3637

3738
GIT_INLINE(bool) git_tree_entry__is_tree(const struct git_tree_entry *e)

0 commit comments

Comments
 (0)