44package git
55
66import (
7+ "context"
78 "crypto/sha256"
89 "fmt"
10+ "path"
911
1012 "code.gitea.io/gitea/modules/cache"
1113 "code.gitea.io/gitea/modules/log"
@@ -17,25 +19,21 @@ func getCacheKey(repoPath, commitID, entryPath string) string {
1719 return fmt .Sprintf ("last_commit:%x" , hashBytes )
1820}
1921
20- // LastCommitCache represents a cache to store last commit
21- type LastCommitCache struct {
22- repoPath string
23- ttl func () int64
24- repo * Repository
25- commitCache map [string ]* Commit
26- cache cache.StringCache
22+ // lastCommitCache represents a cache to store last commit
23+ type lastCommitCache struct {
24+ repoPath string
25+ repo * Repository
26+ ttl func () int64
27+ cache cache.StringCache
2728}
2829
29- // NewLastCommitCache creates a new last commit cache for repo
30- func NewLastCommitCache ( count int64 , repoPath string , gitRepo * Repository , cache cache.StringCache ) * LastCommitCache {
30+ // newLastCommitCache creates a new last commit cache for repo
31+ func newLastCommitCache ( repoPath string , gitRepo * Repository , cache cache.StringCache ) * lastCommitCache {
3132 if cache == nil {
3233 return nil
3334 }
34- if count < setting .CacheService .LastCommit .CommitsCount {
35- return nil
36- }
3735
38- return & LastCommitCache {
36+ return & lastCommitCache {
3937 repoPath : repoPath ,
4038 repo : gitRepo ,
4139 ttl : setting .LastCommitCacheTTLSeconds ,
@@ -44,7 +42,7 @@ func NewLastCommitCache(count int64, repoPath string, gitRepo *Repository, cache
4442}
4543
4644// Put put the last commit id with commit and entry path
47- func (c * LastCommitCache ) Put (ref , entryPath , commitID string ) error {
45+ func (c * lastCommitCache ) Put (ref , entryPath , commitID string ) error {
4846 if c == nil || c .cache == nil {
4947 return nil
5048 }
@@ -53,7 +51,7 @@ func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
5351}
5452
5553// Get gets the last commit information by commit id and entry path
56- func (c * LastCommitCache ) Get (ref , entryPath string ) (* Commit , error ) {
54+ func (c * lastCommitCache ) Get (ref , entryPath string ) (* Commit , error ) {
5755 if c == nil || c .cache == nil {
5856 return nil , nil
5957 }
@@ -63,27 +61,12 @@ func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) {
6361 return nil , nil
6462 }
6563
66- log .Debug ("LastCommitCache hit level 1: [%s:%s:%s]" , ref , entryPath , commitID )
67- if c .commitCache != nil {
68- if commit , ok := c .commitCache [commitID ]; ok {
69- log .Debug ("LastCommitCache hit level 2: [%s:%s:%s]" , ref , entryPath , commitID )
70- return commit , nil
71- }
72- }
73-
74- commit , err := c .repo .GetCommit (commitID )
75- if err != nil {
76- return nil , err
77- }
78- if c .commitCache == nil {
79- c .commitCache = make (map [string ]* Commit )
80- }
81- c .commitCache [commitID ] = commit
82- return commit , nil
64+ log .Debug ("LastCommitCache hit: [%s:%s:%s]" , ref , entryPath , commitID )
65+ return c .repo .GetCommit (commitID )
8366}
8467
8568// GetCommitByPath gets the last commit for the entry in the provided commit
86- func (c * LastCommitCache ) GetCommitByPath (commitID , entryPath string ) (* Commit , error ) {
69+ func (c * lastCommitCache ) GetCommitByPath (commitID , entryPath string ) (* Commit , error ) {
8770 sha , err := NewIDFromString (commitID )
8871 if err != nil {
8972 return nil , err
@@ -105,3 +88,26 @@ func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit,
10588
10689 return lastCommit , nil
10790}
91+
92+ func (c * lastCommitCache ) getLastCommitForPathsByCache (commitID , treePath string , paths []string ) (map [string ]* Commit , []string , error ) {
93+ var unHitEntryPaths []string
94+ results := make (map [string ]* Commit )
95+ for _ , p := range paths {
96+ lastCommit , err := c .Get (commitID , path .Join (treePath , p ))
97+ if err != nil {
98+ return nil , nil , err
99+ }
100+ if lastCommit != nil {
101+ results [p ] = lastCommit
102+ continue
103+ }
104+
105+ unHitEntryPaths = append (unHitEntryPaths , p )
106+ }
107+
108+ return results , unHitEntryPaths , nil
109+ }
110+
111+ func (repo * Repository ) CacheCommit (ctx context.Context , commit * Commit ) error {
112+ return repo .lastCommitCache .CacheCommit (ctx , commit )
113+ }
0 commit comments