Skip to content

Fix parsing of non-extended file headers and lines starting with -- #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestParseHunksAndPrintHunks(t *testing.T) {
{filename: "empty_new.diff"},
{filename: "oneline_hunk.diff"},
{filename: "empty.diff"},
{filename: "sample_hunk_lines_start_with_minuses.diff"},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
Expand Down
33 changes: 30 additions & 3 deletions diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (r *MultiFileDiffReader) ReadFile() (*FileDiff, error) {
if e.Err == ErrNoFileHeader || e.Err == ErrExtendedHeadersEOF {
return nil, io.EOF
}
return nil, err

case OverflowError:
r.nextFileFirstLine = []byte(e)
Expand Down Expand Up @@ -513,9 +514,22 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
r.hunk.Section = section
} else {
// Read hunk body line.

// If the line starts with `---` and the next one with `+++` we're
// looking at a non-extended file header and need to abort.
if bytes.HasPrefix(line, []byte("---")) {
ok, err := peekPrefix(r.reader, "+++")
if err != nil {
return r.hunk, err
}
if ok {
return r.hunk, &ParseError{r.line, r.offset, &ErrBadHunkLine{Line: line}}
}
}

// If the line starts with the hunk prefix, this hunk is complete.
if bytes.HasPrefix(line, hunkPrefix) {
// Saw start of new hunk, so this hunk is
// complete. But we've already read in the next hunk's
// But we've already read in the next hunk's
// header, so we need to be sure that the next call to
// ReadHunk starts with that header.
r.nextHunkHeaderLine = line
Expand All @@ -527,7 +541,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
return r.hunk, nil
}

if len(line) >= 1 && (!linePrefix(line[0]) || bytes.HasPrefix(line, []byte("--- "))) {
if len(line) >= 1 && !linePrefix(line[0]) {
// Bad hunk header line. If we're reading a multi-file
// diff, this may be the end of the current
// file. Return a "rich" error that lets our caller
Expand Down Expand Up @@ -579,6 +593,19 @@ func linePrefix(c byte) bool {
return false
}

// peekPrefix peeks into the given reader to check whether the next
// bytes match the given prefix.
func peekPrefix(reader *bufio.Reader, prefix string) (bool, error) {
next, err := reader.Peek(len(prefix))
if err != nil {
if err == io.EOF {
return false, nil
}
return false, err
}
return bytes.HasPrefix(next, []byte(prefix)), nil
}

// normalizeHeader takes a header of the form:
// "@@ -linestart[,chunksize] +linestart[,chunksize] @@ section"
// and returns two strings, with the first in the form:
Expand Down
6 changes: 6 additions & 0 deletions diff/testdata/sample_hunk_lines_start_with_minuses.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@@ -1,5 +1,3 @@
select 1;
--- this is my query
select 2;
select 3;
--- this is the last line