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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package entity
import (
"encoding/json"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/lamport"
)
const opsEntryName = "ops"
const versionEntryPrefix = "version-"
const createClockEntryPrefix = "create-clock-"
const editClockEntryPrefix = "edit-clock-"
type operationPack struct {
Operations []Operation
CreateTime lamport.Time
EditTime lamport.Time
}
// func (opp *operationPack) MarshalJSON() ([]byte, error) {
// return json.Marshal(struct {
// Operations []Operation `json:"ops"`
// }{
// Operations: opp.Operations,
// })
// }
func readOperationPack(def Definition, repo repository.RepoData, treeHash repository.Hash) (*operationPack, error) {
entries, err := repo.ReadTree(treeHash)
if err != nil {
return nil, err
}
// check the format version first, fail early instead of trying to read something
// var version uint
// for _, entry := range entries {
// if strings.HasPrefix(entry.Name, versionEntryPrefix) {
// v, err := strconv.ParseUint(strings.TrimPrefix(entry.Name, versionEntryPrefix), 10, 64)
// if err != nil {
// return nil, errors.Wrap(err, "can't read format version")
// }
// if v > 1<<12 {
// return nil, fmt.Errorf("format version too big")
// }
// version = uint(v)
// break
// }
// }
// if version == 0 {
// return nil, NewErrUnknowFormat(def.formatVersion)
// }
// if version != def.formatVersion {
// return nil, NewErrInvalidFormat(version, def.formatVersion)
// }
var ops []Operation
var createTime lamport.Time
var editTime lamport.Time
for _, entry := range entries {
if entry.Name == opsEntryName {
data, err := repo.ReadData(entry.Hash)
if err != nil {
return nil, errors.Wrap(err, "failed to read git blob data")
}
ops, err = unmarshallOperations(def, data)
if err != nil {
return nil, err
}
break
}
if strings.HasPrefix(entry.Name, createClockEntryPrefix) {
v, err := strconv.ParseUint(strings.TrimPrefix(entry.Name, createClockEntryPrefix), 10, 64)
if err != nil {
return nil, errors.Wrap(err, "can't read creation lamport time")
}
createTime = lamport.Time(v)
}
if strings.HasPrefix(entry.Name, editClockEntryPrefix) {
v, err := strconv.ParseUint(strings.TrimPrefix(entry.Name, editClockEntryPrefix), 10, 64)
if err != nil {
return nil, errors.Wrap(err, "can't read edit lamport time")
}
editTime = lamport.Time(v)
}
}
return &operationPack{
Operations: ops,
CreateTime: createTime,
EditTime: editTime,
}, nil
}
func unmarshallOperations(def Definition, data []byte) ([]Operation, error) {
aux := struct {
Operations []json.RawMessage `json:"ops"`
}{}
if err := json.Unmarshal(data, &aux); err != nil {
return nil, err
}
ops := make([]Operation, 0, len(aux.Operations))
for _, raw := range aux.Operations {
// delegate to specialized unmarshal function
op, err := def.operationUnmarshaler(raw)
if err != nil {
return nil, err
}
ops = append(ops, op)
}
return ops, nil
}
|