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 op.Apply(b.snap)
31 b.snap.Operations = append(b.snap.Operations, op)
32}
33
34// Commit intercept Bug.Commit() to update the snapshot efficiently
35func (b *WithSnapshot) Commit(repo repository.ClockedRepo) error {
36 err := b.Bug.Commit(repo)
37
38 if err != nil {
39 b.snap = nil
40 return err
41 }
42
43 // Commit() shouldn't change anything of the bug state apart from the
44 // initial ID set
45
46 if b.snap == nil {
47 return nil
48 }
49
50 b.snap.id = b.Bug.id
51 return nil
52}
53
54// Merge intercept Bug.Merge() and clear the snapshot
55func (b *WithSnapshot) Merge(repo repository.Repo, other Interface) (bool, error) {
56 b.snap = nil
57 return b.Bug.Merge(repo, other)
58}