Skip to content

Commit fdc031c

Browse files
committed
Make last commit cache always available
1 parent 648df8a commit fdc031c

15 files changed

+93
-170
lines changed

modules/git/commit.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ func (c *Commit) ParentCount() int {
7878

7979
// GetCommitByPath return the commit of relative path object.
8080
func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
81-
if c.repo.LastCommitCache != nil {
82-
return c.repo.LastCommitCache.GetCommitByPath(c.ID.String(), relpath)
83-
}
84-
return c.repo.getCommitByPathWithID(c.ID, relpath)
81+
return c.repo.lastCommitCache.GetCommitByPath(c.ID.String(), relpath)
8582
}
8683

8784
// AddChanges marks local changes to be ready for commit.

modules/git/commit_info_gogit.go

-19
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,6 @@ func getFileHashes(c cgobject.CommitNode, treePath string, paths []string) (map[
153153
return hashes, nil
154154
}
155155

156-
func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
157-
var unHitEntryPaths []string
158-
results := make(map[string]*Commit)
159-
for _, p := range paths {
160-
lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
161-
if err != nil {
162-
return nil, nil, err
163-
}
164-
if lastCommit != nil {
165-
results[p] = lastCommit
166-
continue
167-
}
168-
169-
unHitEntryPaths = append(unHitEntryPaths, p)
170-
}
171-
172-
return results, unHitEntryPaths, nil
173-
}
174-
175156
// GetLastCommitForPaths returns last commit information
176157
func GetLastCommitForPaths(ctx context.Context, cache *LastCommitCache, c cgobject.CommitNode, treePath string, paths []string) (map[string]*Commit, error) {
177158
refSha := c.ID().String()

modules/git/commit_info_nogogit.go

+11-38
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12-
"path"
1312
"sort"
1413

1514
"code.gitea.io/gitea/modules/log"
@@ -27,29 +26,22 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
2726
var err error
2827

2928
var revs map[string]*Commit
30-
if commit.repo.LastCommitCache != nil {
31-
var unHitPaths []string
32-
revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, commit.repo.LastCommitCache)
29+
30+
var unHitPaths []string
31+
revs, unHitPaths, err = commit.repo.lastCommitCache.getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths)
32+
if err != nil {
33+
return nil, nil, err
34+
}
35+
if len(unHitPaths) > 0 {
36+
sort.Strings(unHitPaths)
37+
commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths)
3338
if err != nil {
3439
return nil, nil, err
3540
}
36-
if len(unHitPaths) > 0 {
37-
sort.Strings(unHitPaths)
38-
commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths)
39-
if err != nil {
40-
return nil, nil, err
41-
}
4241

43-
for pth, found := range commits {
44-
revs[pth] = found
45-
}
42+
for pth, found := range commits {
43+
revs[pth] = found
4644
}
47-
} else {
48-
sort.Strings(entryPaths)
49-
revs, err = GetLastCommitForPaths(ctx, commit, treePath, entryPaths)
50-
}
51-
if err != nil {
52-
return nil, nil, err
5345
}
5446

5547
commitsInfo := make([]CommitInfo, len(tes))
@@ -97,25 +89,6 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
9789
return commitsInfo, treeCommit, nil
9890
}
9991

