Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit c4be044

Browse files
committed
blame: fix edge case with missing \n in content length causing mismatched length error
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 8153e04 commit c4be044

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

blame.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,25 @@ func newLine(author, text string, date time.Time, hash plumbing.Hash) *Line {
123123
}
124124

125125
func newLines(contents []string, commits []*object.Commit) ([]*Line, error) {
126-
if len(contents) != len(commits) {
127-
return nil, errors.New("contents and commits have different length")
126+
lcontents := len(contents)
127+
lcommits := len(commits)
128+
129+
if lcontents != lcommits {
130+
if lcontents == lcommits-1 && contents[lcontents-1] != "\n" {
131+
contents = append(contents, "\n")
132+
} else {
133+
return nil, errors.New("contents and commits have different length")
134+
}
128135
}
129-
result := make([]*Line, 0, len(contents))
136+
137+
result := make([]*Line, 0, lcontents)
130138
for i := range contents {
131-
l := newLine(commits[i].Author.Email, contents[i], commits[i].Author.When, commits[i].Hash)
132-
result = append(result, l)
139+
result = append(result, newLine(
140+
commits[i].Author.Email, contents[i],
141+
commits[i].Author.When, commits[i].Hash,
142+
))
133143
}
144+
134145
return result, nil
135146
}
136147

blame_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"gopkg.in/src-d/go-git.v4/plumbing"
5+
"gopkg.in/src-d/go-git.v4/plumbing/object"
56

67
. "gopkg.in/check.v1"
78
"gopkg.in/src-d/go-git-fixtures.v3"
@@ -13,6 +14,31 @@ type BlameSuite struct {
1314

1415
var _ = Suite(&BlameSuite{})
1516

17+
func (s *BlameSuite) TestNewLines(c *C) {
18+
h := plumbing.NewHash("ce9f123d790717599aaeb76bc62510de437761be")
19+
lines, err := newLines([]string{"foo"}, []*object.Commit{{
20+
Hash: h,
21+
Message: "foo",
22+
}})
23+
24+
c.Assert(err, IsNil)
25+
c.Assert(lines, HasLen, 1)
26+
c.Assert(lines[0].Text, Equals, "foo")
27+
c.Assert(lines[0].Hash, Equals, h)
28+
}
29+
30+
func (s *BlameSuite) TestNewLinesWithNewLine(c *C) {
31+
lines, err := newLines([]string{"foo"}, []*object.Commit{
32+
{Message: "foo"},
33+
{Message: "bar"},
34+
})
35+
36+
c.Assert(err, IsNil)
37+
c.Assert(lines, HasLen, 2)
38+
c.Assert(lines[0].Text, Equals, "foo")
39+
c.Assert(lines[1].Text, Equals, "\n")
40+
}
41+
1642
type blameTest struct {
1743
repo string
1844
rev string

0 commit comments

Comments
 (0)