Skip to content

Commit df045ce

Browse files
author
Edward Thomson
authored
Merge pull request libgit2#4003 from libgit2/cmn/tree-updater-ordering
Use the sorted input in the tree updater
2 parents 904e1e7 + 8977658 commit df045ce

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/tree.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,8 +1164,8 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli
11641164
goto cleanup;
11651165

11661166
for (i = 0; i < nupdates; i++) {
1167-
const git_tree_update *last_update = i == 0 ? NULL : &updates[i-1];
1168-
const git_tree_update *update = &updates[i];
1167+
const git_tree_update *last_update = i == 0 ? NULL : git_vector_get(&entries, i-1);
1168+
const git_tree_update *update = git_vector_get(&entries, i);
11691169
size_t common_prefix = 0, steps_up, j;
11701170
const char *path;
11711171

@@ -1200,6 +1200,9 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli
12001200

12011201
last = git_array_last(stack);
12021202
entry = last->tree ? git_tree_entry_byname(last->tree, component.ptr) : NULL;
1203+
if (!entry)
1204+
entry = treebuilder_get(last->bld, component.ptr);
1205+
12031206
if (entry && git_tree_entry_type(entry) != GIT_OBJ_TREE) {
12041207
giterr_set(GITERR_TREE, "D/F conflict when updating tree");
12051208
error = -1;

tests/object/tree/update.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,63 @@ void test_object_tree_update__add_blobs(void)
196196
git_tree_free(base_tree);
197197
}
198198

199+
void test_object_tree_update__add_blobs_unsorted(void)
200+
{
201+
git_oid tree_index_id, tree_updater_id, base_id;
202+
git_tree *base_tree;
203+
git_index *idx;
204+
git_index_entry entry = { {0} };
205+
int i;
206+
const char *paths[] = {
207+
"some/deep/path",
208+
"a/path/elsewhere",
209+
"some/other/path",
210+
};
211+
212+
git_tree_update updates[] = {
213+
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[0]},
214+
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[1]},
215+
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[2]},
216+
};
217+
218+
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
219+
220+
entry.mode = GIT_FILEMODE_BLOB;
221+
cl_git_pass(git_oid_fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92"));
222+
223+
for (i = 0; i < 3; i++) {
224+
cl_git_pass(git_oid_fromstr(&updates[i].id, "fa49b077972391ad58037050f2a75f74e3671e92"));
225+
}
226+
227+
for (i = 0; i < 2; i++) {
228+
int j;
229+
230+
/* Create it with an index */
231+
cl_git_pass(git_index_new(&idx));
232+
233+
base_tree = NULL;
234+
if (i == 1) {
235+
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
236+
cl_git_pass(git_index_read_tree(idx, base_tree));
237+
}
238+
239+
for (j = 0; j < 3; j++) {
240+
entry.path = paths[j];
241+
cl_git_pass(git_index_add(idx, &entry));
242+
}
243+
244+
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
245+
git_index_free(idx);
246+
247+
/* Perform the same operations via the tree updater */
248+
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 3, updates));
249+
250+
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
251+
}
252+
253+
git_tree_free(base_tree);
254+
}
255+
199256
void test_object_tree_update__add_conflict(void)
200257
{
201258
int i;

0 commit comments

Comments
 (0)