@@ -117,20 +117,26 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
117117 return commit , nil
118118}
119119
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 ) {
122122 if len (commitID ) != 40 {
123123 var err error
124- actualCommitID , err := NewCommand ("rev-parse" , commitID ).RunInDir (repo .Path )
124+ actualCommitID , err := NewCommand ("rev-parse" , "--verify" , commitID ).RunInDir (repo .Path )
125125 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 , "" }
128129 }
129- return nil , err
130+ return SHA1 {} , err
130131 }
131132 commitID = actualCommitID
132133 }
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 )
134140 if err != nil {
135141 return nil , err
136142 }
@@ -243,6 +249,7 @@ func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) {
243249}
244250
245251// 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.
246253func (repo * Repository ) FileChangedBetweenCommits (filename , id1 , id2 string ) (bool , error ) {
247254 stdout , err := NewCommand ("diff" , "--name-only" , "-z" , id1 , id2 , "--" , filename ).RunInDirBytes (repo .Path )
248255 if err != nil {
0 commit comments