Skip to content

Commit 6049834

Browse files
committed
Report short sources as conflicts during apply
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.
1 parent 7413262 commit 6049834

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

gitdiff/apply_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func TestApplyTextFragment(t *testing.T) {
3131
Src: "text_fragment_error.src",
3232
Patch: "text_fragment_error_short_src_before.patch",
3333
},
34-
Err: io.ErrUnexpectedEOF,
34+
Err: &Conflict{},
3535
},
3636
"errorShortSrc": {
3737
Files: applyFiles{
3838
Src: "text_fragment_error.src",
3939
Patch: "text_fragment_error_short_src.patch",
4040
},
41-
Err: io.ErrUnexpectedEOF,
41+
Err: &Conflict{},
4242
},
4343
"errorContextConflict": {
4444
Files: applyFiles{

gitdiff/apply_text.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gitdiff
22

33
import (
4+
"errors"
45
"io"
56
)
67

@@ -85,6 +86,11 @@ func (a *TextApplier) ApplyFragment(f *TextFragment) error {
8586
preimage := make([][]byte, fragEnd-start)
8687
n, err := a.lineSrc.ReadLinesAt(preimage, start)
8788
if err != nil {
89+
// an EOF indicates that source file is shorter than the patch expects,
90+
// which should be reported as a conflict rather than a generic error
91+
if errors.Is(err, io.EOF) {
92+
err = &Conflict{"src has fewer lines than required by fragment"}
93+
}
8894
return applyError(err, lineNum(start+int64(n)))
8995
}
9096

0 commit comments

Comments
 (0)