diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-11-20 01:59:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-20 01:59:18 +0100 |
commit | 4cbf210eb44ae648545e84da2414e3215a3399a1 (patch) | |
tree | c22984dc43e5c5bb317efa35d523f83d6db7a323 /plumbing/object/commit.go | |
parent | 65b4be36594ca91d3be49f9f6e7d8c8d92d2e42d (diff) | |
parent | e09fa242c1f97547527fa0cb9f6288f9ae17479e (diff) | |
download | go-git-4cbf210eb44ae648545e84da2414e3215a3399a1.tar.gz |
Merge pull request #534 from josharian/firstparent
plumbing: object, commit.Parent() method
Diffstat (limited to 'plumbing/object/commit.go')
-rw-r--r-- | plumbing/object/commit.go | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index fad3dac..b2f1f15 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -3,6 +3,7 @@ package object import ( "bufio" "bytes" + "errors" "fmt" "io" "strings" @@ -98,6 +99,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. |