Fix tree-entry attribute convertion (fix corrupted trees) Magic constant replaced by direct to-string covertion because of: 1) with value length 6 (040000 - subtree) final tree will be corrupted; 2) for wrong values length <6 final tree will be corrupted too.
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
diff --git a/src/tree.c b/src/tree.c
index 64f81d7..b7daf39 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -424,7 +424,8 @@ int git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *b
if (entry->removed)
continue;
- size += (entry->attr > 0x7FF) ? 7 : 6;
+ snprintf(filemode, sizeof(filemode), "%o ", entry->attr);
+ size += strlen(filemode);
size += entry->filename_len + 1;
size += GIT_OID_RAWSZ;
}
diff --git a/tests/t09-tree.c b/tests/t09-tree.c
index 9b39dfd..af992fd 100644
--- a/tests/t09-tree.c
+++ b/tests/t09-tree.c
@@ -32,6 +32,7 @@ static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7";
static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
+static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
#if 0
static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
@@ -159,6 +160,44 @@ BEGIN_TEST(write2, "write a tree from a memory")
close_temp_repo(repo);
END_TEST
+BEGIN_TEST(write3, "write a hierarchical tree from a memory")
+ git_repository *repo;
+ git_treebuilder *builder;
+ git_tree *tree;
+ git_oid id, bid, subtree_id, id2, id3;
+ git_oid id_hiearar;
+
+ must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
+ git_oid_mkstr(&id, first_tree);
+ git_oid_mkstr(&id2, second_tree);
+ git_oid_mkstr(&id3, third_tree);
+ git_oid_mkstr(&bid, blob_oid);
+
+ //create subtree
+ must_pass(git_treebuilder_create(&builder, NULL));
+ must_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
+ must_pass(git_treebuilder_write(&subtree_id,repo,builder));
+ git_treebuilder_free(builder);
+
+ // create parent tree
+ must_pass(git_tree_lookup(&tree, repo, &id));
+ must_pass(git_treebuilder_create(&builder, tree));
+ must_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000));
+ must_pass(git_treebuilder_write(&id_hiearar,repo,builder));
+ git_treebuilder_free(builder);
+ git_tree_close(tree);
+
+ must_be_true(git_oid_cmp(&id_hiearar, &id3) == 0);
+
+ // check data is correct
+ must_pass(git_tree_lookup(&tree, repo, &id_hiearar));
+ must_be_true(2 == git_tree_entrycount(tree));
+ git_tree_close(tree);
+
+ close_temp_repo(repo);
+
+END_TEST
+
BEGIN_SUITE(tree)
//ADD_TEST(print0);
ADD_TEST(read0);
@@ -166,5 +205,6 @@ BEGIN_SUITE(tree)
//ADD_TEST(write0);
//ADD_TEST(write1);
ADD_TEST(write2);
+ ADD_TEST(write3);
END_SUITE