]>
Commit | Line | Data |
---|---|---|
ac3d33df JK |
1 | #include "clar_libgit2.h" |
2 | #include "git2/sys/repository.h" | |
3 | ||
4 | #include "apply.h" | |
5 | #include "repository.h" | |
ac3d33df JK |
6 | |
7 | #include "../patch/patch_common.h" | |
8 | ||
9 | static git_repository *repo = NULL; | |
10 | ||
11 | void test_apply_partial__initialize(void) | |
12 | { | |
13 | repo = cl_git_sandbox_init("renames"); | |
14 | } | |
15 | ||
16 | void test_apply_partial__cleanup(void) | |
17 | { | |
18 | cl_git_sandbox_cleanup(); | |
19 | } | |
20 | ||
21 | static int skip_addition( | |
22 | const git_diff_hunk *hunk, | |
23 | void *payload) | |
24 | { | |
25 | GIT_UNUSED(payload); | |
26 | ||
27 | return (hunk->new_lines > hunk->old_lines) ? 1 : 0; | |
28 | } | |
29 | ||
30 | static int skip_deletion( | |
31 | const git_diff_hunk *hunk, | |
32 | void *payload) | |
33 | { | |
34 | GIT_UNUSED(payload); | |
35 | ||
36 | return (hunk->new_lines < hunk->old_lines) ? 1 : 0; | |
37 | } | |
38 | ||
39 | static int skip_change( | |
40 | const git_diff_hunk *hunk, | |
41 | void *payload) | |
42 | { | |
43 | GIT_UNUSED(payload); | |
44 | ||
45 | return (hunk->new_lines == hunk->old_lines) ? 1 : 0; | |
46 | } | |
47 | ||
48 | static int abort_addition( | |
49 | const git_diff_hunk *hunk, | |
50 | void *payload) | |
51 | { | |
52 | GIT_UNUSED(payload); | |
53 | ||
54 | return (hunk->new_lines > hunk->old_lines) ? GIT_EUSER : 0; | |
55 | } | |
56 | ||
57 | static int abort_deletion( | |
58 | const git_diff_hunk *hunk, | |
59 | void *payload) | |
60 | { | |
61 | GIT_UNUSED(payload); | |
62 | ||
63 | return (hunk->new_lines < hunk->old_lines) ? GIT_EUSER : 0; | |
64 | } | |
65 | ||
66 | static int abort_change( | |
67 | const git_diff_hunk *hunk, | |
68 | void *payload) | |
69 | { | |
70 | GIT_UNUSED(payload); | |
71 | ||
72 | return (hunk->new_lines == hunk->old_lines) ? GIT_EUSER : 0; | |
73 | } | |
74 | ||
75 | static int apply_buf( | |
76 | const char *old, | |
77 | const char *oldname, | |
78 | const char *new, | |
79 | const char *newname, | |
80 | const char *expected, | |
81 | const git_diff_options *diff_opts, | |
82 | git_apply_hunk_cb hunk_cb, | |
83 | void *payload) | |
84 | { | |
85 | git_patch *patch; | |
e579e0f7 MB |
86 | git_str result = GIT_STR_INIT; |
87 | git_str patchbuf = GIT_STR_INIT; | |
ac3d33df JK |
88 | git_apply_options opts = GIT_APPLY_OPTIONS_INIT; |
89 | char *filename; | |
90 | unsigned int mode; | |
91 | int error; | |
92 | size_t oldsize = strlen(old); | |
93 | size_t newsize = strlen(new); | |
94 | ||
95 | opts.hunk_cb = hunk_cb; | |
96 | opts.payload = payload; | |
97 | ||
98 | cl_git_pass(git_patch_from_buffers(&patch, old, oldsize, oldname, new, newsize, newname, diff_opts)); | |
99 | if ((error = git_apply__patch(&result, &filename, &mode, old, oldsize, patch, &opts)) == 0) { | |
100 | cl_assert_equal_s(expected, result.ptr); | |
101 | cl_assert_equal_s(newname, filename); | |
102 | cl_assert_equal_i(0100644, mode); | |
103 | } | |
104 | ||
105 | git__free(filename); | |
e579e0f7 MB |
106 | git_str_dispose(&result); |
107 | git_str_dispose(&patchbuf); | |
ac3d33df JK |
108 | git_patch_free(patch); |
109 | ||
110 | return error; | |
111 | } | |
112 | ||
113 | void test_apply_partial__prepend_and_change_skip_addition(void) | |
114 | { | |
115 | cl_git_pass(apply_buf( | |
116 | FILE_ORIGINAL, "file.txt", | |
117 | FILE_PREPEND_AND_CHANGE, "file.txt", | |
118 | FILE_ORIGINAL, NULL, skip_addition, NULL)); | |
119 | } | |
120 | ||
121 | void test_apply_partial__prepend_and_change_nocontext_skip_addition(void) | |
122 | { | |
123 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
124 | diff_opts.context_lines = 0; | |
125 | ||
126 | cl_git_pass(apply_buf( | |
127 | FILE_ORIGINAL, "file.txt", | |
128 | FILE_PREPEND_AND_CHANGE, "file.txt", | |
129 | FILE_CHANGE_MIDDLE, &diff_opts, skip_addition, NULL)); | |
130 | } | |
131 | ||
132 | void test_apply_partial__prepend_and_change_nocontext_abort_addition(void) | |
133 | { | |
134 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
135 | diff_opts.context_lines = 0; | |
136 | ||
137 | cl_git_fail(apply_buf( | |
138 | FILE_ORIGINAL, "file.txt", | |
139 | FILE_PREPEND_AND_CHANGE, "file.txt", | |
140 | FILE_ORIGINAL, &diff_opts, abort_addition, NULL)); | |
141 | } | |
142 | ||
143 | void test_apply_partial__prepend_and_change_skip_change(void) | |
144 | { | |
145 | cl_git_pass(apply_buf( | |
146 | FILE_ORIGINAL, "file.txt", | |
147 | FILE_PREPEND_AND_CHANGE, "file.txt", | |
148 | FILE_PREPEND_AND_CHANGE, NULL, skip_change, NULL)); | |
149 | } | |
150 | ||
151 | void test_apply_partial__prepend_and_change_nocontext_skip_change(void) | |
152 | { | |
153 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
154 | diff_opts.context_lines = 0; | |
155 | ||
156 | cl_git_pass(apply_buf( | |
157 | FILE_ORIGINAL, "file.txt", | |
158 | FILE_PREPEND_AND_CHANGE, "file.txt", | |
159 | FILE_PREPEND, &diff_opts, skip_change, NULL)); | |
160 | } | |
161 | ||
162 | void test_apply_partial__prepend_and_change_nocontext_abort_change(void) | |
163 | { | |
164 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
165 | diff_opts.context_lines = 0; | |
166 | ||
167 | cl_git_fail(apply_buf( | |
168 | FILE_ORIGINAL, "file.txt", | |
169 | FILE_PREPEND_AND_CHANGE, "file.txt", | |
170 | FILE_PREPEND, &diff_opts, abort_change, NULL)); | |
171 | } | |
172 | ||
173 | void test_apply_partial__delete_and_change_skip_deletion(void) | |
174 | { | |
175 | cl_git_pass(apply_buf( | |
176 | FILE_ORIGINAL, "file.txt", | |
177 | FILE_DELETE_AND_CHANGE, "file.txt", | |
178 | FILE_ORIGINAL, NULL, skip_deletion, NULL)); | |
179 | } | |
180 | ||
181 | void test_apply_partial__delete_and_change_nocontext_skip_deletion(void) | |
182 | { | |
183 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
184 | diff_opts.context_lines = 0; | |
185 | ||
186 | cl_git_pass(apply_buf( | |
187 | FILE_ORIGINAL, "file.txt", | |
188 | FILE_DELETE_AND_CHANGE, "file.txt", | |
189 | FILE_CHANGE_MIDDLE, &diff_opts, skip_deletion, NULL)); | |
190 | } | |
191 | ||
192 | void test_apply_partial__delete_and_change_nocontext_abort_deletion(void) | |
193 | { | |
194 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
195 | diff_opts.context_lines = 0; | |
196 | ||
197 | cl_git_fail(apply_buf( | |
198 | FILE_ORIGINAL, "file.txt", | |
199 | FILE_DELETE_AND_CHANGE, "file.txt", | |
200 | FILE_ORIGINAL, &diff_opts, abort_deletion, NULL)); | |
201 | } | |
202 | ||
203 | void test_apply_partial__delete_and_change_skip_change(void) | |
204 | { | |
205 | cl_git_pass(apply_buf( | |
206 | FILE_ORIGINAL, "file.txt", | |
207 | FILE_DELETE_AND_CHANGE, "file.txt", | |
208 | FILE_DELETE_AND_CHANGE, NULL, skip_change, NULL)); | |
209 | } | |
210 | ||
211 | void test_apply_partial__delete_and_change_nocontext_skip_change(void) | |
212 | { | |
213 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
214 | diff_opts.context_lines = 0; | |
215 | ||
216 | cl_git_pass(apply_buf( | |
217 | FILE_ORIGINAL, "file.txt", | |
218 | FILE_DELETE_AND_CHANGE, "file.txt", | |
219 | FILE_DELETE_FIRSTLINE, &diff_opts, skip_change, NULL)); | |
220 | } | |
221 | ||
222 | void test_apply_partial__delete_and_change_nocontext_abort_change(void) | |
223 | { | |
224 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
225 | diff_opts.context_lines = 0; | |
226 | ||
227 | cl_git_fail(apply_buf( | |
228 | FILE_ORIGINAL, "file.txt", | |
229 | FILE_DELETE_AND_CHANGE, "file.txt", | |
230 | FILE_DELETE_FIRSTLINE, &diff_opts, abort_change, NULL)); | |
231 | } |