This repository was archived by the owner on Sep 11, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +19
-6
lines changed Expand file tree Collapse file tree 2 files changed +19
-6
lines changed Original file line number Diff line number Diff line change @@ -92,14 +92,15 @@ func (c *Commit) NumParents() int {
92
92
return len (c .ParentHashes )
93
93
}
94
94
95
- var ErrNoParents = errors .New ("commit has no parents " )
95
+ var ErrParentNotFound = errors .New ("commit parent not found " )
96
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
97
+ // Parent returns the ith parent of a commit .
98
+ func (c * Commit ) Parent ( i int ) (* Commit , error ) {
99
+ if len (c .ParentHashes ) == 0 || i > len ( c . ParentHashes ) - 1 {
100
+ return nil , ErrParentNotFound
101
101
}
102
- return GetCommit (c .s , c .ParentHashes [0 ])
102
+
103
+ return GetCommit (c .s , c .ParentHashes [i ])
103
104
}
104
105
105
106
// File returns the file with the specified "path" in the commit and a
Original file line number Diff line number Diff line change @@ -67,6 +67,18 @@ func (s *SuiteCommit) TestParents(c *C) {
67
67
i .Close ()
68
68
}
69
69
70
+ func (s * SuiteCommit ) TestParent (c * C ) {
71
+ commit , err := s .Commit .Parent (1 )
72
+ c .Assert (err , IsNil )
73
+ c .Assert (commit .Hash .String (), Equals , "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69" )
74
+ }
75
+
76
+ func (s * SuiteCommit ) TestParentNotFound (c * C ) {
77
+ commit , err := s .Commit .Parent (42 )
78
+ c .Assert (err , Equals , ErrParentNotFound )
79
+ c .Assert (commit , IsNil )
80
+ }
81
+
70
82
func (s * SuiteCommit ) TestPatch (c * C ) {
71
83
from := s .commit (c , plumbing .NewHash ("918c48b83bd081e863dbe1b80f8998f058cd8294" ))
72
84
to := s .commit (c , plumbing .NewHash ("6ecf0ef2c2dffb796033e5a02219af86ec6584e5" ))
You can’t perform that action at this time.
0 commit comments