aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-28 23:51:47 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-29 00:51:54 +0200
commit794d014fae9a78bd8664e6628a20902bd6dc767a (patch)
tree059fe4469787f792b6ddc4c29e6232c4263ece53 /bug/operation.go
parent1bf268cebc84a9de1e538cbb54bcc0f434022192 (diff)
downloadgit-bug-794d014fae9a78bd8664e6628a20902bd6dc767a.tar.gz
bug: define a hash-based identifier for an operation
Diffstat (limited to 'bug/operation.go')
-rw-r--r--bug/operation.go40
1 files changed, 34 insertions, 6 deletions
diff --git a/bug/operation.go b/bug/operation.go
index cd4094dc..4c6f2e5a 100644
--- a/bug/operation.go
+++ b/bug/operation.go
@@ -1,11 +1,13 @@
package bug
import (
- "github.com/MichaelMure/git-bug/util/git"
- "github.com/pkg/errors"
-
+ "crypto/sha256"
+ "encoding/json"
"fmt"
"time"
+
+ "github.com/MichaelMure/git-bug/util/git"
+ "github.com/pkg/errors"
)
// OperationType is an operation type identifier
@@ -24,6 +26,8 @@ const (
type Operation interface {
// base return the OpBase of the Operation, for package internal use
base() *OpBase
+ // Hash return the hash of the operation
+ Hash() (git.Hash, error)
// Time return the time when the operation was added
Time() time.Time
// GetUnixTime return the unix timestamp when the operation was added
@@ -40,11 +44,35 @@ type Operation interface {
GetMetadata(key string) (string, bool)
}
+func hashRaw(data []byte) git.Hash {
+ hasher := sha256.New()
+ return git.Hash(fmt.Sprintf("%x", hasher.Sum(data)))
+}
+
+// hash compute the hash of the serialized operation
+func hashOperation(op Operation) (git.Hash, error) {
+ base := op.base()
+
+ if base.hash != "" {
+ return base.hash, nil
+ }
+
+ data, err := json.Marshal(op)
+ if err != nil {
+ return "", err
+ }
+
+ base.hash = hashRaw(data)
+
+ return base.hash, nil
+}
+
// OpBase implement the common code for all operations
type OpBase struct {
- OperationType OperationType `json:"type"`
- Author Person `json:"author"`
- UnixTime int64 `json:"timestamp"`
+ OperationType OperationType `json:"type"`
+ Author Person `json:"author"`
+ UnixTime int64 `json:"timestamp"`
+ hash git.Hash
Metadata map[string]string `json:"metadata,omitempty"`
}