Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions modules/git/last_commit_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ package git
import (
"crypto/sha256"
"fmt"
"sync"

"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

var lastCommitPool = sync.Pool{
New: func() any {
return &Commit{}
},
}

func getCacheKey(repoPath, commitID, entryPath string) string {
hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", repoPath, commitID, entryPath)))
return fmt.Sprintf("last_commit:%x", hashBytes)
Expand Down Expand Up @@ -58,20 +65,25 @@ func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) {
return nil, nil
}

commit := lastCommitPool.Get().(*Commit)
defer lastCommitPool.Put(commit)
var err error
var ok bool

commitID, ok := c.cache.Get(getCacheKey(c.repoPath, ref, entryPath))
if !ok || commitID == "" {
return nil, nil
}

log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, commitID)
if c.commitCache != nil {
if commit, ok := c.commitCache[commitID]; ok {
if commit, ok = c.commitCache[commitID]; ok {
log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, commitID)
return commit, nil
}
}

commit, err := c.repo.GetCommit(commitID)
commit, err = c.repo.GetCommit(commitID)
if err != nil {
return nil, err
}
Expand Down