]> git.proxmox.com Git - libgit2.git/blob - tests/apply/fromdiff.c
New upstream version 0.28.1+dfsg.1
[libgit2.git] / tests / apply / fromdiff.c
1 #include "clar_libgit2.h"
2 #include "git2/sys/repository.h"
3
4 #include "apply.h"
5 #include "repository.h"
6 #include "buf_text.h"
7
8 #include "../patch/patch_common.h"
9
10 static git_repository *repo = NULL;
11 static git_diff_options binary_opts = GIT_DIFF_OPTIONS_INIT;
12
13 void test_apply_fromdiff__initialize(void)
14 {
15 repo = cl_git_sandbox_init("renames");
16
17 binary_opts.flags |= GIT_DIFF_SHOW_BINARY;
18 }
19
20 void test_apply_fromdiff__cleanup(void)
21 {
22 cl_git_sandbox_cleanup();
23 }
24
25 static int apply_gitbuf(
26 const git_buf *old,
27 const char *oldname,
28 const git_buf *new,
29 const char *newname,
30 const char *patch_expected,
31 const git_diff_options *diff_opts)
32 {
33 git_patch *patch;
34 git_buf result = GIT_BUF_INIT;
35 git_buf patchbuf = GIT_BUF_INIT;
36 char *filename;
37 unsigned int mode;
38 int error;
39
40 cl_git_pass(git_patch_from_buffers(&patch,
41 old ? old->ptr : NULL, old ? old->size : 0,
42 oldname,
43 new ? new->ptr : NULL, new ? new->size : 0,
44 newname,
45 diff_opts));
46
47 if (patch_expected) {
48 cl_git_pass(git_patch_to_buf(&patchbuf, patch));
49 cl_assert_equal_s(patch_expected, patchbuf.ptr);
50 }
51
52 error = git_apply__patch(&result, &filename, &mode, old ? old->ptr : NULL, old ? old->size : 0, patch, NULL);
53
54 if (error == 0 && new == NULL) {
55 cl_assert_equal_i(0, result.size);
56 cl_assert_equal_p(NULL, filename);
57 cl_assert_equal_i(0, mode);
58 }
59 else if (error == 0) {
60 cl_assert_equal_s(new->ptr, result.ptr);
61 cl_assert_equal_s(newname ? newname : oldname, filename);
62 cl_assert_equal_i(0100644, mode);
63 }
64
65 git__free(filename);
66 git_buf_dispose(&result);
67 git_buf_dispose(&patchbuf);
68 git_patch_free(patch);
69
70 return error;
71 }
72
73 static int apply_buf(
74 const char *old,
75 const char *oldname,
76 const char *new,
77 const char *newname,
78 const char *patch_expected,
79 const git_diff_options *diff_opts)
80 {
81 git_buf o = GIT_BUF_INIT, n = GIT_BUF_INIT,
82 *optr = NULL, *nptr = NULL;
83
84 if (old) {
85 o.ptr = (char *)old;
86 o.size = strlen(old);
87 optr = &o;
88 }
89
90 if (new) {
91 n.ptr = (char *)new;
92 n.size = strlen(new);
93 nptr = &n;
94 }
95
96 return apply_gitbuf(optr, oldname, nptr, newname, patch_expected, diff_opts);
97 }
98
99 void test_apply_fromdiff__change_middle(void)
100 {
101 cl_git_pass(apply_buf(
102 FILE_ORIGINAL, "file.txt",
103 FILE_CHANGE_MIDDLE, "file.txt",
104 PATCH_ORIGINAL_TO_CHANGE_MIDDLE, NULL));
105 }
106
107 void test_apply_fromdiff__change_middle_nocontext(void)
108 {
109 git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
110 diff_opts.context_lines = 0;
111
112 cl_git_pass(apply_buf(
113 FILE_ORIGINAL, "file.txt",
114 FILE_CHANGE_MIDDLE, "file.txt",
115 PATCH_ORIGINAL_TO_CHANGE_MIDDLE_NOCONTEXT, &diff_opts));
116 }
117
118 void test_apply_fromdiff__change_firstline(void)
119 {
120 cl_git_pass(apply_buf(
121 FILE_ORIGINAL, "file.txt",
122 FILE_CHANGE_FIRSTLINE, "file.txt",
123 PATCH_ORIGINAL_TO_CHANGE_FIRSTLINE, NULL));
124 }
125
126 void test_apply_fromdiff__lastline(void)
127 {
128 cl_git_pass(apply_buf(
129 FILE_ORIGINAL, "file.txt",
130 FILE_CHANGE_LASTLINE, "file.txt",
131 PATCH_ORIGINAL_TO_CHANGE_LASTLINE, NULL));
132 }
133
134 void test_apply_fromdiff__prepend(void)
135 {
136 cl_git_pass(apply_buf(
137 FILE_ORIGINAL, "file.txt",
138 FILE_PREPEND, "file.txt",
139 PATCH_ORIGINAL_TO_PREPEND, NULL));
140 }
141
142 void test_apply_fromdiff__prepend_nocontext(void)
143 {
144 git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
145 diff_opts.context_lines = 0;
146
147 cl_git_pass(apply_buf(
148 FILE_ORIGINAL, "file.txt",
149 FILE_PREPEND, "file.txt",
150 PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT, &diff_opts));
151 }
152
153 void test_apply_fromdiff__prepend_and_change(void)
154 {
155 cl_git_pass(apply_buf(
156 FILE_ORIGINAL, "file.txt",
157 FILE_PREPEND_AND_CHANGE, "file.txt",
158 PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE, NULL));
159 }
160
161 void test_apply_fromdiff__prepend_and_change_nocontext(void)
162 {
163 git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
164 diff_opts.context_lines = 0;
165
166 cl_git_pass(apply_buf(
167 FILE_ORIGINAL, "file.txt",
168 FILE_PREPEND_AND_CHANGE, "file.txt",
169 PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE_NOCONTEXT, &diff_opts));
170 }
171
172 void test_apply_fromdiff__delete_and_change(void)
173 {
174 cl_git_pass(apply_buf(
175 FILE_ORIGINAL, "file.txt",
176 FILE_DELETE_AND_CHANGE, "file.txt",
177 PATCH_ORIGINAL_TO_DELETE_AND_CHANGE, NULL));
178 }
179
180 void test_apply_fromdiff__delete_and_change_nocontext(void)
181 {
182 git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
183 diff_opts.context_lines = 0;
184
185 cl_git_pass(apply_buf(
186 FILE_ORIGINAL, "file.txt",
187 FILE_DELETE_AND_CHANGE, "file.txt",
188 PATCH_ORIGINAL_TO_DELETE_AND_CHANGE_NOCONTEXT, &diff_opts));
189 }
190
191 void test_apply_fromdiff__delete_firstline(void)
192 {
193 cl_git_pass(apply_buf(
194 FILE_ORIGINAL, "file.txt",
195 FILE_DELETE_FIRSTLINE, "file.txt",
196 PATCH_ORIGINAL_TO_DELETE_FIRSTLINE, NULL));
197 }
198
199 void test_apply_fromdiff__append(void)
200 {
201 cl_git_pass(apply_buf(
202 FILE_ORIGINAL, "file.txt",
203 FILE_APPEND, "file.txt",
204 PATCH_ORIGINAL_TO_APPEND, NULL));
205 }
206
207 void test_apply_fromdiff__append_nocontext(void)
208 {
209 git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
210 diff_opts.context_lines = 0;
211
212 cl_git_pass(apply_buf(
213 FILE_ORIGINAL, "file.txt",
214 FILE_APPEND, "file.txt",
215 PATCH_ORIGINAL_TO_APPEND_NOCONTEXT, &diff_opts));
216 }
217
218 void test_apply_fromdiff__prepend_and_append(void)
219 {
220 cl_git_pass(apply_buf(
221 FILE_ORIGINAL, "file.txt",
222 FILE_PREPEND_AND_APPEND, "file.txt",
223 PATCH_ORIGINAL_TO_PREPEND_AND_APPEND, NULL));
224 }
225
226 void test_apply_fromdiff__to_empty_file(void)
227 {
228 cl_git_pass(apply_buf(
229 FILE_ORIGINAL, "file.txt",
230 "", NULL,
231 PATCH_ORIGINAL_TO_EMPTY_FILE, NULL));
232 }
233
234 void test_apply_fromdiff__from_empty_file(void)
235 {
236 cl_git_pass(apply_buf(
237 "", NULL,
238 FILE_ORIGINAL, "file.txt",
239 PATCH_EMPTY_FILE_TO_ORIGINAL, NULL));
240 }
241
242 void test_apply_fromdiff__add(void)
243 {
244 cl_git_pass(apply_buf(
245 NULL, NULL,
246 FILE_ORIGINAL, "file.txt",
247 PATCH_ADD_ORIGINAL, NULL));
248 }
249
250 void test_apply_fromdiff__delete(void)
251 {
252 cl_git_pass(apply_buf(
253 FILE_ORIGINAL, "file.txt",
254 NULL, NULL,
255 PATCH_DELETE_ORIGINAL, NULL));
256 }
257
258 void test_apply_fromdiff__no_change(void)
259 {
260 cl_git_pass(apply_buf(
261 FILE_ORIGINAL, "file.txt",
262 FILE_ORIGINAL, "file.txt",
263 "", NULL));
264 }
265
266 void test_apply_fromdiff__binary_add(void)
267 {
268 git_buf newfile = GIT_BUF_INIT;
269
270 newfile.ptr = FILE_BINARY_DELTA_MODIFIED;
271 newfile.size = FILE_BINARY_DELTA_MODIFIED_LEN;
272
273 cl_git_pass(apply_gitbuf(
274 NULL, NULL,
275 &newfile, "binary.bin",
276 NULL, &binary_opts));
277 }
278
279 void test_apply_fromdiff__binary_no_change(void)
280 {
281 git_buf original = GIT_BUF_INIT;
282
283 original.ptr = FILE_BINARY_DELTA_ORIGINAL;
284 original.size = FILE_BINARY_DELTA_ORIGINAL_LEN;
285
286 cl_git_pass(apply_gitbuf(
287 &original, "binary.bin",
288 &original, "binary.bin",
289 "", &binary_opts));
290 }
291
292 void test_apply_fromdiff__binary_change_delta(void)
293 {
294 git_buf original = GIT_BUF_INIT, modified = GIT_BUF_INIT;
295
296 original.ptr = FILE_BINARY_DELTA_ORIGINAL;
297 original.size = FILE_BINARY_DELTA_ORIGINAL_LEN;
298
299 modified.ptr = FILE_BINARY_DELTA_MODIFIED;
300 modified.size = FILE_BINARY_DELTA_MODIFIED_LEN;
301
302 cl_git_pass(apply_gitbuf(
303 &original, "binary.bin",
304 &modified, "binary.bin",
305 NULL, &binary_opts));
306 }
307
308 void test_apply_fromdiff__binary_change_literal(void)
309 {
310 git_buf original = GIT_BUF_INIT, modified = GIT_BUF_INIT;
311
312 original.ptr = FILE_BINARY_LITERAL_ORIGINAL;
313 original.size = FILE_BINARY_LITERAL_ORIGINAL_LEN;
314
315 modified.ptr = FILE_BINARY_LITERAL_MODIFIED;
316 modified.size = FILE_BINARY_LITERAL_MODIFIED_LEN;
317
318 cl_git_pass(apply_gitbuf(
319 &original, "binary.bin",
320 &modified, "binary.bin",
321 NULL, &binary_opts));
322 }
323
324 void test_apply_fromdiff__binary_delete(void)
325 {
326 git_buf original = GIT_BUF_INIT;
327
328 original.ptr = FILE_BINARY_DELTA_MODIFIED;
329 original.size = FILE_BINARY_DELTA_MODIFIED_LEN;
330
331 cl_git_pass(apply_gitbuf(
332 &original, "binary.bin",
333 NULL, NULL,
334 NULL, &binary_opts));
335 }