Skip to content

Commit 3197067

Browse files
authored
Include file path in patch apply exception messages (#8)
When a patch fails to apply, the error message now includes the file path being patched, making it easier to debug which file caused the failure. Previously, only the HunkHeader information (line start/count) was shown, which could be ambiguous when the same pattern exists in multiple files. Fixes moderneinc/customer-requests#1759
1 parent 2083e0c commit 3197067

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

jgit/src/main/java/org/openrewrite/jgit/api/ApplyCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ private void applyText(Repository repository, String path, RawText rt,
600600
// We assume hunks to be ordered
601601
if (hh.getNewStartLine() <= lastHunkNewLine) {
602602
throw new PatchApplyException(MessageFormat
603-
.format(JGitText.get().patchApplyException, hh));
603+
.format(JGitText.get().patchApplyErrorWithHunk, path, hh, "hunk out of sequence"));
604604
}
605605
lastHunkNewLine = hh.getNewStartLine();
606606

@@ -622,7 +622,7 @@ && canApplyAt(hunkLines, newLines, 0)) {
622622
break;
623623
}
624624
throw new PatchApplyException(MessageFormat
625-
.format(JGitText.get().patchApplyException, hh));
625+
.format(JGitText.get().patchApplyErrorWithHunk, path, hh, "cannot apply hunk"));
626626
}
627627
// Hunk lines as reported by the hunk may be off, so don't rely on
628628
// them.
@@ -634,7 +634,7 @@ && canApplyAt(hunkLines, newLines, 0)) {
634634
}
635635
if (applyAt < afterLastHunk) {
636636
throw new PatchApplyException(MessageFormat
637-
.format(JGitText.get().patchApplyException, hh));
637+
.format(JGitText.get().patchApplyErrorWithHunk, path, hh, "hunk application position overlaps with previous hunk"));
638638
}
639639
boolean applies = false;
640640
int oldLinesInHunk = hh.getLinesContext()
@@ -673,7 +673,7 @@ && canApplyAt(hunkLines, newLines, 0)) {
673673
}
674674
if (!applies) {
675675
throw new PatchApplyException(MessageFormat
676-
.format(JGitText.get().patchApplyException, hh));
676+
.format(JGitText.get().patchApplyErrorWithHunk, path, hh, "hunk does not apply to file content"));
677677
}
678678
// Hunk applies at applyAt. Apply it, and update afterLastHunk and
679679
// lineNumberShift

0 commit comments

Comments
 (0)