Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c756448

Browse files
committed
Merge pull request libgit2#3797 from libgit2/cmn/remove-single-entry
tree: handle removal of all entries in the updater
2 parents c91a1dc + a2cb471 commit c756448

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/tree.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,15 @@ static int create_popped_tree(tree_stack_entry *current, tree_stack_entry *poppe
10931093
git_oid new_tree;
10941094

10951095
git_tree_free(popped->tree);
1096+
1097+
/* If the tree would be empty, remove it from the one higher up */
1098+
if (git_treebuilder_entrycount(popped->bld) == 0) {
1099+
git_treebuilder_free(popped->bld);
1100+
error = git_treebuilder_remove(current->bld, popped->name);
1101+
git__free(popped->name);
1102+
return error;
1103+
}
1104+
10961105
error = git_treebuilder_write(&new_tree, popped->bld);
10971106
git_treebuilder_free(popped->bld);
10981107

tests/object/tree/update.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ void test_object_tree_update__remove_blob_deeper(void)
7171
git_tree_free(base_tree);
7272
}
7373

74+
void test_object_tree_update__remove_all_entries(void)
75+
{
76+
git_oid tree_index_id, tree_updater_id, base_id;
77+
git_tree *base_tree;
78+
git_index *idx;
79+
const char *path1 = "subdir/subdir2/README";
80+
const char *path2 = "subdir/subdir2/new.txt";
81+
82+
git_tree_update updates[] = {
83+
{ GIT_TREE_UPDATE_REMOVE, {{0}}, GIT_FILEMODE_BLOB /* ignored */, path1},
84+
{ GIT_TREE_UPDATE_REMOVE, {{0}}, GIT_FILEMODE_BLOB /* ignored */, path2},
85+
};
86+
87+
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
88+
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
89+
90+
/* Create it with an index */
91+
cl_git_pass(git_index_new(&idx));
92+
cl_git_pass(git_index_read_tree(idx, base_tree));
93+
cl_git_pass(git_index_remove(idx, path1, 0));
94+
cl_git_pass(git_index_remove(idx, path2, 0));
95+
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
96+
git_index_free(idx);
97+
98+
/* Perform the same operation via the tree updater */
99+
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 2, updates));
100+
101+
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
102+
103+
git_tree_free(base_tree);
104+
}
105+
74106
void test_object_tree_update__replace_blob(void)
75107
{
76108
git_oid tree_index_id, tree_updater_id, base_id;

0 commit comments

Comments
 (0)