patch: add support for partial patch application Add hunk callback parameter to git_apply__patch to allow hunks to be skipped.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
diff --git a/tests/apply/fromdiff.c b/tests/apply/fromdiff.c
index 3b15602..8a6d8fa 100644
--- a/tests/apply/fromdiff.c
+++ b/tests/apply/fromdiff.c
@@ -150,6 +150,52 @@ void test_apply_fromdiff__prepend_nocontext(void)
PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT, &diff_opts));
}
+void test_apply_fromdiff__prepend_and_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE, NULL));
+}
+
+void test_apply_fromdiff__prepend_and_change_nocontext(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE_NOCONTEXT, &diff_opts));
+}
+
+void test_apply_fromdiff__delete_and_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_DELETE_AND_CHANGE, NULL));
+}
+
+void test_apply_fromdiff__delete_and_change_nocontext(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ PATCH_ORIGINAL_TO_DELETE_AND_CHANGE_NOCONTEXT, &diff_opts));
+}
+
+void test_apply_fromdiff__delete_firstline(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_FIRSTLINE, "file.txt",
+ PATCH_ORIGINAL_TO_DELETE_FIRSTLINE, NULL));
+}
+
void test_apply_fromdiff__append(void)
{
cl_git_pass(apply_buf(
diff --git a/tests/apply/partial.c b/tests/apply/partial.c
new file mode 100644
index 0000000..bdbf35a
--- /dev/null
+++ b/tests/apply/partial.c
@@ -0,0 +1,161 @@
+#include "clar_libgit2.h"
+#include "git2/sys/repository.h"
+
+#include "apply.h"
+#include "repository.h"
+#include "buf_text.h"
+
+#include "../patch/patch_common.h"
+
+static git_repository *repo = NULL;
+
+void test_apply_partial__initialize(void)
+{
+ repo = cl_git_sandbox_init("renames");
+}
+
+void test_apply_partial__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+static int skip_addition(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines > hunk->old_lines) ? 1 : 0;
+}
+
+static int skip_deletion(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines < hunk->old_lines) ? 1 : 0;
+}
+
+static int skip_change(
+ const git_diff_hunk *hunk,
+ void *payload)
+{
+ GIT_UNUSED(payload);
+
+ return (hunk->new_lines == hunk->old_lines) ? 1 : 0;
+}
+
+static int apply_buf(
+ const char *old,
+ const char *oldname,
+ const char *new,
+ const char *newname,
+ const char *expected,
+ const git_diff_options *diff_opts,
+ git_apply_hunk_cb hunk_cb,
+ void *payload)
+{
+ git_patch *patch;
+ git_buf result = GIT_BUF_INIT;
+ git_buf patchbuf = GIT_BUF_INIT;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+ char *filename;
+ unsigned int mode;
+ int error;
+ size_t oldsize = strlen(old);
+ size_t newsize = strlen(new);
+
+ opts.hunk_cb = hunk_cb;
+ opts.payload = payload;
+
+ cl_git_pass(git_patch_from_buffers(&patch, old, oldsize, oldname, new, newsize, newname, diff_opts));
+ if ((error = git_apply__patch(&result, &filename, &mode, old, oldsize, patch, &opts)) == 0) {
+ cl_assert_equal_s(expected, result.ptr);
+ cl_assert_equal_s(newname, filename);
+ cl_assert_equal_i(0100644, mode);
+ }
+
+ git__free(filename);
+ git_buf_free(&result);
+ git_buf_free(&patchbuf);
+ git_patch_free(patch);
+
+ return error;
+}
+
+void test_apply_partial__prepend_and_change_skip_addition(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_ORIGINAL, NULL, skip_addition, NULL));
+}
+
+void test_apply_partial__prepend_and_change_nocontext_skip_addition(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_CHANGE_MIDDLE, &diff_opts, skip_addition, NULL));
+}
+
+void test_apply_partial__prepend_and_change_skip_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_PREPEND_AND_CHANGE, NULL, skip_change, NULL));
+}
+
+void test_apply_partial__prepend_and_change_nocontext_skip_change(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_PREPEND_AND_CHANGE, "file.txt",
+ FILE_PREPEND, &diff_opts, skip_change, NULL));
+}
+
+void test_apply_partial__delete_and_change_skip_deletion(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_ORIGINAL, NULL, skip_deletion, NULL));
+}
+
+void test_apply_partial__delete_and_change_nocontext_skip_deletion(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_CHANGE_MIDDLE, &diff_opts, skip_deletion, NULL));
+}
+
+void test_apply_partial__delete_and_change_skip_change(void)
+{
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_DELETE_AND_CHANGE, NULL, skip_change, NULL));
+}
+
+void test_apply_partial__delete_and_change_nocontext_skip_change(void)
+{
+ git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
+ diff_opts.context_lines = 0;
+
+ cl_git_pass(apply_buf(
+ FILE_ORIGINAL, "file.txt",
+ FILE_DELETE_AND_CHANGE, "file.txt",
+ FILE_DELETE_FIRSTLINE, &diff_opts, skip_change, NULL));
+}
diff --git a/tests/patch/patch_common.h b/tests/patch/patch_common.h
index e838e60..3f2668d 100644
--- a/tests/patch/patch_common.h
+++ b/tests/patch/patch_common.h
@@ -220,6 +220,112 @@
"@@ -0,0 +1 @@\n" \
"+insert at front\n"
+/* An insertion at the beginning of the file and change in the middle */
+
+#define FILE_PREPEND_AND_CHANGE \
+ "insert at front\n" \
+ "hey!\n" \
+ "this is some context!\n" \
+ "around some lines\n" \
+ "that will change\n" \
+ "yes it is!\n" \
+ "(THIS line is changed!)\n" \
+ "and this\n" \
+ "is additional context\n" \
+ "below it!\n"
+
+#define PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..f73c8bb 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1,9 +1,10 @@\n" \
+ "+insert at front\n" \
+ " hey!\n" \
+ " this is some context!\n" \
+ " around some lines\n" \
+ " that will change\n" \
+ " yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n" \
+ " and this\n" \
+ " is additional context\n" \
+ " below it!\n"
+
+#define PATCH_ORIGINAL_TO_PREPEND_AND_CHANGE_NOCONTEXT \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..f73c8bb 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -0,0 +1 @@\n" \
+ "+insert at front\n" \
+ "@@ -6 +7 @@ yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n"
+
+/* A deletion at the beginning of the file and a change in the middle */
+
+#define FILE_DELETE_AND_CHANGE \
+ "this is some context!\n" \
+ "around some lines\n" \
+ "that will change\n" \
+ "yes it is!\n" \
+ "(THIS line is changed!)\n" \
+ "and this\n" \
+ "is additional context\n" \
+ "below it!\n"
+
+#define PATCH_ORIGINAL_TO_DELETE_AND_CHANGE \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..1e2dfa6 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1,9 +1,8 @@\n" \
+ "-hey!\n" \
+ " this is some context!\n" \
+ " around some lines\n" \
+ " that will change\n" \
+ " yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n" \
+ " and this\n" \
+ " is additional context\n" \
+ " below it!\n"
+
+#define PATCH_ORIGINAL_TO_DELETE_AND_CHANGE_NOCONTEXT \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..1e2dfa6 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1 +0,0 @@\n" \
+ "-hey!\n" \
+ "@@ -6 +5 @@ yes it is!\n" \
+ "-(this line is changed)\n" \
+ "+(THIS line is changed!)\n"
+
+/* A deletion at the beginning of the file */
+
+#define FILE_DELETE_FIRSTLINE \
+ "this is some context!\n" \
+ "around some lines\n" \
+ "that will change\n" \
+ "yes it is!\n" \
+ "(this line is changed)\n" \
+ "and this\n" \
+ "is additional context\n" \
+ "below it!\n"
+
+#define PATCH_ORIGINAL_TO_DELETE_FIRSTLINE \
+ "diff --git a/file.txt b/file.txt\n" \
+ "index 9432026..f31fa13 100644\n" \
+ "--- a/file.txt\n" \
+ "+++ b/file.txt\n" \
+ "@@ -1,4 +1,3 @@\n" \
+ "-hey!\n" \
+ " this is some context!\n" \
+ " around some lines\n" \
+ " that will change\n"
+
/* An insertion at the end of the file (and the resultant patch) */
#define FILE_APPEND \