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 bugCacheFile = "bug-cache"
24const identityCacheFile = "identity-cache"
25
26// 1: original format
27// 2: added cache for identities with a reference in the bug cache
28const formatVersion = 2
29
30type ErrInvalidCacheFormat struct {
31 message string
32}
33
34func (e ErrInvalidCacheFormat) Error() string {
35 return e.message
36}
37
38// RepoCache is a cache for a Repository. This cache has multiple functions:
39//
40// 1. After being loaded, a Bug is kept in memory in the cache, allowing for fast
41// access later.
42// 2. The cache maintain on memory and on disk a pre-digested excerpt for each bug,
43// allowing for fast querying the whole set of bugs without having to load
44// them individually.
45// 3. The cache guarantee that a single instance of a Bug is loaded at once, avoiding
46// loss of data that we could have with multiple copies in the same process.
47// 4. The same way, the cache maintain in memory a single copy of the loaded identities.
48//
49// The cache also protect the on-disk data by locking the git repository for its
50// own usage, by writing a lock file. Of course, normal git operations are not
51// affected, only git-bug related one.
52type RepoCache struct {
53 // the underlying repo
54 repo repository.ClockedRepo
55
56 // excerpt of bugs data for all bugs
57 bugExcerpts map[string]*BugExcerpt
58 // bug loaded in memory
59 bugs map[string]*BugCache
60
61 // excerpt of identities data for all identities
62 identitiesExcerpts map[string]*IdentityExcerpt
63 // identities loaded in memory
64 identities map[string]*IdentityCache
65
66 // the user identity's id, if known
67 userIdentityId string
68}
69
70func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
71 c := &RepoCache{
72 repo: r,
73 bugs: make(map[string]*BugCache),
74 identities: make(map[string]*IdentityCache),
75 }
76
77 err := c.lock()
78 if err != nil {
79 return &RepoCache{}, err
80 }
81
82 err = c.load()
83 if err == nil {
84 return c, nil
85 }
86 if _, ok := err.(ErrInvalidCacheFormat); ok {
87 return nil, err
88 }
89
90 err = c.buildCache()
91 if err != nil {
92 return nil, err
93 }
94
95 return c, c.write()
96}
97
98// GetPath returns the path to the repo.
99func (c *RepoCache) GetPath() string {
100 return c.repo.GetPath()
101}
102
103// GetPath returns the path to the repo.
104func (c *RepoCache) GetCoreEditor() (string, error) {
105 return c.repo.GetCoreEditor()
106}
107
108// GetUserName returns the name the the user has used to configure git
109func (c *RepoCache) GetUserName() (string, error) {
110 return c.repo.GetUserName()
111}
112
113// GetUserEmail returns the email address that the user has used to configure git.
114func (c *RepoCache) GetUserEmail() (string, error) {
115 return c.repo.GetUserEmail()
116}
117
118// StoreConfig store a single key/value pair in the config of the repo
119func (c *RepoCache) StoreConfig(key string, value string) error {
120 return c.repo.StoreConfig(key, value)
121}
122
123// ReadConfigs read all key/value pair matching the key prefix
124func (c *RepoCache) ReadConfigs(keyPrefix string) (map[string]string, error) {
125 return c.repo.ReadConfigs(keyPrefix)
126}
127
128// RmConfigs remove all key/value pair matching the key prefix
129func (c *RepoCache) RmConfigs(keyPrefix string) error {
130 return c.repo.RmConfigs(keyPrefix)
131}
132
133func (c *RepoCache) lock() error {
134 lockPath := repoLockFilePath(c.repo)
135
136 err := repoIsAvailable(c.repo)
137 if err != nil {
138 return err
139 }
140
141 f, err := os.Create(lockPath)
142 if err != nil {
143 return err
144 }
145
146 pid := fmt.Sprintf("%d", os.Getpid())
147 _, err = f.WriteString(pid)
148 if err != nil {
149 return err
150 }
151
152 return f.Close()
153}
154
155func (c *RepoCache) Close() error {
156 lockPath := repoLockFilePath(c.repo)
157 return os.Remove(lockPath)
158}
159
160// bugUpdated is a callback to trigger when the excerpt of a bug changed,
161// that is each time a bug is updated
162func (c *RepoCache) bugUpdated(id string) error {
163 b, ok := c.bugs[id]
164 if !ok {
165 panic("missing bug in the cache")
166 }
167
168 c.bugExcerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
169
170 // we only need to write the bug cache
171 return c.writeBugCache()
172}
173
174// identityUpdated is a callback to trigger when the excerpt of an identity
175// changed, that is each time an identity is updated
176func (c *RepoCache) identityUpdated(id string) error {
177 i, ok := c.identities[id]
178 if !ok {
179 panic("missing identity in the cache")
180 }
181
182 c.identitiesExcerpts[id] = NewIdentityExcerpt(i.Identity)
183
184 // we only need to write the identity cache
185 return c.writeIdentityCache()
186}
187
188// load will try to read from the disk all the cache files
189func (c *RepoCache) load() error {
190 err := c.loadBugCache()
191 if err != nil {
192 return err
193 }
194 return c.loadIdentityCache()
195}
196
197// load will try to read from the disk the bug cache file
198func (c *RepoCache) loadBugCache() error {
199 f, err := os.Open(bugCacheFilePath(c.repo))
200 if err != nil {
201 return err
202 }
203
204 decoder := gob.NewDecoder(f)
205
206 aux := struct {
207 Version uint
208 Excerpts map[string]*BugExcerpt
209 }{}
210
211 err = decoder.Decode(&aux)
212 if err != nil {
213 return err
214 }
215
216 if aux.Version != 2 {
217 return ErrInvalidCacheFormat{
218 message: fmt.Sprintf("unknown cache format version %v", aux.Version),
219 }
220 }
221
222 c.bugExcerpts = aux.Excerpts
223 return nil
224}
225
226// load will try to read from the disk the identity cache file
227func (c *RepoCache) loadIdentityCache() error {
228 f, err := os.Open(identityCacheFilePath(c.repo))
229 if err != nil {
230 return err
231 }
232
233 decoder := gob.NewDecoder(f)
234
235 aux := struct {
236 Version uint
237 Excerpts map[string]*IdentityExcerpt
238 }{}
239
240 err = decoder.Decode(&aux)
241 if err != nil {
242 return err
243 }
244
245 if aux.Version != 2 {
246 return ErrInvalidCacheFormat{
247 message: fmt.Sprintf("unknown cache format version %v", aux.Version),
248 }
249 }
250
251 c.identitiesExcerpts = aux.Excerpts
252 return nil
253}
254
255// write will serialize on disk all the cache files
256func (c *RepoCache) write() error {
257 err := c.writeBugCache()
258 if err != nil {
259 return err
260 }
261 return c.writeIdentityCache()
262}
263
264// write will serialize on disk the bug cache file
265func (c *RepoCache) writeBugCache() error {
266 var data bytes.Buffer
267
268 aux := struct {
269 Version uint
270 Excerpts map[string]*BugExcerpt
271 }{
272 Version: formatVersion,
273 Excerpts: c.bugExcerpts,
274 }
275
276 encoder := gob.NewEncoder(&data)
277
278 err := encoder.Encode(aux)
279 if err != nil {
280 return err
281 }
282
283 f, err := os.Create(bugCacheFilePath(c.repo))
284 if err != nil {
285 return err
286 }
287
288 _, err = f.Write(data.Bytes())
289 if err != nil {
290 return err
291 }
292
293 return f.Close()
294}
295
296// write will serialize on disk the identity cache file
297func (c *RepoCache) writeIdentityCache() error {
298 var data bytes.Buffer
299
300 aux := struct {
301 Version uint
302 Excerpts map[string]*IdentityExcerpt
303 }{
304 Version: formatVersion,
305 Excerpts: c.identitiesExcerpts,
306 }
307
308 encoder := gob.NewEncoder(&data)
309
310 err := encoder.Encode(aux)
311 if err != nil {
312 return err
313 }
314
315 f, err := os.Create(identityCacheFilePath(c.repo))
316 if err != nil {
317 return err
318 }
319
320 _, err = f.Write(data.Bytes())
321 if err != nil {
322 return err
323 }
324
325 return f.Close()
326}
327
328func bugCacheFilePath(repo repository.Repo) string {
329 return path.Join(repo.GetPath(), ".git", "git-bug", bugCacheFile)
330}
331
332func identityCacheFilePath(repo repository.Repo) string {
333 return path.Join(repo.GetPath(), ".git", "git-bug", identityCacheFile)
334}
335
336func (c *RepoCache) buildCache() error {
337 _, _ = fmt.Fprintf(os.Stderr, "Building identity cache... ")
338
339 c.identitiesExcerpts = make(map[string]*IdentityExcerpt)
340
341 allIdentities := identity.ReadAllLocalIdentities(c.repo)
342
343 for i := range allIdentities {
344 if i.Err != nil {
345 return i.Err
346 }
347
348 c.identitiesExcerpts[i.Identity.Id()] = NewIdentityExcerpt(i.Identity)
349 }
350
351 _, _ = fmt.Fprintln(os.Stderr, "Done.")
352
353 _, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ")
354
355 c.bugExcerpts = make(map[string]*BugExcerpt)
356
357 allBugs := bug.ReadAllLocalBugs(c.repo)
358
359 for b := range allBugs {
360 if b.Err != nil {
361 return b.Err
362 }
363
364 snap := b.Bug.Compile()
365 c.bugExcerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
366 }
367
368 _, _ = fmt.Fprintln(os.Stderr, "Done.")
369 return nil
370}
371
372// ResolveBug retrieve a bug matching the exact given id
373func (c *RepoCache) ResolveBug(id string) (*BugCache, error) {
374 cached, ok := c.bugs[id]
375 if ok {
376 return cached, nil
377 }
378
379 b, err := bug.ReadLocalBug(c.repo, id)
380 if err != nil {
381 return nil, err
382 }
383
384 cached = NewBugCache(c, b)
385 c.bugs[id] = cached
386
387 return cached, nil
388}
389
390// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
391// bugs match.
392func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
393 // preallocate but empty
394 matching := make([]string, 0, 5)
395
396 for id := range c.bugExcerpts {
397 if strings.HasPrefix(id, prefix) {
398 matching = append(matching, id)
399 }
400 }
401
402 if len(matching) > 1 {
403 return nil, bug.ErrMultipleMatch{Matching: matching}
404 }
405
406 if len(matching) == 0 {
407 return nil, bug.ErrBugNotExist
408 }
409
410 return c.ResolveBug(matching[0])
411}
412
413// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
414// its Create operation, that is, the first operation. It fails if multiple bugs
415// match.
416func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
417 // preallocate but empty
418 matching := make([]string, 0, 5)
419
420 for id, excerpt := range c.bugExcerpts {
421 if excerpt.CreateMetadata[key] == value {
422 matching = append(matching, id)
423 }
424 }
425
426 if len(matching) > 1 {
427 return nil, bug.ErrMultipleMatch{Matching: matching}
428 }
429
430 if len(matching) == 0 {
431 return nil, bug.ErrBugNotExist
432 }
433
434 return c.ResolveBug(matching[0])
435}
436
437// QueryBugs return the id of all Bug matching the given Query
438func (c *RepoCache) QueryBugs(query *Query) []string {
439 if query == nil {
440 return c.AllBugsIds()
441 }
442
443 var filtered []*BugExcerpt
444
445 for _, excerpt := range c.bugExcerpts {
446 if query.Match(c, excerpt) {
447 filtered = append(filtered, excerpt)
448 }
449 }
450
451 var sorter sort.Interface
452
453 switch query.OrderBy {
454 case OrderById:
455 sorter = BugsById(filtered)
456 case OrderByCreation:
457 sorter = BugsByCreationTime(filtered)
458 case OrderByEdit:
459 sorter = BugsByEditTime(filtered)
460 default:
461 panic("missing sort type")
462 }
463
464 if query.OrderDirection == OrderDescending {
465 sorter = sort.Reverse(sorter)
466 }
467
468 sort.Sort(sorter)
469
470 result := make([]string, len(filtered))
471
472 for i, val := range filtered {
473 result[i] = val.Id
474 }
475
476 return result
477}
478
479// AllBugsIds return all known bug ids
480func (c *RepoCache) AllBugsIds() []string {
481 result := make([]string, len(c.bugExcerpts))
482
483 i := 0
484 for _, excerpt := range c.bugExcerpts {
485 result[i] = excerpt.Id
486 i++
487 }
488
489 return result
490}
491
492// AllBugExcerpt return all known bug excerpt.
493// This maps is read-only.
494func (c *RepoCache) AllBugExcerpt() map[string]*BugExcerpt {
495 return c.bugExcerpts
496}
497
498// ValidLabels list valid labels
499//
500// Note: in the future, a proper label policy could be implemented where valid
501// labels are defined in a configuration file. Until that, the default behavior
502// is to return the list of labels already used.
503func (c *RepoCache) ValidLabels() []bug.Label {
504 set := map[bug.Label]interface{}{}
505
506 for _, excerpt := range c.bugExcerpts {
507 for _, l := range excerpt.Labels {
508 set[l] = nil
509 }
510 }
511
512 result := make([]bug.Label, len(set))
513
514 i := 0
515 for l := range set {
516 result[i] = l
517 i++
518 }
519
520 // Sort
521 sort.Slice(result, func(i, j int) bool {
522 return string(result[i]) < string(result[j])
523 })
524
525 return result
526}
527
528// NewBug create a new bug
529// The new bug is written in the repository (commit)
530func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
531 return c.NewBugWithFiles(title, message, nil)
532}
533
534// NewBugWithFiles create a new bug with attached files for the message
535// The new bug is written in the repository (commit)
536func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
537 author, err := c.GetUserIdentity()
538 if err != nil {
539 return nil, err
540 }
541
542 return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
543}
544
545// NewBugWithFilesMeta create a new bug with attached files for the message, as
546// well as metadata for the Create operation.
547// The new bug is written in the repository (commit)
548func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, error) {
549 b, op, err := bug.CreateWithFiles(author.Identity, unixTime, title, message, files)
550 if err != nil {
551 return nil, err
552 }
553
554 for key, value := range metadata {
555 op.SetMetadata(key, value)
556 }
557
558 err = b.Commit(c.repo)
559 if err != nil {
560 return nil, err
561 }
562
563 if _, has := c.bugs[b.Id()]; has {
564 return nil, fmt.Errorf("bug %s already exist in the cache", b.Id())
565 }
566
567 cached := NewBugCache(c, b)
568 c.bugs[b.Id()] = cached
569
570 // force the write of the excerpt
571 err = c.bugUpdated(b.Id())
572 if err != nil {
573 return nil, err
574 }
575
576 return cached, nil
577}
578
579// Fetch retrieve update from a remote
580// This does not change the local bugs state
581func (c *RepoCache) Fetch(remote string) (string, error) {
582 return bug.Fetch(c.repo, remote)
583}
584
585// MergeAll will merge all the available remote bug
586func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
587 // TODO: add identities
588
589 out := make(chan bug.MergeResult)
590
591 // Intercept merge results to update the cache properly
592 go func() {
593 defer close(out)
594
595 results := bug.MergeAll(c.repo, remote)
596 for result := range results {
597 out <- result
598
599 if result.Err != nil {
600 continue
601 }
602
603 id := result.Id
604
605 switch result.Status {
606 case bug.MergeStatusNew, bug.MergeStatusUpdated:
607 b := result.Bug
608 snap := b.Compile()
609 c.bugExcerpts[id] = NewBugExcerpt(b, &snap)
610 }
611 }
612
613 err := c.write()
614
615 // No easy way out here ..
616 if err != nil {
617 panic(err)
618 }
619 }()
620
621 return out
622}
623
624// Push update a remote with the local changes
625func (c *RepoCache) Push(remote string) (string, error) {
626 return bug.Push(c.repo, remote)
627}
628
629func repoLockFilePath(repo repository.Repo) string {
630 return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
631}
632
633// repoIsAvailable check is the given repository is locked by a Cache.
634// Note: this is a smart function that will cleanup the lock file if the
635// corresponding process is not there anymore.
636// If no error is returned, the repo is free to edit.
637func repoIsAvailable(repo repository.Repo) error {
638 lockPath := repoLockFilePath(repo)
639
640 // Todo: this leave way for a racey access to the repo between the test
641 // if the file exist and the actual write. It's probably not a problem in
642 // practice because using a repository will be done from user interaction
643 // or in a context where a single instance of git-bug is already guaranteed
644 // (say, a server with the web UI running). But still, that might be nice to
645 // have a mutex or something to guard that.
646
647 // Todo: this will fail if somehow the filesystem is shared with another
648 // computer. Should add a configuration that prevent the cleaning of the
649 // lock file
650
651 f, err := os.Open(lockPath)
652
653 if err != nil && !os.IsNotExist(err) {
654 return err
655 }
656
657 if err == nil {
658 // lock file already exist
659 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
660 if err != nil {
661 return err
662 }
663 if len(buf) == 10 {
664 return fmt.Errorf("the lock file should be < 10 bytes")
665 }
666
667 pid, err := strconv.Atoi(string(buf))
668 if err != nil {
669 return err
670 }
671
672 if process.IsRunning(pid) {
673 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
674 }
675
676 // The lock file is just laying there after a crash, clean it
677
678 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
679 err = f.Close()
680 if err != nil {
681 return err
682 }
683
684 err = os.Remove(lockPath)
685 if err != nil {
686 return err
687 }
688 }
689
690 return nil
691}
692
693// ResolveIdentity retrieve an identity matching the exact given id
694func (c *RepoCache) ResolveIdentity(id string) (*IdentityCache, error) {
695 cached, ok := c.identities[id]
696 if ok {
697 return cached, nil
698 }
699
700 i, err := identity.ReadLocal(c.repo, id)
701 if err != nil {
702 return nil, err
703 }
704
705 cached = NewIdentityCache(c, i)
706 c.identities[id] = cached
707
708 return cached, nil
709}
710
711// ResolveIdentityPrefix retrieve an Identity matching an id prefix.
712// It fails if multiple identities match.
713func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*IdentityCache, error) {
714 // preallocate but empty
715 matching := make([]string, 0, 5)
716
717 for id := range c.identitiesExcerpts {
718 if strings.HasPrefix(id, prefix) {
719 matching = append(matching, id)
720 }
721 }
722
723 if len(matching) > 1 {
724 return nil, identity.ErrMultipleMatch{Matching: matching}
725 }
726
727 if len(matching) == 0 {
728 return nil, identity.ErrIdentityNotExist
729 }
730
731 return c.ResolveIdentity(matching[0])
732}
733
734// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on
735// one of it's version. If multiple version have the same key, the first defined take precedence.
736func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
737 // preallocate but empty
738 matching := make([]string, 0, 5)
739
740 for id, i := range c.identitiesExcerpts {
741 if i.ImmutableMetadata[key] == value {
742 matching = append(matching, id)
743 }
744 }
745
746 if len(matching) > 1 {
747 return nil, identity.ErrMultipleMatch{Matching: matching}
748 }
749
750 if len(matching) == 0 {
751 return nil, identity.ErrIdentityNotExist
752 }
753
754 return c.ResolveIdentity(matching[0])
755}
756
757// AllIdentityIds return all known identity ids
758func (c *RepoCache) AllIdentityIds() []string {
759 result := make([]string, len(c.identitiesExcerpts))
760
761 i := 0
762 for _, excerpt := range c.identitiesExcerpts {
763 result[i] = excerpt.Id
764 i++
765 }
766
767 return result
768}
769
770// AllIdentityExcerpt return all known identities excerpt.
771// This maps is read-only.
772func (c *RepoCache) AllIdentityExcerpt() map[string]*IdentityExcerpt {
773 return c.identitiesExcerpts
774}
775
776func (c *RepoCache) SetUserIdentity(i *IdentityCache) error {
777 err := identity.SetUserIdentity(c.repo, i.Identity)
778 if err != nil {
779 return err
780 }
781
782 // Make sure that everything is fine
783 if _, ok := c.identities[i.Id()]; !ok {
784 panic("SetUserIdentity while the identity is not from the cache, something is wrong")
785 }
786
787 c.userIdentityId = i.Id()
788
789 return nil
790}
791
792func (c *RepoCache) GetUserIdentity() (*IdentityCache, error) {
793 if c.userIdentityId != "" {
794 i, ok := c.identities[c.userIdentityId]
795 if ok {
796 return i, nil
797 }
798 }
799
800 i, err := identity.GetUserIdentity(c.repo)
801 if err != nil {
802 return nil, err
803 }
804
805 cached := NewIdentityCache(c, i)
806 c.identities[i.Id()] = cached
807 c.userIdentityId = i.Id()
808
809 return cached, nil
810}
811
812// NewIdentity create a new identity
813// The new identity is written in the repository (commit)
814func (c *RepoCache) NewIdentity(name string, email string) (*IdentityCache, error) {
815 return c.NewIdentityRaw(name, email, "", "", nil)
816}
817
818// NewIdentityFull create a new identity
819// The new identity is written in the repository (commit)
820func (c *RepoCache) NewIdentityFull(name string, email string, login string, avatarUrl string) (*IdentityCache, error) {
821 return c.NewIdentityRaw(name, email, login, avatarUrl, nil)
822}
823
824func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avatarUrl string, metadata map[string]string) (*IdentityCache, error) {
825 i := identity.NewIdentityFull(name, email, login, avatarUrl)
826
827 for key, value := range metadata {
828 i.SetMetadata(key, value)
829 }
830
831 err := i.Commit(c.repo)
832 if err != nil {
833 return nil, err
834 }
835
836 if _, has := c.identities[i.Id()]; has {
837 return nil, fmt.Errorf("identity %s already exist in the cache", i.Id())
838 }
839
840 cached := NewIdentityCache(c, i)
841 c.identities[i.Id()] = cached
842
843 // force the write of the excerpt
844 err = c.identityUpdated(i.Id())
845 if err != nil {
846 return nil, err
847 }
848
849 return cached, nil
850}