Skip to content

Commit 7024450

Browse files
Detect file header, when parsing Hunk (#53)
The version before was failing to identify next file header (without extended lines before it), while was parsing Hunk. Hunk reader identify line as a part of the hunk, if it starts with , + or -. Because of this, file header starting with --- , was also identified as part of the hunk.
1 parent b49127c commit 7024450

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

diff/diff_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,25 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
630630
},
631631
},
632632
},
633+
{
634+
filename: "sample_multi_file_without_extended.diff",
635+
wantDiffs: []*FileDiff{
636+
{
637+
OrigName: "source_1_a/file_1.txt",
638+
OrigTime: nil,
639+
NewName: "source_1_c/file_1.txt",
640+
NewTime: nil,
641+
Extended: nil,
642+
},
643+
{
644+
OrigName: "source_1_a/file_2.txt",
645+
OrigTime: nil,
646+
NewName: "source_1_c/file_2.txt",
647+
NewTime: nil,
648+
Extended: nil,
649+
},
650+
},
651+
},
633652
}
634653
for _, test := range tests {
635654
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))

diff/parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time
308308
type OverflowError string
309309

310310
func (e OverflowError) Error() string {
311-
return fmt.Sprintf("overflowed into next file: %s", e)
311+
return fmt.Sprintf("overflowed into next file: %s", string(e))
312312
}
313313

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

534-
if len(line) >= 1 && !linePrefix(line[0]) {
534+
if len(line) >= 1 && (!linePrefix(line[0]) || bytes.HasPrefix(line, []byte("--- "))) {
535535
// Bad hunk header line. If we're reading a multi-file
536536
// diff, this may be the end of the current
537537
// file. Return a "rich" error that lets our caller
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--- source_1_a/file_1.txt
2+
+++ source_1_c/file_1.txt
3+
@@ -1,11 +1,13 @@
4+
Text generated by www.randomtextgenerator.com
5+
6+
+
7+
In to am attended desirous raptures declared diverted confined at.
8+
+In to are desirous declared diverted confined at.
9+
+In to am attended raptures declared diverted confined at.
10+
+In to am attended desirous confined at.
11+
Collected instantly remaining up certainly to necessary as.
12+
Over walk dull into son boy door went new.
13+
At or happiness commanded daughters as.
14+
Is handsome an declared at received in extended vicinity subjects.
15+
-Is handsome received in extended vicinity subjects.
16+
-Is handsome an declared at vicinity subjects.
17+
Into miss on he over been late pain an.
18+
Only week bore boy what fat case left use.
19+
--- source_1_a/file_2.txt
20+
+++ source_1_c/file_2.txt
21+
@@ -4,8 +4,8 @@
22+
Still round match we to.
23+
Frankness pronounce daughters remainder extensive has but.
24+
+Happiness cordially one determine concluded fat.
25+
Plenty season beyond by hardly giving of.
26+
-Consulted or acuteness dejection an smallness if.
27+
-Outward general passage another as it.
28+
Very his are come man walk one next.
29+
Delighted prevailed supported too not remainder perpetual who furnished.
30+
Nay affronting bed projection compliment instrument.
31+
+Still round match we to here.

0 commit comments

Comments
 (0)