Report short sources as conflicts during apply (#63)

Previously, this returned an io.ErrUnexpectedEOF. This is not wrong, in
that we did unexpectedly hit the end of the input file, but it is vague
and implies a possible library bug rather than a problem with the patch
or the input. This condition is really a conflict, as the changes
described by the patch are not compatible with the state of the input.
This commit is contained in:
Billy Keyes
2025-09-03 17:26:05 -07:00
committed by GitHub
parent 7413262a18
commit 17bd72f0e2
2 changed files with 8 additions and 2 deletions
+2 -2
View File
@@ -31,14 +31,14 @@ func TestApplyTextFragment(t *testing.T) {
Src: "text_fragment_error.src",
Patch: "text_fragment_error_short_src_before.patch",
},
Err: io.ErrUnexpectedEOF,
Err: &Conflict{},
},
"errorShortSrc": {
Files: applyFiles{
Src: "text_fragment_error.src",
Patch: "text_fragment_error_short_src.patch",
},
Err: io.ErrUnexpectedEOF,
Err: &Conflict{},
},
"errorContextConflict": {
Files: applyFiles{
+6
View File
@@ -1,6 +1,7 @@
package gitdiff
import (
"errors"
"io"
)
@@ -85,6 +86,11 @@ func (a *TextApplier) ApplyFragment(f *TextFragment) error {
preimage := make([][]byte, fragEnd-start)
n, err := a.lineSrc.ReadLinesAt(preimage, start)
if err != nil {
// an EOF indicates that source file is shorter than the patch expects,
// which should be reported as a conflict rather than a generic error
if errors.Is(err, io.EOF) {
err = &Conflict{"src has fewer lines than required by fragment"}
}
return applyError(err, lineNum(start+int64(n)))
}