0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-12-10 15:24:54 +01:00

Merge 3c599a7d139e608ee91dfacaf751ce6f11dbaaec into 98ef79d73a6a546241dd02959ae17f136369b604

This commit is contained in:
Lunny Xiao 2025-12-08 05:00:11 +00:00 committed by GitHub
commit 1c92df4915
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 118 additions and 201 deletions

View File

@ -1905,9 +1905,6 @@ LEVEL = Info
;; Time to keep items in cache if not used, default is 8760 hours. ;; Time to keep items in cache if not used, default is 8760 hours.
;; Setting it to -1 disables caching ;; Setting it to -1 disables caching
;ITEM_TTL = 8760h ;ITEM_TTL = 8760h
;;
;; Only enable the cache when repository's commits count great than
;COMMITS_COUNT = 1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View File

@ -76,10 +76,7 @@ func (c *Commit) ParentCount() int {
// GetCommitByPath return the commit of relative path object. // GetCommitByPath return the commit of relative path object.
func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) { func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
if c.repo.LastCommitCache != nil { return c.repo.lastCommitCache.GetCommitByPath(c.ID.String(), relpath)
return c.repo.LastCommitCache.GetCommitByPath(c.ID.String(), relpath)
}
return c.repo.getCommitByPathWithID(c.ID, relpath)
} }
// AddChanges marks local changes to be ready for commit. // AddChanges marks local changes to be ready for commit.

View File

