fix git_treebuilder_insert probrem. couldn't add new entry when inserting new one with `git_treebuilder_insert`.
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
diff --git a/src/tree.c b/src/tree.c
index 693ed1c..b474d39 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -364,7 +364,7 @@ int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, con
git_oid_cpy(&entry->oid, id);
entry->attr = attributes;
- if (pos != GIT_ENOTFOUND) {
+ if (pos == GIT_ENOTFOUND) {
if (git_vector_insert(&bld->entries, entry) < 0)
return GIT_ENOMEM;
}
diff --git a/tests/t09-tree.c b/tests/t09-tree.c
index e4252db..1ccce8e 100644
--- a/tests/t09-tree.c
+++ b/tests/t09-tree.c
@@ -29,6 +29,10 @@
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
+static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
+static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7";
+static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
+
#if 0
static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
{
@@ -126,11 +130,36 @@ BEGIN_TEST(write0, "write a tree from an index")
END_TEST
#endif
+BEGIN_TEST(write2, "write a tree from a memory")
+ git_repository *repo;
+ git_index *index;
+ git_treebuilder *builder;
+ git_tree *tree;
+ git_oid id;
+ git_oid bid;
+ git_oid rid;
+
+ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+ git_oid_mkstr(&id, first_tree);
+ git_oid_mkstr(&bid, blob_oid);
+
+ //create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
+ must_pass(git_tree_lookup(&tree, repo, &id));
+ must_pass(git_treebuilder_create(&builder, tree));
+ must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
+ must_pass(git_treebuilder_write(&rid,repo,builder));
+
+ char out[41];
+ git_oid_to_string(out,41,&rid);
+ must_pass(strcmp(out,second_tree));
+END_TEST
+
BEGIN_SUITE(tree)
//ADD_TEST(print0);
ADD_TEST(read0);
ADD_TEST(read1);
//ADD_TEST(write0);
//ADD_TEST(write1);
+ ADD_TEST(write2);
END_SUITE