]> git.proxmox.com Git - libgit2.git/blame - tests/apply/workdir.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / apply / workdir.c
CommitLineData
ac3d33df
JK
1#include "clar_libgit2.h"
2#include "apply_helpers.h"
3
4static git_repository *repo;
5
6#define TEST_REPO_PATH "merge-recursive"
7
8void test_apply_workdir__initialize(void)
9{
10 git_oid oid;
11 git_commit *commit;
12
13 repo = cl_git_sandbox_init(TEST_REPO_PATH);
14
15 git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
16 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
17 cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
18 git_commit_free(commit);
19}
20
21void test_apply_workdir__cleanup(void)
22{
23 cl_git_sandbox_cleanup();
24}
25
26void test_apply_workdir__generated_diff(void)
27{
28 git_oid a_oid, b_oid;
29 git_commit *a_commit, *b_commit;
30 git_tree *a_tree, *b_tree;
31 git_diff *diff;
32 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
33
34 struct merge_index_entry workdir_expected[] = {
35 { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
36 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
37 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
38 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
39 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
40 { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
41 };
42 size_t workdir_expected_cnt = sizeof(workdir_expected) /
43 sizeof(struct merge_index_entry);
44
45 git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707");
46 git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f"); cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
47 cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
48
49 cl_git_pass(git_commit_tree(&a_tree, a_commit));
50 cl_git_pass(git_commit_tree(&b_tree, b_commit));
51
52 cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &opts));
53 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
54
55 validate_index_unchanged(repo);
56 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
57
58 git_diff_free(diff);
59 git_tree_free(a_tree);
60 git_tree_free(b_tree);
61 git_commit_free(a_commit);
62 git_commit_free(b_commit);
63}
64
65void test_apply_workdir__parsed_diff(void)
66{
67 git_diff *diff;
68
69 struct merge_index_entry workdir_expected[] = {
70 { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
71 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
72 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
73 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
74 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
75 { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
76 };
77 size_t workdir_expected_cnt = sizeof(workdir_expected) /
78 sizeof(struct merge_index_entry);
79
80 cl_git_pass(git_diff_from_buffer(&diff,
81 DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
82 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
83
84 validate_index_unchanged(repo);
85 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
86
87 git_diff_free(diff);
88}
89
90void test_apply_workdir__removes_file(void)
91{
92 git_diff *diff;
93
94 struct merge_index_entry workdir_expected[] = {
95 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
96 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
97 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
98 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
99 { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
100 };
101 size_t workdir_expected_cnt = sizeof(workdir_expected) /
102 sizeof(struct merge_index_entry);
103
104 cl_git_pass(git_diff_from_buffer(&diff, DIFF_DELETE_FILE,
105 strlen(DIFF_DELETE_FILE)));
106 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
107
108 validate_index_unchanged(repo);
109 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
110
111 git_diff_free(diff);
112}
113
114void test_apply_workdir__adds_file(void)
115{
116 git_diff *diff;
117
118 struct merge_index_entry workdir_expected[] = {
119 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
120 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
121 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
122 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
123 { 0100644, "6370543fcfedb3e6516ec53b06158f3687dc1447", 0, "newfile.txt" },
124 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
125 { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
126 };
127 size_t workdir_expected_cnt = sizeof(workdir_expected) /
128 sizeof(struct merge_index_entry);
129
130 cl_git_pass(git_diff_from_buffer(&diff,
131 DIFF_ADD_FILE, strlen(DIFF_ADD_FILE)));
132 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
133
134 validate_index_unchanged(repo);
135 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
136
137 git_diff_free(diff);
138}
139
140void test_apply_workdir__modified_index_with_unmodified_workdir_is_ok(void)
141{
142 git_index *index;
143 git_index_entry idx_entry = {{0}};
144 git_diff *diff;
145
146 const char *diff_file = DIFF_MODIFY_TWO_FILES;
147
148 struct merge_index_entry index_expected[] = {
149 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
150 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
151 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
152 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
153 { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "veal.txt" }
154 };
155 size_t index_expected_cnt = sizeof(index_expected) /
156 sizeof(struct merge_index_entry);
157
158 struct merge_index_entry workdir_expected[] = {
159 { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
160 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
161 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
162 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
163 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
164 { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
165 };
166 size_t workdir_expected_cnt = sizeof(workdir_expected) /
167 sizeof(struct merge_index_entry);
168
169 /* mutate the index and leave the workdir matching HEAD */
170 cl_git_pass(git_repository_index(&index, repo));
171
172 idx_entry.mode = 0100644;
173 idx_entry.path = "veal.txt";
174 cl_git_pass(git_oid_fromstr(&idx_entry.id, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d"));
175
176 cl_git_pass(git_index_add(index, &idx_entry));
177 cl_git_pass(git_index_remove(index, "asparagus.txt", 0));
178 cl_git_pass(git_index_write(index));
179
180 cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
181 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
182
183 validate_apply_index(repo, index_expected, index_expected_cnt);
184 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
185
186 git_index_free(index);
187 git_diff_free(diff);
188}
189
190void test_apply_workdir__application_failure_leaves_workdir_unmodified(void)
191{
192 git_diff *diff;
193
194 const char *diff_file = DIFF_MODIFY_TWO_FILES;
195
196 struct merge_index_entry workdir_expected[] = {
197 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
198 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
199 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
200 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
201 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
202 { 0100644, "8684724651336001c5dbce74bed6736d2443958d", 0, "veal.txt" },
203 };
204 size_t workdir_expected_cnt = sizeof(workdir_expected) /
205 sizeof(struct merge_index_entry);
206
207 /* mutate the workdir */
208 cl_git_rewritefile("merge-recursive/veal.txt",
209 "This is a modification.\n");
210
211 cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
212 cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
213
214 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
215
216 git_diff_free(diff);
217}
218
219void test_apply_workdir__keeps_nonconflicting_changes(void)
220{
221 git_diff *diff;
222
223 struct merge_index_entry workdir_expected[] = {
224 { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
225 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
226 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
227 { 0100644, "f75ba05f340c51065cbea2e1fdbfe5fe13144c97", 0, "gravy.txt" },
228 { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
229 };
230 size_t workdir_expected_cnt = sizeof(workdir_expected) /
231 sizeof(struct merge_index_entry);
232
233 cl_git_rmfile("merge-recursive/oyster.txt");
234 cl_git_rewritefile("merge-recursive/gravy.txt", "Hello, world.\n");
235
236 cl_git_pass(git_diff_from_buffer(&diff,
237 DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
238 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
239
240 validate_index_unchanged(repo);
241 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
242
243 git_diff_free(diff);
244}
245
246void test_apply_workdir__can_apply_nonconflicting_file_changes(void)
247{
248 git_diff *diff;
249
250 const char *diff_file = DIFF_MODIFY_TWO_FILES;
251
252 struct merge_index_entry workdir_expected[] = {
253 { 0100644, "5db1a0fef164cb66cc0c00d35cc5af979ddc1a64", 0, "asparagus.txt" },
254 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
255 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
256 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
257 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
258 { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
259 };
260 size_t workdir_expected_cnt = sizeof(workdir_expected) /
261 sizeof(struct merge_index_entry);
262
263 /*
264 * Replace the workdir file with a version that is different than
265 * HEAD but such that the patch still applies cleanly. This item
266 * has a new line appended.
267 */
268 cl_git_append2file("merge-recursive/asparagus.txt",
269 "This line is added in the workdir.\n");
270
271 cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
272 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
273
274 validate_index_unchanged(repo);
275 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
276
277 git_diff_free(diff);
278}
279
280void test_apply_workdir__change_mode(void)
281{
282#ifndef GIT_WIN32
283 git_diff *diff;
284
285 const char *diff_file = DIFF_EXECUTABLE_FILE;
286
287 struct merge_index_entry workdir_expected[] = {
288 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
289 { 0100755, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
290 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
291 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
292 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
293 { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
294 };
295 size_t workdir_expected_cnt = sizeof(workdir_expected) /
296 sizeof(struct merge_index_entry);
297
298 cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file)));
299 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, NULL));
300
301 validate_index_unchanged(repo);
302 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
303
304 git_diff_free(diff);
305#endif
306}
307
308void test_apply_workdir__apply_many_changes_one(void)
309{
310 git_diff *diff;
311 git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
312
313 struct merge_index_entry workdir_expected[] = {
314 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
315 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
316 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
317 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
318 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
319 { 0100644, "c9d7d5d58088bc91f6e06f17ca3a205091568d3a", 0, "veal.txt" },
320 };
321 size_t workdir_expected_cnt = sizeof(workdir_expected) /
322 sizeof(struct merge_index_entry);
323
324 cl_git_pass(git_diff_from_buffer(&diff,
325 DIFF_MANY_CHANGES_ONE, strlen(DIFF_MANY_CHANGES_ONE)));
326 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, &opts));
327
328 validate_index_unchanged(repo);
329 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
330
331 git_diff_free(diff);
332}
333
334void test_apply_workdir__apply_many_changes_two(void)
335{
336 git_diff *diff;
337 git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
338
339 struct merge_index_entry workdir_expected[] = {
340 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
341 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
342 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
343 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
344 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
345 { 0100644, "6b943d65af6d8db74d747284fa4ca7d716ad5bbb", 0, "veal.txt" },
346 };
347 size_t workdir_expected_cnt = sizeof(workdir_expected) /
348 sizeof(struct merge_index_entry);
349
350 cl_git_pass(git_diff_from_buffer(&diff,
351 DIFF_MANY_CHANGES_TWO, strlen(DIFF_MANY_CHANGES_TWO)));
352 cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, &opts));
353
354 validate_index_unchanged(repo);
355 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
356
357 git_diff_free(diff);
358}