From 6d6f02d11f1b65fc0f5fb6ebeed6e8f32d2c5fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Thu, 18 Apr 2019 11:59:40 +0200 Subject: [PATCH 1/3] plumbing: commit.StatsContext and fix for root commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Máximo Cuadros --- plumbing/object/commit.go | 36 +++++++++++++++++----------- plumbing/object/commit_stats_test.go | 17 +++++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index e2543426a..092e1fe42 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -76,7 +76,7 @@ func (c *Commit) Tree() (*Tree, error) { return GetTree(c.s, c.TreeHash) } -// Patch returns the Patch between the actual commit and the provided one. +// PatchContext returns the Patch between the actual commit and the provided one. // Error will be return if context expires. Provided context must be non-nil func (c *Commit) PatchContext(ctx context.Context, to *Commit) (*Patch, error) { fromTree, err := c.Tree() @@ -291,25 +291,33 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { return err } -// Stats shows the status of commit. +// Stats returns the stats of a commit. func (c *Commit) Stats() (FileStats, error) { - // Get the previous commit. - ci := c.Parents() - parentCommit, err := ci.Next() + return c.StatsContext(context.Background()) +} + +// StatsContext returns the stats of a commit. Error will be return if context +// expires. Provided context must be non-nil +func (c *Commit) StatsContext(ctx context.Context) (FileStats, error) { + fromTree, err := c.Tree() if err != nil { - if err == io.EOF { - emptyNoder := treeNoder{} - parentCommit = &Commit{ - Hash: emptyNoder.hash, - // TreeHash: emptyNoder.parent.Hash, - s: c.s, - } - } else { + return nil, err + } + + toTree := &Tree{} + if c.NumParents() != 0 { + firstParent, err := c.Parents().Next() + if err != nil { + return nil, err + } + + toTree, err = firstParent.Tree() + if err != nil { return nil, err } } - patch, err := parentCommit.Patch(c) + patch, err := toTree.PatchContext(ctx, fromTree) if err != nil { return nil, err } diff --git a/plumbing/object/commit_stats_test.go b/plumbing/object/commit_stats_test.go index ce366a227..2fb3f086f 100644 --- a/plumbing/object/commit_stats_test.go +++ b/plumbing/object/commit_stats_test.go @@ -1,6 +1,7 @@ package object_test import ( + "context" "time" "gopkg.in/src-d/go-git.v4" @@ -26,9 +27,25 @@ func (s *CommitStatsSuite) TestStats(c *C) { aCommit, err := r.CommitObject(hash) c.Assert(err, IsNil) + fileStats, err := aCommit.StatsContext(context.Background()) + 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_RootCommit(c *C) { + r, hash := s.writeHisotry(c, []byte("foo\n")) + + aCommit, err := r.CommitObject(hash) + c.Assert(err, IsNil) + fileStats, err := aCommit.Stats() c.Assert(err, IsNil) + c.Assert(fileStats, HasLen, 1) c.Assert(fileStats[0].Name, Equals, "foo") c.Assert(fileStats[0].Addition, Equals, 1) c.Assert(fileStats[0].Deletion, Equals, 0) From 8bc15d3dfe8bb1ea88d97eace49b25e5db9243e1 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Thu, 18 Apr 2019 12:42:13 +0200 Subject: [PATCH 2/3] Update plumbing/object/commit.go Co-Authored-By: mcuadros --- plumbing/object/commit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index 092e1fe42..9ae8dabc5 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -77,7 +77,7 @@ func (c *Commit) Tree() (*Tree, error) { } // PatchContext returns the Patch between the actual commit and the provided one. -// Error will be return if context expires. Provided context must be non-nil +// Error will be return if context expires. Provided context must be non-nil. func (c *Commit) PatchContext(ctx context.Context, to *Commit) (*Patch, error) { fromTree, err := c.Tree() if err != nil { From 660a39c6a3a7a7f3230c3b05d938fd598efd78ce Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Thu, 18 Apr 2019 12:42:19 +0200 Subject: [PATCH 3/3] Update plumbing/object/commit.go Co-Authored-By: mcuadros --- plumbing/object/commit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index 9ae8dabc5..b569d3ce2 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -297,7 +297,7 @@ func (c *Commit) Stats() (FileStats, error) { } // StatsContext returns the stats of a commit. Error will be return if context -// expires. Provided context must be non-nil +// expires. Provided context must be non-nil. func (c *Commit) StatsContext(ctx context.Context) (FileStats, error) { fromTree, err := c.Tree() if err != nil {