Skip to content

Commit 1d9fcb0

Browse files
committed
Revert "fix no-newline edge case"
This reverts commit ba2c541.
1 parent ba2c541 commit 1d9fcb0

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

diff/diff_test.go

+31-23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"path/filepath"
77
"reflect"
8+
"strings"
89
"testing"
910
)
1011

@@ -53,11 +54,7 @@ func TestParseHunksAndPrintHunks(t *testing.T) {
5354
{
5455
filename: "sample_hunks_no_newline.diff",
5556
},
56-
{
57-
filename: "sample_hunks_no_newline2.diff",
58-
},
5957
{filename: "empty.diff"},
60-
{filename: "oneline_hunk.diff"},
6158
}
6259
for _, test := range tests {
6360
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
@@ -83,25 +80,6 @@ func TestParseHunksAndPrintHunks(t *testing.T) {
8380
}
8481
}
8582

86-
func TestStripModifierLines(t *testing.T) {
87-
orig := `- a
88-
\ No newline at end of file
89-
+ b
90-
\ No newline at end of file
91-
`
92-
want := `- a
93-
+ b
94-
`
95-
96-
hunks := []*Hunk{{Body: []byte(orig)}}
97-
StripModifierLines(hunks)
98-
stripped := string(hunks[0].Body)
99-
100-
if stripped != want {
101-
t.Errorf("Want:\n%s\nbut got:\n%s", want, stripped)
102-
}
103-
}
104-
10583
func TestParseFileDiffAndPrintFileDiff(t *testing.T) {
10684
tests := []struct {
10785
filename string
@@ -182,6 +160,36 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
182160
}
183161
}
184162

163+
func TestNoNewlineAtEnd(t *testing.T) {
164+
orig := `@@ -1,1 +1,1 @@
165+
-b
166+
+b
167+
\ No newline at end of file
168+
`
169+
170+
hunks, err := ParseHunks([]byte(orig))
171+
if err != nil {
172+
t.Fatal("ParseHunks: %s", err)
173+
}
174+
175+
for _, hunk := range hunks {
176+
if body := string(hunk.Body); strings.Contains(body, "No newline") {
177+
t.Errorf("after parse, hunk body contains 'No newline...' string\n\nbody is:\n%q", body)
178+
}
179+
if bytes.HasSuffix(hunk.Body, []byte{'\n'}) {
180+
t.Errorf("after parse, hunk body ends with newline\n\nbody is:\n%q", hunk.Body)
181+
}
182+
183+
printed, err := PrintHunks(hunks)
184+
if err != nil {
185+
t.Fatal("PrintHunks: %s", err)
186+
}
187+
if printed := string(printed); printed != orig {
188+
t.Errorf("printed diff hunks != original diff hunks\n\n# PrintHunks output:\n%q\n\n# Original:\n%q", printed, orig)
189+
}
190+
}
191+
}
192+
185193
func TestFileDiff_Stat(t *testing.T) {
186194
tests := map[string]struct {
187195
hunks []*Hunk

diff/parse.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,6 @@ func ParseHunks(diff []byte) ([]*Hunk, error) {
304304
return hunks, nil
305305
}
306306

307-
// StripModifierLines strips the modifier line "\ No newline at end of file", which
308-
// indicates that the preceding line has no terminating newline character. This makes it
309-
// so that all lines in the hunk corresponds to actual lines in the file.
310-
func StripModifierLines(hunks []*Hunk) {
311-
for _, hunk := range hunks {
312-
hunk.Body = bytes.Replace(hunk.Body, []byte(NoNewlineMessage+"\n"), []byte{}, -1)
313-
}
314-
}
315-
316307
// NewHunksReader returns a new HunksReader that reads unified diff hunks
317308
// from r.
318309
func NewHunksReader(r io.Reader) *HunksReader {
@@ -400,6 +391,13 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
400391
// handle that case.
401392
return r.hunk, &ParseError{r.line, r.offset, &ErrBadHunkLine{Line: line}}
402393
}
394+
if bytes.Equal(line, []byte(noNewlineMessage)) {
395+
// Remove previous line's newline.
396+
if len(r.hunk.Body) != 0 {
397+
r.hunk.Body = r.hunk.Body[:len(r.hunk.Body)-1]
398+
}
399+
continue
400+
}
403401

404402
r.hunk.Body = append(r.hunk.Body, line...)
405403
r.hunk.Body = append(r.hunk.Body, '\n')
@@ -416,7 +414,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
416414
return nil, io.EOF
417415
}
418416

419-
const NoNewlineMessage = `\ No newline at end of file`
417+
const noNewlineMessage = `\ No newline at end of file`
420418

421419
// linePrefixes is the set of all characters a valid line in a diff
422420
// hunk can start with. '\' can appear in diffs when no newline is

diff/print.go

+11
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ func PrintHunks(hunks []*Hunk) ([]byte, error) {
8989
if _, err := buf.Write(hunk.Body); err != nil {
9090
return nil, err
9191
}
92+
if !bytes.HasSuffix(hunk.Body, []byte{'\n'}) {
93+
if _, err := fmt.Fprintln(&buf); err != nil {
94+
return nil, err
95+
}
96+
if _, err := buf.Write([]byte(noNewlineMessage)); err != nil {
97+
return nil, err
98+
}
99+
if _, err := fmt.Fprintln(&buf); err != nil {
100+
return nil, err
101+
}
102+
}
92103
}
93104
return buf.Bytes(), nil
94105
}

diff/testdata/oneline_hunk.diff

-3
This file was deleted.

diff/testdata/sample_hunks_no_newline2.diff

-5
This file was deleted.

0 commit comments

Comments
 (0)