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 1 commit
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
30 changes: 27 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 := nextLineHasPrefix(r.reader, []byte("+++"))
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,16 @@ func linePrefix(c byte) bool {
return false
}

// nextLineHasPrefix peeks into the given reader to check whether the next
// bytes match the given prefix.
func nextLineHasPrefix(reader *bufio.Reader, prefix []byte) (bool, error) {
next, err := reader.Peek(len(prefix))
if err != nil {
return false, err
}
return bytes.HasPrefix(next, 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
5 changes: 5 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,5 @@
@@ -1,4 +1,3 @@
select 1;
--- this is my query
select 2;
select 3;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@@ -1,4 +1,3 @@
select 1;
--- this is my query
select 2;
select 3;
@@ -1,4 +1,3 @@
select 1;
--- this is my query
+++ this could break?
select 2;
select 3;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a case that works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, with the fix here or in general?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the logic above that checks the next hunk line for +++ and fails if it finds it.
Like when I change a line prefix from -- to ++, the diff would look like

line
---line
+++line
line

I guess. Would this be parseable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I use this diff, it crashes:

diff --git a/foobar.sql b/foobar.sql
index 9537d7b..f73c856 100644
--- a/foobar.sql
+++ b/foobar.sql
@@ -1,5 +1,5 @@
 select 1;
--- this is my query
+++ this is my query
 select 2;
 select 3;
--- this is the last line
+++ this is the last line

I'll try to see if I can make that work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keegancsmith @eseliger what do you think of this? #56

It adds a custom type, lineReader, so we can do two-line lookahead and check whether the --- and +++ are followed by a @@ , which is what git does. It's built on top of this PR and all tests pass, including the case raised here by @eseliger.