All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] git-apply: fix --3way with binary patch
@ 2021-07-28  2:44 Jerry Zhang
  2021-07-28  4:29 ` Junio C Hamano
  2021-07-28  4:30 ` Junio C Hamano
  0 siblings, 2 replies; 17+ messages in thread
From: Jerry Zhang @ 2021-07-28  2:44 UTC (permalink / raw)
  To: git, gitster, lilinchao; +Cc: Jerry Zhang

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


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
  2021-07-28  2:44 [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
@ 2021-07-28  4:29 ` Junio C Hamano
  2021-07-28  4:30 ` Junio C Hamano
  1 sibling, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-07-28  4:29 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: git, lilinchao

Jerry Zhang <jerry@skydio.com> writes:

> 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 &&

Is it safe to use $TEST_DIRECTORY without quoting?  I doubt it, as
it is $(pwd) of whereever the testing user extracted our source
tarball.  

In other words, you'd need this.

diff --git w/t/t4108-apply-threeway.sh c/t/t4108-apply-threeway.sh
index d32748f899..cc3aa3314a 100755
--- w/t/t4108-apply-threeway.sh
+++ c/t/t4108-apply-threeway.sh
@@ -232,11 +232,11 @@ test_expect_success 'apply with --3way --cached and conflicts' '
 
 test_expect_success 'apply binary file patch' '
 	git reset --hard main &&
-	cp $TEST_DIRECTORY/test-binary-1.png bin.png &&
+	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 &&
+	cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
 
 	git diff --binary >bin.diff &&
 	git reset --hard &&
@@ -247,11 +247,11 @@ test_expect_success 'apply binary file patch' '
 
 test_expect_success 'apply binary file patch with 3way' '
 	git reset --hard main &&
-	cp $TEST_DIRECTORY/test-binary-1.png bin.png &&
+	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 &&
+	cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
 
 	git diff --binary >bin.diff &&
 	git reset --hard &&
@@ -262,11 +262,11 @@ test_expect_success 'apply binary file patch with 3way' '
 
 test_expect_success 'apply full-index patch with 3way' '
 	git reset --hard main &&
-	cp $TEST_DIRECTORY/test-binary-1.png bin.png &&
+	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 &&
+	cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
 
 	git diff --full-index >bin.diff &&
 	git reset --hard &&

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
  2021-07-28  2:44 [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
  2021-07-28  4:29 ` Junio C Hamano
@ 2021-07-28  4:30 ` Junio C Hamano
  2021-07-28 17:55   ` [PATCH] ll-merge: teach ll_binary_merge() a trivial three-way merge Junio C Hamano
  2021-07-28 19:38   ` [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
  1 sibling, 2 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-07-28  4:30 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: git, lilinchao

Jerry Zhang <jerry@skydio.com> writes:

> 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) {

Thanks for a quick turnaround.  However.

Because apply.c::three_way_merge() calls into ll_merge() that lets
the low-level custom merge drivers to take over the actual merge, I
do not think your "if binary, bypass and never call try_threway() at
all" is the right solution.  The custom merge driver user uses for
the path may successfully perform such a "trivial" three-way merge
and return success.

Why does the current code that lets threeway tried first fails to
fall back to direct application?  The code before your change, if
fed a binary patch that does not apply, would have failed the direct
application first *and* then fell back to the threeway (if only to
fail because we do not let binary files be merged), no?

Is it that try_threeway()'s way to express failure slightly
different from how direct application reports failure, but your
change used the same "only if it is negative, we fail and fallback"
logic?  IIRC, apply_fragments() which is the meat of the direct
application logic reports failures by negative, but try_threeway()
can return positive non-zero to signal a "recoverable" failure (aka
"conflicted merge").  Which should lead us to explore a different
approach, which is ...

    Would it be possible for a patch to leave conflicts when
    try_threeway() was attempted, but will cleanly apply if direct
    application is done?

If so, perhaps

 - we first run try_threeway() and see if it cleanly resolves; if
   so, we are done.

 - then we try direct application and see if it cleanly applies; if
   so, we are done.

 - finally we run try_threeway() again and let it fail with
   conflict.

might be the right sequence?  We theoretically could omit the first
of these three steps, but that would mean we'd write 923cd87a
(git-apply: try threeway first when "--3way" is used, 2021-04-06)
off as a failed experiment and revert it, which would not be ideal.


Also, independent from this "if we claim we try threeway first and
fall back to direct application, we really should do so" fix we are
discussing, I think our default binary merge can be a bit more
lenient and resolve this particular case of applying the binary
patch taken from itself (i.e. a patch that takes A to B gets applied
using --3way option to A).  I wonder if it can be as simple as the
attached patch.  FWIW, this change is sufficient (without the change
to apply.c we are reviewing here) to make your new tests in t4108
pass.

---- >8 ------- >8 ------- >8 ------- >8 ------- >8 ------- >8 ----
Subject: ll-merge: teach ll_binary_merge() a trivial three-way merge

The low-level binary merge code assumed that the caller will not
feed trivial merges that would have been resolved at the tree level;
because of this, ll_binary_merge() assumes the ancestor is different
from either side, always failing the merge in conflict unless -Xours
or -Xtheirs is in effect.

But "git apply --3way" codepath could ask us to perform three-way
merge between two binaries A and B using A as the ancestor version.
The current code always fails such an application, but when given a
binary patch that turns A into B and asked to apply it to A, there
is no reason to fail such a request---we can trivially tell that the
result must be B.

Arguably, this fix may belong to one level higher at ll_merge()
function, which dispatches to lower-level merge drivers, possibly
even before it renormalizes the three input buffers.  But let's
first see how this goes.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 ll-merge.c | 56 +++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 39 insertions(+), 17 deletions(-)

diff --git c/ll-merge.c w/ll-merge.c
index 261657578c..bc8038d404 100644
--- c/ll-merge.c
+++ w/ll-merge.c
@@ -46,6 +46,13 @@ void reset_merge_attributes(void)
 	merge_attributes = NULL;
 }
 
+static int same_mmfile(mmfile_t *a, mmfile_t *b)
+{
+	if (a->size != b->size)
+		return 0;
+	return !memcmp(a->ptr, b->ptr, a->size);
+}
+
 /*
  * Built-in low-levels
  */
@@ -58,9 +65,18 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
 			   const struct ll_merge_options *opts,
 			   int marker_size)
 {
+	int status;
 	mmfile_t *stolen;
 	assert(opts);
 
+	/*
+	 * With -Xtheirs or -Xours, we have cleanly merged;
+	 * otherwise we got a conflict, unless 3way trivially
+	 * resolves.
+	 */
+	status = (opts->variant == XDL_MERGE_FAVOR_OURS ||
+		  opts->variant == XDL_MERGE_FAVOR_THEIRS) ? 0 : 1;
+
 	/*
 	 * The tentative merge result is the common ancestor for an
 	 * internal merge.  For the final merge, it is "ours" by
@@ -68,18 +84,30 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
 	 */
 	if (opts->virtual_ancestor) {
 		stolen = orig;
+		status = 0;
 	} else {
-		switch (opts->variant) {
-		default:
-			warning("Cannot merge binary files: %s (%s vs. %s)",
-				path, name1, name2);
-			/* fallthru */
-		case XDL_MERGE_FAVOR_OURS:
-			stolen = src1;
-			break;
-		case XDL_MERGE_FAVOR_THEIRS:
+		if (same_mmfile(orig, src1)) {
 			stolen = src2;
-			break;
+			status = 0;
+		} else if (same_mmfile(orig, src2)) { 
+			stolen = src1;
+			status = 0;
+		} else if (same_mmfile(src1, src2)) {
+			stolen = src1;
+			status = 0;
+		} else {
+			switch (opts->variant) {
+			default:
+				warning("Cannot merge binary files: %s (%s vs. %s)",
+					path, name1, name2);
+				/* fallthru */
+			case XDL_MERGE_FAVOR_OURS:
+				stolen = src1;
+				break;
+			case XDL_MERGE_FAVOR_THEIRS:
+				stolen = src2;
+				break;
+			}
 		}
 	}
 
@@ -87,13 +115,7 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
 	result->size = stolen->size;
 	stolen->ptr = NULL;
 
-	/*
-	 * With -Xtheirs or -Xours, we have cleanly merged;
-	 * otherwise we got a conflict.
-	 */
-	return opts->variant == XDL_MERGE_FAVOR_OURS ||
-	       opts->variant == XDL_MERGE_FAVOR_THEIRS ?
-	       0 : 1;
+	return status;
 }
 
 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH] ll-merge: teach ll_binary_merge() a trivial three-way merge
  2021-07-28  4:30 ` Junio C Hamano
@ 2021-07-28 17:55   ` Junio C Hamano
  2021-07-28 23:49     ` Elijah Newren
  2021-07-28 19:38   ` [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
  1 sibling, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2021-07-28 17:55 UTC (permalink / raw)
  To: git; +Cc: Jerry Zhang, Elijah Newren

The low-level binary merge code assumed that the caller will not
feed trivial merges that would have been resolved at the tree level;
because of this, ll_binary_merge() assumes the ancestor is different
from either side, always failing the merge in conflict unless -Xours
or -Xtheirs is in effect.

But "git apply --3way" codepath could ask us to perform three-way
merge between two binaries A and B using A as the ancestor version.
The current code always fails such an application, but when given a
binary patch that turns A into B and asked to apply it to A, there
is no reason to fail such a request---we can trivially tell that the
result must be B.

Arguably, this fix may belong to one level higher at ll_merge()
function, which dispatches to lower-level merge drivers, possibly
even before it renormalizes the three input buffers.  But let's
first see how this goes.

Signed-off-by: Jerry Zhang <jerry@skydio.com>
[jc: stolen new tests from Jerry's patch]
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---

 * This time as a proper patch form.  I am asking Elijah's input as
   I suspect this belongs to ll_merge() layer and it may impact not
   just "apply --3way" codepath (which is the primary intended user
   of this "feature") but the merge strategies.  On the other hand,
   properly written merge strategies would not pass trivial merges
   down to the low-level backends, so it may not matter much to,
   say, "merge -sort" and friends.

 ll-merge.c                | 56 +++++++++++++++++++++++++++------------
 t/t4108-apply-threeway.sh | 45 +++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 17 deletions(-)

diff --git a/ll-merge.c b/ll-merge.c
index 261657578c..301e244971 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -46,6 +46,13 @@ void reset_merge_attributes(void)
 	merge_attributes = NULL;
 }
 
+static int same_mmfile(mmfile_t *a, mmfile_t *b)
+{
+	if (a->size != b->size)
+		return 0;
+	return !memcmp(a->ptr, b->ptr, a->size);
+}
+
 /*
  * Built-in low-levels
  */
@@ -58,9 +65,18 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
 			   const struct ll_merge_options *opts,
 			   int marker_size)
 {
+	int status;
 	mmfile_t *stolen;
 	assert(opts);
 
+	/*
+	 * With -Xtheirs or -Xours, we have cleanly merged;
+	 * otherwise we got a conflict, unless 3way trivially
+	 * resolves.
+	 */
+	status = (opts->variant == XDL_MERGE_FAVOR_OURS ||
+		  opts->variant == XDL_MERGE_FAVOR_THEIRS) ? 0 : 1;
+
 	/*
 	 * The tentative merge result is the common ancestor for an
 	 * internal merge.  For the final merge, it is "ours" by
@@ -68,18 +84,30 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
 	 */
 	if (opts->virtual_ancestor) {
 		stolen = orig;
+		status = 0;
 	} else {
-		switch (opts->variant) {
-		default:
-			warning("Cannot merge binary files: %s (%s vs. %s)",
-				path, name1, name2);
-			/* fallthru */
-		case XDL_MERGE_FAVOR_OURS:
-			stolen = src1;
-			break;
-		case XDL_MERGE_FAVOR_THEIRS:
+		if (same_mmfile(orig, src1)) {
 			stolen = src2;
-			break;
+			status = 0;
+		} else if (same_mmfile(orig, src2)) {
+			stolen = src1;
+			status = 0;
+		} else if (same_mmfile(src1, src2)) {
+			stolen = src1;
+			status = 0;
+		} else {
+			switch (opts->variant) {
+			default:
+				warning("Cannot merge binary files: %s (%s vs. %s)",
+					path, name1, name2);
+				/* fallthru */
+			case XDL_MERGE_FAVOR_OURS:
+				stolen = src1;
+				break;
+			case XDL_MERGE_FAVOR_THEIRS:
+				stolen = src2;
+				break;
+			}
 		}
 	}
 
@@ -87,13 +115,7 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
 	result->size = stolen->size;
 	stolen->ptr = NULL;
 
-	/*
-	 * With -Xtheirs or -Xours, we have cleanly merged;
-	 * otherwise we got a conflict.
-	 */
-	return opts->variant == XDL_MERGE_FAVOR_OURS ||
-	       opts->variant == XDL_MERGE_FAVOR_THEIRS ?
-	       0 : 1;
+	return status;
 }
 
 static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
diff --git a/t/t4108-apply-threeway.sh b/t/t4108-apply-threeway.sh
index 65147efdea..cc3aa3314a 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-561-g6177dfa0d2


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
  2021-07-28  4:30 ` Junio C Hamano
  2021-07-28 17:55   ` [PATCH] ll-merge: teach ll_binary_merge() a trivial three-way merge Junio C Hamano
@ 2021-07-28 19:38   ` Jerry Zhang
  2021-07-28 20:04     ` Jerry Zhang
  2021-07-28 20:08     ` Junio C Hamano
  1 sibling, 2 replies; 17+ messages in thread
From: Jerry Zhang @ 2021-07-28 19:38 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List, lilinchao

On Tue, Jul 27, 2021 at 9:30 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Jerry Zhang <jerry@skydio.com> writes:
>
> > 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) {
>
> Thanks for a quick turnaround.  However.
>
> Because apply.c::three_way_merge() calls into ll_merge() that lets
> the low-level custom merge drivers to take over the actual merge, I
> do not think your "if binary, bypass and never call try_threway() at
> all" is the right solution.  The custom merge driver user uses for
> the path may successfully perform such a "trivial" three-way merge
> and return success.
I understand now, thanks for the explanation
>
> Why does the current code that lets threeway tried first fails to
> fall back to direct application?  The code before your change, if
> fed a binary patch that does not apply, would have failed the direct
> application first *and* then fell back to the threeway (if only to
> fail because we do not let binary files be merged), no?
>
> Is it that try_threeway()'s way to express failure slightly
> different from how direct application reports failure, but your
> change used the same "only if it is negative, we fail and fallback"
> logic?  IIRC, apply_fragments() which is the meat of the direct
> application logic reports failures by negative, but try_threeway()
> can return positive non-zero to signal a "recoverable" failure (aka
> "conflicted merge").  Which should lead us to explore a different
> approach, which is ...
>
>     Would it be possible for a patch to leave conflicts when
>     try_threeway() was attempted, but will cleanly apply if direct
>     application is done?
>
> If so, perhaps
>
>  - we first run try_threeway() and see if it cleanly resolves; if
>    so, we are done.
>
>  - then we try direct application and see if it cleanly applies; if
>    so, we are done.
>
>  - finally we run try_threeway() again and let it fail with
>    conflict.
>
> might be the right sequence?  We theoretically could omit the first
> of these three steps, but that would mean we'd write 923cd87a
> (git-apply: try threeway first when "--3way" is used, 2021-04-06)
> off as a failed experiment and revert it, which would not be ideal.
>
>
> Also, independent from this "if we claim we try threeway first and
> fall back to direct application, we really should do so" fix we are
> discussing, I think our default binary merge can be a bit more
> lenient and resolve this particular case of applying the binary
> patch taken from itself (i.e. a patch that takes A to B gets applied
> using --3way option to A).  I wonder if it can be as simple as the
> attached patch.  FWIW, this change is sufficient (without the change
> to apply.c we are reviewing here) to make your new tests in t4108
> pass.
So basically, another way of stating the problem would be that binary
patches can apply cleanly with direct application in some cases where
merge application is not clean. If i understand correctly this is unique
to binary files, although it would be possible for a user to supply a custom
merge driver for text files that is worse than direct application, that is
most likely heavy user error that we shouldn't have to cater to. However
the issue with binary is that the *default* merge driver is actually worse
than direct application (in some cases). Therefore our options are

1. do as you suggest and run 3way -> direct -> 3way. I would modify
this and say we should only attempt this for binary patches, since a text
file that fails 3way would most likely also fail direct, so it would be a waste
of time to try it. furthermore if we cache results from the first 3way and
return them after attempting direct, it can save us from having to compute
the 3way twice, so would be no worse than our current performance.

2. improve the default binary merge driver to be at least as good as direct
application. this would allow us to say overall that "merge drivers should
be at least as intelligent as direct patch application" and would greatly
simplify logic in apply.c. Your change is a good first step in allowing it
to handle more cases. A trivial way to make the binary merge driver
at least as good as patch application is to generate a patch and apply
it as part of the merge. I imagine this would have other consequences
though as many parts of git use the binary merge driver.

Separately I think it would be a worthwhile follow-up patch to also handle
trivial three-way merges in try_threeway(). This would:
1. Allow us to compare oid instead of the entire file buffer, which would be
faster.
2. Handle trivial merges of all file types, which would save time.

>
> ---- >8 ------- >8 ------- >8 ------- >8 ------- >8 ------- >8 ----
> Subject: ll-merge: teach ll_binary_merge() a trivial three-way merge
>
> The low-level binary merge code assumed that the caller will not
> feed trivial merges that would have been resolved at the tree level;
> because of this, ll_binary_merge() assumes the ancestor is different
> from either side, always failing the merge in conflict unless -Xours
> or -Xtheirs is in effect.
>
> But "git apply --3way" codepath could ask us to perform three-way
> merge between two binaries A and B using A as the ancestor version.
> The current code always fails such an application, but when given a
> binary patch that turns A into B and asked to apply it to A, there
> is no reason to fail such a request---we can trivially tell that the
> result must be B.
>
> Arguably, this fix may belong to one level higher at ll_merge()
> function, which dispatches to lower-level merge drivers, possibly
> even before it renormalizes the three input buffers.  But let's
> first see how this goes.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>  ll-merge.c | 56 +++++++++++++++++++++++++++++++++++++++-----------------
>  1 file changed, 39 insertions(+), 17 deletions(-)
>
> diff --git c/ll-merge.c w/ll-merge.c
> index 261657578c..bc8038d404 100644
> --- c/ll-merge.c
> +++ w/ll-merge.c
> @@ -46,6 +46,13 @@ void reset_merge_attributes(void)
>         merge_attributes = NULL;
>  }
>
> +static int same_mmfile(mmfile_t *a, mmfile_t *b)
> +{
> +       if (a->size != b->size)
> +               return 0;
> +       return !memcmp(a->ptr, b->ptr, a->size);
> +}
> +
>  /*
>   * Built-in low-levels
>   */
> @@ -58,9 +65,18 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
>                            const struct ll_merge_options *opts,
>                            int marker_size)
>  {
> +       int status;
>         mmfile_t *stolen;
>         assert(opts);
>
> +       /*
> +        * With -Xtheirs or -Xours, we have cleanly merged;
> +        * otherwise we got a conflict, unless 3way trivially
> +        * resolves.
> +        */
> +       status = (opts->variant == XDL_MERGE_FAVOR_OURS ||
> +                 opts->variant == XDL_MERGE_FAVOR_THEIRS) ? 0 : 1;
> +
>         /*
>          * The tentative merge result is the common ancestor for an
>          * internal merge.  For the final merge, it is "ours" by
> @@ -68,18 +84,30 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
>          */
>         if (opts->virtual_ancestor) {
>                 stolen = orig;
> +               status = 0;
>         } else {
> -               switch (opts->variant) {
> -               default:
> -                       warning("Cannot merge binary files: %s (%s vs. %s)",
> -                               path, name1, name2);
> -                       /* fallthru */
> -               case XDL_MERGE_FAVOR_OURS:
> -                       stolen = src1;
> -                       break;
> -               case XDL_MERGE_FAVOR_THEIRS:
> +               if (same_mmfile(orig, src1)) {
>                         stolen = src2;
> -                       break;
> +                       status = 0;
> +               } else if (same_mmfile(orig, src2)) {
> +                       stolen = src1;
> +                       status = 0;
> +               } else if (same_mmfile(src1, src2)) {
> +                       stolen = src1;
> +                       status = 0;
> +               } else {
> +                       switch (opts->variant) {
> +                       default:
> +                               warning("Cannot merge binary files: %s (%s vs. %s)",
> +                                       path, name1, name2);
> +                               /* fallthru */
> +                       case XDL_MERGE_FAVOR_OURS:
> +                               stolen = src1;
> +                               break;
> +                       case XDL_MERGE_FAVOR_THEIRS:
> +                               stolen = src2;
> +                               break;
> +                       }
>                 }
>         }
>
> @@ -87,13 +115,7 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
>         result->size = stolen->size;
>         stolen->ptr = NULL;
>
> -       /*
> -        * With -Xtheirs or -Xours, we have cleanly merged;
> -        * otherwise we got a conflict.
> -        */
> -       return opts->variant == XDL_MERGE_FAVOR_OURS ||
> -              opts->variant == XDL_MERGE_FAVOR_THEIRS ?
> -              0 : 1;
> +       return status;
>  }
>
>  static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
  2021-07-28 19:38   ` [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
@ 2021-07-28 20:04     ` Jerry Zhang
  2021-07-28 20:08     ` Junio C Hamano
  1 sibling, 0 replies; 17+ messages in thread
From: Jerry Zhang @ 2021-07-28 20:04 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List, lilinchao

On Wed, Jul 28, 2021 at 12:38 PM Jerry Zhang <jerry@skydio.com> wrote:
>
> On Tue, Jul 27, 2021 at 9:30 PM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > Jerry Zhang <jerry@skydio.com> writes:
> >
> > > 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) {
> >
> > Thanks for a quick turnaround.  However.
> >
> > Because apply.c::three_way_merge() calls into ll_merge() that lets
> > the low-level custom merge drivers to take over the actual merge, I
> > do not think your "if binary, bypass and never call try_threway() at
> > all" is the right solution.  The custom merge driver user uses for
> > the path may successfully perform such a "trivial" three-way merge
> > and return success.
> I understand now, thanks for the explanation
> >
> > Why does the current code that lets threeway tried first fails to
> > fall back to direct application?  The code before your change, if
> > fed a binary patch that does not apply, would have failed the direct
> > application first *and* then fell back to the threeway (if only to
> > fail because we do not let binary files be merged), no?
> >
> > Is it that try_threeway()'s way to express failure slightly
> > different from how direct application reports failure, but your
> > change used the same "only if it is negative, we fail and fallback"
> > logic?  IIRC, apply_fragments() which is the meat of the direct
> > application logic reports failures by negative, but try_threeway()
> > can return positive non-zero to signal a "recoverable" failure (aka
> > "conflicted merge").  Which should lead us to explore a different
> > approach, which is ...
> >
> >     Would it be possible for a patch to leave conflicts when
> >     try_threeway() was attempted, but will cleanly apply if direct
> >     application is done?
> >
> > If so, perhaps
> >
> >  - we first run try_threeway() and see if it cleanly resolves; if
> >    so, we are done.
> >
> >  - then we try direct application and see if it cleanly applies; if
> >    so, we are done.
> >
> >  - finally we run try_threeway() again and let it fail with
> >    conflict.
> >
> > might be the right sequence?  We theoretically could omit the first
> > of these three steps, but that would mean we'd write 923cd87a
> > (git-apply: try threeway first when "--3way" is used, 2021-04-06)
> > off as a failed experiment and revert it, which would not be ideal.
> >
> >
> > Also, independent from this "if we claim we try threeway first and
> > fall back to direct application, we really should do so" fix we are
> > discussing, I think our default binary merge can be a bit more
> > lenient and resolve this particular case of applying the binary
> > patch taken from itself (i.e. a patch that takes A to B gets applied
> > using --3way option to A).  I wonder if it can be as simple as the
> > attached patch.  FWIW, this change is sufficient (without the change
> > to apply.c we are reviewing here) to make your new tests in t4108
> > pass.
> So basically, another way of stating the problem would be that binary
> patches can apply cleanly with direct application in some cases where
> merge application is not clean. If i understand correctly this is unique
> to binary files, although it would be possible for a user to supply a custom
> merge driver for text files that is worse than direct application, that is
> most likely heavy user error that we shouldn't have to cater to. However
> the issue with binary is that the *default* merge driver is actually worse
> than direct application (in some cases). Therefore our options are
>
> 1. do as you suggest and run 3way -> direct -> 3way. I would modify
> this and say we should only attempt this for binary patches, since a text
> file that fails 3way would most likely also fail direct, so it would be a waste
> of time to try it. furthermore if we cache results from the first 3way and
> return them after attempting direct, it can save us from having to compute
> the 3way twice, so would be no worse than our current performance.
>
> 2. improve the default binary merge driver to be at least as good as direct
> application. this would allow us to say overall that "merge drivers should
> be at least as intelligent as direct patch application" and would greatly
> simplify logic in apply.c. Your change is a good first step in allowing it
> to handle more cases. A trivial way to make the binary merge driver
> at least as good as patch application is to generate a patch and apply
> it as part of the merge. I imagine this would have other consequences
> though as many parts of git use the binary merge driver.

Hmm I would have thought that binary patches allow context, similar to this
test snippet
"
 test_expect_success 'apply complex 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" &&

     echo 1 >>bin.png &&
     git diff --binary >bin.diff &&
     git reset --hard &&

     cat $TEST_DIRECTORY/test-binary-2.png
$TEST_DIRECTORY/test-binary-1.png >bin.png &&
     git add bin.png &&
     git commit -m "change binary file" &&

     # Apply must succeed.
     git apply bin.diff
 '
"
but upon running it I see that normal patch application still requires the
preimage to match exactly.
"
error: the patch applies to 'bin.png'
(836481bd1b9b6bd7a1bb8939cf4ea01e05946850), which does not match the
current contents.
error: bin.png: patch does not apply
"

So at least in regards to making the default binary merge driver "at
least as intelligent" as
direct patch application, your patch ought to do it.

>
> Separately I think it would be a worthwhile follow-up patch to also handle
> trivial three-way merges in try_threeway(). This would:
> 1. Allow us to compare oid instead of the entire file buffer, which would be
> faster.
> 2. Handle trivial merges of all file types, which would save time.
>
> >
> > ---- >8 ------- >8 ------- >8 ------- >8 ------- >8 ------- >8 ----
> > Subject: ll-merge: teach ll_binary_merge() a trivial three-way merge
> >
> > The low-level binary merge code assumed that the caller will not
> > feed trivial merges that would have been resolved at the tree level;
> > because of this, ll_binary_merge() assumes the ancestor is different
> > from either side, always failing the merge in conflict unless -Xours
> > or -Xtheirs is in effect.
> >
> > But "git apply --3way" codepath could ask us to perform three-way
> > merge between two binaries A and B using A as the ancestor version.
> > The current code always fails such an application, but when given a
> > binary patch that turns A into B and asked to apply it to A, there
> > is no reason to fail such a request---we can trivially tell that the
> > result must be B.
> >
> > Arguably, this fix may belong to one level higher at ll_merge()
> > function, which dispatches to lower-level merge drivers, possibly
> > even before it renormalizes the three input buffers.  But let's
> > first see how this goes.
> >
> > Signed-off-by: Junio C Hamano <gitster@pobox.com>
> > ---
> >  ll-merge.c | 56 +++++++++++++++++++++++++++++++++++++++-----------------
> >  1 file changed, 39 insertions(+), 17 deletions(-)
> >
> > diff --git c/ll-merge.c w/ll-merge.c
> > index 261657578c..bc8038d404 100644
> > --- c/ll-merge.c
> > +++ w/ll-merge.c
> > @@ -46,6 +46,13 @@ void reset_merge_attributes(void)
> >         merge_attributes = NULL;
> >  }
> >
> > +static int same_mmfile(mmfile_t *a, mmfile_t *b)
> > +{
> > +       if (a->size != b->size)
> > +               return 0;
> > +       return !memcmp(a->ptr, b->ptr, a->size);
> > +}
> > +
> >  /*
> >   * Built-in low-levels
> >   */
> > @@ -58,9 +65,18 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
> >                            const struct ll_merge_options *opts,
> >                            int marker_size)
> >  {
> > +       int status;
> >         mmfile_t *stolen;
> >         assert(opts);
> >
> > +       /*
> > +        * With -Xtheirs or -Xours, we have cleanly merged;
> > +        * otherwise we got a conflict, unless 3way trivially
> > +        * resolves.
> > +        */
> > +       status = (opts->variant == XDL_MERGE_FAVOR_OURS ||
> > +                 opts->variant == XDL_MERGE_FAVOR_THEIRS) ? 0 : 1;
> > +
> >         /*
> >          * The tentative merge result is the common ancestor for an
> >          * internal merge.  For the final merge, it is "ours" by
> > @@ -68,18 +84,30 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
> >          */
> >         if (opts->virtual_ancestor) {
> >                 stolen = orig;
> > +               status = 0;
> >         } else {
> > -               switch (opts->variant) {
> > -               default:
> > -                       warning("Cannot merge binary files: %s (%s vs. %s)",
> > -                               path, name1, name2);
> > -                       /* fallthru */
> > -               case XDL_MERGE_FAVOR_OURS:
> > -                       stolen = src1;
> > -                       break;
> > -               case XDL_MERGE_FAVOR_THEIRS:
> > +               if (same_mmfile(orig, src1)) {
> >                         stolen = src2;
> > -                       break;
> > +                       status = 0;
> > +               } else if (same_mmfile(orig, src2)) {
> > +                       stolen = src1;
> > +                       status = 0;
> > +               } else if (same_mmfile(src1, src2)) {
> > +                       stolen = src1;
> > +                       status = 0;
> > +               } else {
> > +                       switch (opts->variant) {
> > +                       default:
> > +                               warning("Cannot merge binary files: %s (%s vs. %s)",
> > +                                       path, name1, name2);
> > +                               /* fallthru */
> > +                       case XDL_MERGE_FAVOR_OURS:
> > +                               stolen = src1;
> > +                               break;
> > +                       case XDL_MERGE_FAVOR_THEIRS:
> > +                               stolen = src2;
> > +                               break;
> > +                       }
> >                 }
> >         }
> >
> > @@ -87,13 +115,7 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
> >         result->size = stolen->size;
> >         stolen->ptr = NULL;
> >
> > -       /*
> > -        * With -Xtheirs or -Xours, we have cleanly merged;
> > -        * otherwise we got a conflict.
> > -        */
> > -       return opts->variant == XDL_MERGE_FAVOR_OURS ||
> > -              opts->variant == XDL_MERGE_FAVOR_THEIRS ?
> > -              0 : 1;
> > +       return status;
> >  }
> >
> >  static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
  2021-07-28 19:38   ` [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
  2021-07-28 20:04     ` Jerry Zhang
@ 2021-07-28 20:08     ` Junio C Hamano
  2021-07-28 20:37       ` Jerry Zhang
  1 sibling, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2021-07-28 20:08 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: Git Mailing List, lilinchao

Jerry Zhang <jerry@skydio.com> writes:

> So basically, another way of stating the problem would be that binary
> patches can apply cleanly with direct application in some cases where
> merge application is not clean. If i understand correctly this is unique
> to binary files, although it would be possible for a user to supply a custom
> merge driver for text files that is worse than direct application, that is
> most likely heavy user error that we shouldn't have to cater to.

Not really.  The built-in binary merge driver luckily had such
characteristics to allow us to catch this regression, but I see no
reason to believe that it is unique to binary.  Funky merge backends
like union merges can turn an otherwise conflicting merge into a
clean merge even for non-binary files.  And no, it is not an error
for a merge driver to fail "apply --3way" merge on incoming data
that "apply --no-3way" would apply cleanly.

> However
> the issue with binary is that the *default* merge driver is actually worse
> than direct application (in some cases).

> 1. do as you suggest and run 3way -> direct -> 3way. I would modify
> this and say we should only attempt this for binary patches, since a text
> file that fails 3way would most likely also fail direct,...

No, I do not trust our (myself and your) unsubstantiated belief that
it is limited to binary.  We saw a problem with binary, and I would
think it is a tip of iceberg for any non-straight-text-merge backend
(and I do not have any sound reason to believe that straight
text-merge backend will not have this issue).  I'd rather treat this
as coalmine canary.

I think the real problem, even without the "try threeway, fall back
to direct application, and then try threeway again", is that after
swapping the fallback order, a failed threeway does *not* fall back
to direct application in this case.  Regardless of what ll_merge()
and its backend does, if they fail, shouldn't the caller of
try_threeway() notice the failure and fall back to direct
application, just like the earlier code tried direct application
first and then _always_ fell back to threeway if it failed?  I do
not know exactly why today's code fails to do so, but I suspect that
fixing that is the real solution, no?

Independent from that, I suspect that it may be a good thing to do
to (at least optionally) allow ll_merge() to notice trivial merges
that proper merge frontends would never ask it to do and resolve
them trivially.  The patch you saw from me to ll_merge_binary() may
do so at a wrong layer (doing it in ll_merge() before it dispatches
to ll_merge_binary() and other backends might be a better approach)
but would be a good starting point for that independent effort, but
"apply --3way" should work correctly even with user-configured merge
drivers (after all, the "direct application first and then fall back
to 3way" code would have worked perfectly fine even with broken
custom merge drivers in the case we are discussing right now).

Thanks.



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
  2021-07-28 20:08     ` Junio C Hamano
@ 2021-07-28 20:37       ` Jerry Zhang
  2021-07-28 21:01         ` Junio C Hamano
  0 siblings, 1 reply; 17+ messages in thread
From: Jerry Zhang @ 2021-07-28 20:37 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List, lilinchao

On Wed, Jul 28, 2021 at 1:08 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Jerry Zhang <jerry@skydio.com> writes:
>
> > So basically, another way of stating the problem would be that binary
> > patches can apply cleanly with direct application in some cases where
> > merge application is not clean. If i understand correctly this is unique
> > to binary files, although it would be possible for a user to supply a custom
> > merge driver for text files that is worse than direct application, that is
> > most likely heavy user error that we shouldn't have to cater to.
>
> Not really.  The built-in binary merge driver luckily had such
> characteristics to allow us to catch this regression, but I see no
> reason to believe that it is unique to binary.  Funky merge backends
> like union merges can turn an otherwise conflicting merge into a
> clean merge even for non-binary files.  And no, it is not an error
> for a merge driver to fail "apply --3way" merge on incoming data
> that "apply --no-3way" would apply cleanly.
>
> > However
> > the issue with binary is that the *default* merge driver is actually worse
> > than direct application (in some cases).
>
> > 1. do as you suggest and run 3way -> direct -> 3way. I would modify
> > this and say we should only attempt this for binary patches, since a text
> > file that fails 3way would most likely also fail direct,...
>
> No, I do not trust our (myself and your) unsubstantiated belief that
> it is limited to binary.  We saw a problem with binary, and I would
> think it is a tip of iceberg for any non-straight-text-merge backend
> (and I do not have any sound reason to believe that straight
> text-merge backend will not have this issue).  I'd rather treat this
> as coalmine canary.

I see, this could be true. But then we would also have the case where
a merge driver results in conflict and direct patch application applies cleanly,
*but* the direct patch application is actually incorrect (for reasons relating
to the original purpose of the patch to switch the order, that direct patch
application can be wrong for files of repeating content). In this case
the user might actually want to see the conflict as it is more correct, but
it is being hidden by our fallback.

If the backwards compatibility story is going to get messy like this, perhaps
the best solution is to make a new flag similar to "--actually-3way" that
will attempt 3way and nothing else, and users who know what they want
can use that to get what they want.
>
> I think the real problem, even without the "try threeway, fall back
> to direct application, and then try threeway again", is that after
> swapping the fallback order, a failed threeway does *not* fall back
> to direct application in this case.  Regardless of what ll_merge()
> and its backend does, if they fail, shouldn't the caller of
> try_threeway() notice the failure and fall back to direct
> application, just like the earlier code tried direct application
> first and then _always_ fell back to threeway if it failed?  I do
> not know exactly why today's code fails to do so, but I suspect that
> fixing that is the real solution, no?

Well it isn't really failing right? Failing 3way would be not finding
the object ids in the database, which would indicate a failure to
even attempt 3way. This would result in fallback to direct application.
What we're seeing is that 3way application results in conflicts where
direct application would not result in conflicts. Having a conflict is
currently not a reason for the code to fall back to direct application,
here is the relevant line:
"
         try_threeway(state, &image, patch, st, ce) < 0) {
"
try_threeway returns 1 in case of conflict, 0 for success, and -1
for true errors.

>
> Independent from that, I suspect that it may be a good thing to do
> to (at least optionally) allow ll_merge() to notice trivial merges
> that proper merge frontends would never ask it to do and resolve
> them trivially.  The patch you saw from me to ll_merge_binary() may
> do so at a wrong layer (doing it in ll_merge() before it dispatches
> to ll_merge_binary() and other backends might be a better approach)
> but would be a good starting point for that independent effort, but
> "apply --3way" should work correctly even with user-configured merge
> drivers (after all, the "direct application first and then fall back
> to 3way" code would have worked perfectly fine even with broken
> custom merge drivers in the case we are discussing right now).
>
> Thanks.
>
>

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
  2021-07-28 20:37       ` Jerry Zhang
@ 2021-07-28 21:01         ` Junio C Hamano
  0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-07-28 21:01 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: Git Mailing List, lilinchao

Jerry Zhang <jerry@skydio.com> writes:

> Well it isn't really failing right? Failing 3way would be not finding
> the object ids in the database, which would indicate a failure to
> even attempt 3way. This would result in fallback to direct application.
> What we're seeing is that 3way application results in conflicts where
> direct application would not result in conflicts. Having a conflict is
> currently not a reason for the code to fall back to direct application,
> here is the relevant line:
> "
>          try_threeway(state, &image, patch, st, ce) < 0) {
> "
> try_threeway returns 1 in case of conflict, 0 for success, and -1
> for true errors.

Yup, I know.  That is why I questioned if this "< 0" is a bug in my
earlier message in this exchange.

Thanks.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ll-merge: teach ll_binary_merge() a trivial three-way merge
  2021-07-28 17:55   ` [PATCH] ll-merge: teach ll_binary_merge() a trivial three-way merge Junio C Hamano
@ 2021-07-28 23:49     ` Elijah Newren
  2021-07-29  1:06       ` Junio C Hamano
  0 siblings, 1 reply; 17+ messages in thread
From: Elijah Newren @ 2021-07-28 23:49 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List, Jerry Zhang

On Wed, Jul 28, 2021 at 11:55 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> The low-level binary merge code assumed that the caller will not
> feed trivial merges that would have been resolved at the tree level;
> because of this, ll_binary_merge() assumes the ancestor is different
> from either side, always failing the merge in conflict unless -Xours
> or -Xtheirs is in effect.
>
> But "git apply --3way" codepath could ask us to perform three-way
> merge between two binaries A and B using A as the ancestor version.
> The current code always fails such an application, but when given a
> binary patch that turns A into B and asked to apply it to A, there
> is no reason to fail such a request---we can trivially tell that the
> result must be B.
>
> Arguably, this fix may belong to one level higher at ll_merge()
> function, which dispatches to lower-level merge drivers, possibly
> even before it renormalizes the three input buffers.  But let's
> first see how this goes.
>
> Signed-off-by: Jerry Zhang <jerry@skydio.com>
> [jc: stolen new tests from Jerry's patch]
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>
>  * This time as a proper patch form.  I am asking Elijah's input as
>    I suspect this belongs to ll_merge() layer and it may impact not
>    just "apply --3way" codepath (which is the primary intended user
>    of this "feature") but the merge strategies.  On the other hand,
>    properly written merge strategies would not pass trivial merges
>    down to the low-level backends, so it may not matter much to,
>    say, "merge -sort" and friends.

The patch looks correct if we choose to modify the ll_binary_merge level.

I agree that properly written merge strategies (at least both
merge-recursive and merge-ort) would not pass trivial merges down to
the low-level backends...but I think this change still matters to them
from a performance perspective.  Additional up-front full content
comparisons feel like an unnecessary performance penalty, so if we do
something like this, keeping it at the ll_binary_merge() level to
limit it to binary files would limit the penalty.  However...

It appears that try_threeway() in apply.c is already computing the
OIDs of the blobs involved, so it looks like the full content
comparison is unnecessary even in the apply --3way case.  If we moved
the trivial-merge check to that function, it could just compare the
OIDs rather than comparing the full content.

>  ll-merge.c                | 56 +++++++++++++++++++++++++++------------
>  t/t4108-apply-threeway.sh | 45 +++++++++++++++++++++++++++++++
>  2 files changed, 84 insertions(+), 17 deletions(-)
>
> diff --git a/ll-merge.c b/ll-merge.c
> index 261657578c..301e244971 100644
> --- a/ll-merge.c
> +++ b/ll-merge.c
> @@ -46,6 +46,13 @@ void reset_merge_attributes(void)
>         merge_attributes = NULL;
>  }
>
> +static int same_mmfile(mmfile_t *a, mmfile_t *b)
> +{
> +       if (a->size != b->size)
> +               return 0;
> +       return !memcmp(a->ptr, b->ptr, a->size);
> +}
> +
>  /*
>   * Built-in low-levels
>   */
> @@ -58,9 +65,18 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
>                            const struct ll_merge_options *opts,
>                            int marker_size)
>  {
> +       int status;
>         mmfile_t *stolen;
>         assert(opts);
>
> +       /*
> +        * With -Xtheirs or -Xours, we have cleanly merged;
> +        * otherwise we got a conflict, unless 3way trivially
> +        * resolves.
> +        */
> +       status = (opts->variant == XDL_MERGE_FAVOR_OURS ||
> +                 opts->variant == XDL_MERGE_FAVOR_THEIRS) ? 0 : 1;
> +
>         /*
>          * The tentative merge result is the common ancestor for an
>          * internal merge.  For the final merge, it is "ours" by
> @@ -68,18 +84,30 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
>          */
>         if (opts->virtual_ancestor) {
>                 stolen = orig;
> +               status = 0;
>         } else {
> -               switch (opts->variant) {
> -               default:
> -                       warning("Cannot merge binary files: %s (%s vs. %s)",
> -                               path, name1, name2);
> -                       /* fallthru */
> -               case XDL_MERGE_FAVOR_OURS:
> -                       stolen = src1;
> -                       break;
> -               case XDL_MERGE_FAVOR_THEIRS:
> +               if (same_mmfile(orig, src1)) {
>                         stolen = src2;
> -                       break;
> +                       status = 0;
> +               } else if (same_mmfile(orig, src2)) {
> +                       stolen = src1;
> +                       status = 0;
> +               } else if (same_mmfile(src1, src2)) {
> +                       stolen = src1;
> +                       status = 0;
> +               } else {
> +                       switch (opts->variant) {
> +                       default:
> +                               warning("Cannot merge binary files: %s (%s vs. %s)",
> +                                       path, name1, name2);
> +                               /* fallthru */
> +                       case XDL_MERGE_FAVOR_OURS:
> +                               stolen = src1;
> +                               break;
> +                       case XDL_MERGE_FAVOR_THEIRS:
> +                               stolen = src2;
> +                               break;
> +                       }
>                 }
>         }
>
> @@ -87,13 +115,7 @@ static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
>         result->size = stolen->size;
>         stolen->ptr = NULL;
>
> -       /*
> -        * With -Xtheirs or -Xours, we have cleanly merged;
> -        * otherwise we got a conflict.
> -        */
> -       return opts->variant == XDL_MERGE_FAVOR_OURS ||
> -              opts->variant == XDL_MERGE_FAVOR_THEIRS ?
> -              0 : 1;
> +       return status;
>  }
>
>  static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
> diff --git a/t/t4108-apply-threeway.sh b/t/t4108-apply-threeway.sh
> index 65147efdea..cc3aa3314a 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-561-g6177dfa0d2

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ll-merge: teach ll_binary_merge() a trivial three-way merge
  2021-07-28 23:49     ` Elijah Newren
@ 2021-07-29  1:06       ` Junio C Hamano
  2021-09-05 19:06         ` [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way" Junio C Hamano
  0 siblings, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2021-07-29  1:06 UTC (permalink / raw)
  To: Elijah Newren; +Cc: Git Mailing List, Jerry Zhang

Elijah Newren <newren@gmail.com> writes:

> It appears that try_threeway() in apply.c is already computing the
> OIDs of the blobs involved, so it looks like the full content
> comparison is unnecessary even in the apply --3way case.  If we moved
> the trivial-merge check to that function, it could just compare the
> OIDs rather than comparing the full content.

Yeah, if we trust merge backends and only fix "apply --3way"
codepath, which I actually am OK with, I agree that it would be
vastly simpler and nicer to do it in try_threeway().

Thanks.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way"
  2021-07-29  1:06       ` Junio C Hamano
@ 2021-09-05 19:06         ` Junio C Hamano
  2021-09-06 18:57           ` Elijah Newren
  2021-09-06 21:59           ` Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-05 19:06 UTC (permalink / raw)
  To: git; +Cc: lilinchao, Elijah Newren, jerry

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>
---

 apply.c                   | 21 ++++++++++++++++++
 t/t4108-apply-threeway.sh | 45 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/apply.c b/apply.c
index 44bc31d6eb..c9f9503e90 100644
--- a/apply.c
+++ b/apply.c
@@ -3467,6 +3467,21 @@ static int load_preimage(struct apply_state *state,
 	return 0;
 }
 
+static int resolve_to(struct image *image, const struct object_id *result_id)
+{
+	unsigned long size;
+	enum object_type type;
+
+	clear_image(image);
+
+	image->buf = read_object_file(result_id, &type, &size);
+	if (!image->buf || type != OBJ_BLOB)
+		die("unable to read blob object %s", oid_to_hex(result_id));
+	image->len = size;
+
+	return 0;
+}
+
 static int three_way_merge(struct apply_state *state,
 			   struct image *image,
 			   char *path,
@@ -3478,6 +3493,12 @@ static int three_way_merge(struct apply_state *state,
 	mmbuffer_t result = { NULL };
 	int status;
 
+	/* resolve trivial cases first */
+	if (oideq(base, ours))
+		return resolve_to(image, theirs);
+	else if (oideq(base, theirs) || oideq(ours, theirs))
+		return resolve_to(image, ours);
+
 	read_mmblob(&base_file, base);
 	read_mmblob(&our_file, ours);
 	read_mmblob(&their_file, theirs);
diff --git a/t/t4108-apply-threeway.sh b/t/t4108-apply-threeway.sh
index 65147efdea..cc3aa3314a 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.33.0-408-g8e1aa136b3


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way"
  2021-09-05 19:06         ` [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way" Junio C Hamano
@ 2021-09-06 18:57           ` Elijah Newren
  2021-09-06 21:59           ` Ævar Arnfjörð Bjarmason
  1 sibling, 0 replies; 17+ messages in thread
From: Elijah Newren @ 2021-09-06 18:57 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List, lilinchao, Jerry Zhang

On Sun, Sep 5, 2021 at 12:07 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> 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>
> ---
>
>  apply.c                   | 21 ++++++++++++++++++
>  t/t4108-apply-threeway.sh | 45 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 66 insertions(+)
>
> diff --git a/apply.c b/apply.c
> index 44bc31d6eb..c9f9503e90 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -3467,6 +3467,21 @@ static int load_preimage(struct apply_state *state,
>         return 0;
>  }
>
> +static int resolve_to(struct image *image, const struct object_id *result_id)
> +{
> +       unsigned long size;
> +       enum object_type type;
> +
> +       clear_image(image);
> +
> +       image->buf = read_object_file(result_id, &type, &size);
> +       if (!image->buf || type != OBJ_BLOB)
> +               die("unable to read blob object %s", oid_to_hex(result_id));
> +       image->len = size;
> +
> +       return 0;
> +}
> +
>  static int three_way_merge(struct apply_state *state,
>                            struct image *image,
>                            char *path,
> @@ -3478,6 +3493,12 @@ static int three_way_merge(struct apply_state *state,
>         mmbuffer_t result = { NULL };
>         int status;
>
> +       /* resolve trivial cases first */
> +       if (oideq(base, ours))
> +               return resolve_to(image, theirs);
> +       else if (oideq(base, theirs) || oideq(ours, theirs))
> +               return resolve_to(image, ours);
> +
>         read_mmblob(&base_file, base);
>         read_mmblob(&our_file, ours);
>         read_mmblob(&their_file, theirs);
> diff --git a/t/t4108-apply-threeway.sh b/t/t4108-apply-threeway.sh
> index 65147efdea..cc3aa3314a 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.33.0-408-g8e1aa136b3

Reviewed-by: Elijah Newren <newren@gmail.com>

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way"
  2021-09-05 19:06         ` [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way" Junio C Hamano
  2021-09-06 18:57           ` Elijah Newren
@ 2021-09-06 21:59           ` Ævar Arnfjörð Bjarmason
  2021-09-07  2:32             ` Junio C Hamano
  2021-09-07 20:15             ` Junio C Hamano
  1 sibling, 2 replies; 17+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-09-06 21:59 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, lilinchao, Elijah Newren, jerry


On Sun, Sep 05 2021, Junio C Hamano wrote:

> +	if (!image->buf || type != OBJ_BLOB)
> +		die("unable to read blob object %s", oid_to_hex(result_id));

This die() message seems to only be applicable to the first condition
here, shouldn't this be:

    if (!image->buf)
        die(_("unable to read blob object %s"), oid_to_hex(result_id));
    if (type != OBJ_BLOB)
        die(_("object %s is %s, expected blob"), oid_to_hex(result_id), type_name(type));

Also as shown there, missing _() for marking the translation.

> [...]
> +test_expect_success 'apply binary file patch' '
> +	git reset --hard main &&

Partly this is cleaning up a mess after an existing test, but here
there's no reason we can't use test_when_finished() for all the new
tests to make them clean up after themselves:

diff --git a/t/t4108-apply-threeway.sh b/t/t4108-apply-threeway.sh
index cc3aa3314a3..c3c9b52e30d 100755
--- a/t/t4108-apply-threeway.sh
+++ b/t/t4108-apply-threeway.sh
@@ -232,6 +232,8 @@ test_expect_success 'apply with --3way --cached and conflicts' '
 
 test_expect_success 'apply binary file patch' '
 	git reset --hard main &&
+	test_when_finished "git reset --hard main" &&
+
 	cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
 	git add bin.png &&
 	git commit -m "add binary file" &&
@@ -246,7 +248,8 @@ test_expect_success 'apply binary file patch' '
 '
 
 test_expect_success 'apply binary file patch with 3way' '
-	git reset --hard main &&
+	test_when_finished "git reset --hard main" &&
+
 	cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
 	git add bin.png &&
 	git commit -m "add binary file" &&
@@ -261,7 +264,8 @@ test_expect_success 'apply binary file patch with 3way' '
 '
 
 test_expect_success 'apply full-index patch with 3way' '
-	git reset --hard main &&
+	test_when_finished "git reset --hard main" &&
+
 	cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
 	git add bin.png &&
 	git commit -m "add binary file" &&

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way"
  2021-09-06 21:59           ` Ævar Arnfjörð Bjarmason
@ 2021-09-07  2:32             ` Junio C Hamano
  2021-09-07 20:15             ` Junio C Hamano
  1 sibling, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-07  2:32 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: git, lilinchao, Elijah Newren, jerry

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> On Sun, Sep 05 2021, Junio C Hamano wrote:
>
>> +	if (!image->buf || type != OBJ_BLOB)
>> +		die("unable to read blob object %s", oid_to_hex(result_id));
>
> This die() message seems to only be applicable to the first condition
> here, shouldn't this be:

As this directly was lifted from read_mmblob(), it should be exactly
spelled as I wrote.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way"
  2021-09-06 21:59           ` Ævar Arnfjörð Bjarmason
  2021-09-07  2:32             ` Junio C Hamano
@ 2021-09-07 20:15             ` Junio C Hamano
  1 sibling, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-07 20:15 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: git, lilinchao, Elijah Newren, jerry

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> Partly this is cleaning up a mess after an existing test, but here
> there's no reason we can't use test_when_finished() for all the new
> tests to make them clean up after themselves:

I do not mind if somebody wants to send in a janitorial patch after
the dust settles, but adding "test_when_finished reset --hard" after
each "refs --hard" at the beginning of each test is not something I
would expect to see.  Such a patch should first choose between "each
test cleans after itself" and "expect previous ones may have left a
mess, so each test clears the slate sufficiently before it starts"
and then stick to the approach, not mixture of both, I would think.

Thanks.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-apply: fix --3way with binary patch
       [not found] <ee1d1a82ef4d11ebbb4cd4ae5278bc123046@skydio.com>
@ 2021-07-28  4:45 ` lilinchao
  0 siblings, 0 replies; 17+ messages in thread
From: lilinchao @ 2021-07-28  4:45 UTC (permalink / raw)
  To: jerry, git, Junio C Hamano; +Cc: jerry

I am happy to see that you fix this problem in time.

Some application platform(like Gitlab, Gitee) will use "git apply --index xxx.patch" to merge patch file,
in order to compatible with most types of files, they will use "--3way", so  although as Juno said
"We refuse to merge binary files, so I would not be surprised if we failed the 3way in this case",
we would like it still behave as before that falling back to normal patch application, so thanks again to fix this.

>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
>

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2021-09-07 20:15 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-28  2:44 [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
2021-07-28  4:29 ` Junio C Hamano
2021-07-28  4:30 ` Junio C Hamano
2021-07-28 17:55   ` [PATCH] ll-merge: teach ll_binary_merge() a trivial three-way merge Junio C Hamano
2021-07-28 23:49     ` Elijah Newren
2021-07-29  1:06       ` Junio C Hamano
2021-09-05 19:06         ` [PATCH v2] apply: resolve trivial merge without hitting ll-merge with "--3way" Junio C Hamano
2021-09-06 18:57           ` Elijah Newren
2021-09-06 21:59           ` Ævar Arnfjörð Bjarmason
2021-09-07  2:32             ` Junio C Hamano
2021-09-07 20:15             ` Junio C Hamano
2021-07-28 19:38   ` [PATCH] git-apply: fix --3way with binary patch Jerry Zhang
2021-07-28 20:04     ` Jerry Zhang
2021-07-28 20:08     ` Junio C Hamano
2021-07-28 20:37       ` Jerry Zhang
2021-07-28 21:01         ` Junio C Hamano
     [not found] <ee1d1a82ef4d11ebbb4cd4ae5278bc123046@skydio.com>
2021-07-28  4:45 ` lilinchao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.