1package bug
2
3import (
4 "crypto/sha256"
5 "encoding/json"
6 "fmt"
7 "time"
8
9 "github.com/MichaelMure/git-bug/identity"
10
11 "github.com/MichaelMure/git-bug/util/git"
12 "github.com/pkg/errors"
13)
14
15// OperationType is an operation type identifier
16type OperationType int
17
18const (
19 _ OperationType = iota
20 CreateOp
21 SetTitleOp
22 AddCommentOp
23 SetStatusOp
24 LabelChangeOp
25 EditCommentOp
26 NoOpOp
27 SetMetadataOp
28)
29
30// Operation define the interface to fulfill for an edit operation of a Bug
31type Operation interface {
32 // base return the OpBase of the Operation, for package internal use
33 base() *OpBase
34 // Hash return the hash of the operation, to be used for back references
35 Hash() (git.Hash, error)
36 // Time return the time when the operation was added
37 Time() time.Time
38 // GetUnixTime return the unix timestamp when the operation was added
39 GetUnixTime() int64
40 // GetFiles return the files needed by this operation
41 GetFiles() []git.Hash
42 // Apply the operation to a Snapshot to create the final state
43 Apply(snapshot *Snapshot)
44 // Validate check if the operation is valid (ex: a title is a single line)
45 Validate() error
46 // SetMetadata store arbitrary metadata about the operation
47 SetMetadata(key string, value string)
48 // GetMetadata retrieve arbitrary metadata about the operation
49 GetMetadata(key string) (string, bool)
50 // AllMetadata return all metadata for this operation
51 AllMetadata() map[string]string
52}
53
54func hashRaw(data []byte) git.Hash {
55 hasher := sha256.New()
56 // Write can't fail
57 _, _ = hasher.Write(data)
58 return git.Hash(fmt.Sprintf("%x", hasher.Sum(nil)))
59}
60
61// hash compute the hash of the serialized operation
62func hashOperation(op Operation) (git.Hash, error) {
63 // TODO: this might not be the best idea: if a single bit change in the output of json.Marshal, this will break.
64 // Idea: hash the segment of serialized data (= immutable) instead of the go object in memory
65
66 base := op.base()
67
68 if base.hash != "" {
69 return base.hash, nil
70 }
71
72 data, err := json.Marshal(op)
73 if err != nil {
74 return "", err
75 }
76
77 base.hash = hashRaw(data)
78
79 return base.hash, nil
80}
81
82// OpBase implement the common code for all operations
83type OpBase struct {
84 OperationType OperationType
85 Author identity.Interface
86 UnixTime int64
87 Metadata map[string]string
88 // Not serialized. Store the op's hash in memory.
89 hash git.Hash
90 // Not serialized. Store the extra metadata in memory,
91 // compiled from SetMetadataOperation.
92 extraMetadata map[string]string
93}
94
95// newOpBase is the constructor for an OpBase
96func newOpBase(opType OperationType, author identity.Interface, unixTime int64) OpBase {
97 return OpBase{
98 OperationType: opType,
99 Author: author,
100 UnixTime: unixTime,
101 }
102}
103
104func (op OpBase) MarshalJSON() ([]byte, error) {
105 return json.Marshal(struct {
106 OperationType OperationType `json:"type"`
107 Author identity.Interface `json:"author"`
108 UnixTime int64 `json:"timestamp"`
109 Metadata map[string]string `json:"metadata,omitempty"`
110 }{
111 OperationType: op.OperationType,
112 Author: op.Author,
113 UnixTime: op.UnixTime,
114 Metadata: op.Metadata,
115 })
116}
117
118func (op *OpBase) UnmarshalJSON(data []byte) error {
119 aux := struct {
120 OperationType OperationType `json:"type"`
121 Author json.RawMessage `json:"author"`
122 UnixTime int64 `json:"timestamp"`
123 Metadata map[string]string `json:"metadata,omitempty"`
124 }{}
125
126 if err := json.Unmarshal(data, &aux); err != nil {
127 return err
128 }
129
130 // delegate the decoding of the identity
131 author, err := identity.UnmarshalJSON(aux.Author)
132 if err != nil {
133 return err
134 }
135
136 op.OperationType = aux.OperationType
137 op.Author = author
138 op.UnixTime = aux.UnixTime
139 op.Metadata = aux.Metadata
140
141 return nil
142}
143
144// Time return the time when the operation was added
145func (op *OpBase) Time() time.Time {
146 return time.Unix(op.UnixTime, 0)
147}
148
149// GetUnixTime return the unix timestamp when the operation was added
150func (op *OpBase) GetUnixTime() int64 {
151 return op.UnixTime
152}
153
154// GetFiles return the files needed by this operation
155func (op *OpBase) GetFiles() []git.Hash {
156 return nil
157}
158
159// Validate check the OpBase for errors
160func opBaseValidate(op Operation, opType OperationType) error {
161 if op.base().OperationType != opType {
162 return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
163 }
164
165 if op.GetUnixTime() == 0 {
166 return fmt.Errorf("time not set")
167 }
168
169 if op.base().Author == nil {
170 return fmt.Errorf("author not set")
171 }
172
173 if err := op.base().Author.Validate(); err != nil {
174 return errors.Wrap(err, "author")
175 }
176
177 for _, hash := range op.GetFiles() {
178 if !hash.IsValid() {
179 return fmt.Errorf("file with invalid hash %v", hash)
180 }
181 }
182
183 return nil
184}
185
186// SetMetadata store arbitrary metadata about the operation
187func (op *OpBase) SetMetadata(key string, value string) {
188 if op.Metadata == nil {
189 op.Metadata = make(map[string]string)
190 }
191
192 op.Metadata[key] = value
193 op.hash = ""
194}
195
196// GetMetadata retrieve arbitrary metadata about the operation
197func (op *OpBase) GetMetadata(key string) (string, bool) {
198 val, ok := op.Metadata[key]
199
200 if ok {
201 return val, true
202 }
203
204 // extraMetadata can't replace the original operations value if any
205 val, ok = op.extraMetadata[key]
206
207 return val, ok
208}
209
210// AllMetadata return all metadata for this operation
211func (op *OpBase) AllMetadata() map[string]string {
212 result := make(map[string]string)
213
214 for key, val := range op.extraMetadata {
215 result[key] = val
216 }
217
218 // Original metadata take precedence
219 for key, val := range op.Metadata {
220 result[key] = val
221 }
222
223 return result
224}