From d34f33bf2938dd4e5a521218ab314d60d782f82a Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 13 Apr 2021 17:33:26 +0100 Subject: [PATCH 1/2] Improve performance when there are multiple commits in the last commit cache Signed-off-by: Andrew Thornton --- modules/git/commit_info_nogogit.go | 5 ++++- modules/git/last_commit_cache_nogogit.go | 9 +++++++-- modules/git/repo_commit_nogogit.go | 7 +++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/modules/git/commit_info_nogogit.go b/modules/git/commit_info_nogogit.go index b844468c8c41b..943a2cf59985e 100644 --- a/modules/git/commit_info_nogogit.go +++ b/modules/git/commit_info_nogogit.go @@ -102,10 +102,13 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo } func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) { + wr, rd, cancel := CatFileBatch(cache.repo.Path) + defer cancel() + var unHitEntryPaths []string var results = make(map[string]*Commit) for _, p := range paths { - lastCommit, err := cache.Get(commitID, path.Join(treePath, p)) + lastCommit, err := cache.Get(commitID, path.Join(treePath, p), wr, rd) if err != nil { return nil, nil, err } diff --git a/modules/git/last_commit_cache_nogogit.go b/modules/git/last_commit_cache_nogogit.go index 0e52d165386d0..0a1babb112148 100644 --- a/modules/git/last_commit_cache_nogogit.go +++ b/modules/git/last_commit_cache_nogogit.go @@ -7,6 +7,8 @@ package git import ( + "bufio" + "io" "path" ) @@ -34,7 +36,7 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64, } // Get get the last commit information by commit id and entry path -func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) { +func (c *LastCommitCache) Get(ref, entryPath string, wr *io.PipeWriter, rd *bufio.Reader) (interface{}, error) { v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath)) if vs, ok := v.(string); ok { log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs) @@ -46,7 +48,10 @@ func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) { if err != nil { return nil, err } - commit, err := c.repo.getCommit(id) + if _, err := wr.Write([]byte(vs + "\n")); err != nil { + return nil, err + } + commit, err := c.repo.getCommitFromBatchReader(rd, id) if err != nil { return nil, err } diff --git a/modules/git/repo_commit_nogogit.go b/modules/git/repo_commit_nogogit.go index 0a92de17037ef..f1703dd7b6616 100644 --- a/modules/git/repo_commit_nogogit.go +++ b/modules/git/repo_commit_nogogit.go @@ -9,7 +9,6 @@ package git import ( "bufio" "errors" - "fmt" "io" "io/ioutil" "strings" @@ -69,6 +68,11 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) { }() bufReader := bufio.NewReader(stdoutReader) + + return repo.getCommitFromReader(bufReader, id) +} + +func (repo *Repository) getCommitFromReader(bufReader *bufio.Reader, id SHA1) (*Commit, error) { _, typ, size, err := ReadBatchLine(bufReader) if err != nil { if errors.Is(err, io.EOF) { @@ -106,7 +110,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(), From d51a5214f5b968132eaa83186b13e7408ddd125c Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 13 Apr 2021 17:56:42 +0100 Subject: [PATCH 2/2] read refs directly if we can Signed-off-by: Andrew Thornton --- modules/git/repo_commit_nogogit.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_commit_nogogit.go b/modules/git/repo_commit_nogogit.go index f1703dd7b6616..df56b26b01594 100644 --- a/modules/git/repo_commit_nogogit.go +++ b/modules/git/repo_commit_nogogit.go @@ -11,6 +11,8 @@ import ( "errors" "io" "io/ioutil" + "os" + "path/filepath" "strings" ) @@ -33,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,10 +83,10 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) { bufReader := bufio.NewReader(stdoutReader) - return repo.getCommitFromReader(bufReader, id) + return repo.getCommitFromBatchReader(bufReader, id) } -func (repo *Repository) getCommitFromReader(bufReader *bufio.Reader, id SHA1) (*Commit, error) { +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) {