1package bug
2
3import "github.com/MichaelMure/git-bug/repository"
4
5var _ Interface = &WithSnapshot{}
6
7// WithSnapshot encapsulate a Bug and maintain the corresponding Snapshot efficiently
8type WithSnapshot struct {
9 *Bug
10 snap *Snapshot
11}
12
13// Snapshot return the current snapshot
14func (b *WithSnapshot) Snapshot() *Snapshot {
15 if b.snap == nil {
16 snap := b.Bug.Compile()
17 b.snap = &snap
18 }
19 return b.snap
20}
21
22// Append intercept Bug.Append() to update the snapshot efficiently
23func (b *WithSnapshot) Append(op Operation) {
24 b.Bug.Append(op)
25
26 if b.snap == nil {
27 return
28 }
29
30 snap := op.Apply(*b.snap)
31 snap.Operations = append(snap.Operations, op)
32
33 b.snap = &snap
34}
35
36// Commit intercept Bug.Commit() to update the snapshot efficiently
37func (b *WithSnapshot) Commit(repo repository.Repo) error {
38 err := b.Bug.Commit(repo)
39
40 if err != nil {
41 b.snap = nil
42 return err
43 }
44
45 // Commit() shouldn't change anything of the bug state apart from the
46 // initial ID set
47
48 if b.snap == nil {
49 return nil
50 }
51
52 b.snap.id = b.Bug.id
53 return nil
54}
55
56// Merge intercept Bug.Merge() and clear the snapshot
57func (b *WithSnapshot) Merge(repo repository.Repo, other Interface) (bool, error) {
58 b.snap = nil
59 return b.Bug.Merge(repo, other)
60}