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// ResolveBugExcerpt retrieve a BugExcerpt matching the exact given id
391func (c *RepoCache) ResolveBugExcerpt(id string) (*BugExcerpt, error) {
392 e, ok := c.bugExcerpts[id]
393 if !ok {
394 return nil, bug.ErrBugNotExist
395 }
396
397 return e, nil
398}
399
400// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
401// bugs match.
402func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
403 // preallocate but empty
404 matching := make([]string, 0, 5)
405
406 for id := range c.bugExcerpts {
407 if strings.HasPrefix(id, prefix) {
408 matching = append(matching, id)
409 }
410 }
411
412 if len(matching) > 1 {
413 return nil, bug.ErrMultipleMatch{Matching: matching}
414 }
415
416 if len(matching) == 0 {
417 return nil, bug.ErrBugNotExist
418 }
419
420 return c.ResolveBug(matching[0])
421}
422
423// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
424// its Create operation, that is, the first operation. It fails if multiple bugs
425// match.
426func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
427 // preallocate but empty
428 matching := make([]string, 0, 5)
429
430 for id, excerpt := range c.bugExcerpts {
431 if excerpt.CreateMetadata[key] == value {
432 matching = append(matching, id)
433 }
434 }
435
436 if len(matching) > 1 {
437 return nil, bug.ErrMultipleMatch{Matching: matching}
438 }
439
440 if len(matching) == 0 {
441 return nil, bug.ErrBugNotExist
442 }
443
444 return c.ResolveBug(matching[0])
445}
446
447// QueryBugs return the id of all Bug matching the given Query
448func (c *RepoCache) QueryBugs(query *Query) []string {
449 if query == nil {
450 return c.AllBugsIds()
451 }
452
453 var filtered []*BugExcerpt
454
455 for _, excerpt := range c.bugExcerpts {
456 if query.Match(c, excerpt) {
457 filtered = append(filtered, excerpt)
458 }
459 }
460
461 var sorter sort.Interface
462
463 switch query.OrderBy {
464 case OrderById:
465 sorter = BugsById(filtered)
466 case OrderByCreation:
467 sorter = BugsByCreationTime(filtered)
468 case OrderByEdit:
469 sorter = BugsByEditTime(filtered)
470 default:
471 panic("missing sort type")
472 }
473
474 if query.OrderDirection == OrderDescending {
475 sorter = sort.Reverse(sorter)
476 }
477
478 sort.Sort(sorter)
479
480 result := make([]string, len(filtered))
481
482 for i, val := range filtered {
483 result[i] = val.Id
484 }
485
486 return result
487}
488
489// AllBugsIds return all known bug ids
490func (c *RepoCache) AllBugsIds() []string {
491 result := make([]string, len(c.bugExcerpts))
492
493 i := 0
494 for _, excerpt := range c.bugExcerpts {
495 result[i] = excerpt.Id
496 i++
497 }
498
499 return result
500}
501
502// ValidLabels list valid labels
503//
504// Note: in the future, a proper label policy could be implemented where valid
505// labels are defined in a configuration file. Until that, the default behavior
506// is to return the list of labels already used.
507func (c *RepoCache) ValidLabels() []bug.Label {
508 set := map[bug.Label]interface{}{}
509
510 for _, excerpt := range c.bugExcerpts {
511 for _, l := range excerpt.Labels {
512 set[l] = nil
513 }
514 }
515
516 result := make([]bug.Label, len(set))
517
518 i := 0
519 for l := range set {
520 result[i] = l
521 i++
522 }
523
524 // Sort
525 sort.Slice(result, func(i, j int) bool {
526 return string(result[i]) < string(result[j])
527 })
528
529 return result
530}
531
532// NewBug create a new bug
533// The new bug is written in the repository (commit)
534func (c *RepoCache) NewBug(title string, message string) (*BugCache, error) {
535 return c.NewBugWithFiles(title, message, nil)
536}
537
538// NewBugWithFiles create a new bug with attached files for the message
539// The new bug is written in the repository (commit)
540func (c *RepoCache) NewBugWithFiles(title string, message string, files []git.Hash) (*BugCache, error) {
541 author, err := c.GetUserIdentity()
542 if err != nil {
543 return nil, err
544 }
545
546 return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
547}
548
549// NewBugWithFilesMeta create a new bug with attached files for the message, as
550// well as metadata for the Create operation.
551// The new bug is written in the repository (commit)
552func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title string, message string, files []git.Hash, metadata map[string]string) (*BugCache, error) {
553 b, op, err := bug.CreateWithFiles(author.Identity, unixTime, title, message, files)
554 if err != nil {
555 return nil, err
556 }
557
558 for key, value := range metadata {
559 op.SetMetadata(key, value)
560 }
561
562 err = b.Commit(c.repo)
563 if err != nil {
564 return nil, err
565 }
566
567 if _, has := c.bugs[b.Id()]; has {
568 return nil, fmt.Errorf("bug %s already exist in the cache", b.Id())
569 }
570
571 cached := NewBugCache(c, b)
572 c.bugs[b.Id()] = cached
573
574 // force the write of the excerpt
575 err = c.bugUpdated(b.Id())
576 if err != nil {
577 return nil, err
578 }
579
580 return cached, nil
581}
582
583// Fetch retrieve update from a remote
584// This does not change the local bugs state
585func (c *RepoCache) Fetch(remote string) (string, error) {
586 return bug.Fetch(c.repo, remote)
587}
588
589// MergeAll will merge all the available remote bug
590func (c *RepoCache) MergeAll(remote string) <-chan bug.MergeResult {
591 // TODO: add identities
592
593 out := make(chan bug.MergeResult)
594
595 // Intercept merge results to update the cache properly
596 go func() {
597 defer close(out)
598
599 results := bug.MergeAll(c.repo, remote)
600 for result := range results {
601 out <- result
602
603 if result.Err != nil {
604 continue
605 }
606
607 id := result.Id
608
609 switch result.Status {
610 case bug.MergeStatusNew, bug.MergeStatusUpdated:
611 b := result.Bug
612 snap := b.Compile()
613 c.bugExcerpts[id] = NewBugExcerpt(b, &snap)
614 }
615 }
616
617 err := c.write()
618
619 // No easy way out here ..
620 if err != nil {
621 panic(err)
622 }
623 }()
624
625 return out
626}
627
628// Push update a remote with the local changes
629func (c *RepoCache) Push(remote string) (string, error) {
630 return bug.Push(c.repo, remote)
631}
632
633func repoLockFilePath(repo repository.Repo) string {
634 return path.Join(repo.GetPath(), ".git", "git-bug", lockfile)
635}
636
637// repoIsAvailable check is the given repository is locked by a Cache.
638// Note: this is a smart function that will cleanup the lock file if the
639// corresponding process is not there anymore.
640// If no error is returned, the repo is free to edit.
641func repoIsAvailable(repo repository.Repo) error {
642 lockPath := repoLockFilePath(repo)
643
644 // Todo: this leave way for a racey access to the repo between the test
645 // if the file exist and the actual write. It's probably not a problem in
646 // practice because using a repository will be done from user interaction
647 // or in a context where a single instance of git-bug is already guaranteed
648 // (say, a server with the web UI running). But still, that might be nice to
649 // have a mutex or something to guard that.
650
651 // Todo: this will fail if somehow the filesystem is shared with another
652 // computer. Should add a configuration that prevent the cleaning of the
653 // lock file
654
655 f, err := os.Open(lockPath)
656
657 if err != nil && !os.IsNotExist(err) {
658 return err
659 }
660
661 if err == nil {
662 // lock file already exist
663 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
664 if err != nil {
665 return err
666 }
667 if len(buf) == 10 {
668 return fmt.Errorf("the lock file should be < 10 bytes")
669 }
670
671 pid, err := strconv.Atoi(string(buf))
672 if err != nil {
673 return err
674 }
675
676 if process.IsRunning(pid) {
677 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
678 }
679
680 // The lock file is just laying there after a crash, clean it
681
682 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
683 err = f.Close()
684 if err != nil {
685 return err
686 }
687
688 err = os.Remove(lockPath)
689 if err != nil {
690 return err
691 }
692 }
693
694 return nil
695}
696
697// ResolveIdentity retrieve an identity matching the exact given id
698func (c *RepoCache) ResolveIdentity(id string) (*IdentityCache, error) {
699 cached, ok := c.identities[id]
700 if ok {
701 return cached, nil
702 }
703
704 i, err := identity.ReadLocal(c.repo, id)
705 if err != nil {
706 return nil, err
707 }
708
709 cached = NewIdentityCache(c, i)
710 c.identities[id] = cached
711
712 return cached, nil
713}
714
715// ResolveIdentityExcerpt retrieve a IdentityExcerpt matching the exact given id
716func (c *RepoCache) ResolveIdentityExcerpt(id string) (*IdentityExcerpt, error) {
717 e, ok := c.identitiesExcerpts[id]
718 if !ok {
719 return nil, identity.ErrIdentityNotExist
720 }
721
722 return e, nil
723}
724
725// ResolveIdentityPrefix retrieve an Identity matching an id prefix.
726// It fails if multiple identities match.
727func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*IdentityCache, error) {
728 // preallocate but empty
729 matching := make([]string, 0, 5)
730
731 for id := range c.identitiesExcerpts {
732 if strings.HasPrefix(id, prefix) {
733 matching = append(matching, id)
734 }
735 }
736
737 if len(matching) > 1 {
738 return nil, identity.ErrMultipleMatch{Matching: matching}
739 }
740
741 if len(matching) == 0 {
742 return nil, identity.ErrIdentityNotExist
743 }
744
745 return c.ResolveIdentity(matching[0])
746}
747
748// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on
749// one of it's version. If multiple version have the same key, the first defined take precedence.
750func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
751 // preallocate but empty
752 matching := make([]string, 0, 5)
753
754 for id, i := range c.identitiesExcerpts {
755 if i.ImmutableMetadata[key] == value {
756 matching = append(matching, id)
757 }
758 }
759
760 if len(matching) > 1 {
761 return nil, identity.ErrMultipleMatch{Matching: matching}
762 }
763
764 if len(matching) == 0 {
765 return nil, identity.ErrIdentityNotExist
766 }
767
768 return c.ResolveIdentity(matching[0])
769}
770
771// AllIdentityIds return all known identity ids
772func (c *RepoCache) AllIdentityIds() []string {
773 result := make([]string, len(c.identitiesExcerpts))
774
775 i := 0
776 for _, excerpt := range c.identitiesExcerpts {
777 result[i] = excerpt.Id
778 i++
779 }
780
781 return result
782}
783
784func (c *RepoCache) SetUserIdentity(i *IdentityCache) error {
785 err := identity.SetUserIdentity(c.repo, i.Identity)
786 if err != nil {
787 return err
788 }
789
790 // Make sure that everything is fine
791 if _, ok := c.identities[i.Id()]; !ok {
792 panic("SetUserIdentity while the identity is not from the cache, something is wrong")
793 }
794
795 c.userIdentityId = i.Id()
796
797 return nil
798}
799
800func (c *RepoCache) GetUserIdentity() (*IdentityCache, error) {
801 if c.userIdentityId != "" {
802 i, ok := c.identities[c.userIdentityId]
803 if ok {
804 return i, nil
805 }
806 }
807
808 i, err := identity.GetUserIdentity(c.repo)
809 if err != nil {
810 return nil, err
811 }
812
813 cached := NewIdentityCache(c, i)
814 c.identities[i.Id()] = cached
815 c.userIdentityId = i.Id()
816
817 return cached, nil
818}
819
820// NewIdentity create a new identity
821// The new identity is written in the repository (commit)
822func (c *RepoCache) NewIdentity(name string, email string) (*IdentityCache, error) {
823 return c.NewIdentityRaw(name, email, "", "", nil)
824}
825
826// NewIdentityFull create a new identity
827// The new identity is written in the repository (commit)
828func (c *RepoCache) NewIdentityFull(name string, email string, login string, avatarUrl string) (*IdentityCache, error) {
829 return c.NewIdentityRaw(name, email, login, avatarUrl, nil)
830}
831
832func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avatarUrl string, metadata map[string]string) (*IdentityCache, error) {
833 i := identity.NewIdentityFull(name, email, login, avatarUrl)
834
835 for key, value := range metadata {
836 i.SetMetadata(key, value)
837 }
838
839 err := i.Commit(c.repo)
840 if err != nil {
841 return nil, err
842 }
843
844 if _, has := c.identities[i.Id()]; has {
845 return nil, fmt.Errorf("identity %s already exist in the cache", i.Id())
846 }
847
848 cached := NewIdentityCache(c, i)
849 c.identities[i.Id()] = cached
850
851 // force the write of the excerpt
852 err = c.identityUpdated(i.Id())
853 if err != nil {
854 return nil, err
855 }
856
857 return cached, nil
858}