Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 2fa2b63

Browse files
authored
add last commit cache interface (#144)
* add last commit cache interface * fix tests
1 parent 0aea7f1 commit 2fa2b63

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

cache.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package git
6+
7+
// LastCommitCache cache
8+
type LastCommitCache interface {
9+
Get(repoPath, ref, entryPath string) (*Commit, error)
10+
Put(repoPath, ref, entryPath string, commit *Commit) error
11+
}

commit_info.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,20 @@ func (state *getCommitsInfoState) getTargetedEntryPath() string {
7272
}
7373

7474
// repeatedly perform targeted searches for unpopulated entries
75-
func targetedSearch(state *getCommitsInfoState, done chan error) {
75+
func targetedSearch(state *getCommitsInfoState, done chan error, cache LastCommitCache) {
7676
for {
7777
entryPath := state.getTargetedEntryPath()
7878
if len(entryPath) == 0 {
7979
done <- nil
8080
return
8181
}
82+
if cache != nil {
83+
commit, err := cache.Get(state.headCommit.repo.Path, state.headCommit.ID.String(), entryPath)
84+
if err == nil && commit != nil {
85+
state.update(entryPath, commit)
86+
continue
87+
}
88+
}
8289
command := NewCommand("rev-list", "-1", state.headCommit.ID.String(), "--", entryPath)
8390
output, err := command.RunInDir(state.headCommit.repo.Path)
8491
if err != nil {
@@ -96,6 +103,9 @@ func targetedSearch(state *getCommitsInfoState, done chan error) {
96103
return
97104
}
98105
state.update(entryPath, commit)
106+
if cache != nil {
107+
cache.Put(state.headCommit.repo.Path, state.headCommit.ID.String(), entryPath, commit)
108+
}
99109
}
100110
}
101111

@@ -118,9 +128,9 @@ func initGetCommitInfoState(entries Entries, headCommit *Commit, treePath string
118128
}
119129

120130
// GetCommitsInfo gets information of all commits that are corresponding to these entries
121-
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
131+
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCommitCache) ([][]interface{}, error) {
122132
state := initGetCommitInfoState(tes, commit, treePath)
123-
if err := getCommitsInfo(state); err != nil {
133+
if err := getCommitsInfo(state, cache); err != nil {
124134
return nil, err
125135
}
126136
if len(state.commits) < len(state.entryPaths) {
@@ -188,7 +198,7 @@ func (state *getCommitsInfoState) update(entryPath string, commit *Commit) bool
188198

189199
const getCommitsInfoPretty = "--pretty=format:%H %ct %s"
190200

191-
func getCommitsInfo(state *getCommitsInfoState) error {
201+
func getCommitsInfo(state *getCommitsInfoState, cache LastCommitCache) error {
192202
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
193203
defer cancel()
194204

@@ -215,7 +225,7 @@ func getCommitsInfo(state *getCommitsInfoState) error {
215225
numThreads := runtime.NumCPU()
216226
done := make(chan error, numThreads)
217227
for i := 0; i < numThreads; i++ {
218-
go targetedSearch(state, done)
228+
go targetedSearch(state, done, cache)
219229
}
220230

221231
scanner := bufio.NewScanner(readCloser)

commit_info_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
5151
assert.NoError(t, err)
5252
entries, err := tree.ListEntries()
5353
assert.NoError(t, err)
54-
commitsInfo, err := entries.GetCommitsInfo(commit, testCase.Path)
54+
commitsInfo, err := entries.GetCommitsInfo(commit, testCase.Path, nil)
5555
assert.NoError(t, err)
5656
assert.Len(t, commitsInfo, len(testCase.ExpectedIDs))
5757
for _, commitInfo := range commitsInfo {
@@ -106,7 +106,7 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
106106
b.ResetTimer()
107107
b.Run(benchmark.name, func(b *testing.B) {
108108
for i := 0; i < b.N; i++ {
109-
_, err := entries.GetCommitsInfo(commit, "")
109+
_, err := entries.GetCommitsInfo(commit, "", nil)
110110
if err != nil {
111111
b.Fatal(err)
112112
}

0 commit comments

Comments
 (0)