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

plumbing: object, commit.Parent() method #534

Merged
merged 2 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions plumbing/object/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package object
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -91,6 +92,17 @@ func (c *Commit) NumParents() int {
return len(c.ParentHashes)
}

var ErrParentNotFound = errors.New("commit parent not found")

// 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[i])
}

// File returns the file with the specified "path" in the commit and a
// nil error if the file exists. If the file does not exist, it returns
// a nil file and the ErrFileNotFound error.
Expand Down
12 changes: 12 additions & 0 deletions plumbing/object/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down