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
|
package packfile
import (
"bytes"
"gopkg.in/src-d/go-git.v4/core"
)
// Format specifies if the packfile uses ref-deltas or ofs-deltas.
type Format int
// Possible values of the Format type.
const (
UnknownFormat Format = iota
OFSDeltaFormat
REFDeltaFormat
)
var (
// ErrMaxObjectsLimitReached is returned by Decode when the number
// of objects in the packfile is higher than
// Decoder.MaxObjectsLimit.
ErrMaxObjectsLimitReached = NewError("max. objects limit reached")
// ErrInvalidObject is returned by Decode when an invalid object is
// found in the packfile.
ErrInvalidObject = NewError("invalid git object")
// ErrPackEntryNotFound is returned by Decode when a reference in
// the packfile references and unknown object.
ErrPackEntryNotFound = NewError("can't find a pack entry")
// ErrZLib is returned by Decode when there was an error unzipping
// the packfile contents.
ErrZLib = NewError("zlib reading error")
// ErrNotSeeker not seeker supported
ErrNotSeeker = NewError("no seeker capable decode")
// ErrCannotRecall is returned by RecallByOffset or RecallByHash if the object
// to recall cannot be returned.
ErrCannotRecall = NewError("cannot recall object")
)
// Decoder reads and decodes packfiles from an input stream.
type Decoder struct {
s *Scanner
o core.ObjectStorage
tx core.TxObjectStorage
offsets map[int64]core.Hash
crcs map[core.Hash]uint32
}
// NewDecoder returns a new Decoder that reads from r.
func NewDecoder(s *Scanner, o core.ObjectStorage) *Decoder {
return &Decoder{
s: s,
o: o,
tx: o.Begin(),
offsets: make(map[int64]core.Hash, 0),
crcs: make(map[core.Hash]uint32, 0),
}
}
// Decode reads a packfile and stores it in the value pointed to by s.
func (d *Decoder) Decode() (checksum core.Hash, err error) {
if err := d.doDecode(); err != nil {
return core.ZeroHash, err
}
return d.s.Checksum()
}
func (d *Decoder) doDecode() error {
_, count, err := d.s.Header()
if err != nil {
return err
}
if err := d.readObjects(count); err != nil {
if err := d.tx.Rollback(); err != nil {
return nil
}
return err
}
if err := d.tx.Commit(); err != nil {
return err
}
return nil
}
func (d *Decoder) readObjects(count uint32) error {
for i := 0; i < int(count); i++ {
_, err := d.ReadObject()
if err != nil {
return err
}
}
return nil
}
// ReadObject reads a object from the stream and return it
func (d *Decoder) ReadObject() (core.Object, error) {
h, err := d.s.NextObjectHeader()
if err != nil {
return nil, err
}
obj := d.o.NewObject()
obj.SetSize(h.Length)
obj.SetType(h.Type)
var crc uint32
switch h.Type {
case core.CommitObject, core.TreeObject, core.BlobObject, core.TagObject:
crc, err = d.fillRegularObjectContent(obj)
case core.REFDeltaObject:
crc, err = d.fillREFDeltaObjectContent(obj, h.Reference)
case core.OFSDeltaObject:
crc, err = d.fillOFSDeltaObjectContent(obj, h.OffsetReference)
default:
err = ErrInvalidObject.AddDetails("type %q", h.Type)
}
if err != nil {
return obj, err
}
d.remember(obj, h.Offset, crc)
if _, err := d.tx.Set(obj); err != nil {
return nil, err
}
return obj, nil
}
// ReadObjectAt reads an object at the given location
func (d *Decoder) ReadObjectAt(offset int64) (core.Object, error) {
beforeJump, err := d.s.Seek(offset)
if err != nil {
return nil, err
}
defer func() {
_, seekErr := d.s.Seek(beforeJump)
if err == nil {
err = seekErr
}
}()
return d.ReadObject()
}
func (d *Decoder) fillRegularObjectContent(obj core.Object) (uint32, error) {
w, err := obj.Writer()
if err != nil {
return 0, err
}
_, crc, err := d.s.NextObject(w)
return crc, err
}
func (d *Decoder) fillREFDeltaObjectContent(obj core.Object, ref core.Hash) (uint32, error) {
buf := bytes.NewBuffer(nil)
_, crc, err := d.s.NextObject(buf)
if err != nil {
return 0, err
}
base, err := d.recallByHash(ref)
if err != nil {
return 0, err
}
obj.SetType(base.Type())
return crc, ApplyDelta(obj, base, buf.Bytes())
}
func (d *Decoder) fillOFSDeltaObjectContent(obj core.Object, offset int64) (uint32, error) {
buf := bytes.NewBuffer(nil)
_, crc, err := d.s.NextObject(buf)
if err != nil {
return 0, err
}
base, err := d.recallByOffset(offset)
if err != nil {
return 0, err
}
obj.SetType(base.Type())
return crc, ApplyDelta(obj, base, buf.Bytes())
}
func (d *Decoder) remember(obj core.Object, offset int64, crc uint32) {
h := obj.Hash()
d.offsets[offset] = h
d.crcs[h] = crc
}
func (d *Decoder) recallByOffset(o int64) (core.Object, error) {
h, ok := d.offsets[o]
if ok {
return d.recallByHash(h)
}
return d.ReadObjectAt(o)
}
func (d *Decoder) recallByHash(h core.Hash) (core.Object, error) {
return d.tx.Get(core.AnyObject, h)
}
// Offsets returns the objects read offset
func (d *Decoder) Offsets() map[core.Hash]int64 {
i := make(map[core.Hash]int64, len(d.offsets))
for o, h := range d.offsets {
i[h] = o
}
return i
}
// CRCs returns the CRC-32 for each objected read
func (d *Decoder) CRCs() map[core.Hash]uint32 {
return d.crcs
}
// Close close the Scanner, usually this mean that the whole reader is read and
// discarded
func (d *Decoder) Close() error {
return d.s.Close()
}
|