1package cache
2
3import (
4 "bytes"
5 "encoding/gob"
6 "fmt"
7 "io"
8 "io/ioutil"
9 "os"
10 "path"
11 "sort"
12 "strconv"
13 "strings"
14 "time"
15
16 "github.com/MichaelMure/git-bug/bug"
17 "github.com/MichaelMure/git-bug/identity"
18 "github.com/MichaelMure/git-bug/repository"
19 "github.com/MichaelMure/git-bug/util/git"
20 "github.com/MichaelMure/git-bug/util/process"
21)
22
23const cacheFile = "cache"
24const formatVersion = 1
25
26type RepoCache struct {
27 // the underlying repo
28 repo repository.ClockedRepo
29 // excerpt of bugs data for all bugs
30 excerpts map[string]*BugExcerpt
31 // bug loaded in memory
32 bugs map[string]*BugCache
33}
34
35func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
36 c := &RepoCache{
37 repo: r,
38 bugs: make(map[string]*BugCache),
39 }
40
41 err := c.lock()
42 if err != nil {
43 return &RepoCache{}, err
44 }
45
46 err = c.load()
47 if err == nil {
48 return c, nil
49 }
50
51 err = c.buildCache()
52 if err != nil {
53 return nil, err
54 }
55
56 return c, c.write()
57}
58
59// GetPath returns the path to the repo.
60func (c *RepoCache) GetPath() string {
61 return c.repo.GetPath()
62}
63
64// GetPath returns the path to the repo.
65func (c *RepoCache) GetCoreEditor() (string, error) {
66 return c.repo.GetCoreEditor()
67}
68
69// GetUserName returns the name the the user has used to configure git
70func (c *RepoCache) GetUserName() (string, error) {
71 return c.repo.GetUserName()
72}
73
74// GetUserEmail returns the email address that the user has used to configure git.
75func (c *RepoCache) GetUserEmail() (string, error) {
76 return c.repo.GetUserEmail()
77}
78
79// StoreConfig store a single key/value pair in the config of the repo
80func (c *RepoCache) StoreConfig(key string, value string) error {
81 return c.repo.StoreConfig(key, value)
82}
83
84// ReadConfigs read all key/value pair matching the key prefix
85func (c *RepoCache) ReadConfigs(keyPrefix string) (map[string]string, error) {
86 return c.repo.ReadConfigs(keyPrefix)
87}
88
89// RmConfigs remove all key/value pair matching the key prefix
90func (c *RepoCache) RmConfigs(keyPrefix string) error {
91 return c.repo.RmConfigs(keyPrefix)
92}
93
94func (c *RepoCache) lock() error {
95 lockPath := repoLockFilePath(c.repo)
96
97 err := repoIsAvailable(c.repo)
98 if err != nil {
99 return err
100 }
101
102 f, err := os.Create(lockPath)
103 if err != nil {
104 return err
105 }
106
107 pid := fmt.Sprintf("%d", os.Getpid())
108 _, err = f.WriteString(pid)
109 if err != nil {
110 return err
111 }
112
113 return f.Close()
114}
115
116func (c *RepoCache) Close() error {
117 lockPath := repoLockFilePath(c.repo)
118 return os.Remove(lockPath)
119}
120
121// bugUpdated is a callback to trigger when the excerpt of a bug changed,
122// that is each time a bug is updated
123func (c *RepoCache) bugUpdated(id string) error {
124 b, ok := c.bugs[id]
125 if !ok {
126 panic("missing bug in the cache")
127 }
128
129 c.excerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
130
131 return c.write()
132}
133
134// load will try to read from the disk the bug cache file
135func (c *RepoCache) load() error {
136 f, err := os.Open(cacheFilePath(c.repo))
137 if err != nil {
138 return err
139 }
140
141 decoder := gob.NewDecoder(f)
142
143 aux := struct {
144 Version uint
145 Excerpts map[string]*BugExcerpt
146 }{}
147
148 err = decoder.Decode(&aux)
149 if err != nil {
150 return err
151 }
152
153 if aux.Version != 1 {
154 return fmt.Errorf("unknown cache format version %v", aux.Version)
155 }
156
157 c.excerpts = aux.Excerpts
158 return nil
159}
160
161// write will serialize on disk the bug cache file
162func (c *RepoCache) write() error {
163 var data bytes.Buffer
164
165 aux := struct {
166 Version uint
167 Excerpts map[string]*BugExcerpt
168 }{
169 Version: formatVersion,
170 Excerpts: c.excerpts,
171 }
172
173 encoder := gob.NewEncoder(&data)
174
175 err := encoder.Encode(aux)
176 if err != nil {
177 return err
178 }
179
180 f, err := os.Create(cacheFilePath(c.repo))
181 if err != nil {
182 return err
183 }
184
185 _, err = f.Write(data.Bytes())
186 if err != nil {
187 return err
188 }
189
190 return f.Close()
191}
192
193func cacheFilePath(repo repository.Repo) string {
194 return path.Join(repo.GetPath(), ".git", "git-bug", cacheFile)
195}
196
197func (c *RepoCache) buildCache() error {
198 _, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ")
199
200 c.excerpts = make(map[string]*BugExcerpt)
201
202 allBugs := bug.ReadAllLocalBugs(c.repo)
203
204 for b := range allBugs {
205 if b.Err != nil {
206 return b.Err
207 }
208
209 snap := b.Bug.Compile()
210 c.excerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
211 }
212
213 _, _ = fmt.Fprintln(os.Stderr, "Done.")
214 return nil
215}
216
217// ResolveBug retrieve a bug matching the exact given id
218func (c *RepoCache) ResolveBug(id string) (*BugCache, error) {
219 cached, ok := c.bugs[id]
220 if ok {
221 return cached, nil
222 }
223
224 b, err := bug.ReadLocalBug(c.repo, id)
225 if err != nil {
226 return nil, err
227 }
228
229 cached = NewBugCache(c, b)
230 c.bugs[id] = cached
231
232 return cached, nil
233}
234
235// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
236// bugs match.
237func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
238 // preallocate but empty
239 matching := make([]string, 0, 5)
240
241 for id := range c.excerpts {
242 if strings.HasPrefix(id, prefix) {
243 matching = append(matching, id)
244 }
245 }
246
247 if len(matching) > 1 {
248 return nil, bug.ErrMultipleMatch{Matching: matching}
249 }
250
251 if len(matching) == 0 {
252 return nil, bug.ErrBugNotExist
253 }
254
255 return c.ResolveBug(matching[0])
256}
257
258// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
259// its Create operation, that is, the first operation. It fails if multiple bugs
260// match.
261func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
262 // preallocate but empty
263 matching := make([]string, 0, 5)
264
265 for id, excerpt := range c.excerpts {
266 if excerpt.CreateMetadata[key] == value {
267 matching = append(matching, id)
268 }
269 }
270
271 if len(matching) > 1 {
272 return nil, bug.ErrMultipleMatch{Matching: matching}
273 }
274
275 if len(matching) == 0 {
276 return nil, bug.ErrBugNotExist
277 }
278
279 return c.ResolveBug(matching[0])
280}
281
282func (c *RepoCache) QueryBugs(query *Query) []string {
283 if query == nil {
284 return c.AllBugsIds()
285 }
286
287 var filtered []*BugExcerpt
288
289 for _, excerpt := range c.excerpts {
290 if query.Match(excerpt) {
291 filtered = append(filtered, excerpt)
292 }
293 }
294
295 var sorter sort.Interface
296
297 switch query.OrderBy {
298 case OrderById:
299 sorter = BugsById(filtered)
300 case OrderByCreation:
301 sorter = BugsByCreationTime(filtered)
302 case OrderByEdit:
303 sorter = BugsByEditTime(filtered)
304 default:
305 panic("missing sort type")
306 }
307
308 if query.OrderDirection == OrderDescending {
309 sorter = sort.Reverse(sorter)
310 }
311
312 sort.Sort(sorter)
313
314 result := make([]string, len(filtered))
315
316 for i, val := range filtered {
317 result[i] = val.Id
318 }
319
320 return result
321}
322
323// AllBugsIds return all known bug ids
324func (c *RepoCache) AllBugsIds() []string {
325 result := make([]string, len(c.excerpts))
326
327 i := 0
328 for _, excerpt := range c.excerpts {
329 result[i] = excerpt.Id
330 i++
331 }
332
333 return result
334}
335
336// ClearAllBugs clear all bugs kept in memory
337func (c *RepoCache) ClearAllBugs() {
338 c.bugs = make(map[string]*BugCache)
339}
340
341// ValidLabels list valid labels
342//
343// Note: in the future, a proper label policy could be implemented where valid
344// labels are defined in a configuration file. Until that, the default behavior
345// is to return the list of labels already used.
346func (c *RepoCache) ValidLabels() []bug.Label {
347 set := map[bug.Label]interface{}{}
348
349 for _, excerpt := range c.excerpts {
350 for _, l := range excerpt.Labels {
351 set[l] = nil
352 }
353 }
354
355 result := make([]bug.Label, len(set))
356
357 i := 0
358 for l := range set {
359 result[i] = l
360 i++
361 }
362
363 // Sort
364 sort.Slice(result, func(i, j int) bool {
365 return string(result[i]) < string(result[j])
366 })
367
368 return result
369}
370
371// NewBug create a new bug
372// The new bug is written in the repository (commit)
373func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
374 return c.NewBugWithFiles(title, message, nil)
375}
376
377// NewBugWithFiles create a new bug with attached files for the message
378// The new bug is written in the repository (commit)
379func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
380 author, err := identity.GetIdentity(c.repo)
381 if err != nil {
382 return nil, err
383 }
384
385 return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
386}
387
388// NewBugWithFilesMeta create a new bug with attached files for the message, as
389// well as metadata for the Create operation.
390// The new bug is written in the repository (commit)
391func (c *RepoCache) NewBugRaw(author *identity.Identity, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, error) {
392 b, op, err := bug.CreateWithFiles(author, unixTime, title, message, files)
393 if err != nil {
394 return nil, err
395 }
396
397 for key, value := range metadata {
398 op.SetMetadata(key, value)
399 }
400
401 err = b.Commit(c.repo)
402 if err != nil {
403 return nil, err
404 }
405
406 cached := NewBugCache(c, b)
407 c.bugs[b.Id()] = cached
408
409 err = c.bugUpdated(b.Id())
410 if err != nil {
411 return nil, err
412 }
413
414 return cached, nil
415}
416
417// Fetch retrieve update from a remote
418// This does not change the local bugs state
419func (c *RepoCache) Fetch(remote string) (string, error) {
420 return bug.Fetch(c.repo, remote)
421}
422
423// MergeAll will merge all the available remote bug
424func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
425 out := make(chan bug.MergeResult)
426
427 // Intercept merge results to update the cache properly
428 go func() {
429 defer close(out)
430
431 results := bug.MergeAll(c.repo, remote)
432 for result := range results {
433 out <- result
434
435 if result.Err != nil {
436 continue
437 }
438
439 id := result.Id
440
441 switch result.Status {
442 case bug.MergeStatusNew, bug.MergeStatusUpdated:
443 b := result.Bug
444 snap := b.Compile()
445 c.excerpts[id] = NewBugExcerpt(b, &snap)
446 }
447 }
448
449 err := c.write()
450
451 // No easy way out here ..
452 if err != nil {
453 panic(err)
454 }
455 }()
456
457 return out
458}
459
460// Push update a remote with the local changes
461func (c *RepoCache) Push(remote string) (string, error) {
462 return bug.Push(c.repo, remote)
463}
464
465func repoLockFilePath(repo repository.Repo) string {
466 return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
467}
468
469// repoIsAvailable check is the given repository is locked by a Cache.
470// Note: this is a smart function that will cleanup the lock file if the
471// corresponding process is not there anymore.
472// If no error is returned, the repo is free to edit.
473func repoIsAvailable(repo repository.Repo) error {
474 lockPath := repoLockFilePath(repo)
475
476 // Todo: this leave way for a racey access to the repo between the test
477 // if the file exist and the actual write. It's probably not a problem in
478 // practice because using a repository will be done from user interaction
479 // or in a context where a single instance of git-bug is already guaranteed
480 // (say, a server with the web UI running). But still, that might be nice to
481 // have a mutex or something to guard that.
482
483 // Todo: this will fail if somehow the filesystem is shared with another
484 // computer. Should add a configuration that prevent the cleaning of the
485 // lock file
486
487 f, err := os.Open(lockPath)
488
489 if err != nil && !os.IsNotExist(err) {
490 return err
491 }
492
493 if err == nil {
494 // lock file already exist
495 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
496 if err != nil {
497 return err
498 }
499 if len(buf) == 10 {
500 return fmt.Errorf("the lock file should be < 10 bytes")
501 }
502
503 pid, err := strconv.Atoi(string(buf))
504 if err != nil {
505 return err
506 }
507
508 if process.IsRunning(pid) {
509 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
510 }
511
512 // The lock file is just laying there after a crash, clean it
513
514 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
515 err = f.Close()
516 if err != nil {
517 return err
518 }
519
520 err = os.Remove(lockPath)
521 if err != nil {
522 return err
523 }
524 }
525
526 return nil
527}