From 2a9b544f175ce8bd0268e5516485888738164e41 Mon Sep 17 00:00:00 2001 From: oleksiishnyra Date: Thu, 14 Mar 2019 23:06:17 +0200 Subject: [PATCH 1/2] plumbing: object, Count stats properly when no new line added at the end. Fixes #1074 Signed-off-by: Oleksii Shnyra --- plumbing/object/patch.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plumbing/object/patch.go b/plumbing/object/patch.go index adeaccb0a..068589eff 100644 --- a/plumbing/object/patch.go +++ b/plumbing/object/patch.go @@ -320,11 +320,18 @@ func getFileStatsFromFilePatches(filePatches []fdiff.FilePatch) FileStats { } for _, chunk := range fp.Chunks() { + s := chunk.Content() switch chunk.Type() { case fdiff.Add: - cs.Addition += strings.Count(chunk.Content(), "\n") + cs.Addition += strings.Count(s, "\n") + if s[len(s)-1] != '\n' { + cs.Addition++ + } case fdiff.Delete: - cs.Deletion += strings.Count(chunk.Content(), "\n") + cs.Deletion += strings.Count(s, "\n") + if s[len(s)-1] != '\n' { + cs.Deletion++ + } } } From f69d20660a0fd331dfa5ec1c77dd3daa6ac00bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Thu, 18 Apr 2019 10:03:31 +0200 Subject: [PATCH 2/2] plumbing: commit.Stat test suite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Máximo Cuadros --- plumbing/object/commit_stats_test.go | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 plumbing/object/commit_stats_test.go diff --git a/plumbing/object/commit_stats_test.go b/plumbing/object/commit_stats_test.go new file mode 100644 index 000000000..ce366a227 --- /dev/null +++ b/plumbing/object/commit_stats_test.go @@ -0,0 +1,78 @@ +package object_test + +import ( + "time" + + "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/object" + "gopkg.in/src-d/go-git.v4/storage/memory" + + . "gopkg.in/check.v1" + "gopkg.in/src-d/go-billy.v4/memfs" + "gopkg.in/src-d/go-billy.v4/util" + "gopkg.in/src-d/go-git-fixtures.v3" +) + +type CommitStatsSuite struct { + fixtures.Suite +} + +var _ = Suite(&CommitStatsSuite{}) + +func (s *CommitStatsSuite) TestStats(c *C) { + r, hash := s.writeHisotry(c, []byte("foo\n"), []byte("foo\nbar\n")) + + aCommit, err := r.CommitObject(hash) + c.Assert(err, IsNil) + + fileStats, err := aCommit.Stats() + c.Assert(err, IsNil) + + c.Assert(fileStats[0].Name, Equals, "foo") + c.Assert(fileStats[0].Addition, Equals, 1) + c.Assert(fileStats[0].Deletion, Equals, 0) + c.Assert(fileStats[0].String(), Equals, " foo | 1 +\n") +} + +func (s *CommitStatsSuite) TestStats_WithoutNewLine(c *C) { + r, hash := s.writeHisotry(c, []byte("foo\nbar"), []byte("foo\nbar\n")) + + aCommit, err := r.CommitObject(hash) + c.Assert(err, IsNil) + + fileStats, err := aCommit.Stats() + c.Assert(err, IsNil) + + c.Assert(fileStats[0].Name, Equals, "foo") + c.Assert(fileStats[0].Addition, Equals, 1) + c.Assert(fileStats[0].Deletion, Equals, 1) + c.Assert(fileStats[0].String(), Equals, " foo | 2 +-\n") +} + +func (s *CommitStatsSuite) writeHisotry(c *C, files ...[]byte) (*git.Repository, plumbing.Hash) { + cm := &git.CommitOptions{ + Author: &object.Signature{Name: "Foo", Email: "foo@example.local", When: time.Now()}, + } + + fs := memfs.New() + r, err := git.Init(memory.NewStorage(), fs) + c.Assert(err, IsNil) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + var hash plumbing.Hash + for _, content := range files { + util.WriteFile(fs, "foo", content, 0644) + + _, err = w.Add("foo") + c.Assert(err, IsNil) + + hash, err = w.Commit("foo\n", cm) + c.Assert(err, IsNil) + + } + + return r, hash +}