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

Commit a0b45cc

Browse files
committed
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.
1 parent b29ccd9 commit a0b45cc

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

plumbing/object/commit.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package object
33
import (
44
"bufio"
55
"bytes"
6+
"errors"
67
"fmt"
78
"io"
89
"strings"
@@ -91,6 +92,16 @@ func (c *Commit) NumParents() int {
9192
return len(c.ParentHashes)
9293
}
9394

95+
var ErrNoParents = errors.New("commit has no parents")
96+
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
101+
}
102+
return GetCommit(c.s, c.ParentHashes[0])
103+
}
104+
94105
// File returns the file with the specified "path" in the commit and a
95106
// nil error if the file exists. If the file does not exist, it returns
96107
// a nil file and the ErrFileNotFound error.

0 commit comments

Comments
 (0)