diff options
author | zeripath <art27@cantab.net> | 2021-05-02 02:16:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-02 03:16:08 +0200 |
commit | a926ff919db105f7ceed63bb858b6b80d96157dc (patch) | |
tree | 417fd759f635b89d7299e44a5b904af77cd0d972 /modules/git/repo_commit_nogogit.go | |
parent | fda2e4549fcfb4a08977c91520be44d6e4442f26 (diff) | |
download | gitea-a926ff919db105f7ceed63bb858b6b80d96157dc.tar.gz gitea-a926ff919db105f7ceed63bb858b6b80d96157dc.zip |
Performance improvement for last commit cache and show-ref (#15455)
* Improve performance when there are multiple commits in the last commit cache
* read refs directly if we can
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/git/repo_commit_nogogit.go')
-rw-r--r-- | modules/git/repo_commit_nogogit.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/modules/git/repo_commit_nogogit.go b/modules/git/repo_commit_nogogit.go index 0a92de1703..df56b26b01 100644 --- a/modules/git/repo_commit_nogogit.go +++ b/modules/git/repo_commit_nogogit.go @@ -9,9 +9,10 @@ package git import ( "bufio" "errors" - "fmt" "io" "io/ioutil" + "os" + "path/filepath" "strings" ) @@ -34,6 +35,18 @@ func (repo *Repository) ResolveReference(name string) (string, error) { // GetRefCommitID returns the last commit ID string of given reference (branch or tag). func (repo *Repository) GetRefCommitID(name string) (string, error) { + if strings.HasPrefix(name, "refs/") { + // We're gonna try just reading the ref file as this is likely to be quicker than other options + fileInfo, err := os.Lstat(filepath.Join(repo.Path, name)) + if err == nil && fileInfo.Mode().IsRegular() && fileInfo.Size() == 41 { + ref, err := ioutil.ReadFile(filepath.Join(repo.Path, name)) + + if err == nil && SHAPattern.Match(ref[:40]) && ref[40] == '\n' { + return string(ref[:40]), nil + } + } + } + stdout, err := NewCommand("show-ref", "--verify", "--hash", name).RunInDir(repo.Path) if err != nil { if strings.Contains(err.Error(), "not a valid ref") { @@ -69,6 +82,11 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) { }() bufReader := bufio.NewReader(stdoutReader) + + return repo.getCommitFromBatchReader(bufReader, id) +} + +func (repo *Repository) getCommitFromBatchReader(bufReader *bufio.Reader, id SHA1) (*Commit, error) { _, typ, size, err := ReadBatchLine(bufReader) if err != nil { if errors.Is(err, io.EOF) { @@ -106,7 +124,6 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) { case "commit": return CommitFromReader(repo, id, io.LimitReader(bufReader, size)) default: - _ = stdoutReader.CloseWithError(fmt.Errorf("unknown typ: %s", typ)) log("Unknown typ: %s", typ) return nil, ErrNotExist{ ID: id.String(), |