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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
package packfile
import (
"bytes"
"errors"
"io"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
)
// Observer interface is implemented by index encoders.
type Observer interface {
// OnHeader is called when a new packfile is opened.
OnHeader(count uint32) error
// OnInflatedObjectHeader is called for each object header read.
OnInflatedObjectHeader(t plumbing.ObjectType, objSize int64, pos int64) error
// OnInflatedObjectContent is called for each decoded object.
OnInflatedObjectContent(h plumbing.Hash, pos int64, crc uint32) error
// OnFooter is called when decoding is done.
OnFooter(h plumbing.Hash) error
}
// Parser decodes a packfile and calls any observer associated to it. Is used
// to generate indexes.
type Parser struct {
scanner *Scanner
count uint32
oi []*objectInfo
oiByHash map[plumbing.Hash]*objectInfo
oiByOffset map[int64]*objectInfo
hashOffset map[plumbing.Hash]int64
checksum plumbing.Hash
cache *cache.ObjectLRU
ob []Observer
}
// NewParser creates a new Parser struct.
func NewParser(scanner *Scanner, ob ...Observer) *Parser {
return &Parser{
scanner: scanner,
ob: ob,
count: 0,
cache: cache.NewObjectLRUDefault(),
}
}
// Parse start decoding phase of the packfile.
func (p *Parser) Parse() (plumbing.Hash, error) {
err := p.init()
if err != nil {
return plumbing.ZeroHash, err
}
err = p.firstPass()
if err != nil {
return plumbing.ZeroHash, err
}
err = p.resolveDeltas()
if err != nil {
return plumbing.ZeroHash, err
}
for _, o := range p.ob {
err := o.OnFooter(p.checksum)
if err != nil {
return plumbing.ZeroHash, err
}
}
return p.checksum, nil
}
func (p *Parser) init() error {
_, c, err := p.scanner.Header()
if err != nil {
return err
}
for _, o := range p.ob {
err := o.OnHeader(c)
if err != nil {
return err
}
}
p.count = c
p.oiByHash = make(map[plumbing.Hash]*objectInfo, p.count)
p.oiByOffset = make(map[int64]*objectInfo, p.count)
p.oi = make([]*objectInfo, p.count)
return nil
}
func (p *Parser) firstPass() error {
buf := new(bytes.Buffer)
for i := uint32(0); i < p.count; i++ {
buf.Truncate(0)
oh, err := p.scanner.NextObjectHeader()
if err != nil {
return err
}
delta := false
var ota *objectInfo
switch t := oh.Type; t {
case plumbing.OFSDeltaObject, plumbing.REFDeltaObject:
delta = true
var parent *objectInfo
var ok bool
if t == plumbing.OFSDeltaObject {
parent, ok = p.oiByOffset[oh.OffsetReference]
} else {
parent, ok = p.oiByHash[oh.Reference]
}
if !ok {
// TODO improve error
return errors.New("Reference delta not found")
}
ota = newDeltaObject(oh.Offset, oh.Length, t, parent)
parent.Children = append(parent.Children, ota)
default:
ota = newBaseObject(oh.Offset, oh.Length, t)
}
size, crc, err := p.scanner.NextObject(buf)
if err != nil {
return err
}
ota.Crc32 = crc
ota.PackSize = size
ota.Length = oh.Length
if !delta {
ota.Write(buf.Bytes())
ota.SHA1 = ota.Sum()
}
p.oiByOffset[oh.Offset] = ota
p.oiByHash[oh.Reference] = ota
p.oi[i] = ota
}
checksum, err := p.scanner.Checksum()
p.checksum = checksum
if err == io.EOF {
return nil
}
return err
}
func (p *Parser) resolveDeltas() error {
for _, obj := range p.oi {
for _, o := range p.ob {
err := o.OnInflatedObjectHeader(obj.Type, obj.Length, obj.Offset)
if err != nil {
return err
}
err = o.OnInflatedObjectContent(obj.SHA1, obj.Offset, obj.Crc32)
if err != nil {
return err
}
}
if !obj.IsDelta() && len(obj.Children) > 0 {
var err error
base, err := p.get(obj)
if err != nil {
return err
}
for _, child := range obj.Children {
_, err = p.resolveObject(child, base)
if err != nil {
return err
}
}
}
}
return nil
}
func (p *Parser) get(o *objectInfo) ([]byte, error) {
e, ok := p.cache.Get(o.SHA1)
if ok {
r, err := e.Reader()
if err != nil {
return nil, err
}
buf := make([]byte, e.Size())
_, err = r.Read(buf)
if err != nil {
return nil, err
}
return buf, nil
}
// Read from disk
if o.DiskType.IsDelta() {
base, err := p.get(o.Parent)
if err != nil {
return nil, err
}
data, err := p.resolveObject(o, base)
if err != nil {
return nil, err
}
if len(o.Children) > 0 {
m := &plumbing.MemoryObject{}
m.Write(data)
m.SetType(o.Type)
m.SetSize(o.Size())
p.cache.Put(m)
}
return data, nil
}
data, err := p.readData(o)
if err != nil {
return nil, err
}
if len(o.Children) > 0 {
m := &plumbing.MemoryObject{}
m.Write(data)
m.SetType(o.Type)
m.SetSize(o.Size())
p.cache.Put(m)
}
return data, nil
}
func (p *Parser) resolveObject(
o *objectInfo,
base []byte) ([]byte, error) {
if !o.DiskType.IsDelta() {
return nil, nil
}
data, err := p.readData(o)
if err != nil {
return nil, err
}
data, err = applyPatchBase(o, data, base)
if err != nil {
return nil, err
}
return data, nil
}
func (p *Parser) readData(o *objectInfo) ([]byte, error) {
buf := new(bytes.Buffer)
// TODO: skip header. Header size can be calculated with the offset of the
// next offset in the first pass.
p.scanner.SeekFromStart(o.Offset)
_, err := p.scanner.NextObjectHeader()
if err != nil {
return nil, err
}
buf.Truncate(0)
_, _, err = p.scanner.NextObject(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func applyPatchBase(ota *objectInfo, data, base []byte) ([]byte, error) {
patched, err := PatchDelta(base, data)
if err != nil {
return nil, err
}
ota.Type = ota.Parent.Type
hash := plumbing.ComputeHash(ota.Type, patched)
ota.SHA1 = hash
return patched, nil
}
type objectInfo struct {
plumbing.Hasher
Offset int64
Length int64
HeaderLength int64
PackSize int64
Type plumbing.ObjectType
DiskType plumbing.ObjectType
Crc32 uint32
Parent *objectInfo
Children []*objectInfo
SHA1 plumbing.Hash
}
func newBaseObject(offset, length int64, t plumbing.ObjectType) *objectInfo {
return newDeltaObject(offset, length, t, nil)
}
func newDeltaObject(
offset, length int64,
t plumbing.ObjectType,
parent *objectInfo,
) *objectInfo {
children := make([]*objectInfo, 0)
obj := &objectInfo{
Hasher: plumbing.NewHasher(t, length),
Offset: offset,
Length: length,
PackSize: 0,
Type: t,
DiskType: t,
Crc32: 0,
Parent: parent,
Children: children,
}
return obj
}
func (o *objectInfo) IsDelta() bool {
return o.Type.IsDelta()
}
func (o *objectInfo) Size() int64 {
return o.Length
}
|