diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-11-20 01:55:48 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-11-20 01:55:48 +0100 |
commit | e09fa242c1f97547527fa0cb9f6288f9ae17479e (patch) | |
tree | fea2c5955e9a50612833affcb301779a94e71adb /plumbing | |
parent | a0b45cc5508ae48b01799ca800e464888ed598be (diff) | |
download | go-git-e09fa242c1f97547527fa0cb9f6288f9ae17479e.tar.gz |
plumbing: object, commit.Parent() method
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/object/commit.go | 13 | ||||
-rw-r--r-- | plumbing/object/commit_test.go | 12 |
2 files changed, 19 insertions, 6 deletions
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index 772ce17..a994efb 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -92,14 +92,15 @@ func (c *Commit) NumParents() int { return len(c.ParentHashes) } -var ErrNoParents = errors.New("commit has no parents") +var ErrParentNotFound = errors.New("commit parent not found") -// FirstParent returns the first parent of c. -func (c *Commit) FirstParent() (*Commit, error) { - if len(c.ParentHashes) == 0 { - return nil, ErrNoParents +// Parent returns the ith parent of a commit. +func (c *Commit) Parent(i int) (*Commit, error) { + if len(c.ParentHashes) == 0 || i > len(c.ParentHashes)-1 { + return nil, ErrParentNotFound } - return GetCommit(c.s, c.ParentHashes[0]) + + return GetCommit(c.s, c.ParentHashes[i]) } // File returns the file with the specified "path" in the commit and a diff --git a/plumbing/object/commit_test.go b/plumbing/object/commit_test.go index e89302d..213dfba 100644 --- a/plumbing/object/commit_test.go +++ b/plumbing/object/commit_test.go @@ -67,6 +67,18 @@ func (s *SuiteCommit) TestParents(c *C) { i.Close() } +func (s *SuiteCommit) TestParent(c *C) { + commit, err := s.Commit.Parent(1) + c.Assert(err, IsNil) + c.Assert(commit.Hash.String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69") +} + +func (s *SuiteCommit) TestParentNotFound(c *C) { + commit, err := s.Commit.Parent(42) + c.Assert(err, Equals, ErrParentNotFound) + c.Assert(commit, IsNil) +} + func (s *SuiteCommit) TestPatch(c *C) { from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294")) to := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |