@@ -9,9 +9,10 @@ package git
9
9
import (
10
10
"bufio"
11
11
"errors"
12
- "fmt"
13
12
"io"
14
13
"io/ioutil"
14
+ "os"
15
+ "path/filepath"
15
16
"strings"
16
17
)
17
18
@@ -34,6 +35,18 @@ func (repo *Repository) ResolveReference(name string) (string, error) {
34
35
35
36
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
36
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
+
37
50
stdout , err := NewCommand ("show-ref" , "--verify" , "--hash" , name ).RunInDir (repo .Path )
38
51
if err != nil {
39
52
if strings .Contains (err .Error (), "not a valid ref" ) {
@@ -69,6 +82,11 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
69
82
}()
70
83
71
84
bufReader := bufio .NewReader (stdoutReader )
85
+
86
+ return repo .getCommitFromBatchReader (bufReader , id )
87
+ }
88
+
89
+ func (repo * Repository ) getCommitFromBatchReader (bufReader * bufio.Reader , id SHA1 ) (* Commit , error ) {
72
90
_ , typ , size , err := ReadBatchLine (bufReader )
73
91
if err != nil {
74
92
if errors .Is (err , io .EOF ) {
@@ -106,7 +124,6 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
106
124
case "commit" :
107
125
return CommitFromReader (repo , id , io .LimitReader (bufReader , size ))
108
126
default :
109
- _ = stdoutReader .CloseWithError (fmt .Errorf ("unknown typ: %s" , typ ))
110
127
log ("Unknown typ: %s" , typ )
111
128
return nil , ErrNotExist {
112
129
ID : id .String (),
0 commit comments