aboutsummaryrefslogtreecommitdiffstats
path: root/bug/op_set_metadata.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-10-21 00:55:58 +0200
committerMichael Muré <batolettre@gmail.com>2018-10-21 00:55:58 +0200
commit82701f8ce786dd0252c62d2c28ee30e6790b248f (patch)
treea885e736870737ba71187b14f9d7a38d47870938 /bug/op_set_metadata.go
parentd37ffa6bd49f6f39a4de03dc2fa101832a1befe8 (diff)
downloadgit-bug-82701f8ce786dd0252c62d2c28ee30e6790b248f.tar.gz
bug: add a new SetMetadataOperation to retroactively tag operations
Diffstat (limited to 'bug/op_set_metadata.go')
-rw-r--r--bug/op_set_metadata.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/bug/op_set_metadata.go b/bug/op_set_metadata.go
new file mode 100644
index 00000000..68cf1d51
--- /dev/null
+++ b/bug/op_set_metadata.go
@@ -0,0 +1,72 @@
+package bug
+
+import "github.com/MichaelMure/git-bug/util/git"
+
+var _ Operation = &SetMetadataOperation{}
+
+type SetMetadataOperation struct {
+ OpBase
+ Target git.Hash `json:"target"`
+ NewMetadata map[string]string `json:"new_metadata"`
+}
+
+func (op *SetMetadataOperation) base() *OpBase {
+ return &op.OpBase
+}
+
+func (op *SetMetadataOperation) Hash() (git.Hash, error) {
+ return hashOperation(op)
+}
+
+func (op *SetMetadataOperation) Apply(snapshot *Snapshot) {
+ for _, target := range snapshot.Operations {
+ hash, err := target.Hash()
+ if err != nil {
+ // Should never error unless a programming error happened
+ // (covered in OpBase.Validate())
+ panic(err)
+ }
+
+ if hash == op.Target {
+ base := target.base()
+
+ if base.extraMetadata == nil {
+ base.extraMetadata = make(map[string]string)
+ }
+
+ for key, val := range op.NewMetadata {
+ if _, exist := base.extraMetadata[key]; !exist {
+ base.extraMetadata[key] = val
+ }
+ }
+
+ return
+ }
+ }
+}
+
+func (op *SetMetadataOperation) Validate() error {
+ if err := opBaseValidate(op, SetMetadataOp); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func NewSetMetadataOp(author Person, unixTime int64, target git.Hash, newMetadata map[string]string) *SetMetadataOperation {
+ return &SetMetadataOperation{
+ OpBase: newOpBase(SetMetadataOp, author, unixTime),
+ Target: target,
+ NewMetadata: newMetadata,
+ }
+}
+
+// Convenience function to apply the operation
+func SetMetadata(b Interface, author Person, unixTime int64, target git.Hash, newMetadata map[string]string) (*SetMetadataOperation, error) {
+ SetMetadataOp := NewSetMetadataOp(author, unixTime, target, newMetadata)
+ if err := SetMetadataOp.Validate(); err != nil {
+ return nil, err
+ }
+ b.Append(SetMetadataOp)
+ return SetMetadataOp, nil
+}