apply: introduce a hunk callback Introduce a callback to patch application that allows consumers to cancel hunk application.
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
diff --git a/include/git2/apply.h b/include/git2/apply.h
index eb9cad4..7cc1c22 100644
--- a/include/git2/apply.h
+++ b/include/git2/apply.h
@@ -38,6 +38,22 @@ typedef int (*git_apply_delta_cb)(
void *payload);
/**
+ * When applying a patch, callback that will be made per hunk.
+ *
+ * When the callback:
+ * - returns < 0, the apply process will be aborted.
+ * - returns > 0, the hunk will not be applied, but the apply process
+ * continues
+ * - returns 0, the hunk is applied, and the apply process continues.
+ *
+ * @param hunk The hunk to be applied
+ * @param payload User-specified payload
+ */
+typedef int (*git_apply_hunk_cb)(
+ const git_diff_hunk *hunk,
+ void *payload);
+
+/**
* Apply options structure
*
* Initialize with `GIT_APPLY_OPTIONS_INIT`. Alternatively, you can
@@ -49,6 +65,7 @@ typedef struct {
unsigned int version;
git_apply_delta_cb delta_cb;
+ git_apply_hunk_cb hunk_cb;
void *payload;
} git_apply_options;
diff --git a/src/apply.c b/src/apply.c
index 9534267..8d94e78 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -167,15 +167,36 @@ static int update_hunk(
return 0;
}
+typedef struct {
+ git_apply_options opts;
+ size_t skipped_new_lines;
+ size_t skipped_old_lines;
+} apply_hunks_ctx;
+
static int apply_hunk(
patch_image *image,
git_patch *patch,
- git_patch_hunk *hunk)
+ git_patch_hunk *hunk,
+ apply_hunks_ctx *ctx)
{
patch_image preimage = PATCH_IMAGE_INIT, postimage = PATCH_IMAGE_INIT;
size_t line_num, i;
int error = 0;
+ if (ctx->opts.hunk_cb) {
+ error = ctx->opts.hunk_cb(&hunk->hunk, ctx->opts.payload);
+
+ if (error) {
+ if (error > 0) {
+ ctx->skipped_new_lines += hunk->hunk.new_lines;
+ ctx->skipped_old_lines += hunk->hunk.old_lines;
+ error = 0;
+ }
+
+ goto done;
+ }
+ }
+
for (i = 0; i < hunk->line_count; i++) {
size_t linenum = hunk->line_start + i;
git_diff_line *line = git_array_get(patch->lines, linenum);
@@ -198,7 +219,14 @@ static int apply_hunk(
}
}
- line_num = hunk->hunk.new_start ? hunk->hunk.new_start - 1 : 0;
+ if (hunk->hunk.new_start) {
+ line_num = hunk->hunk.new_start -
+ ctx->skipped_new_lines +
+ ctx->skipped_old_lines -
+ 1;
+ } else {
+ line_num = 0;
+ }
if (!find_hunk_linenum(&line_num, image, &preimage, line_num)) {
error = apply_err("hunk at line %d did not apply",
@@ -219,7 +247,8 @@ static int apply_hunks(
git_buf *out,
const char *source,
size_t source_len,
- git_patch *patch)
+ git_patch *patch,
+ apply_hunks_ctx *ctx)
{
git_patch_hunk *hunk;
git_diff_line *line;
@@ -231,7 +260,7 @@ static int apply_hunks(
goto done;
git_array_foreach(patch->hunks, i, hunk) {
- if ((error = apply_hunk(&image, patch, hunk)) < 0)
+ if ((error = apply_hunk(&image, patch, hunk, ctx)) < 0)
goto done;
}
@@ -339,14 +368,19 @@ int git_apply__patch(
unsigned int *mode_out,
const char *source,
size_t source_len,
- git_patch *patch)
+ git_patch *patch,
+ const git_apply_options *given_opts)
{
+ apply_hunks_ctx ctx = { GIT_APPLY_OPTIONS_INIT };
char *filename = NULL;
unsigned int mode = 0;
int error = 0;
assert(contents_out && filename_out && mode_out && (source || !source_len) && patch);
+ if (given_opts)
+ memcpy(&ctx.opts, given_opts, sizeof(git_apply_options));
+
*filename_out = NULL;
*mode_out = 0;
@@ -361,7 +395,7 @@ int git_apply__patch(
if (patch->delta->flags & GIT_DIFF_FLAG_BINARY)
error = apply_binary(contents_out, source, source_len, patch);
else if (patch->hunks.size)
- error = apply_hunks(contents_out, source, source_len, patch);
+ error = apply_hunks(contents_out, source, source_len, patch, &ctx);
else
error = git_buf_put(contents_out, source, source_len);
@@ -457,7 +491,7 @@ static int apply_one(
if (delta->status != GIT_DELTA_DELETED) {
if ((error = git_apply__patch(&post_contents, &filename, &mode,
- pre_contents.ptr, pre_contents.size, patch)) < 0 ||
+ pre_contents.ptr, pre_contents.size, patch, opts)) < 0 ||
(error = git_blob_create_frombuffer(&post_id, repo,
post_contents.ptr, post_contents.size)) < 0)
goto done;
diff --git a/src/apply.h b/src/apply.h
index b29460c..11ec756 100644
--- a/src/apply.h
+++ b/src/apply.h
@@ -10,6 +10,7 @@
#include "common.h"
#include "git2/patch.h"
+#include "git2/apply.h"
#include "buffer.h"
extern int git_apply__patch(
@@ -18,6 +19,7 @@ extern int git_apply__patch(
unsigned int *mode,
const char *source,
size_t source_len,
- git_patch *patch);
+ git_patch *patch,
+ const git_apply_options *opts);
#endif
diff --git a/tests/apply/callbacks.c b/tests/apply/callbacks.c
index 91bdfa3..1b759dc 100644
--- a/tests/apply/callbacks.c
+++ b/tests/apply/callbacks.c
@@ -88,3 +88,41 @@ void test_apply_callbacks__delta_can_skip(void)
git_diff_free(diff);
}
+
+static int hunk_skip_odds_cb(const git_diff_hunk *hunk, void *payload)
+{
+ int *count = (int *)payload;
+ GIT_UNUSED(hunk);
+
+ return ((*count)++ % 2 == 1);
+}
+
+void test_apply_callbacks__hunk_can_skip(void)
+{
+ git_diff *diff;
+ git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
+ int count = 0;
+
+ struct merge_index_entry workdir_expected[] = {
+ { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
+ { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
+ { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
+ { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
+ { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
+ { 0100644, "06f751b6ba4f017ddbf4248015768300268e092a", 0, "veal.txt" },
+ };
+ size_t workdir_expected_cnt = sizeof(workdir_expected) /
+ sizeof(struct merge_index_entry);
+
+ opts.hunk_cb = hunk_skip_odds_cb;
+ opts.payload = &count;
+
+ cl_git_pass(git_diff_from_buffer(&diff,
+ DIFF_MANY_CHANGES_ONE, strlen(DIFF_MANY_CHANGES_ONE)));
+ cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_WORKDIR, &opts));
+
+ validate_index_unchanged(repo);
+ validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
+
+ git_diff_free(diff);
+}
diff --git a/tests/apply/fromdiff.c b/tests/apply/fromdiff.c
index 6110609..3b15602 100644
--- a/tests/apply/fromdiff.c
+++ b/tests/apply/fromdiff.c
@@ -49,7 +49,7 @@ static int apply_gitbuf(
cl_assert_equal_s(patch_expected, patchbuf.ptr);
}
- error = git_apply__patch(&result, &filename, &mode, old ? old->ptr : NULL, old ? old->size : 0, patch);
+ error = git_apply__patch(&result, &filename, &mode, old ? old->ptr : NULL, old ? old->size : 0, patch, NULL);
if (error == 0 && new == NULL) {
cl_assert_equal_i(0, result.size);
diff --git a/tests/apply/fromfile.c b/tests/apply/fromfile.c
index 0ace639..6d4379b 100644
--- a/tests/apply/fromfile.c
+++ b/tests/apply/fromfile.c
@@ -39,7 +39,7 @@ static int apply_patchfile(
cl_git_pass(git_patch_from_buffer(&patch, patchfile, strlen(patchfile), NULL));
- error = git_apply__patch(&result, &filename, &mode, old, old_len, patch);
+ error = git_apply__patch(&result, &filename, &mode, old, old_len, patch, NULL);
if (error == 0) {
cl_assert_equal_i(new_len, result.size);