Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d353d86

Browse files
committedSep 17, 2014
treat the 'No newline at end of file' message as an encoding artifact;
don't store in hunk body
1 parent 2b120f9 commit d353d86

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed
 

‎diff/diff_test.go

Lines changed: 31 additions & 0 deletions
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

@@ -158,3 +159,33 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
158159
}
159160
}
160161
}
162+
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+
}

‎diff/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
391391
// handle that case.
392392
return r.hunk, &ParseError{r.line, r.offset, &ErrBadHunkLine{Line: line}}
393393
}
394-
if string(line) == noNewlineMessage {
394+
if bytes.Equal(line, []byte(noNewlineMessage)) {
395395
// Remove previous line's newline.
396396
if len(r.hunk.Body) != 0 {
397397
r.hunk.Body = r.hunk.Body[:len(r.hunk.Body)-1]
@@ -414,7 +414,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {
414414
return nil, io.EOF
415415
}
416416

417-
const noNewlineMessage = "\\ No newline at end of file\n"
417+
const noNewlineMessage = `\ No newline at end of file`
418418

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

‎diff/print.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,20 @@ func PrintHunks(hunks []*Hunk) ([]byte, error) {
8686
if _, err := fmt.Fprintln(&buf); err != nil {
8787
return nil, err
8888
}
89-
if len(hunk.Body) != 0 && hunk.Body[len(hunk.Body)-1] != '\n' {
90-
// Append message if hunk.Body doesn't end with a newline.
91-
hunk.Body = append(hunk.Body, noNewlineMessage...)
92-
}
9389
if _, err := buf.Write(hunk.Body); err != nil {
9490
return nil, err
9591
}
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+
}
96103
}
97104
return buf.Bytes(), nil
98105
}

0 commit comments

Comments
 (0)