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

Commit

Permalink
Merge pull request #986 from mcuadros/fix-blame
Browse files Browse the repository at this point in the history
blame: fix edge case with missing \n in content length causing mismatched length error
  • Loading branch information
mcuadros authored Oct 16, 2018
2 parents 41d6f2c + c4be044 commit 236ae86
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
21 changes: 16 additions & 5 deletions blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
26 changes: 26 additions & 0 deletions blame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 236ae86

Please sign in to comment.