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