treebuilder: set the attributes before sorting and inserting We need to set the attributes before we try to insert it into the vector, as the comparison function needs to know whether the entry is a tree or not.
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
diff --git a/src/tree.c b/src/tree.c
index b64efe4..4ddb26b 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -667,10 +667,16 @@ int git_treebuilder_insert(
entry->removed = 0;
bld->entrycount++;
}
+
+ entry->attr = filemode;
+ git_oid_cpy(&entry->oid, id);
} else {
entry = alloc_entry(filename);
GITERR_CHECK_ALLOC(entry);
+ entry->attr = filemode;
+ git_oid_cpy(&entry->oid, id);
+
if (git_vector_insert_sorted(&bld->entries, entry, NULL) < 0) {
git__free(entry);
return -1;
@@ -679,9 +685,6 @@ int git_treebuilder_insert(
bld->entrycount++;
}
- git_oid_cpy(&entry->oid, id);
- entry->attr = filemode;
-
if (entry_out)
*entry_out = entry;
diff --git a/tests/object/tree/write.c b/tests/object/tree/write.c
index 45356e8..ef7adaf 100644
--- a/tests/object/tree/write.c
+++ b/tests/object/tree/write.c
@@ -396,3 +396,64 @@ void test_object_tree_write__cruel_paths(void)
git_tree_free(tree);
}
+
+void test_object_tree_write__from_tree_git_sorting(void)
+{
+ git_index *index;
+ git_odb *db;
+ git_treebuilder *bld;
+ git_oid first_tree = {{0}}, second_tree = {{0}}, subtree_id = {{0}}, second_tree_bld = {{0}};
+ git_tree *tree;
+ git_index_entry entry = {{0}};
+ const char *foo = "foo\n";
+ const char *bar = "bar\n";
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_clear(index));
+ cl_git_pass(git_repository_odb(&db, g_repo));
+
+ cl_git_pass(git_odb_write(&entry.id, db, foo, strlen(foo), GIT_OBJ_BLOB));
+ entry.mode = GIT_FILEMODE_BLOB;
+ entry.path = "foo.txt";
+
+ cl_git_pass(git_index_add(index, &entry));
+ cl_git_pass(git_index_write_tree(&first_tree, index));
+
+ /* Clear the index and re-add so we don't use any tree caches */
+ cl_git_pass(git_index_clear(index));
+ cl_git_pass(git_index_add(index, &entry));
+
+ cl_git_pass(git_odb_write(&entry.id, db, bar, strlen(bar), GIT_OBJ_BLOB));
+ entry.mode = GIT_FILEMODE_BLOB;
+ entry.path = "foo/bar";
+
+ cl_git_pass(git_index_add(index, &entry));
+ cl_git_pass(git_index_write_tree(&second_tree, index));
+
+ /*
+ * The index has now written the tree from scratch. We will
+ * create a builder taking the first tree as a starting point
+ * and create the second tree.
+ */
+
+ cl_git_pass(git_treebuilder_create(&bld, NULL));
+
+ cl_git_pass(git_treebuilder_insert(NULL, bld, "bar", &entry.id, entry.mode));
+ cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, bld));
+ git_treebuilder_free(bld);
+
+ /* assert subtree_id is the same as the one from the index */
+
+ cl_git_pass(git_tree_lookup(&tree, g_repo, &first_tree));
+ cl_git_pass(git_treebuilder_create(&bld, tree));
+
+ cl_git_pass(git_treebuilder_insert(NULL, bld, "foo", &subtree_id, GIT_FILEMODE_TREE));
+ cl_git_pass(git_treebuilder_write(&second_tree_bld, g_repo, bld));
+ git_treebuilder_free(bld);
+
+ cl_assert(!git_oid_cmp(&second_tree, &second_tree_bld));
+
+ git_tree_free(tree);
+ git_odb_free(db);
+ git_index_free(index);
+}