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

Commit aa6f288

Browse files
authored
plumbing: commit.StatsContext and fix for orphan commit (#1115)
plumbing: commit.StatsContext and fix for root commit
1 parent cc5579e commit aa6f288

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

plumbing/object/commit.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func (c *Commit) Tree() (*Tree, error) {
7676
return GetTree(c.s, c.TreeHash)
7777
}
7878

79-
// Patch returns the Patch between the actual commit and the provided one.
80-
// Error will be return if context expires. Provided context must be non-nil
79+
// PatchContext returns the Patch between the actual commit and the provided one.
80+
// Error will be return if context expires. Provided context must be non-nil.
8181
func (c *Commit) PatchContext(ctx context.Context, to *Commit) (*Patch, error) {
8282
fromTree, err := c.Tree()
8383
if err != nil {
@@ -291,25 +291,33 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
291291
return err
292292
}
293293

294-
// Stats shows the status of commit.
294+
// Stats returns the stats of a commit.
295295
func (c *Commit) Stats() (FileStats, error) {
296-
// Get the previous commit.
297-
ci := c.Parents()
298-
parentCommit, err := ci.Next()
296+
return c.StatsContext(context.Background())
297+
}
298+
299+
// StatsContext returns the stats of a commit. Error will be return if context
300+
// expires. Provided context must be non-nil.
301+
func (c *Commit) StatsContext(ctx context.Context) (FileStats, error) {
302+
fromTree, err := c.Tree()
299303
if err != nil {
300-
if err == io.EOF {
301-
emptyNoder := treeNoder{}
302-
parentCommit = &Commit{
303-
Hash: emptyNoder.hash,
304-
// TreeHash: emptyNoder.parent.Hash,
305-
s: c.s,
306-
}
307-
} else {
304+
return nil, err
305+
}
306+
307+
toTree := &Tree{}
308+
if c.NumParents() != 0 {
309+
firstParent, err := c.Parents().Next()
310+
if err != nil {
311+
return nil, err
312+
}
313+
314+
toTree, err = firstParent.Tree()
315+
if err != nil {
308316
return nil, err
309317
}
310318
}
311319

312-
patch, err := parentCommit.Patch(c)
320+
patch, err := toTree.PatchContext(ctx, fromTree)
313321
if err != nil {
314322
return nil, err
315323
}

plumbing/object/commit_stats_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package object_test
22

33
import (
4+
"context"
45
"time"
56

67
"gopkg.in/src-d/go-git.v4"
@@ -26,9 +27,25 @@ func (s *CommitStatsSuite) TestStats(c *C) {
2627
aCommit, err := r.CommitObject(hash)
2728
c.Assert(err, IsNil)
2829

30+
fileStats, err := aCommit.StatsContext(context.Background())
31+
c.Assert(err, IsNil)
32+
33+
c.Assert(fileStats[0].Name, Equals, "foo")
34+
c.Assert(fileStats[0].Addition, Equals, 1)
35+
c.Assert(fileStats[0].Deletion, Equals, 0)
36+
c.Assert(fileStats[0].String(), Equals, " foo | 1 +\n")
37+
}
38+
39+
func (s *CommitStatsSuite) TestStats_RootCommit(c *C) {
40+
r, hash := s.writeHisotry(c, []byte("foo\n"))
41+
42+
aCommit, err := r.CommitObject(hash)
43+
c.Assert(err, IsNil)
44+
2945
fileStats, err := aCommit.Stats()
3046
c.Assert(err, IsNil)
3147

48+
c.Assert(fileStats, HasLen, 1)
3249
c.Assert(fileStats[0].Name, Equals, "foo")
3350
c.Assert(fileStats[0].Addition, Equals, 1)
3451
c.Assert(fileStats[0].Deletion, Equals, 0)

0 commit comments

Comments
 (0)