1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package bug
import (
"bytes"
"encoding/gob"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util"
)
// OperationPack represent an ordered set of operation to apply
// to a Bug. These operations are stored in a single Git commit.
//
// These commits will be linked together in a linear chain of commits
// inside Git to form the complete ordered chain of operation to
// apply to get the final state of the Bug
type OperationPack struct {
Operations []Operation
// Private field so not serialized by gob
commitHash util.Hash
}
// ParseOperationPack will deserialize an OperationPack from raw bytes
func ParseOperationPack(data []byte) (*OperationPack, error) {
reader := bytes.NewReader(data)
decoder := gob.NewDecoder(reader)
var opp OperationPack
err := decoder.Decode(&opp)
if err != nil {
return nil, err
}
return &opp, nil
}
// Serialize will serialise an OperationPack into raw bytes
func (opp *OperationPack) Serialize() ([]byte, error) {
var data bytes.Buffer
encoder := gob.NewEncoder(&data)
err := encoder.Encode(*opp)
if err != nil {
return nil, err
}
return data.Bytes(), nil
}
// Append a new operation to the pack
func (opp *OperationPack) Append(op Operation) {
opp.Operations = append(opp.Operations, op)
}
// IsEmpty tell if the OperationPack is empty
func (opp *OperationPack) IsEmpty() bool {
return len(opp.Operations) == 0
}
// IsValid tell if the OperationPack is considered valid
func (opp *OperationPack) IsValid() bool {
return !opp.IsEmpty()
}
// Write will serialize and store the OperationPack as a git blob and return
// its hash
func (opp *OperationPack) Write(repo repository.Repo) (util.Hash, error) {
data, err := opp.Serialize()
if err != nil {
return "", err
}
hash, err := repo.StoreData(data)
if err != nil {
return "", err
}
return hash, nil
}
// Make a deep copy
func (opp *OperationPack) Clone() OperationPack {
clone := OperationPack{
Operations: make([]Operation, len(opp.Operations)),
commitHash: opp.commitHash,
}
for i, op := range opp.Operations {
clone.Operations[i] = op
}
return clone
}
|