4
4
package git
5
5
6
6
import (
7
+ "context"
7
8
"crypto/sha256"
8
9
"fmt"
10
+ "path"
9
11
10
12
"code.gitea.io/gitea/modules/cache"
11
13
"code.gitea.io/gitea/modules/log"
@@ -17,25 +19,21 @@ func getCacheKey(repoPath, commitID, entryPath string) string {
17
19
return fmt .Sprintf ("last_commit:%x" , hashBytes )
18
20
}
19
21
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
27
28
}
28
29
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 {
31
32
if cache == nil {
32
33
return nil
33
34
}
34
- if count < setting .CacheService .LastCommit .CommitsCount {
35
- return nil
36
- }
37
35
38
- return & LastCommitCache {
36
+ return & lastCommitCache {
39
37
repoPath : repoPath ,
40
38
repo : gitRepo ,
41
39
ttl : setting .LastCommitCacheTTLSeconds ,
@@ -44,7 +42,7 @@ func NewLastCommitCache(count int64, repoPath string, gitRepo *Repository, cache
44
42
}
45
43
46
44
// 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 {
48
46
if c == nil || c .cache == nil {
49
47
return nil
50
48
}
@@ -53,7 +51,7 @@ func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
53
51
}
54
52
55
53
// 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 ) {
57
55
if c == nil || c .cache == nil {
58
56
return nil , nil
59
57
}
@@ -63,27 +61,12 @@ func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) {
63
61
return nil , nil
64
62
}
65
63
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 )
83
66
}
84
67
85
68
// 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 ) {
87
70
sha , err := NewIDFromString (commitID )
88
71
if err != nil {
89
72
return nil , err
@@ -105,3 +88,26 @@ func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit,
105
88
106
89
return lastCommit , nil
107
90
}
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