From a0b45cc5508ae48b01799ca800e464888ed598be Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 3 Aug 2017 16:46:57 -0700 Subject: plumbing/object: add Commit.FirstParent First parents are somewhat special in git. There's even a --first-parent flag to 'git log'. Add a helper method to look them up. This avoids boilerplate and spares the client from having to arrange for a handle to the Storer, which is stored in the unexported field Commit.s. --- plumbing/object/commit.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'plumbing/object/commit.go') diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index c5a1867..772ce17 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -3,6 +3,7 @@ package object import ( "bufio" "bytes" + "errors" "fmt" "io" "strings" @@ -91,6 +92,16 @@ func (c *Commit) NumParents() int { return len(c.ParentHashes) } +var ErrNoParents = errors.New("commit has no parents") + +// FirstParent returns the first parent of c. +func (c *Commit) FirstParent() (*Commit, error) { + if len(c.ParentHashes) == 0 { + return nil, ErrNoParents + } + return GetCommit(c.s, c.ParentHashes[0]) +} + // 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. -- cgit From e09fa242c1f97547527fa0cb9f6288f9ae17479e Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Mon, 20 Nov 2017 01:55:48 +0100 Subject: plumbing: object, commit.Parent() method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Máximo Cuadros --- plumbing/object/commit.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'plumbing/object/commit.go') 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 -- cgit