@ -34,28 +34,19 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit *
return nil, nil, err return nil, nil, err
} }
var revs map[string]*Commit revs, unHitPaths, err := commit.repo.lastCommitCache.getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths)
if commit.repo.LastCommitCache != nil { if err != nil {
var unHitPaths []string return nil, nil, err
revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, commit.repo.LastCommitCache) }
if len(unHitPaths) > 0 {
revs2, err := GetLastCommitForPaths(ctx, commit.repo.lastCommitCache, c, treePath, unHitPaths)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
if len(unHitPaths) > 0 {
revs2, err := GetLastCommitForPaths(ctx, commit.repo.LastCommitCache, c, treePath, unHitPaths)
if err != nil {
return nil, nil, err
}
for k, v := range revs2 { for k, v := range revs2 {
revs[k] = v revs[k] = v
}
} }
} else {
revs, err = GetLastCommitForPaths(ctx, nil, c, treePath, entryPaths)
}
if err != nil {
return nil, nil, err
} }
commit.repo.gogitStorage.Close() commit.repo.gogitStorage.Close()
@ -143,27 +134,8 @@ func getFileHashes(c cgobject.CommitNode, treePath string, paths []string) (map[
return hashes, nil return hashes, nil
} }
func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
var unHitEntryPaths []string
results := make(map[string]*Commit)
for _, p := range paths {
lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
if err != nil {
return nil, nil, err
}
if lastCommit != nil {
results[p] = lastCommit
continue
}
unHitEntryPaths = append(unHitEntryPaths, p)
}
return results, unHitEntryPaths, nil
}
// GetLastCommitForPaths returns last commit information // GetLastCommitForPaths returns last commit information
func GetLastCommitForPaths(ctx context.Context, cache *LastCommitCache, c cgobject.CommitNode, treePath string, paths []string) (map[string]*Commit, error) { func GetLastCommitForPaths(ctx context.Context, cache *lastCommitCache, c cgobject.CommitNode, treePath string, paths []string) (map[string]*Commit, error) {
refSha := c.ID().String() refSha := c.ID().String()
// We do a tree traversal with nodes sorted by commit time // We do a tree traversal with nodes sorted by commit time

View File

@ -26,27 +26,20 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit *
var err error var err error
var revs map[string]*Commit var revs map[string]*Commit
if commit.repo.LastCommitCache != nil {
var unHitPaths []string var unHitPaths []string
revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, commit.repo.LastCommitCache) revs, unHitPaths, err = commit.repo.lastCommitCache.getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths)
if err != nil {
return nil, nil, err
}
if len(unHitPaths) > 0 {
sort.Strings(unHitPaths)
commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
if len(unHitPaths) > 0 {
sort.Strings(unHitPaths)
commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths)
if err != nil {
return nil, nil, err
}
maps.Copy(revs, commits) maps.Copy(revs, commits)
}
} else {
sort.Strings(entryPaths)
revs, err = GetLastCommitForPaths(ctx, commit, treePath, entryPaths)
}
if err != nil {
return nil, nil, err
} }
commitsInfo := make([]CommitInfo, len(tes)) commitsInfo := make([]CommitInfo, len(tes))
@ -84,25 +77,6 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit *
return commitsInfo, treeCommit, nil return commitsInfo, treeCommit, nil
} }
func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
var unHitEntryPaths []string
results := make(map[string]*Commit)
for _, p := range paths {
lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
if err != nil {
return nil, nil, err
}
if lastCommit != nil {
results[p] = lastCommit
continue
}
unHitEntryPaths = append(unHitEntryPaths, p)
}
return results, unHitEntryPaths, nil
}
// GetLastCommitForPaths returns last commit information // GetLastCommitForPaths returns last commit information
func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) { func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) {
// We read backwards from the commit to obtain all of the commits // We read backwards from the commit to obtain all of the commits

View File

@ -4,8 +4,10 @@
package git package git
import ( import (
"context"
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"path"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -17,25 +19,21 @@ func getCacheKey(repoPath, commitID, entryPath string) string {
return fmt.Sprintf("last_commit:%x", hashBytes) return fmt.Sprintf("last_commit:%x", hashBytes)
} }
// LastCommitCache represents a cache to store last commit // lastCommitCache represents a cache to store last commit
type LastCommitCache struct { type lastCommitCache struct {
repoPath string repoPath string
ttl func() int64 repo *Repository
repo *Repository ttl func() int64
commitCache map[string]*Commit cache cache.StringCache
cache cache.StringCache
} }
// NewLastCommitCache creates a new last commit cache for repo // newLastCommitCache creates a new last commit cache for repo
func NewLastCommitCache(count int64, repoPath string, gitRepo *Repository, cache cache.StringCache) *LastCommitCache { func newLastCommitCache(repoPath string, gitRepo *Repository, cache cache.StringCache) *lastCommitCache {
if cache == nil { if cache == nil {
return nil return nil
} }
if count < setting.CacheService.LastCommit.CommitsCount {
return nil
}
return &LastCommitCache{ return &lastCommitCache{
repoPath: repoPath, repoPath: repoPath,
repo: gitRepo, repo: gitRepo,
ttl: setting.LastCommitCacheTTLSeconds, ttl: setting.LastCommitCacheTTLSeconds,
@ -44,7 +42,7 @@ func NewLastCommitCache(count int64, repoPath string, gitRepo *Repository, cache
} }
// Put put the last commit id with commit and entry path // Put put the last commit id with commit and entry path
func (c *LastCommitCache) Put(ref, entryPath, commitID string) error { func (c *lastCommitCache) Put(ref, entryPath, commitID string) error {
if c == nil || c.cache == nil { if c == nil || c.cache == nil {
return nil return nil
} }
@ -53,7 +51,7 @@ func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
} }
// Get gets the last commit information by commit id and entry path // Get gets the last commit information by commit id and entry path
func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) { func (c *lastCommitCache) Get(ref, entryPath string) (*Commit, error) {
if c == nil || c.cache == nil { if c == nil || c.cache == nil {
return nil, nil return nil, nil
} }
@ -63,27 +61,12 @@ func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) {
return nil, nil return nil, nil
} }
log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, commitID) log.Debug("LastCommitCache hit: [%s:%s:%s]", ref, entryPath, commitID)
if c.commitCache != nil { return c.repo.GetCommit(commitID)
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)
if err != nil {
return nil, err
}
if c.commitCache == nil {
c.commitCache = make(map[string]*Commit)
}
c.commitCache[commitID] = commit
return commit, nil
} }
// GetCommitByPath gets the last commit for the entry in the provided commit // GetCommitByPath gets the last commit for the entry in the provided commit
func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit, error) { func (c *lastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit, error) {
sha, err := NewIDFromString(commitID) sha, err := NewIDFromString(commitID)
if err != nil { if err != nil {
return nil, err return nil, err
@ -105,3 +88,26 @@ func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit,
return lastCommit, nil return lastCommit, nil
} }
func (c *lastCommitCache) getLastCommitForPathsByCache(commitID, treePath string, paths []string) (map[string]*Commit, []string, error) {
var unHitEntryPaths []string
results := make(map[string]*Commit)
for _, p := range paths {
lastCommit, err := c.Get(commitID, path.Join(treePath, p))
if err != nil {
return nil, nil, err
}
if lastCommit != nil {
results[p] = lastCommit
continue
}
unHitEntryPaths = append(unHitEntryPaths, p)
}
return results, unHitEntryPaths, nil
}
func (repo *Repository) CacheCommit(ctx context.Context, commit *Commit) error {
return repo.lastCommitCache.CacheCommit(ctx, commit)
}

View File

@ -13,21 +13,18 @@ import (
) )
// CacheCommit will cache the commit from the gitRepository // CacheCommit will cache the commit from the gitRepository
func (c *Commit) CacheCommit(ctx context.Context) error { func (c *lastCommitCache) CacheCommit(ctx context.Context, commit *Commit) error {
if c.repo.LastCommitCache == nil {
return nil
}
commitNodeIndex, _ := c.repo.CommitNodeIndex() commitNodeIndex, _ := c.repo.CommitNodeIndex()
index, err := commitNodeIndex.Get(plumbing.Hash(c.ID.RawValue())) index, err := commitNodeIndex.Get(plumbing.Hash(commit.ID.RawValue()))
if err != nil { if err != nil {
return err return err
} }
return c.recursiveCache(ctx, index, &c.Tree, "", 1) return c.recursiveCache(ctx, index, commit, &commit.Tree, "", 1)
} }
func (c *Commit) recursiveCache(ctx context.Context, index cgobject.CommitNode, tree *Tree, treePath string, level int) error { func (c *lastCommitCache) recursiveCache(ctx context.Context, index cgobject.CommitNode, commit *Commit, tree *Tree, treePath string, level int) error {
if level == 0 { if level == 0 {
return nil return nil
} }
@ -44,7 +41,7 @@ func (c *Commit) recursiveCache(ctx context.Context, index cgobject.CommitNode,
entryMap[entry.Name()] = entry entryMap[entry.Name()] = entry
} }
commits, err := GetLastCommitForPaths(ctx, c.repo.LastCommitCache, index, treePath, entryPaths) commits, err := GetLastCommitForPaths(ctx, c, index, treePath, entryPaths)
if err != nil { if err != nil {
return err return err
} }
@ -55,7 +52,7 @@ func (c *Commit) recursiveCache(ctx context.Context, index cgobject.CommitNode,
if err != nil { if err != nil {
return err return err
} }
if err := c.recursiveCache(ctx, index, subTree, entry, level-1); err != nil { if err := c.recursiveCache(ctx, index, commit, subTree, entry, level-1); err != nil {
return err return err
} }
} }

View File

@ -10,14 +10,11 @@ import (
) )
// CacheCommit will cache the commit from the gitRepository // CacheCommit will cache the commit from the gitRepository
func (c *Commit) CacheCommit(ctx context.Context) error { func (c *lastCommitCache) CacheCommit(ctx context.Context, commit *Commit) error {
if c.repo.LastCommitCache == nil { return c.recursiveCache(ctx, commit, &commit.Tree, "", 1)
return nil
}
return c.recursiveCache(ctx, &c.Tree, "", 1)
} }
func (c *Commit) recursiveCache(ctx context.Context, tree *Tree, treePath string, level int) error { func (c *lastCommitCache) recursiveCache(ctx context.Context, commit *Commit, tree *Tree, treePath string, level int) error {
if level == 0 { if level == 0 {
return nil return nil
} }
@ -32,7 +29,7 @@ func (c *Commit) recursiveCache(ctx context.Context, tree *Tree, treePath string
entryPaths[i] = entry.Name() entryPaths[i] = entry.Name()
} }
_, err = WalkGitLog(ctx, c.repo, c, treePath, entryPaths...) _, err = WalkGitLog(ctx, c.repo, commit, treePath, entryPaths...)
if err != nil { if err != nil {
return err return err
} }
@ -44,7 +41,7 @@ func (c *Commit) recursiveCache(ctx context.Context, tree *Tree, treePath string
if err != nil { if err != nil {
return err return err
} }
if err := c.recursiveCache(ctx, subTree, treeEntry.Name(), level-1); err != nil { if err := c.recursiveCache(ctx, commit, subTree, treeEntry.Name(), level-1); err != nil {
return err return err
} }
} }

