diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2019-10-15 11:30:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-15 11:30:15 +0200 |
commit | c7e9e20feccc4ad46ab3cdd11f795643c4503685 (patch) | |
tree | 0d190415e0dde3bce6fe27bffbc06ab1901c237e /plumbing/format/diff/unified_encoder.go | |
parent | 8d20cc5916edf7cfa6a9c5ed069f0640dc823c12 (diff) | |
parent | 883ed6f63e9a92cb1d25266df5074982774cec8a (diff) | |
download | go-git-c7e9e20feccc4ad46ab3cdd11f795643c4503685.tar.gz |
Merge pull request #1225 from sjansen/issues/936
format: diff, Handle no newline at end of file. Fixes #936
Diffstat (limited to 'plumbing/format/diff/unified_encoder.go')
-rw-r--r-- | plumbing/format/diff/unified_encoder.go | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go index 169242d..ce3bc7c 100644 --- a/plumbing/format/diff/unified_encoder.go +++ b/plumbing/format/diff/unified_encoder.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io" + "regexp" "strings" "gopkg.in/src-d/go-git.v4/plumbing" @@ -25,9 +26,10 @@ const ( tPath = "+++ %s\n" binary = "Binary files %s and %s differ\n" - addLine = "+%s\n" - deleteLine = "-%s\n" - equalLine = " %s\n" + addLine = "+%s%s" + deleteLine = "-%s%s" + equalLine = " %s%s" + noNewLine = "\n\\ No newline at end of file\n" oldMode = "old mode %o\n" newMode = "new mode %o\n" @@ -216,7 +218,7 @@ func (c *hunksGenerator) processHunk(i int, op Operation) { linesBefore = c.ctxLines } - c.current = &hunk{ctxPrefix: ctxPrefix} + c.current = &hunk{ctxPrefix: strings.TrimSuffix(ctxPrefix, "\n")} c.current.AddOp(Equal, c.beforeContext...) switch op { @@ -279,12 +281,13 @@ func (c *hunksGenerator) processEqualsLines(ls []string, i int) { } } +var splitLinesRE = regexp.MustCompile(`[^\n]*(\n|$)`) + func splitLines(s string) []string { - out := strings.Split(s, "\n") + out := splitLinesRE.FindAllString(s, -1) if out[len(out)-1] == "" { out = out[:len(out)-1] } - return out } @@ -346,7 +349,7 @@ type op struct { } func (o *op) String() string { - var prefix string + var prefix, suffix string switch o.t { case Add: prefix = addLine @@ -355,6 +358,10 @@ func (o *op) String() string { case Equal: prefix = equalLine } + n := len(o.text) + if n > 0 && o.text[n-1] != '\n' { + suffix = noNewLine + } - return fmt.Sprintf(prefix, o.text) + return fmt.Sprintf(prefix, o.text, suffix) } |