repo_cache.go

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