apply: resolve trivial merge without hitting ll-merge with "--3way"

The ll_binary_merge() function assumes that the ancestor blob is different from either side of the new versions, and always fails the merge in conflict, unless -Xours or -Xtheirs is in effect. The normal "merge" machineries all resolve the trivial cases (e.g. if our side changed while their side did not, the result is ours) without triggering the file-level merge drivers, so the assumption is warranted. The code path in "git apply --3way", however, does not check for the trivial three-way merge situation and always calls the file-level merge drivers. This used to be perfectly OK back when we always first attempted a straight patch application and used the three-way code path only as a fallback. Any binary patch that can be applied as a trivial three-way merge (e.g. the patch is based exactly on the version we happen to have) would always cleanly apply, so the ll_binary_merge() that is not prepared to see the trivial case would not have to handle such a case. This no longer is true after we made "--3way" to mean "first try three-way and then fall back to straight application", and made "git apply -3" on a binary patch that is based on the current version no longer apply. Teach "git apply -3" to first check for the trivial merge cases and resolve them without hitting the file-level merge drivers. Signed-off-by: Jerry Zhang <jerry@skydio.com> [jc: stolen tests from Jerry's patch] Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 5, 2021 at 12:06 UTC 57f183b698ca4f967266577687f09b09a79f0b8c
2 files changed +66
apply.c
+21
@@ -3467,6 +3467,21 @@ static int load_preimage(struct apply_state *state,
3467 return 0;
3468 }
3469
3470 +static int resolve_to(struct image *image, const struct object_id *result_id)
3471 +{
3472 + unsigned long size;
3473 + enum object_type type;
3474 +
3475 + clear_image(image);
3476 +
3477 + image->buf = read_object_file(result_id, &type, &size);
3478 + if (!image->buf || type != OBJ_BLOB)
3479 + die("unable to read blob object %s", oid_to_hex(result_id));
3480 + image->len = size;
3481 +
3482 + return 0;
3483 +}
3484 +
3485 static int three_way_merge(struct apply_state *state,
3486 struct image *image,
3487 char *path,
@@ -3478,6 +3493,12 @@ static int three_way_merge(struct apply_state *state,
3493 mmbuffer_t result = { NULL };
3494 int status;
3495
3496 + /* resolve trivial cases first */
3497 + if (oideq(base, ours))
3498 + return resolve_to(image, theirs);
3499 + else if (oideq(base, theirs) || oideq(ours, theirs))
3500 + return resolve_to(image, ours);
3501 +
3502 read_mmblob(&base_file, base);
3503 read_mmblob(&our_file, ours);
3504 read_mmblob(&their_file, theirs);
t/t4108-apply-threeway.sh
+45
@@ -230,4 +230,49 @@ test_expect_success 'apply with --3way --cached and conflicts' '
230 test_cmp expect.diff actual.diff
231 '
232
233 +test_expect_success 'apply binary file patch' '
234 + git reset --hard main &&
235 + cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
236 + git add bin.png &&
237 + git commit -m "add binary file" &&
238 +
239 + cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
240 +
241 + git diff --binary >bin.diff &&
242 + git reset --hard &&
243 +
244 + # Apply must succeed.
245 + git apply bin.diff
246 +'
247 +
248 +test_expect_success 'apply binary file patch with 3way' '
249 + git reset --hard main &&
250 + cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
251 + git add bin.png &&
252 + git commit -m "add binary file" &&
253 +
254 + cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
255 +
256 + git diff --binary >bin.diff &&
257 + git reset --hard &&
258 +
259 + # Apply must succeed.
260 + git apply --3way --index bin.diff
261 +'
262 +
263 +test_expect_success 'apply full-index patch with 3way' '
264 + git reset --hard main &&
265 + cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
266 + git add bin.png &&
267 + git commit -m "add binary file" &&
268 +
269 + cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
270 +
271 + git diff --full-index >bin.diff &&
272 + git reset --hard &&
273 +
274 + # Apply must succeed.
275 + git apply --3way --index bin.diff
276 +'
277 +
278 test_done