From c0c59ccf2d320c1d50f3ad949e5369ae167b8fb5 Mon Sep 17 00:00:00 2001 From: Patrick Hiesel Date: Mon, 27 May 2024 16:11:40 +0200 Subject: [PATCH] PatchApplier: Set a boolean on the result if conflict markers were added This will let callers show a different error message or mark the state as conflicting. Change-Id: Id8eea614b6b8d54c62b49ffbac90599e6f4c5efa --- .../eclipse/jgit/patch/PatchApplierTest.java | 4 +- .../org/eclipse/jgit/patch/PatchApplier.java | 37 ++++++++++++++++--- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java index 8f3478db18..eec403a976 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java @@ -376,7 +376,7 @@ public class PatchApplierTest { error.hh = null; // We don't assert the hunk header as it is a // complex object with lots of internal state. assertEquals(error, new PatchApplier.Result.Error( - "cannot apply hunk", "allowconflict", null)); + "cannot apply hunk", "allowconflict", null, true)); verifyChange(result, "allowconflict", true, 1); } @@ -391,7 +391,7 @@ public class PatchApplierTest { error.hh = null; // We don't assert the hunk header as it is a // complex object with lots of internal state. assertEquals(error, new PatchApplier.Result.Error( - "cannot apply hunk", "ConflictOutOfBounds", null)); + "cannot apply hunk", "ConflictOutOfBounds", null, true)); verifyChange(result, "ConflictOutOfBounds", true, 1); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java b/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java index 84c2ec43dc..1a98d79f99 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java @@ -184,10 +184,27 @@ public class PatchApplier { @Nullable HunkHeader hh; - Error(String msg, String oldFileName, @Nullable HunkHeader hh) { + final boolean isGitConflict; + + Error(String msg, String oldFileName, @Nullable HunkHeader hh, + boolean isGitConflict) { this.msg = msg; this.oldFileName = oldFileName; this.hh = hh; + this.isGitConflict = isGitConflict; + } + + /** + * Signals if as part of encountering this error, conflict markers + * were added to the file. + * + * @return {@code true} if conflict markers were added for this + * error. + * + * @since 6.10 + */ + public boolean isGitConflict() { + return isGitConflict; } @Override @@ -213,12 +230,14 @@ public class PatchApplier { Error error = (Error) o; return Objects.equals(msg, error.msg) && Objects.equals(oldFileName, error.oldFileName) - && Objects.equals(hh, error.hh); + && Objects.equals(hh, error.hh) + && isGitConflict == error.isGitConflict; } @Override public int hashCode() { - return Objects.hash(msg, oldFileName, hh); + return Objects.hash(msg, oldFileName, hh, + Boolean.valueOf(isGitConflict)); } } @@ -257,8 +276,14 @@ public class PatchApplier { return errors; } - private void addError(String msg,String oldFileName, @Nullable HunkHeader hh) { - errors.add(new Error(msg, oldFileName, hh)); + private void addError(String msg, String oldFileName, + @Nullable HunkHeader hh) { + errors.add(new Error(msg, oldFileName, hh, false)); + } + + private void addErrorWithGitConflict(String msg, String oldFileName, + @Nullable HunkHeader hh) { + errors.add(new Error(msg, oldFileName, hh, true)); } } @@ -1020,7 +1045,7 @@ public class PatchApplier { // only works if the pre-image SHA is contained in the repo. // If that was the case, cherry-picking the original commit // should be preferred to apply a patch. - result.addError("cannot apply hunk", fh.getOldPath(), hh); //$NON-NLS-1$ + result.addErrorWithGitConflict("cannot apply hunk", fh.getOldPath(), hh); //$NON-NLS-1$ newLines.add(Math.min(applyAt++, newLines.size()), asBytes("<<<<<<< HEAD")); //$NON-NLS-1$ applyAt += hh.getOldImage().lineCount; -- 2.39.5