]> git.proxmox.com Git - libgit2.git/blob - tests/merge/trees/renames.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / merge / trees / renames.c
1 #include "clar_libgit2.h"
2 #include "git2/repository.h"
3 #include "git2/merge.h"
4 #include "buffer.h"
5 #include "merge.h"
6 #include "../merge_helpers.h"
7 #include "futils.h"
8
9 static git_repository *repo;
10
11 #define TEST_REPO_PATH "merge-resolve"
12
13 #define BRANCH_RENAME_OURS "rename_conflict_ours"
14 #define BRANCH_RENAME_THEIRS "rename_conflict_theirs"
15
16 /* Fixture setup and teardown */
17 void test_merge_trees_renames__initialize(void)
18 {
19 repo = cl_git_sandbox_init(TEST_REPO_PATH);
20 }
21
22 void test_merge_trees_renames__cleanup(void)
23 {
24 cl_git_sandbox_cleanup();
25 }
26
27 void test_merge_trees_renames__index(void)
28 {
29 git_index *index;
30 git_merge_options *opts = NULL;
31
32 struct merge_index_entry merge_index_entries[] = {
33 { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" },
34 { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" },
35 { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 1, "0b-rewritten-in-ours.txt" },
36 { 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 2, "0b-rewritten-in-ours.txt" },
37 { 0100644, "b2d399ae15224e1d58066e3c8df70ce37de7a656", 3, "0b-rewritten-in-ours.txt" },
38 { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" },
39 { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 1, "0c-rewritten-in-theirs.txt" },
40 { 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 2, "0c-rewritten-in-theirs.txt" },
41 { 0100644, "712ebba6669ea847d9829e4f1059d6c830c8b531", 3, "0c-rewritten-in-theirs.txt" },
42 { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" },
43 { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" },
44 { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" },
45 { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" },
46 { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" },
47 { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 2, "3a-newname-in-ours-deleted-in-theirs.txt" },
48 { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 1, "3a-renamed-in-ours-deleted-in-theirs.txt" },
49 { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 3, "3b-newname-in-theirs-deleted-in-ours.txt" },
50 { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 1, "3b-renamed-in-theirs-deleted-in-ours.txt" },
51 { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 2, "4a-newname-in-ours-added-in-theirs.txt" },
52 { 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 3, "4a-newname-in-ours-added-in-theirs.txt" },
53 { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 1, "4a-renamed-in-ours-added-in-theirs.txt" },
54 { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 2, "4b-newname-in-theirs-added-in-ours.txt" },
55 { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 3, "4b-newname-in-theirs-added-in-ours.txt" },
56 { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 1, "4b-renamed-in-theirs-added-in-ours.txt" },
57 { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 2, "5a-newname-in-ours-added-in-theirs.txt" },
58 { 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 3, "5a-newname-in-ours-added-in-theirs.txt" },
59 { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 1, "5a-renamed-in-ours-added-in-theirs.txt" },
60 { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 3, "5a-renamed-in-ours-added-in-theirs.txt" },
61 { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 2, "5b-newname-in-theirs-added-in-ours.txt" },
62 { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 3, "5b-newname-in-theirs-added-in-ours.txt" },
63 { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 1, "5b-renamed-in-theirs-added-in-ours.txt" },
64 { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 2, "5b-renamed-in-theirs-added-in-ours.txt" },
65 { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 2, "6-both-renamed-1-to-2-ours.txt" },
66 { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 3, "6-both-renamed-1-to-2-theirs.txt" },
67 { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 1, "6-both-renamed-1-to-2.txt" },
68 { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 1, "7-both-renamed-side-1.txt" },
69 { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 3, "7-both-renamed-side-1.txt" },
70 { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 1, "7-both-renamed-side-2.txt" },
71 { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 2, "7-both-renamed-side-2.txt" },
72 { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "7-both-renamed.txt" },
73 { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "7-both-renamed.txt" },
74 };
75
76 struct merge_name_entry merge_name_entries[] = {
77 {
78 "3a-renamed-in-ours-deleted-in-theirs.txt",
79 "3a-newname-in-ours-deleted-in-theirs.txt",
80 ""
81 },
82
83 {
84 "3b-renamed-in-theirs-deleted-in-ours.txt",
85 "",
86 "3b-newname-in-theirs-deleted-in-ours.txt",
87 },
88
89 {
90 "4a-renamed-in-ours-added-in-theirs.txt",
91 "4a-newname-in-ours-added-in-theirs.txt",
92 "",
93 },
94
95 {
96 "4b-renamed-in-theirs-added-in-ours.txt",
97 "",
98 "4b-newname-in-theirs-added-in-ours.txt",
99 },
100
101 {
102 "5a-renamed-in-ours-added-in-theirs.txt",
103 "5a-newname-in-ours-added-in-theirs.txt",
104 "5a-renamed-in-ours-added-in-theirs.txt",
105 },
106
107 {
108 "5b-renamed-in-theirs-added-in-ours.txt",
109 "5b-renamed-in-theirs-added-in-ours.txt",
110 "5b-newname-in-theirs-added-in-ours.txt",
111 },
112
113 {
114 "6-both-renamed-1-to-2.txt",
115 "6-both-renamed-1-to-2-ours.txt",
116 "6-both-renamed-1-to-2-theirs.txt",
117 },
118
119 {
120 "7-both-renamed-side-1.txt",
121 "7-both-renamed.txt",
122 "7-both-renamed-side-1.txt",
123 },
124
125 {
126 "7-both-renamed-side-2.txt",
127 "7-both-renamed-side-2.txt",
128 "7-both-renamed.txt",
129 },
130 };
131
132 struct merge_reuc_entry merge_reuc_entries[] = {
133 { "1a-newname-in-ours-edited-in-theirs.txt",
134 0, 0100644, 0,
135 "",
136 "c3d02eeef75183df7584d8d13ac03053910c1301",
137 "" },
138
139 { "1a-newname-in-ours.txt",
140 0, 0100644, 0,
141 "",
142 "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb",
143 "" },
144
145 { "1a-renamed-in-ours-edited-in-theirs.txt",
146 0100644, 0, 0100644,
147 "c3d02eeef75183df7584d8d13ac03053910c1301",
148 "",
149 "0d872f8e871a30208305978ecbf9e66d864f1638" },
150
151 { "1a-renamed-in-ours.txt",
152 0100644, 0, 0100644,
153 "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb",
154 "",
155 "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb" },
156
157 { "1b-newname-in-theirs-edited-in-ours.txt",
158 0, 0, 0100644,
159 "",
160 "",
161 "241a1005cd9b980732741b74385b891142bcba28" },
162
163 { "1b-newname-in-theirs.txt",
164 0, 0, 0100644,
165 "",
166 "",
167 "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136" },
168
169 { "1b-renamed-in-theirs-edited-in-ours.txt",
170 0100644, 0100644, 0,
171 "241a1005cd9b980732741b74385b891142bcba28",
172 "ed9523e62e453e50dd9be1606af19399b96e397a",
173 "" },
174
175 { "1b-renamed-in-theirs.txt",
176 0100644, 0100644, 0,
177 "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136",
178 "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136",
179 "" },
180
181 { "2-newname-in-both.txt",
182 0, 0100644, 0100644,
183 "",
184 "178940b450f238a56c0d75b7955cb57b38191982",
185 "178940b450f238a56c0d75b7955cb57b38191982" },
186
187 { "2-renamed-in-both.txt",
188 0100644, 0, 0,
189 "178940b450f238a56c0d75b7955cb57b38191982",
190 "",
191 "" },
192 };
193
194 cl_git_pass(merge_trees_from_branches(&index, repo,
195 BRANCH_RENAME_OURS, BRANCH_RENAME_THEIRS,
196 opts));
197
198 cl_assert(merge_test_index(index, merge_index_entries, 41));
199 cl_assert(merge_test_names(index, merge_name_entries, 9));
200 cl_assert(merge_test_reuc(index, merge_reuc_entries, 10));
201
202 git_index_free(index);
203 }
204
205 void test_merge_trees_renames__no_rename_index(void)
206 {
207 git_index *index;
208 git_merge_options opts = GIT_MERGE_OPTIONS_INIT;
209
210 struct merge_index_entry merge_index_entries[] = {
211 { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" },
212 { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" },
213 { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 1, "0b-rewritten-in-ours.txt" },
214 { 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 2, "0b-rewritten-in-ours.txt" },
215 { 0100644, "b2d399ae15224e1d58066e3c8df70ce37de7a656", 3, "0b-rewritten-in-ours.txt" },
216 { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" },
217 { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 1, "0c-rewritten-in-theirs.txt" },
218 { 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 2, "0c-rewritten-in-theirs.txt" },
219 { 0100644, "712ebba6669ea847d9829e4f1059d6c830c8b531", 3, "0c-rewritten-in-theirs.txt" },
220 { 0100644, "c3d02eeef75183df7584d8d13ac03053910c1301", 0, "1a-newname-in-ours-edited-in-theirs.txt" },
221 { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" },
222 { 0100644, "c3d02eeef75183df7584d8d13ac03053910c1301", 1, "1a-renamed-in-ours-edited-in-theirs.txt" },
223 { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 3, "1a-renamed-in-ours-edited-in-theirs.txt" },
224 { 0100644, "241a1005cd9b980732741b74385b891142bcba28", 0, "1b-newname-in-theirs-edited-in-ours.txt" },
225 { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" },
226 { 0100644, "241a1005cd9b980732741b74385b891142bcba28", 1, "1b-renamed-in-theirs-edited-in-ours.txt" },
227 { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 2, "1b-renamed-in-theirs-edited-in-ours.txt" },
228 { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" },
229 { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-newname-in-ours-deleted-in-theirs.txt" },
230 { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-newname-in-theirs-deleted-in-ours.txt" },
231 { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 2, "4a-newname-in-ours-added-in-theirs.txt" },
232 { 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 3, "4a-newname-in-ours-added-in-theirs.txt" },
233 { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 2, "4b-newname-in-theirs-added-in-ours.txt" },
234 { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 3, "4b-newname-in-theirs-added-in-ours.txt" },
235 { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 2, "5a-newname-in-ours-added-in-theirs.txt" },
236 { 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 3, "5a-newname-in-ours-added-in-theirs.txt" },
237 { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 2, "5b-newname-in-theirs-added-in-ours.txt" },
238 { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 3, "5b-newname-in-theirs-added-in-ours.txt" },
239 { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-ours.txt" },
240 { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-theirs.txt" },
241 { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "7-both-renamed.txt" },
242 { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "7-both-renamed.txt" },
243 };
244
245 opts.flags &= ~GIT_MERGE_FIND_RENAMES;
246
247 cl_git_pass(merge_trees_from_branches(&index, repo,
248 BRANCH_RENAME_OURS, BRANCH_RENAME_THEIRS,
249 &opts));
250
251 cl_assert(merge_test_index(index, merge_index_entries, 32));
252
253 git_index_free(index);
254 }
255
256 void test_merge_trees_renames__submodules(void)
257 {
258 git_index *index;
259 git_merge_options *opts = NULL;
260
261 struct merge_index_entry merge_index_entries[] = {
262 { 0100644, "cd3e8d4aa06bdc781f264171030bc28f2b370fee", 0, ".gitmodules" },
263 { 0100644, "4dd1ef7569b18d92d93c0a35bb6b93049137b355", 1, "file.txt" },
264 { 0100644, "a2d8d1824c68541cca94ffb90f79291eba495921", 2, "file.txt" },
265 { 0100644, "63ec604d491161ddafdae4179843c26d54bd999a", 3, "file.txt" },
266 { 0160000, "0000000000000000000000000000000000000001", 1, "submodule1" },
267 { 0160000, "0000000000000000000000000000000000000002", 3, "submodule1" },
268 { 0160000, "0000000000000000000000000000000000000003", 0, "submodule2" },
269 };
270
271 cl_git_pass(merge_trees_from_branches(&index, repo,
272 "submodule_rename1", "submodule_rename2",
273 opts));
274 cl_assert(merge_test_index(index, merge_index_entries, 7));
275 git_index_free(index);
276 }
277
278 void test_merge_trees_renames__cache_recomputation(void)
279 {
280 git_oid blob, binary, ancestor_oid, theirs_oid, ours_oid;
281 git_merge_options opts = GIT_MERGE_OPTIONS_INIT;
282 git_buf path = GIT_BUF_INIT;
283 git_treebuilder *builder;
284 git_tree *ancestor_tree, *their_tree, *our_tree;
285 git_index *index;
286 size_t blob_size;
287 void *data;
288 size_t i;
289
290 cl_git_pass(git_oid_fromstr(&blob, "a2d8d1824c68541cca94ffb90f79291eba495921"));
291
292 /*
293 * Create a 50MB blob that consists of NUL bytes only. It is important
294 * that this blob is of a special format, most importantly it cannot
295 * contain more than four non-consecutive newlines or NUL bytes. This
296 * is because of git_hashsig's inner workings where all files with less
297 * than four "lines" are deemed to small.
298 */
299 blob_size = 50 * 1024 * 1024;
300 cl_assert(data = git__calloc(blob_size, 1));
301 cl_git_pass(git_blob_create_from_buffer(&binary, repo, data, blob_size));
302
303 /*
304 * Create the common ancestor, which has 1000 dummy blobs and the binary
305 * blob. The dummy blobs serve as potential rename targets for the
306 * dummy blob.
307 */
308 cl_git_pass(git_treebuilder_new(&builder, repo, NULL));
309 for (i = 0; i < 1000; i++) {
310 cl_git_pass(git_buf_printf(&path, "%"PRIuZ".txt", i));
311 cl_git_pass(git_treebuilder_insert(NULL, builder, path.ptr, &blob, GIT_FILEMODE_BLOB));
312 git_buf_clear(&path);
313 }
314 cl_git_pass(git_treebuilder_insert(NULL, builder, "original.bin", &binary, GIT_FILEMODE_BLOB));
315 cl_git_pass(git_treebuilder_write(&ancestor_oid, builder));
316
317 /* We now the binary blob in our tree. */
318 cl_git_pass(git_treebuilder_remove(builder, "original.bin"));
319 cl_git_pass(git_treebuilder_insert(NULL, builder, "renamed.bin", &binary, GIT_FILEMODE_BLOB));
320 cl_git_pass(git_treebuilder_write(&ours_oid, builder));
321
322 git_treebuilder_free(builder);
323
324 /* And move everything into a subdirectory in their tree. */
325 cl_git_pass(git_treebuilder_new(&builder, repo, NULL));
326 cl_git_pass(git_treebuilder_insert(NULL, builder, "subdir", &ancestor_oid, GIT_FILEMODE_TREE));
327 cl_git_pass(git_treebuilder_write(&theirs_oid, builder));
328
329 /*
330 * Now merge ancestor, ours and theirs. As `git_hashsig` refuses to
331 * create a hash signature for the 50MB binary file, we historically
332 * didn't cache the hashsig computation for it. As a result, we now
333 * started looking up the 50MB blob and scanning it at least 1000
334 * times, which takes a long time.
335 *
336 * The number of 1000 blobs is chosen in such a way that it's
337 * noticeable when the bug creeps in again, as it takes around 12
338 * minutes on my machine to compute the following merge.
339 */
340 opts.target_limit = 5000;
341 cl_git_pass(git_tree_lookup(&ancestor_tree, repo, &ancestor_oid));
342 cl_git_pass(git_tree_lookup(&their_tree, repo, &theirs_oid));
343 cl_git_pass(git_tree_lookup(&our_tree, repo, &ours_oid));
344 cl_git_pass(git_merge_trees(&index, repo, ancestor_tree, our_tree, their_tree, &opts));
345
346 git_treebuilder_free(builder);
347 git_buf_dispose(&path);
348 git_index_free(index);
349 git_tree_free(ancestor_tree);
350 git_tree_free(their_tree);
351 git_tree_free(our_tree);
352 git__free(data);
353 }