View File

@ -384,14 +384,14 @@ heaploop:
changed[i] = false changed[i] = false
if results[i] == "" { if results[i] == "" {
results[i] = current.CommitID results[i] = current.CommitID
if err := repo.LastCommitCache.Put(headRef, path.Join(treepath, paths[i]), current.CommitID); err != nil { if err := repo.lastCommitCache.Put(headRef, path.Join(treepath, paths[i]), current.CommitID); err != nil {
return nil, err return nil, err
} }
delete(path2idx, paths[i]) delete(path2idx, paths[i])
remaining-- remaining--
if results[0] == "" { if results[0] == "" {
results[0] = current.CommitID results[0] = current.CommitID
if err := repo.LastCommitCache.Put(headRef, treepath, current.CommitID); err != nil { if err := repo.lastCommitCache.Put(headRef, treepath, current.CommitID); err != nil {
return nil, err return nil, err
} }
delete(path2idx, "") delete(path2idx, "")

View File

@ -10,6 +10,7 @@ import (
"context" "context"
"path/filepath" "path/filepath"
giteacache "code.gitea.io/gitea/modules/cache"
gitealog "code.gitea.io/gitea/modules/log" gitealog "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -28,14 +29,15 @@ const isGogit = true
type Repository struct { type Repository struct {
Path string Path string
tagCache *ObjectCache[*Tag] tagCache *ObjectCache[*Tag]
commitCache map[string]*Commit
gogitRepo *gogit.Repository gogitRepo *gogit.Repository
gogitStorage *filesystem.Storage gogitStorage *filesystem.Storage
gpgSettings *GPGSettings gpgSettings *GPGSettings
Ctx context.Context Ctx context.Context
LastCommitCache *LastCommitCache lastCommitCache *lastCommitCache
objectFormat ObjectFormat objectFormat ObjectFormat
} }
@ -75,14 +77,18 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
return nil, err return nil, err
} }
return &Repository{ repo := &Repository{
Path: repoPath, Path: repoPath,
gogitRepo: gogitRepo, gogitRepo: gogitRepo,
gogitStorage: storage, gogitStorage: storage,
tagCache: newObjectCache[*Tag](), tagCache: newObjectCache[*Tag](),
commitCache: make(map[string]*Commit),
Ctx: ctx, Ctx: ctx,
objectFormat: ParseGogitHash(plumbing.ZeroHash).Type(), objectFormat: ParseGogitHash(plumbing.ZeroHash).Type(),
}, nil }
repo.lastCommitCache = newLastCommitCache(repo.Path, repo, giteacache.GetCache())
return repo, nil
} }
// Close this repository, in particular close the underlying gogitStorage if this is not nil // Close this repository, in particular close the underlying gogitStorage if this is not nil
@ -94,8 +100,9 @@ func (repo *Repository) Close() error {
gitealog.Error("Error closing storage: %v", err) gitealog.Error("Error closing storage: %v", err)
} }
repo.gogitStorage = nil repo.gogitStorage = nil
repo.LastCommitCache = nil repo.lastCommitCache = nil
repo.tagCache = nil repo.tagCache = nil
repo.commitCache = nil
return nil return nil
} }

