@@ -11,6 +11,8 @@ import (
11
11
"errors"
12
12
"io"
13
13
"io/ioutil"
14
+ "os"
15
+ "path/filepath"
14
16
"strings"
15
17
)
16
18
@@ -33,6 +35,18 @@ func (repo *Repository) ResolveReference(name string) (string, error) {
33
35
34
36
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
35
37
func (repo * Repository ) GetRefCommitID (name string ) (string , error ) {
38
+ if strings .HasPrefix (name , "refs/" ) {
39
+ // We're gonna try just reading the ref file as this is likely to be quicker than other options
40
+ fileInfo , err := os .Lstat (filepath .Join (repo .Path , name ))
41
+ if err == nil && fileInfo .Mode ().IsRegular () && fileInfo .Size () == 41 {
42
+ ref , err := ioutil .ReadFile (filepath .Join (repo .Path , name ))
43
+
44
+ if err == nil && SHAPattern .Match (ref [:40 ]) && ref [40 ] == '\n' {
45
+ return string (ref [:40 ]), nil
46
+ }
47
+ }
48
+ }
49
+
36
50
stdout , err := NewCommand ("show-ref" , "--verify" , "--hash" , name ).RunInDir (repo .Path )
37
51
if err != nil {
38
52
if strings .Contains (err .Error (), "not a valid ref" ) {
@@ -69,10 +83,10 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
69
83
70
84
bufReader := bufio .NewReader (stdoutReader )
71
85
72
- return repo .getCommitFromReader (bufReader , id )
86
+ return repo .getCommitFromBatchReader (bufReader , id )
73
87
}
74
88
75
- func (repo * Repository ) getCommitFromReader (bufReader * bufio.Reader , id SHA1 ) (* Commit , error ) {
89
+ func (repo * Repository ) getCommitFromBatchReader (bufReader * bufio.Reader , id SHA1 ) (* Commit , error ) {
76
90
_ , typ , size , err := ReadBatchLine (bufReader )
77
91
if err != nil {
78
92
if errors .Is (err , io .EOF ) {
0 commit comments