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