1 #include "clar_libgit2.h"
2 #include "git2/sys/repository.h"
5 #include "repository.h"
8 #include "../patch/patch_common.h"
10 static git_repository
*repo
= NULL
;
11 static git_diff_options binary_opts
= GIT_DIFF_OPTIONS_INIT
;
13 void test_apply_fromdiff__initialize(void)
15 repo
= cl_git_sandbox_init("renames");
17 binary_opts
.flags
|= GIT_DIFF_SHOW_BINARY
;
20 void test_apply_fromdiff__cleanup(void)
22 cl_git_sandbox_cleanup();
25 static int apply_gitbuf(
30 const char *patch_expected
,
31 const git_diff_options
*diff_opts
)
34 git_buf result
= GIT_BUF_INIT
;
35 git_buf patchbuf
= GIT_BUF_INIT
;
40 cl_git_pass(git_patch_from_buffers(&patch
,
41 old
? old
->ptr
: NULL
, old
? old
->size
: 0,
43 new ? new->ptr
: NULL
, new ? new->size
: 0,
48 cl_git_pass(git_patch_to_buf(&patchbuf
, patch
));
49 cl_assert_equal_s(patch_expected
, patchbuf
.ptr
);
52 error
= git_apply__patch(&result
, &filename
, &mode
, old
? old
->ptr
: NULL
, old
? old
->size
: 0, patch
, NULL
);
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
);
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
);
66 git_buf_dispose(&result
);
67 git_buf_dispose(&patchbuf
);
68 git_patch_free(patch
);
78 const char *patch_expected
,
79 const git_diff_options
*diff_opts
)
81 git_buf o
= GIT_BUF_INIT
, n
= GIT_BUF_INIT
,
82 *optr
= NULL
, *nptr
= NULL
;
96 return apply_gitbuf(optr
, oldname
, nptr
, newname
, patch_expected
, diff_opts
);
99 void test_apply_fromdiff__change_middle(void)
101 cl_git_pass(apply_buf(
102 FILE_ORIGINAL
, "file.txt",
103 FILE_CHANGE_MIDDLE
, "file.txt",
104 PATCH_ORIGINAL_TO_CHANGE_MIDDLE
, NULL
));
107 void test_apply_fromdiff__change_middle_nocontext(void)
109 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
110 diff_opts
.context_lines
= 0;
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
));
118 void test_apply_fromdiff__change_firstline(void)
120 cl_git_pass(apply_buf(
121 FILE_ORIGINAL
, "file.txt",
122 FILE_CHANGE_FIRSTLINE
, "file.txt",
123 PATCH_ORIGINAL_TO_CHANGE_FIRSTLINE
, NULL
));
126 void test_apply_fromdiff__lastline(void)
128 cl_git_pass(apply_buf(
129 FILE_ORIGINAL
, "file.txt",
130 FILE_CHANGE_LASTLINE
, "file.txt",
131 PATCH_ORIGINAL_TO_CHANGE_LASTLINE
, NULL
));
134 void test_apply_fromdiff__prepend(void)
136 cl_git_pass(apply_buf(
137 FILE_ORIGINAL
, "file.txt",
138 FILE_PREPEND
, "file.txt",
139 PATCH_ORIGINAL_TO_PREPEND
, NULL
));
142 void test_apply_fromdiff__prepend_nocontext(void)
144 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
145 diff_opts
.context_lines
= 0;
147 cl_git_pass(apply_buf(
148 FILE_ORIGINAL
, "file.txt",
149 FILE_PREPEND
, "file.txt",
150 PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT
, &diff_opts
));
153 void test_apply_fromdiff__prepend_and_change(void)
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
));
161 void test_apply_fromdiff__prepend_and_change_nocontext(void)
163 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
164 diff_opts
.context_lines
= 0;
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
));
172 void test_apply_fromdiff__delete_and_change(void)
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
));
180 void test_apply_fromdiff__delete_and_change_nocontext(void)
182 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
183 diff_opts
.context_lines
= 0;
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
));
191 void test_apply_fromdiff__delete_firstline(void)
193 cl_git_pass(apply_buf(
194 FILE_ORIGINAL
, "file.txt",
195 FILE_DELETE_FIRSTLINE
, "file.txt",
196 PATCH_ORIGINAL_TO_DELETE_FIRSTLINE
, NULL
));
199 void test_apply_fromdiff__append(void)
201 cl_git_pass(apply_buf(
202 FILE_ORIGINAL
, "file.txt",
203 FILE_APPEND
, "file.txt",
204 PATCH_ORIGINAL_TO_APPEND
, NULL
));
207 void test_apply_fromdiff__append_nocontext(void)
209 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
210 diff_opts
.context_lines
= 0;
212 cl_git_pass(apply_buf(
213 FILE_ORIGINAL
, "file.txt",
214 FILE_APPEND
, "file.txt",
215 PATCH_ORIGINAL_TO_APPEND_NOCONTEXT
, &diff_opts
));
218 void test_apply_fromdiff__prepend_and_append(void)
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
));
226 void test_apply_fromdiff__to_empty_file(void)
228 cl_git_pass(apply_buf(
229 FILE_ORIGINAL
, "file.txt",
231 PATCH_ORIGINAL_TO_EMPTY_FILE
, NULL
));
234 void test_apply_fromdiff__from_empty_file(void)
236 cl_git_pass(apply_buf(
238 FILE_ORIGINAL
, "file.txt",
239 PATCH_EMPTY_FILE_TO_ORIGINAL
, NULL
));
242 void test_apply_fromdiff__add(void)
244 cl_git_pass(apply_buf(
246 FILE_ORIGINAL
, "file.txt",
247 PATCH_ADD_ORIGINAL
, NULL
));
250 void test_apply_fromdiff__delete(void)
252 cl_git_pass(apply_buf(
253 FILE_ORIGINAL
, "file.txt",
255 PATCH_DELETE_ORIGINAL
, NULL
));
258 void test_apply_fromdiff__no_change(void)
260 cl_git_pass(apply_buf(
261 FILE_ORIGINAL
, "file.txt",
262 FILE_ORIGINAL
, "file.txt",
266 void test_apply_fromdiff__binary_add(void)
268 git_buf newfile
= GIT_BUF_INIT
;
270 newfile
.ptr
= FILE_BINARY_DELTA_MODIFIED
;
271 newfile
.size
= FILE_BINARY_DELTA_MODIFIED_LEN
;
273 cl_git_pass(apply_gitbuf(
275 &newfile
, "binary.bin",
276 NULL
, &binary_opts
));
279 void test_apply_fromdiff__binary_no_change(void)
281 git_buf original
= GIT_BUF_INIT
;
283 original
.ptr
= FILE_BINARY_DELTA_ORIGINAL
;
284 original
.size
= FILE_BINARY_DELTA_ORIGINAL_LEN
;
286 cl_git_pass(apply_gitbuf(
287 &original
, "binary.bin",
288 &original
, "binary.bin",
292 void test_apply_fromdiff__binary_change_delta(void)
294 git_buf original
= GIT_BUF_INIT
, modified
= GIT_BUF_INIT
;
296 original
.ptr
= FILE_BINARY_DELTA_ORIGINAL
;
297 original
.size
= FILE_BINARY_DELTA_ORIGINAL_LEN
;
299 modified
.ptr
= FILE_BINARY_DELTA_MODIFIED
;
300 modified
.size
= FILE_BINARY_DELTA_MODIFIED_LEN
;
302 cl_git_pass(apply_gitbuf(
303 &original
, "binary.bin",
304 &modified
, "binary.bin",
305 NULL
, &binary_opts
));
308 void test_apply_fromdiff__binary_change_literal(void)
310 git_buf original
= GIT_BUF_INIT
, modified
= GIT_BUF_INIT
;
312 original
.ptr
= FILE_BINARY_LITERAL_ORIGINAL
;
313 original
.size
= FILE_BINARY_LITERAL_ORIGINAL_LEN
;
315 modified
.ptr
= FILE_BINARY_LITERAL_MODIFIED
;
316 modified
.size
= FILE_BINARY_LITERAL_MODIFIED_LEN
;
318 cl_git_pass(apply_gitbuf(
319 &original
, "binary.bin",
320 &modified
, "binary.bin",
321 NULL
, &binary_opts
));
324 void test_apply_fromdiff__binary_delete(void)
326 git_buf original
= GIT_BUF_INIT
;
328 original
.ptr
= FILE_BINARY_DELTA_MODIFIED
;
329 original
.size
= FILE_BINARY_DELTA_MODIFIED_LEN
;
331 cl_git_pass(apply_gitbuf(
332 &original
, "binary.bin",
334 NULL
, &binary_opts
));