bug_cache.go

  1package cache
  2
  3import (
  4	"github.com/MichaelMure/git-bug/bug"
  5	"github.com/MichaelMure/git-bug/bug/operations"
  6	"github.com/MichaelMure/git-bug/repository"
  7	"github.com/MichaelMure/git-bug/util"
  8)
  9
 10type BugCacher interface {
 11	Snapshot() *bug.Snapshot
 12	ClearSnapshot()
 13
 14	// Mutations
 15	AddComment(message string) error
 16	AddCommentWithFiles(message string, files []util.Hash) error
 17	ChangeLabels(added []string, removed []string) error
 18	Open() error
 19	Close() error
 20	SetTitle(title string) error
 21
 22	Commit() error
 23	CommitAsNeeded() error
 24}
 25
 26type BugCache struct {
 27	repo repository.Repo
 28	bug  *bug.Bug
 29	snap *bug.Snapshot
 30}
 31
 32func NewBugCache(repo repository.Repo, b *bug.Bug) BugCacher {
 33	return &BugCache{
 34		repo: repo,
 35		bug:  b,
 36	}
 37}
 38
 39func (c *BugCache) Snapshot() *bug.Snapshot {
 40	if c.snap == nil {
 41		snap := c.bug.Compile()
 42		c.snap = &snap
 43	}
 44	return c.snap
 45}
 46
 47func (c *BugCache) ClearSnapshot() {
 48	c.snap = nil
 49}
 50
 51func (c *BugCache) AddComment(message string) error {
 52	return c.AddCommentWithFiles(message, nil)
 53}
 54
 55func (c *BugCache) AddCommentWithFiles(message string, files []util.Hash) error {
 56	author, err := bug.GetUser(c.repo)
 57	if err != nil {
 58		return err
 59	}
 60
 61	operations.CommentWithFiles(c.bug, author, message, files)
 62
 63	// TODO: perf --> the snapshot could simply be updated with the new op
 64	c.ClearSnapshot()
 65
 66	return nil
 67}
 68
 69func (c *BugCache) ChangeLabels(added []string, removed []string) error {
 70	author, err := bug.GetUser(c.repo)
 71	if err != nil {
 72		return err
 73	}
 74
 75	err = operations.ChangeLabels(nil, c.bug, author, added, removed)
 76	if err != nil {
 77		return err
 78	}
 79
 80	// TODO: perf --> the snapshot could simply be updated with the new op
 81	c.ClearSnapshot()
 82
 83	return nil
 84}
 85
 86func (c *BugCache) Open() error {
 87	author, err := bug.GetUser(c.repo)
 88	if err != nil {
 89		return err
 90	}
 91
 92	operations.Open(c.bug, author)
 93
 94	// TODO: perf --> the snapshot could simply be updated with the new op
 95	c.ClearSnapshot()
 96
 97	return nil
 98}
 99
100func (c *BugCache) Close() error {
101	author, err := bug.GetUser(c.repo)
102	if err != nil {
103		return err
104	}
105
106	operations.Close(c.bug, author)
107
108	// TODO: perf --> the snapshot could simply be updated with the new op
109	c.ClearSnapshot()
110
111	return nil
112}
113
114func (c *BugCache) SetTitle(title string) error {
115	author, err := bug.GetUser(c.repo)
116	if err != nil {
117		return err
118	}
119
120	operations.SetTitle(c.bug, author, title)
121
122	// TODO: perf --> the snapshot could simply be updated with the new op
123	c.ClearSnapshot()
124
125	return nil
126}
127
128func (c *BugCache) Commit() error {
129	return c.bug.Commit(c.repo)
130}
131
132func (c *BugCache) CommitAsNeeded() error {
133	if c.bug.HasPendingOp() {
134		return c.bug.Commit(c.repo)
135	}
136	return nil
137}