This repository was archived by the owner on Sep 11, 2020. It is now read-only.
File tree 2 files changed +24
-0
lines changed
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package object
3
3
import (
4
4
"bufio"
5
5
"bytes"
6
+ "errors"
6
7
"fmt"
7
8
"io"
8
9
"strings"
@@ -98,6 +99,17 @@ func (c *Commit) NumParents() int {
98
99
return len (c .ParentHashes )
99
100
}
100
101
102
+ var ErrParentNotFound = errors .New ("commit parent not found" )
103
+
104
+ // Parent returns the ith parent of a commit.
105
+ func (c * Commit ) Parent (i int ) (* Commit , error ) {
106
+ if len (c .ParentHashes ) == 0 || i > len (c .ParentHashes )- 1 {
107
+ return nil , ErrParentNotFound
108
+ }
109
+
110
+ return GetCommit (c .s , c .ParentHashes [i ])
111
+ }
112
+
101
113
// File returns the file with the specified "path" in the commit and a
102
114
// nil error if the file exists. If the file does not exist, it returns
103
115
// a nil file and the ErrFileNotFound error.
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