100-
func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
101-
var unHitEntryPaths []string
102-
results := make(map[string]*Commit)
103-
for _, p := range paths {
104-
lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
105-
if err != nil {
106-
return nil, nil, err
107-
}
108-
if lastCommit != nil {
109-
results[p] = lastCommit
110-
continue
111-
}
112-
113-
unHitEntryPaths = append(unHitEntryPaths, p)
114-
}
115-
116-
return results, unHitEntryPaths, nil
117-
}
118-
11992
// GetLastCommitForPaths returns last commit information
12093
func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) {
12194
// We read backwards from the commit to obtain all of the commits

modules/git/last_commit_cache.go

+39-33
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package git
55

66
import (
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+
}

modules/git/last_commit_cache_gogit.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@ import (
1313
)
1414

1515
// CacheCommit will cache the commit from the gitRepository
16-
func (c *Commit) CacheCommit(ctx context.Context) error {
17-
if c.repo.LastCommitCache == nil {
18-
return nil
19-
}
16+
func (c *lastCommitCache) CacheCommit(ctx context.Context, commit *Commit) error {
2017
commitNodeIndex, _ := c.repo.CommitNodeIndex()
2118

2219
index, err := commitNodeIndex.Get(plumbing.Hash(c.ID.RawValue()))
2320
if err != nil {
2421
return err
2522
}
2623

27-
return c.recursiveCache(ctx, index, &c.Tree, "", 1)
24+
return c.recursiveCache(ctx, index, commit, &commit.Tree, "", 1)
2825
}
2926

30-
func (c *Commit) recursiveCache(ctx context.Context, index cgobject.CommitNode, tree *Tree, treePath string, level int) error {
27+
func (c *lastCommitCache) recursiveCache(ctx context.Context, index cgobject.CommitNode, commit *Commit, tree *Tree, treePath string, level int) error {
3128
if level == 0 {
3229
return nil
3330
}
@@ -44,7 +41,7 @@ func (c *Commit) recursiveCache(ctx context.Context, index cgobject.CommitNode,
4441
entryMap[entry.Name()] = entry
4542
}
4643

47-
commits, err := GetLastCommitForPaths(ctx, c.repo.LastCommitCache, index, treePath, entryPaths)
44+
commits, err := GetLastCommitForPaths(ctx, c, index, treePath, entryPaths)
4845
if err != nil {
4946
return err
5047
}
@@ -55,7 +52,7 @@ func (c *Commit) recursiveCache(ctx context.Context, index cgobject.CommitNode,
5552
if err != nil {
5653
return err
5754
}
58-
if err := c.recursiveCache(ctx, index, subTree, entry, level-1); err != nil {
55+
if err := c.recursiveCache(ctx, index, commit, subTree, entry, level-1); err != nil {
5956
return err
6057
}
6158
}

modules/git/last_commit_cache_nogogit.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ import (
1010
)
1111

1212
// CacheCommit will cache the commit from the gitRepository
13-
func (c *Commit) CacheCommit(ctx context.Context) error {
14-
if c.repo.LastCommitCache == nil {
15-
return nil
16-
}
17-
return c.recursiveCache(ctx, &c.Tree, "", 1)
13+
func (c *lastCommitCache) CacheCommit(ctx context.Context, commit *Commit) error {
14+
return c.recursiveCache(ctx, commit, &commit.Tree, "", 1)
1815
}
1916

20-
func (c *Commit) recursiveCache(ctx context.Context, tree *Tree, treePath string, level int) error {
17+
func (c *lastCommitCache) recursiveCache(ctx context.Context, commit *Commit, tree *Tree, treePath string, level int) error {
2118
if level == 0 {
2219
return nil
2320
}
@@ -32,7 +29,7 @@ func (c *Commit) recursiveCache(ctx context.Context, tree *Tree, treePath string
3229
entryPaths[i] = entry.Name()
3330
}
3431

35-
_, err = WalkGitLog(ctx, c.repo, c, treePath, entryPaths...)
32+
_, err = WalkGitLog(ctx, c.repo, commit, treePath, entryPaths...)
3633
if err != nil {
3734
return err
3835
}
@@ -44,7 +41,7 @@ func (c *Commit) recursiveCache(ctx context.Context, tree *Tree, treePath string
4441
if err != nil {
4542
return err
4643
}
47-
if err := c.recursiveCache(ctx, subTree, treeEntry.Name(), level-1); err != nil {
44+
if err := c.recursiveCache(ctx, commit, subTree, treeEntry.Name(), level-1); err != nil {
4845
return err
4946
}
5047
}

modules/git/log_name_status.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,14 @@ heaploop:
387387
changed[i] = false
388388
if results[i] == "" {
389389
results[i] = current.CommitID
390-
if err := repo.LastCommitCache.Put(headRef, path.Join(treepath, paths[i]), current.CommitID); err != nil {
390+
if err := repo.lastCommitCache.Put(headRef, path.Join(treepath, paths[i]), current.CommitID); err != nil {
391391
return nil, err
392392
}
393393
delete(path2idx, paths[i])
394394
remaining--
395395
if results[0] == "" {
396396
results[0] = current.CommitID
397-
if err := repo.LastCommitCache.Put(headRef, treepath, current.CommitID); err != nil {
397+
if err := repo.lastCommitCache.Put(headRef, treepath, current.CommitID); err != nil {
398398
return nil, err
399399
}
400400
delete(path2idx, "")

modules/git/repo_base_nogogit.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"path/filepath"
1313

14+
"code.gitea.io/gitea/modules/cache"
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/util"
1617
)
@@ -21,7 +22,8 @@ const isGogit = false
2122
type Repository struct {
2223
Path string
2324

24-
tagCache *ObjectCache[*Tag]
25+
tagCache *ObjectCache[*Tag]
26+
commitCache map[string]*Commit
2527

2628
gpgSettings *GPGSettings
2729

@@ -32,7 +34,7 @@ type Repository struct {
3234
check *Batch
3335

3436
Ctx context.Context
35-
LastCommitCache *LastCommitCache
37+
lastCommitCache *lastCommitCache
3638

3739
objectFormat ObjectFormat
3840
}
@@ -56,11 +58,14 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
5658
return nil, util.NewNotExistErrorf("no such file or directory")
5759
}
5860

59-
return &Repository{
60-
Path: repoPath,
61-
tagCache: newObjectCache[*Tag](),
62-
Ctx: ctx,
63-
}, nil
61+
repo := &Repository{
62+
Path: repoPath,
63+
tagCache: newObjectCache[*Tag](),
64+
commitCache: make(map[string]*Commit),
65+
Ctx: ctx,
66+
}
67+
repo.lastCommitCache = newLastCommitCache(repoPath, repo, cache.GetCache())
68+
return repo, nil
6469
}
6570

6671
// CatFileBatch obtains a CatFileBatch for this repository
@@ -127,7 +132,8 @@ func (repo *Repository) Close() error {
127132
repo.check = nil
128133
repo.checkInUse = false
129134
}
130-
repo.LastCommitCache = nil
135+
repo.lastCommitCache = nil
131136
repo.tagCache = nil
137+
repo.commitCache = nil
132138
return nil
133139
}

0 commit comments

Comments
 (0)