View File

@ -11,6 +11,7 @@ import (
"context" "context"
"path/filepath" "path/filepath"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
) )
@ -21,7 +22,8 @@ const isGogit = false
type Repository struct { type Repository struct {
Path string Path string
tagCache *ObjectCache[*Tag] tagCache *ObjectCache[*Tag]
commitCache map[string]*Commit
gpgSettings *GPGSettings gpgSettings *GPGSettings
@ -32,7 +34,7 @@ type Repository struct {
check *Batch check *Batch
Ctx context.Context Ctx context.Context
LastCommitCache *LastCommitCache lastCommitCache *lastCommitCache
objectFormat ObjectFormat objectFormat ObjectFormat
} }
@ -51,11 +53,14 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
return nil, util.NewNotExistErrorf("no such file or directory") return nil, util.NewNotExistErrorf("no such file or directory")
} }
return &Repository{ repo := &Repository{
Path: repoPath, Path: repoPath,
tagCache: newObjectCache[*Tag](), tagCache: newObjectCache[*Tag](),
Ctx: ctx, commitCache: make(map[string]*Commit),
}, nil Ctx: ctx,
}
repo.lastCommitCache = newLastCommitCache(repoPath, repo, cache.GetCache())
return repo, nil
} }
// CatFileBatch obtains a CatFileBatch for this repository // CatFileBatch obtains a CatFileBatch for this repository
@ -122,7 +127,8 @@ func (repo *Repository) Close() error {
repo.check = nil repo.check = nil
repo.checkInUse = false repo.checkInUse = false
} }
repo.LastCommitCache = nil repo.lastCommitCache = nil
repo.tagCache = nil repo.tagCache = nil
repo.commitCache = nil
return nil return nil
} }

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/modules/git/gitcmd" "code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
) )
@ -27,12 +28,22 @@ func (repo *Repository) GetTagCommitID(name string) (string, error) {
// GetCommit returns commit object of by ID string. // GetCommit returns commit object of by ID string.
func (repo *Repository) GetCommit(commitID string) (*Commit, error) { func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
if commit, ok := repo.commitCache[commitID]; ok {
log.Debug("repo commitCache hit: [%s:%s:%s]", repo.Path, commitID)
return commit, nil
}
id, err := repo.ConvertToGitID(commitID) id, err := repo.ConvertToGitID(commitID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return repo.getCommit(id) commit, err := repo.getCommit(id)
if err != nil {
return nil, err
}
repo.commitCache[commitID] = commit
return commit, nil
} }
// GetBranchCommit returns the last commit of given branch. // GetBranchCommit returns the last commit of given branch.

View File

@ -23,8 +23,7 @@ var CacheService = struct {
Cache `ini:"cache"` Cache `ini:"cache"`
LastCommit struct { LastCommit struct {
TTL time.Duration `ini:"ITEM_TTL"` TTL time.Duration `ini:"ITEM_TTL"`
CommitsCount int64
} `ini:"cache.last_commit"` } `ini:"cache.last_commit"`
}{ }{
Cache: Cache{ Cache: Cache{
@ -33,11 +32,9 @@ var CacheService = struct {
TTL: 16 * time.Hour, TTL: 16 * time.Hour,
}, },
LastCommit: struct { LastCommit: struct {
TTL time.Duration `ini:"ITEM_TTL"` TTL time.Duration `ini:"ITEM_TTL"`
CommitsCount int64
}{ }{
TTL: 8760 * time.Hour, TTL: 8760 * time.Hour,
CommitsCount: 1000,
}, },
} }
@ -63,9 +60,6 @@ func loadCacheFrom(rootCfg ConfigProvider) {
default: default:
log.Fatal("Unknown cache adapter: %s", CacheService.Adapter) log.Fatal("Unknown cache adapter: %s", CacheService.Adapter)
} }
sec = rootCfg.Section("cache.last_commit")
CacheService.LastCommit.CommitsCount = sec.Key("COMMITS_COUNT").MustInt64(1000)
} }
// TTLSeconds returns the TTLSeconds or unix timestamp for memcache // TTLSeconds returns the TTLSeconds or unix timestamp for memcache

