-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrepo.go
285 lines (243 loc) · 5.93 KB
/
repo.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2018 Atelier Disko. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fatih/color"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
var (
ErrNoData = errors.New("not enough or no data")
)
// NewRepository initializes a new Repository. A mainPath must always
// be given, an optional subPath may be given when submodules are in
// use.
func NewRepository(mainPath string, subPath string) (*Repository, error) {
var path string
var repo *git.Repository
path = mainPath
repo, err := git.PlainOpen(mainPath)
if err != nil {
return nil, err
}
var hasFoundMatchingSub bool
if subPath != "" && subPath != mainPath {
wt, err := repo.Worktree()
if err != nil {
return nil, err
}
subs, err := wt.Submodules()
if err != nil {
return nil, err
}
if len(subs) == 0 {
return nil, errors.New("No submodules available. Are you missing a .gitmodules file?")
}
for _, sub := range subs {
if filepath.Join(mainPath, sub.Config().Path) != subPath {
log.Printf("Skipping submodule at %s", filepath.Join(mainPath, sub.Config().Path))
continue
}
subRepo, err := sub.Repository()
if err != nil {
return nil, err
}
path = subPath
repo = subRepo
hasFoundMatchingSub = true
}
if !hasFoundMatchingSub {
return nil, fmt.Errorf("Failed to match subrepository %s to available ones", subPath)
}
}
return &Repository{
Repository: repo,
path: path,
lookup: make(map[string]time.Time, 0),
ticker: time.NewTicker(5 * time.Second),
done: make(chan bool),
}, nil
}
type Repository struct {
sync.RWMutex
*git.Repository
// Lookup table, mapping file paths to modified times.
lookup map[string]time.Time
// Current head reference.
head *plumbing.Reference
// Root of the repository's worktree.
path string
// Ticker which triggers a lookup rebuild.
ticker *time.Ticker
// Quit channel, receiving true, when we are closed.
done chan bool
}
func (r *Repository) StartLookupBuilder() {
yellow := color.New(color.FgYellow)
go func() {
for {
select {
case <-r.ticker.C:
if r.IsLookupStale() {
if err := r.BuildLookup(); err != nil {
log.Print(yellow.Sprintf("Failed to rebuild repository lookup table: %s", err))
continue
}
}
case <-r.done:
log.Print("Stopping repo lookup builder (received quit)...")
return
}
}
}()
}
func (r *Repository) StopLookupBuilder() {
r.done <- true
}
func (r *Repository) Close() {
r.ticker.Stop()
}
func (r *Repository) IsLookupStale() bool {
r.RLock()
defer r.RUnlock()
if r.head == nil {
return false
}
ref, _ := r.Head()
return r.head.Hash() != ref.Hash()
}
// BuildLookup will build the lookup table. This allows lookups of
// a file's modified time. Will add modified time for all files and
// directories discovered in root, which is recursively walked.
//
// Implementation based upon snippet provided in:
// https://github.com/src-d/go-git/issues/604
//
// Also see:
// https://github.com/src-d/go-git/issues/417
// https://github.com/src-d/go-git/issues/826
func (r *Repository) BuildLookup() error {
r.Lock()
defer r.Unlock()
start := time.Now()
pathsCached := make(map[string]bool, 0)
ref, _ := r.Head()
if ref == nil {
log.Printf("No commits in repository %s, yet", r.path)
return nil
}
r.head = ref
err := filepath.Walk(r.path, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
isRoot := filepath.Base(r.path) == f.Name()
if strings.HasPrefix(f.Name(), ".") && !isRoot {
return filepath.SkipDir
}
return nil // Git only knows about files
}
rel, _ := filepath.Rel(r.path, path)
pathsCached[rel] = false
return nil
})
if err != nil {
return fmt.Errorf("Failed to walk directory tree %s: %s", r.path, err)
}
r.lookup = make(map[string]time.Time, 0)
commits, err := r.Log(&git.LogOptions{From: r.head.Hash()})
if err != nil {
return err
}
defer commits.Close()
var prevCommit *object.Commit
var prevTree *object.Tree
Outer:
for {
commit, err := commits.Next()
if err != nil {
break
}
currentTree, err := commit.Tree()
if err != nil {
return err
}
if prevCommit == nil {
prevCommit = commit
prevTree = currentTree
continue
}
changes, err := currentTree.Diff(prevTree)
if err != nil {
return err
}
for _, c := range changes {
if c.To.Name == "" {
continue
}
if isCached, ok := pathsCached[c.To.Name]; !ok || isCached {
// Not interested in this file.
continue
}
r.lookup[c.To.Name] = prevCommit.Author.When
pathsCached[c.To.Name] = true
if len(r.lookup) >= len(pathsCached) {
break Outer
}
}
prevCommit = commit
prevTree = currentTree
}
log.Printf("Created repository lookup table with %d object/s in %s", len(r.lookup), time.Since(start))
return nil
}
// Modified considers any changes in and below given path as a change
// to the path. The path must be absolute and rooted at the repository
// path.
func (r *Repository) Modified(path string) (time.Time, error) {
r.RLock()
defer r.RUnlock()
var modified time.Time
path, err := filepath.Rel(r.path, path)
if err != nil {
return modified, err
}
// Fast path for files.
if m, ok := r.lookup[path]; ok {
return m, nil
}
for p, m := range r.lookup {
if !filepath.HasPrefix(p, path) {
continue
}
if m.After(modified) {
modified = m
}
}
if !modified.IsZero() {
return modified, nil
}
if r.head == nil {
return modified, ErrNoData
}
// When there's only one commit no diffing has been taken place.
// It can be assumed that this is an initial commit adding all
// files.
commit, err := r.CommitObject(r.head.Hash())
if err != nil {
return modified, err
}
return commit.Author.When, nil
}