Skip to content

Detect file header, when parsing Hunk #53

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
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
19 changes: 19 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,25 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
},
},
},
{
filename: "sample_multi_file_without_extended.diff",
wantDiffs: []*FileDiff{
{
OrigName: "source_1_a/file_1.txt",
OrigTime: nil,
NewName: "source_1_c/file_1.txt",
NewTime: nil,
Extended: nil,
},
{
OrigName: "source_1_a/file_2.txt",
OrigTime: nil,
NewName: "source_1_c/file_2.txt",
NewTime: nil,
Extended: nil,
},
},
},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
Expand Down
4 changes: 2 additions & 2 deletions diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time
type OverflowError string

func (e OverflowError) Error() string {
return fmt.Sprintf("overflowed into next file: %s", e)
return fmt.Sprintf("overflowed into next file: %s", string(e))
}

// ReadExtendedHeaders reads the extended header lines, if any, from a
Expand Down Expand Up @@ -531,7 +531,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
return r.hunk, nil
}

if len(line) >= 1 && !linePrefix(line[0]) {
if len(line) >= 1 && (!linePrefix(line[0]) || bytes.HasPrefix(line, []byte("--- "))) {
// 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
31 changes: 31 additions & 0 deletions diff/testdata/sample_multi_file_without_extended.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--- source_1_a/file_1.txt
+++ source_1_c/file_1.txt
@@ -1,11 +1,13 @@
Text generated by www.randomtextgenerator.com

+
In to am attended desirous raptures declared diverted confined at.
+In to are desirous declared diverted confined at.
+In to am attended raptures declared diverted confined at.
+In to am attended desirous confined at.
Collected instantly remaining up certainly to necessary as.
Over walk dull into son boy door went new.
At or happiness commanded daughters as.
Is handsome an declared at received in extended vicinity subjects.
-Is handsome received in extended vicinity subjects.
-Is handsome an declared at vicinity subjects.
Into miss on he over been late pain an.
Only week bore boy what fat case left use.
--- source_1_a/file_2.txt
+++ source_1_c/file_2.txt
@@ -4,8 +4,8 @@
Still round match we to.
Frankness pronounce daughters remainder extensive has but.
+Happiness cordially one determine concluded fat.
Plenty season beyond by hardly giving of.
-Consulted or acuteness dejection an smallness if.
-Outward general passage another as it.
Very his are come man walk one next.
Delighted prevailed supported too not remainder perpetual who furnished.
Nay affronting bed projection compliment instrument.
+Still round match we to here.