View File

@ -18,7 +18,6 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/optional"
@ -76,7 +75,6 @@ func CommitInfoCache(ctx *context.Context) {
return return
} }
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache())
} }
func checkContextUser(ctx *context.Context, uid int64) *user_model.User { func checkContextUser(ctx *context.Context, uid int64) *user_model.User {

View File

@ -984,7 +984,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
} }
} }
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache())
} }
} }

View File

@ -6,28 +6,15 @@ package repository
import ( import (
"context" "context"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
) )
// CacheRef cachhe last commit information of the branch or the tag // CacheRef cache last commit information of the branch or the tag
func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName git.RefName) error { func CacheRef(ctx context.Context, gitRepo *git.Repository, fullRefName git.RefName) error {
commit, err := gitRepo.GetCommit(fullRefName.String()) commit, err := gitRepo.GetCommit(fullRefName.String())
if err != nil { if err != nil {
return err return err
} }
if gitRepo.LastCommitCache == nil { return gitRepo.CacheCommit(ctx, commit)
commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(fullRefName.ShortName(), true), func() (int64, error) {
return gitrepo.CommitsCountOfCommit(ctx, repo, commit.ID.String())
})
if err != nil {
return err
}
gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, repo.FullName(), gitRepo, cache.GetCache())
}
return commit.CacheCommit(ctx)
} }

