git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] git-apply: fix --3way with binary patch

From
Jerry Zhang <jerry@skydio.com>
Date
Jul 28, 2021, 02:44 UTC
Message-ID
<20210728024434.20230-1-jerry@skydio.com>

Binary patches applied with "--3way" will always return a conflict even if the patch should cleanly apply because the low level merge function considers all binary merges without a variant to be conflicting.

Fix by falling back to normal patch application for all binary patches.

Add tests for --3way and normal applications of binary patches.

Fixes: 923cd87ac8 ("git-apply: try threeway first when "--3way" is used")
Signed-off-by: Jerry Zhang <jerry@skydio.com>
---
 apply.c                   |  3 ++-
 t/t4108-apply-threeway.sh | 45 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
diff --git a/apply.c b/apply.c
index 1d2d7e124e..78e52f0dc1 100644
--- a/apply.c
+++ b/apply.c
@@ -3638,7 +3638,8 @@ static int apply_data(struct apply_state *state, struct patch *patch,
 	if (load_preimage(state, &image, patch, st, ce) < 0)
 		return -1;
 
-	if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0) {
+	if (!state->threeway || patch->is_binary ||
+		try_threeway(state, &image, patch, st, ce) < 0) {
 		if (state->apply_verbosity > verbosity_silent &&
 		    state->threeway && !patch->direct_to_threeway)
 			fprintf(stderr, _("Falling back to direct application...\n"));
diff --git a/t/t4108-apply-threeway.sh b/t/t4108-apply-threeway.sh
index 65147efdea..d32748f899 100755
--- a/t/t4108-apply-threeway.sh
+++ b/t/t4108-apply-threeway.sh
@@ -230,4 +230,49 @@ test_expect_success 'apply with --3way --cached and conflicts' '
 	test_cmp expect.diff actual.diff
 '
 
+test_expect_success 'apply binary file patch' '
+	git reset --hard main &&
+	cp $TEST_DIRECTORY/test-binary-1.png bin.png &&
+	git add bin.png &&
+	git commit -m "add binary file" &&
+
+	cp $TEST_DIRECTORY/test-binary-2.png bin.png &&
+
+	git diff --binary >bin.diff &&
+	git reset --hard &&
+
+	# Apply must succeed.
+	git apply bin.diff
+'
+
+test_expect_success 'apply binary file patch with 3way' '
+	git reset --hard main &&
+	cp $TEST_DIRECTORY/test-binary-1.png bin.png &&
+	git add bin.png &&
+	git commit -m "add binary file" &&
+
+	cp $TEST_DIRECTORY/test-binary-2.png bin.png &&
+
+	git diff --binary >bin.diff &&
+	git reset --hard &&
+
+	# Apply must succeed.
+	git apply --3way --index bin.diff
+'
+
+test_expect_success 'apply full-index patch with 3way' '
+	git reset --hard main &&
+	cp $TEST_DIRECTORY/test-binary-1.png bin.png &&
+	git add bin.png &&
+	git commit -m "add binary file" &&
+
+	cp $TEST_DIRECTORY/test-binary-2.png bin.png &&
+
+	git diff --full-index >bin.diff &&
+	git reset --hard &&
+
+	# Apply must succeed.
+	git apply --3way --index bin.diff
+'
+
 test_done
-- 
2.32.0.1314.g6ed4fcc4cc
Next: Junio C Hamano
Message 1 of 16 in “git-apply: fix --3way with binary patch”
  1. git-apply: fix --3way with binary patchJerry Zhang, Jul 28, 2021
  2. Junio C HamanoJul 28, 2021
  3. Junio C HamanoJul 28, 2021
  4. ll-merge: teach ll_binary_merge() a trivial three-way mergeJunio C Hamano, Jul 28, 2021
  5. Elijah NewrenJul 28, 2021
  6. Junio C HamanoJul 29, 2021
  7. apply: resolve trivial merge without hitting ll-merge with "--3way"Junio C Hamano, Sep 5, 2021
  8. Elijah NewrenSep 6, 2021
  9. Ævar Arnfjörð BjarmasonSep 6, 2021
  10. Junio C HamanoSep 7, 2021
  11. Junio C HamanoSep 7, 2021
  12. Jerry ZhangJul 28, 2021
  13. Jerry ZhangJul 28, 2021
  14. Junio C HamanoJul 28, 2021
  15. Jerry ZhangJul 28, 2021
  16. Junio C HamanoJul 28, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.