2018-07-13 17:13:40 +03:00
|
|
|
package bug
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
import (
|
2018-07-14 23:17:37 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2018-07-13 22:21:24 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
"github.com/MichaelMure/git-bug/util"
|
|
|
|
)
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// 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 {
|
2018-07-14 23:17:37 +03:00
|
|
|
Operations []Operation
|
2018-07-17 02:52:56 +03:00
|
|
|
|
|
|
|
// Private field so not serialized by gob
|
|
|
|
commitHash util.Hash
|
2018-07-13 22:21:24 +03:00
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
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
|
2018-07-13 22:21:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opp *OperationPack) Serialize() ([]byte, error) {
|
2018-07-14 23:17:37 +03:00
|
|
|
var data bytes.Buffer
|
|
|
|
|
|
|
|
encoder := gob.NewEncoder(&data)
|
|
|
|
err := encoder.Encode(*opp)
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-14 23:17:37 +03:00
|
|
|
return data.Bytes(), nil
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append a new operation to the pack
|
|
|
|
func (opp *OperationPack) Append(op Operation) {
|
|
|
|
opp.Operations = append(opp.Operations, op)
|
|
|
|
}
|
|
|
|
|
2018-07-13 17:48:55 +03:00
|
|
|
func (opp *OperationPack) IsEmpty() bool {
|
|
|
|
return len(opp.Operations) == 0
|
|
|
|
}
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
func (opp *OperationPack) IsValid() bool {
|
2018-07-13 17:48:55 +03:00
|
|
|
return !opp.IsEmpty()
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
2018-07-13 22:21:24 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-07-17 02:52:56 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|