forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast_commit_queue_gogit.go
177 lines (152 loc) · 4.34 KB
/
last_commit_queue_gogit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// +build gogit
package repository
import (
"context"
"fmt"
"path"
"path/filepath"
"sync"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
)
var lock = sync.Mutex{}
var table = map[CommitCacheRequest]bool{}
var lastCommitQueue queue.UniqueQueue
// CommitCacheRequest represents a cache request
type CommitCacheRequest struct {
Repo string
CommitID string
TreePath string
Recursive bool
}
// Do runs the cache request uniquely ensuring that only one cache request is running for this request triple
func (req *CommitCacheRequest) Do() error {
ctx, cancel, _ := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("Cache: %s:%s:%s:%t", req.Repo, req.CommitID, req.TreePath, req.Recursive))
defer cancel()
recursive := req.Recursive
req.Recursive = false
repo, err := git.OpenRepository(filepath.Join(setting.RepoRootPath, req.Repo+".git"))
if err != nil {
return err
}
commit, err := repo.GetCommit(req.CommitID)
if err != nil {
if git.IsErrNotExist(err) {
return nil
}
return err
}
lccache := git.NewLastCommitCache(req.Repo, repo, setting.LastCommitCacheTTLSeconds, cache.GetCache())
directories := []string{req.TreePath}
for len(directories) > 0 {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
req.TreePath = directories[len(directories)-1]
next, err := req.doTree(ctx, repo, commit, recursive, lccache)
if err != nil {
return err
}
directories = append(next, directories[:len(directories)-1]...)
}
return nil
}
func (req *CommitCacheRequest) doTree(ctx context.Context, repo *git.Repository, commit *git.Commit, recursive bool, lccache *git.LastCommitCache) ([]string, error) {
tree, err := commit.Tree.SubTree(req.TreePath)
if err != nil {
if git.IsErrNotExist(err) {
return nil, nil
}
return nil, err
}
entries, err := tree.ListEntries()
if err != nil {
if git.IsErrNotExist(err) {
return nil, nil
}
return nil, err
}
directories := make([]string, 0, len(entries))
commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
if commitGraphFile != nil {
defer commitGraphFile.Close()
}
commitNode, err := commitNodeIndex.Get(commit.ID)
if err != nil {
return nil, err
}
lock.Lock()
if has := table[*req]; has {
lock.Unlock()
if recursive {
for _, entry := range entries {
if entry.IsDir() {
directories = append(directories, path.Join(req.TreePath, entry.Name()))
}
}
}
return directories, nil
}
table[*req] = true
lock.Unlock()
defer func() {
lock.Lock()
delete(table, *req)
lock.Unlock()
}()
entryPaths := make([]string, 0, len(entries))
for _, entry := range entries {
if recursive && entry.IsDir() {
directories = append(directories, path.Join(req.TreePath, entry.Name()))
}
_, ok := lccache.GetCachedCommitID(req.CommitID, path.Join(req.TreePath, entry.Name()))
if !ok {
entryPaths = append(entryPaths, entry.Name())
}
}
if len(entryPaths) == 0 {
return directories, nil
}
commits, err := git.GetLastCommitForPaths(ctx, commitNode, req.TreePath, entryPaths)
if err != nil {
return nil, err
}
for entryPath, entryCommit := range commits {
if err := lccache.Put(commit.ID.String(), path.Join(req.TreePath, entryPath), entryCommit.ID().String()); err != nil {
return nil, err
}
}
return directories, nil
}
func handle(data ...queue.Data) {
for _, datum := range data {
req := datum.(*CommitCacheRequest)
if err := req.Do(); err != nil {
log.Error("Unable to process commit cache request for %s:%s:%s:%t: %v", req.Repo, req.CommitID, req.TreePath, req.Recursive, err)
}
}
}
// Init initialises the queue
func Init() error {
lastCommitQueue = queue.CreateUniqueQueue("last_commit_queue", handle, &CommitCacheRequest{}).(queue.UniqueQueue)
return nil
}
// UpdateCache queues the the request
func UpdateCache(repo, commitID, treePath string, recursive bool) error {
return lastCommitQueue.Push(&CommitCacheRequest{
Repo: repo,
CommitID: commitID,
TreePath: treePath,
Recursive: recursive,
})
}