@@ -117,20 +117,26 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
117
117
return commit , nil
118
118
}
119
119
120
- // GetCommit returns commit object of by ID string.
121
- func (repo * Repository ) GetCommit (commitID string ) (* Commit , error ) {
120
+ // ConvertToSHA1 returns a Hash object from a potential ID string
121
+ func (repo * Repository ) ConvertToSHA1 (commitID string ) (SHA1 , error ) {
122
122
if len (commitID ) != 40 {
123
123
var err error
124
- actualCommitID , err := NewCommand ("rev-parse" , commitID ).RunInDir (repo .Path )
124
+ actualCommitID , err := NewCommand ("rev-parse" , "--verify" , commitID ).RunInDir (repo .Path )
125
125
if err != nil {
126
- if strings .Contains (err .Error (), "unknown revision or path" ) {
127
- return nil , ErrNotExist {commitID , "" }
126
+ if strings .Contains (err .Error (), "unknown revision or path" ) ||
127
+ strings .Contains (err .Error (), "fatal: Needed a single revision" ) {
128
+ return SHA1 {}, ErrNotExist {commitID , "" }
128
129
}
129
- return nil , err
130
+ return SHA1 {} , err
130
131
}
131
132
commitID = actualCommitID
132
133
}
133
- id , err := NewIDFromString (commitID )
134
+ return NewIDFromString (commitID )
135
+ }
136
+
137
+ // GetCommit returns commit object of by ID string.
138
+ func (repo * Repository ) GetCommit (commitID string ) (* Commit , error ) {
139
+ id , err := repo .ConvertToSHA1 (commitID )
134
140
if err != nil {
135
141
return nil , err
136
142
}
@@ -243,6 +249,7 @@ func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) {
243
249
}
244
250
245
251
// FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2
252
+ // You must ensure that id1 and id2 are valid commit ids.
246
253
func (repo * Repository ) FileChangedBetweenCommits (filename , id1 , id2 string ) (bool , error ) {
247
254
stdout , err := NewCommand ("diff" , "--name-only" , "-z" , id1 , id2 , "--" , filename ).RunInDirBytes (repo .Path )
248
255
if err != nil {
0 commit comments