Skip to content

Commit de77627

Browse files
committed
more APIs
1 parent 615a33a commit de77627

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

commit.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@ type Commit struct {
2424
// submodules map[string]*SubModule
2525
}
2626

27+
// ParentID returns oid of n-th parent (0-based index).
28+
// It returns nil if no such parent exists.
29+
func (c *Commit) ParentID(n int) (sha1, error) {
30+
if n >= len(c.parents) {
31+
return sha1{}, ErrNotExist{"", ""}
32+
}
33+
return c.parents[n], nil
34+
}
35+
36+
// Parent returns n-th parent (0-based index) of the commit.
37+
func (c *Commit) Parent(n int) (*Commit, error) {
38+
id, err := c.ParentID(n)
39+
if err != nil {
40+
return nil, err
41+
}
42+
parent, err := c.repo.getCommit(id)
43+
if err != nil {
44+
return nil, err
45+
}
46+
return parent, nil
47+
}
48+
49+
// ParentCount returns number of parents of the commit.
50+
// 0 if this is the root commit, otherwise 1,2, etc.
51+
func (c *Commit) ParentCount() int {
52+
return len(c.parents)
53+
}
54+
55+
// GetCommitOfRelPath return the commit of relative path object.
2756
func (c *Commit) GetCommitOfRelPath(relpath string) (*Commit, error) {
2857
return c.repo.getCommitOfRelPath(c.ID, relpath)
2958
}

0 commit comments

Comments
 (0)