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

Commit e09fa24

Browse files
committed
plumbing: object, commit.Parent() method
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent a0b45cc commit e09fa24

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

plumbing/object/commit.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ func (c *Commit) NumParents() int {
9292
return len(c.ParentHashes)
9393
}
9494

95-
var ErrNoParents = errors.New("commit has no parents")
95+
var ErrParentNotFound = errors.New("commit parent not found")
9696

97-
// FirstParent returns the first parent of c.
98-
func (c *Commit) FirstParent() (*Commit, error) {
99-
if len(c.ParentHashes) == 0 {
100-
return nil, ErrNoParents
97+
// Parent returns the ith parent of a commit.
98+
func (c *Commit) Parent(i int) (*Commit, error) {
99+
if len(c.ParentHashes) == 0 || i > len(c.ParentHashes)-1 {
100+
return nil, ErrParentNotFound
101101
}
102-
return GetCommit(c.s, c.ParentHashes[0])
102+
103+
return GetCommit(c.s, c.ParentHashes[i])
103104
}
104105

105106
// File returns the file with the specified "path" in the commit and a

plumbing/object/commit_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ func (s *SuiteCommit) TestParents(c *C) {
6767
i.Close()
6868
}
6969

70+
func (s *SuiteCommit) TestParent(c *C) {
71+
commit, err := s.Commit.Parent(1)
72+
c.Assert(err, IsNil)
73+
c.Assert(commit.Hash.String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69")
74+
}
75+
76+
func (s *SuiteCommit) TestParentNotFound(c *C) {
77+
commit, err := s.Commit.Parent(42)
78+
c.Assert(err, Equals, ErrParentNotFound)
79+
c.Assert(commit, IsNil)
80+
}
81+
7082
func (s *SuiteCommit) TestPatch(c *C) {
7183
from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"))
7284
to := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))

0 commit comments

Comments
 (0)