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

Skip to content

Commit c91a1dc

Browse files
committed
Merge pull request libgit2#3794 from libgit2/cmn/tree-update-basename
Tree updater fixups
2 parents 1b56cda + 5341230 commit c91a1dc

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

src/tree.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,22 +1216,29 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli
12161216
{
12171217
/* Make sure we're replacing something of the same type */
12181218
tree_stack_entry *last = git_array_last(stack);
1219-
const char *basename = git_path_basename(update->path);
1219+
char *basename = git_path_basename(update->path);
12201220
const git_tree_entry *e = git_treebuilder_get(last->bld, basename);
12211221
if (e && git_tree_entry_type(e) != git_object__type_from_filemode(update->filemode)) {
1222+
git__free(basename);
12221223
giterr_set(GITERR_TREE, "Cannot replace '%s' with '%s' at '%s'",
12231224
git_object_type2string(git_tree_entry_type(e)),
12241225
git_object_type2string(git_object__type_from_filemode(update->filemode)),
12251226
update->path);
1226-
return -1;
1227+
error = -1;
1228+
goto cleanup;
12271229
}
12281230

12291231
error = git_treebuilder_insert(NULL, last->bld, basename, &update->id, update->filemode);
1232+
git__free(basename);
12301233
break;
12311234
}
12321235
case GIT_TREE_UPDATE_REMOVE:
1233-
error = git_treebuilder_remove(git_array_last(stack)->bld, update->path);
1236+
{
1237+
char *basename = git_path_basename(update->path);
1238+
error = git_treebuilder_remove(git_array_last(stack)->bld, basename);
1239+
git__free(basename);
12341240
break;
1241+
}
12351242
default:
12361243
giterr_set(GITERR_TREE, "unkown action for update");
12371244
error = -1;
@@ -1275,6 +1282,7 @@ int git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseli
12751282
}
12761283
}
12771284

1285+
git_buf_free(&component);
12781286
git_array_clear(stack);
12791287
git_vector_free(&entries);
12801288
return error;

tests/object/tree/update.c

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ static git_repository *g_repo;
55

66
void test_object_tree_update__initialize(void)
77
{
8-
g_repo = cl_git_sandbox_init("testrepo");
8+
g_repo = cl_git_sandbox_init("testrepo2");
99
}
1010

1111
void test_object_tree_update__cleanup(void)
1212
{
13-
cl_git_sandbox_cleanup();
13+
cl_git_sandbox_cleanup();
1414
}
1515

1616
void test_object_tree_update__remove_blob(void)
@@ -24,7 +24,36 @@ void test_object_tree_update__remove_blob(void)
2424
{ GIT_TREE_UPDATE_REMOVE, {{0}}, GIT_FILEMODE_BLOB /* ignored */, path},
2525
};
2626

27-
cl_git_pass(git_oid_fromstr(&base_id, "45dd856fdd4d89b884c340ba0e047752d9b085d6"));
27+
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
28+
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
29+
30+
/* Create it with an index */
31+
cl_git_pass(git_index_new(&idx));
32+
cl_git_pass(git_index_read_tree(idx, base_tree));
33+
cl_git_pass(git_index_remove(idx, path, 0));
34+
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
35+
git_index_free(idx);
36+
37+
/* Perform the same operation via the tree updater */
38+
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
39+
40+
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
41+
42+
git_tree_free(base_tree);
43+
}
44+
45+
void test_object_tree_update__remove_blob_deeper(void)
46+
{
47+
git_oid tree_index_id, tree_updater_id, base_id;
48+
git_tree *base_tree;
49+
git_index *idx;
50+
const char *path = "subdir/README";
51+
52+
git_tree_update updates[] = {
53+
{ GIT_TREE_UPDATE_REMOVE, {{0}}, GIT_FILEMODE_BLOB /* ignored */, path},
54+
};
55+
56+
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
2857
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
2958

3059
/* Create it with an index */
@@ -54,23 +83,23 @@ void test_object_tree_update__replace_blob(void)
5483
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, path},
5584
};
5685

57-
cl_git_pass(git_oid_fromstr(&base_id, "45dd856fdd4d89b884c340ba0e047752d9b085d6"));
86+
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
5887
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
5988

6089
/* Create it with an index */
6190
cl_git_pass(git_index_new(&idx));
6291
cl_git_pass(git_index_read_tree(idx, base_tree));
6392

6493
entry.path = path;
65-
cl_git_pass(git_oid_fromstr(&entry.id, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"));
94+
cl_git_pass(git_oid_fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92"));
6695
entry.mode = GIT_FILEMODE_BLOB;
6796
cl_git_pass(git_index_add(idx, &entry));
6897

6998
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
7099
git_index_free(idx);
71100

72101
/* Perform the same operation via the tree updater */
73-
cl_git_pass(git_oid_fromstr(&updates[0].id, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"));
102+
cl_git_pass(git_oid_fromstr(&updates[0].id, "fa49b077972391ad58037050f2a75f74e3671e92"));
74103
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
75104

76105
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
@@ -97,14 +126,13 @@ void test_object_tree_update__add_blobs(void)
97126
{ GIT_TREE_UPDATE_UPSERT, {{0}}, GIT_FILEMODE_BLOB, paths[2]},
98127
};
99128

100-
cl_git_pass(git_oid_fromstr(&base_id, "45dd856fdd4d89b884c340ba0e047752d9b085d6"));
101-
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
129+
cl_git_pass(git_oid_fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b"));
102130

103131
entry.mode = GIT_FILEMODE_BLOB;
104-
cl_git_pass(git_oid_fromstr(&entry.id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
132+
cl_git_pass(git_oid_fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92"));
105133

106134
for (i = 0; i < 3; i++) {
107-
cl_git_pass(git_oid_fromstr(&updates[i].id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
135+
cl_git_pass(git_oid_fromstr(&updates[i].id, "fa49b077972391ad58037050f2a75f74e3671e92"));
108136
}
109137

110138
for (i = 0; i < 2; i++) {
@@ -132,6 +160,8 @@ void test_object_tree_update__add_blobs(void)
132160

133161
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
134162
}
163+
164+
git_tree_free(base_tree);
135165
}
136166

137167
void test_object_tree_update__add_conflict(void)

0 commit comments

Comments
 (0)