View File

@ -11,9 +11,7 @@ import (
"strings" "strings"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -127,21 +125,7 @@ func GetFileContents(ctx context.Context, repo *repo_model.Repository, gitRepo *
return getFileContentsByEntryInternal(ctx, repo, gitRepo, refCommit, entry, opts) return getFileContentsByEntryInternal(ctx, repo, gitRepo, refCommit, entry, opts)
} }
func addLastCommitCache(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, cacheKey, fullName, sha string) error { func getFileContentsByEntryInternal(_ context.Context, repo *repo_model.Repository, gitRepo *git.Repository, refCommit *utils.RefCommit, entry *git.TreeEntry, opts GetContentsOrListOptions) (*api.ContentsResponse, error) {
if gitRepo.LastCommitCache == nil {
commitsCount, err := cache.GetInt64(cacheKey, func() (int64, error) {
return gitrepo.CommitsCountOfCommit(ctx, repo, sha)
})
if err != nil {
return err
}
gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, fullName, gitRepo, cache.GetCache())
}
return nil
}
func getFileContentsByEntryInternal(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, refCommit *utils.RefCommit, entry *git.TreeEntry, opts GetContentsOrListOptions) (*api.ContentsResponse, error) {
refType := refCommit.RefName.RefType()
commit := refCommit.Commit commit := refCommit.Commit
selfURL, err := url.Parse(repo.APIURL() + "/contents/" + util.PathEscapeSegments(opts.TreePath) + "?ref=" + url.QueryEscape(refCommit.InputRef)) selfURL, err := url.Parse(repo.APIURL() + "/contents/" + util.PathEscapeSegments(opts.TreePath) + "?ref=" + url.QueryEscape(refCommit.InputRef))
if err != nil { if err != nil {
@ -162,11 +146,6 @@ func getFileContentsByEntryInternal(ctx context.Context, repo *repo_model.Reposi
} }
if opts.IncludeCommitMetadata || opts.IncludeCommitMessage { if opts.IncludeCommitMetadata || opts.IncludeCommitMessage {
err = addLastCommitCache(ctx, repo, gitRepo, repo.GetCommitsCountCacheKey(refCommit.InputRef, refType != git.RefTypeCommit), repo.FullName(), refCommit.CommitID)
if err != nil {
return nil, err
}
lastCommit, err := refCommit.Commit.GetCommitByPath(opts.TreePath) lastCommit, err := refCommit.Commit.GetCommitByPath(opts.TreePath)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -215,7 +215,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
notify_service.PushCommits(ctx, pusher, repo, opts, commits) notify_service.PushCommits(ctx, pusher, repo, opts, commits)
// Cache for big repository // Cache for big repository
if err := CacheRef(graceful.GetManager().HammerContext(), repo, gitRepo, opts.RefFullName); err != nil { if err := CacheRef(graceful.GetManager().HammerContext(), gitRepo, opts.RefFullName); err != nil {
log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err) log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err)
} }
} else { } else {

View File

@ -129,14 +129,10 @@ func testViewRepoWithCache(t *testing.T) {
// FIXME: these test don't seem quite right, no enough assert // FIXME: these test don't seem quite right, no enough assert
// no last commit cache // no last commit cache
testView(t) testView(t)
// enable last commit cache for all repositories
oldCommitsCount := setting.CacheService.LastCommit.CommitsCount
setting.CacheService.LastCommit.CommitsCount = 0
// first view will not hit the cache // first view will not hit the cache
testView(t) testView(t)
// second view will hit the cache // second view will hit the cache
testView(t) testView(t)
setting.CacheService.LastCommit.CommitsCount = oldCommitsCount
} }
func testViewRepoPrivate(t *testing.T) { func testViewRepoPrivate(t *testing.T) {