Merge pull request #4160 from pks-t/pks/diff-fixes Diff fixes
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
diff --git a/src/diff.c b/src/diff.c
index 317d495..a93bd4c 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -120,6 +120,41 @@ int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
return 0;
}
+int git_diff_foreach(
+ git_diff *diff,
+ git_diff_file_cb file_cb,
+ git_diff_binary_cb binary_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_line_cb data_cb,
+ void *payload)
+{
+ int error = 0;
+ git_diff_delta *delta;
+ size_t idx;
+
+ assert(diff);
+
+ git_vector_foreach(&diff->deltas, idx, delta) {
+ git_patch *patch;
+
+ /* check flags against patch status */
+ if (git_diff_delta__should_skip(&diff->opts, delta))
+ continue;
+
+ if ((error = git_patch_from_diff(&patch, diff, idx)) != 0)
+ break;
+
+ error = git_patch__invoke_callbacks(patch, file_cb, binary_cb,
+ hunk_cb, data_cb, payload);
+ git_patch_free(patch);
+
+ if (error)
+ break;
+ }
+
+ return error;
+}
+
int git_diff_format_email__append_header_tobuf(
git_buf *out,
const git_oid *id,
diff --git a/src/diff_parse.c b/src/diff_parse.c
index e640063..9391568 100644
--- a/src/diff_parse.c
+++ b/src/diff_parse.c
@@ -37,7 +37,6 @@ static git_diff_parsed *diff_parsed_alloc(void)
GIT_REFCOUNT_INC(diff);
diff->base.type = GIT_DIFF_TYPE_PARSED;
- diff->base.opts.flags &= ~GIT_DIFF_IGNORE_CASE;
diff->base.strcomp = git__strcmp;
diff->base.strncomp = git__strncmp;
diff->base.pfxcomp = git__prefixcmp;
@@ -45,6 +44,9 @@ static git_diff_parsed *diff_parsed_alloc(void)
diff->base.patch_fn = git_patch_parsed_from_diff;
diff->base.free_fn = diff_parsed_free;
+ git_diff_init_options(&diff->base.opts, GIT_DIFF_OPTIONS_VERSION);
+ diff->base.opts.flags &= ~GIT_DIFF_IGNORE_CASE;
+
git_pool_init(&diff->base.pool, 1);
if (git_vector_init(&diff->patches, 0, NULL) < 0 ||
diff --git a/src/patch_generate.c b/src/patch_generate.c
index ab68f58..804fc0e 100644
--- a/src/patch_generate.c
+++ b/src/patch_generate.c
@@ -206,35 +206,14 @@ static int patch_generated_load(git_patch_generated *patch, git_patch_generated_
((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 ||
(patch->nfile.file->flags & GIT_DIFF_FLAG_VALID_ID) != 0));
- /* always try to load workdir content first because filtering may
- * need 2x data size and this minimizes peak memory footprint
- */
- if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = git_diff_file_content__load(
- &patch->ofile, &patch->base.diff_opts)) < 0 ||
- should_skip_binary(patch, patch->ofile.file))
- goto cleanup;
- }
- if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = git_diff_file_content__load(
- &patch->nfile, &patch->base.diff_opts)) < 0 ||
- should_skip_binary(patch, patch->nfile.file))
- goto cleanup;
- }
-
- /* once workdir has been tried, load other data as needed */
- if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = git_diff_file_content__load(
- &patch->ofile, &patch->base.diff_opts)) < 0 ||
- should_skip_binary(patch, patch->ofile.file))
- goto cleanup;
- }
- if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
- if ((error = git_diff_file_content__load(
- &patch->nfile, &patch->base.diff_opts)) < 0 ||
- should_skip_binary(patch, patch->nfile.file))
- goto cleanup;
- }
+ if ((error = git_diff_file_content__load(
+ &patch->ofile, &patch->base.diff_opts)) < 0 ||
+ should_skip_binary(patch, patch->ofile.file))
+ goto cleanup;
+ if ((error = git_diff_file_content__load(
+ &patch->nfile, &patch->base.diff_opts)) < 0 ||
+ should_skip_binary(patch, patch->nfile.file))
+ goto cleanup;
/* if previously missing an oid, and now that we have it the two sides
* are the same (and not submodules), update MODIFIED -> UNMODIFIED
@@ -421,56 +400,6 @@ static int diff_required(git_diff *diff, const char *action)
return -1;
}
-int git_diff_foreach(
- git_diff *diff,
- git_diff_file_cb file_cb,
- git_diff_binary_cb binary_cb,
- git_diff_hunk_cb hunk_cb,
- git_diff_line_cb data_cb,
- void *payload)
-{
- int error = 0;
- git_xdiff_output xo;
- size_t idx;
- git_patch_generated patch;
-
- if ((error = diff_required(diff, "git_diff_foreach")) < 0)
- return error;
-
- memset(&xo, 0, sizeof(xo));
- memset(&patch, 0, sizeof(patch));
- diff_output_init(
- &xo.output, &diff->opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
- git_xdiff_init(&xo, &diff->opts);
-
- git_vector_foreach(&diff->deltas, idx, patch.base.delta) {
-
- /* check flags against patch status */
- if (git_diff_delta__should_skip(&diff->opts, patch.base.delta))
- continue;
-
- if (binary_cb || hunk_cb || data_cb) {
- if ((error = patch_generated_init(&patch, diff, idx)) != 0 ||
- (error = patch_generated_load(&patch, &xo.output)) != 0) {
- git_patch_free(&patch.base);
- return error;
- }
- }
-
- if ((error = patch_generated_invoke_file_callback(&patch, &xo.output)) == 0) {
- if (binary_cb || hunk_cb || data_cb)
- error = patch_generated_create(&patch, &xo.output);
- }
-
- git_patch_free(&patch.base);
-
- if (error)
- break;
- }
-
- return error;
-}
-
typedef struct {
git_patch_generated patch;
git_diff_delta delta;
diff --git a/src/patch_parse.c b/src/patch_parse.c
index f527594..d993c03 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -562,8 +562,9 @@ static int parse_hunk_body(
int newlines = hunk->hunk.new_lines;
for (;
- ctx->remain_len > 4 && (oldlines || newlines) &&
- memcmp(ctx->line, "@@ -", 4) != 0;
+ ctx->remain_len > 1 &&
+ (oldlines || newlines) &&
+ (ctx->remain_len <= 4 || memcmp(ctx->line, "@@ -", 4) != 0);
parse_advance_line(ctx)) {
int origin;
diff --git a/tests/diff/parse.c b/tests/diff/parse.c
index a06813d..acb6eb8 100644
--- a/tests/diff/parse.c
+++ b/tests/diff/parse.c
@@ -196,3 +196,74 @@ void test_diff_parse__get_patch_from_diff(void)
cl_git_sandbox_cleanup();
}
+
+static int file_cb(const git_diff_delta *delta, float progress, void *payload)
+{
+ int *called = (int *) payload;
+ GIT_UNUSED(delta);
+ GIT_UNUSED(progress);
+ (*called)++;
+ return 0;
+}
+
+void test_diff_parse__foreach_works_with_parsed_patch(void)
+{
+ const char patch[] =
+ "diff --git a/obj1 b/obj2\n"
+ "index 1234567..7654321 10644\n"
+ "--- a/obj1\n"
+ "+++ b/obj2\n"
+ "@@ -1 +1 @@\n"
+ "-abcde\n"
+ "+12345\n";
+ int called = 0;
+ git_diff *diff;
+
+ cl_git_pass(git_diff_from_buffer(&diff, patch, strlen(patch)));
+ cl_git_pass(git_diff_foreach(diff, file_cb, NULL, NULL, NULL, &called));
+ cl_assert_equal_i(called, 1);
+
+ git_diff_free(diff);
+}
+
+void test_diff_parse__parsing_minimal_patch_succeeds(void)
+{
+ const char patch[] =
+ "diff --git a/obj1 b/obj2\n"
+ "index 1234567..7654321 10644\n"
+ "--- a/obj1\n"
+ "+++ b/obj2\n"
+ "@@ -1 +1 @@\n"
+ "-a\n"
+ "+\n";
+ git_buf buf = GIT_BUF_INIT;
+ git_diff *diff;
+
+ cl_git_pass(git_diff_from_buffer(&diff, patch, strlen(patch)));
+ cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
+ cl_assert_equal_s(patch, buf.ptr);
+
+ git_diff_free(diff);
+ git_buf_free(&buf);
+}
+
+void test_diff_parse__patch_roundtrip_succeeds(void)
+{
+ const char buf1[] = "a\n", buf2[] = "b\n";
+ git_buf patchbuf = GIT_BUF_INIT, diffbuf = GIT_BUF_INIT;
+ git_patch *patch;
+ git_diff *diff;
+
+ cl_git_pass(git_patch_from_buffers(&patch, buf1, strlen(buf1), "obj1", buf2, strlen(buf2), "obj2", NULL));
+ cl_git_pass(git_patch_to_buf(&patchbuf, patch));
+
+ cl_git_pass(git_diff_from_buffer(&diff, patchbuf.ptr, patchbuf.size));
+ cl_git_pass(git_diff_to_buf(&diffbuf, diff, GIT_DIFF_FORMAT_PATCH));
+
+ cl_assert_equal_s(patchbuf.ptr, diffbuf.ptr);
+
+ git_patch_free(patch);
+ git_diff_free(diff);
+ git_buf_free(&patchbuf);
+ git_buf_free(&diffbuf);
+}