aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2018-10-16 00:12:10 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2018-10-16 00:12:10 +0200
commitc4be04433cf894cead23cd1de2be54c2886e44ad (patch)
tree2f4847115bb33bd571b2d8bcf0e413f246dd2547
parent8153e040f68da6002096ef177a11510f4fb06769 (diff)
downloadgo-git-c4be04433cf894cead23cd1de2be54c2886e44ad.tar.gz
blame: fix edge case with missing \n in content length causing mismatched length error
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
-rw-r--r--blame.go21
-rw-r--r--blame_test.go26
2 files changed, 42 insertions, 5 deletions
diff --git a/blame.go b/blame.go
index 349cdd9..adb72d5 100644
--- a/blame.go
+++ b/blame.go
@@ -123,14 +123,25 @@ func newLine(author, text string, date time.Time, hash plumbing.Hash) *Line {
}
func newLines(contents []string, commits []*object.Commit) ([]*Line, error) {
- if len(contents) != len(commits) {
- return nil, errors.New("contents and commits have different length")
+ lcontents := len(contents)
+ lcommits := len(commits)
+
+ if lcontents != lcommits {
+ if lcontents == lcommits-1 && contents[lcontents-1] != "\n" {
+ contents = append(contents, "\n")
+ } else {
+ return nil, errors.New("contents and commits have different length")
+ }
}
- result := make([]*Line, 0, len(contents))
+
+ result := make([]*Line, 0, lcontents)
for i := range contents {
- l := newLine(commits[i].Author.Email, contents[i], commits[i].Author.When, commits[i].Hash)
- result = append(result, l)
+ result = append(result, newLine(
+ commits[i].Author.Email, contents[i],
+ commits[i].Author.When, commits[i].Hash,
+ ))
}
+
return result, nil
}
diff --git a/blame_test.go b/blame_test.go
index 92911b1..e0ac129 100644
--- a/blame_test.go
+++ b/blame_test.go
@@ -2,6 +2,7 @@ package git
import (
"gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/object"
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-git-fixtures.v3"
@@ -13,6 +14,31 @@ type BlameSuite struct {
var _ = Suite(&BlameSuite{})
+func (s *BlameSuite) TestNewLines(c *C) {
+ h := plumbing.NewHash("ce9f123d790717599aaeb76bc62510de437761be")
+ lines, err := newLines([]string{"foo"}, []*object.Commit{{
+ Hash: h,
+ Message: "foo",
+ }})
+
+ c.Assert(err, IsNil)
+ c.Assert(lines, HasLen, 1)
+ c.Assert(lines[0].Text, Equals, "foo")
+ c.Assert(lines[0].Hash, Equals, h)
+}
+
+func (s *BlameSuite) TestNewLinesWithNewLine(c *C) {
+ lines, err := newLines([]string{"foo"}, []*object.Commit{
+ {Message: "foo"},
+ {Message: "bar"},
+ })
+
+ c.Assert(err, IsNil)
+ c.Assert(lines, HasLen, 2)
+ c.Assert(lines[0].Text, Equals, "foo")
+ c.Assert(lines[1].Text, Equals, "\n")
+}
+
type blameTest struct {
repo string
rev string