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