Skip to content

Commit d51a521

Browse files
committed
read refs directly if we can
Signed-off-by: Andrew Thornton <[email protected]>
1 parent d34f33b commit d51a521

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

modules/git/repo_commit_nogogit.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"errors"
1212
"io"
1313
"io/ioutil"
14+
"os"
15+
"path/filepath"
1416
"strings"
1517
)
1618

@@ -33,6 +35,18 @@ func (repo *Repository) ResolveReference(name string) (string, error) {
3335

3436
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
3537
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+
3650
stdout, err := NewCommand("show-ref", "--verify", "--hash", name).RunInDir(repo.Path)
3751
if err != nil {
3852
if strings.Contains(err.Error(), "not a valid ref") {
@@ -69,10 +83,10 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
6983

7084
bufReader := bufio.NewReader(stdoutReader)
7185

72-
return repo.getCommitFromReader(bufReader, id)
86+
return repo.getCommitFromBatchReader(bufReader, id)
7387
}
7488

75-
func (repo *Repository) getCommitFromReader(bufReader *bufio.Reader, id SHA1) (*Commit, error) {
89+
func (repo *Repository) getCommitFromBatchReader(bufReader *bufio.Reader, id SHA1) (*Commit, error) {
7690
_, typ, size, err := ReadBatchLine(bufReader)
7791
if err != nil {
7892
if errors.Is(err, io.EOF) {

0 commit comments

Comments
 (0)