1package bug
2
3import (
4 "bytes"
5 "encoding/gob"
6 "github.com/MichaelMure/git-bug/repository"
7 "github.com/MichaelMure/git-bug/util"
8)
9
10// OperationPack represent an ordered set of operation to apply
11// to a Bug. These operations are stored in a single Git commit.
12//
13// These commits will be linked together in a linear chain of commits
14// inside Git to form the complete ordered chain of operation to
15// apply to get the final state of the Bug
16type OperationPack struct {
17 Operations []Operation
18}
19
20func ParseOperationPack(data []byte) (*OperationPack, error) {
21 reader := bytes.NewReader(data)
22 decoder := gob.NewDecoder(reader)
23
24 var opp OperationPack
25
26 err := decoder.Decode(&opp)
27
28 if err != nil {
29 return nil, err
30 }
31
32 return &opp, nil
33}
34
35func (opp *OperationPack) Serialize() ([]byte, error) {
36 var data bytes.Buffer
37
38 encoder := gob.NewEncoder(&data)
39 err := encoder.Encode(*opp)
40
41 if err != nil {
42 return nil, err
43 }
44
45 return data.Bytes(), nil
46}
47
48// Append a new operation to the pack
49func (opp *OperationPack) Append(op Operation) {
50 opp.Operations = append(opp.Operations, op)
51}
52
53func (opp *OperationPack) IsEmpty() bool {
54 return len(opp.Operations) == 0
55}
56
57func (opp *OperationPack) IsValid() bool {
58 return !opp.IsEmpty()
59}
60
61func (opp *OperationPack) Write(repo repository.Repo) (util.Hash, error) {
62 data, err := opp.Serialize()
63
64 if err != nil {
65 return "", err
66 }
67
68 hash, err := repo.StoreData(data)
69
70 if err != nil {
71 return "", err
72 }
73
74 return hash, nil
75}