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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
package packfile_test
import (
"io"
"srcd.works/go-billy.v1/memfs"
"github.com/src-d/go-git-fixtures"
"srcd.works/go-git.v4/plumbing"
"srcd.works/go-git.v4/plumbing/format/idxfile"
"srcd.works/go-git.v4/plumbing/format/packfile"
"srcd.works/go-git.v4/plumbing/storer"
"srcd.works/go-git.v4/storage/filesystem"
"srcd.works/go-git.v4/storage/memory"
. "gopkg.in/check.v1"
)
type ReaderSuite struct {
fixtures.Suite
}
var _ = Suite(&ReaderSuite{})
func (s *ReaderSuite) TestNewDecodeNonSeekable(c *C) {
scanner := packfile.NewScanner(nil)
d, err := packfile.NewDecoder(scanner, nil)
c.Assert(d, IsNil)
c.Assert(err, NotNil)
}
func (s *ReaderSuite) TestDecode(c *C) {
fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
scanner := packfile.NewScanner(f.Packfile())
storage := memory.NewStorage()
d, err := packfile.NewDecoder(scanner, storage)
c.Assert(err, IsNil)
defer d.Close()
ch, err := d.Decode()
c.Assert(err, IsNil)
c.Assert(ch, Equals, f.PackfileHash)
assertObjects(c, storage, expectedHashes)
})
}
func (s *ReaderSuite) TestDecodeByTypeRefDelta(c *C) {
f := fixtures.Basic().ByTag("ref-delta").One()
storage := memory.NewStorage()
scanner := packfile.NewScanner(f.Packfile())
d, err := packfile.NewDecoderForType(scanner, storage, plumbing.CommitObject)
c.Assert(err, IsNil)
// Specific offset elements needed to decode correctly the ref-delta
offsets := map[plumbing.Hash]int64{
plumbing.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c"): 84880,
plumbing.NewHash("fb72698cab7617ac416264415f13224dfd7a165e"): 85141,
plumbing.NewHash("eba74343e2f15d62adedfd8c883ee0262b5c8021"): 85300,
}
d.SetOffsets(offsets)
defer d.Close()
_, count, err := scanner.Header()
c.Assert(err, IsNil)
var i uint32
for i = 0; i < count; i++ {
obj, err := d.DecodeObject()
c.Assert(err, IsNil)
if obj != nil {
c.Assert(obj.Type(), Equals, plumbing.CommitObject)
}
}
}
func (s *ReaderSuite) TestDecodeByTypeRefDeltaError(c *C) {
fixtures.Basic().ByTag("ref-delta").Test(c, func(f *fixtures.Fixture) {
storage := memory.NewStorage()
scanner := packfile.NewScanner(f.Packfile())
d, err := packfile.NewDecoderForType(scanner, storage, plumbing.CommitObject)
c.Assert(err, IsNil)
defer d.Close()
_, count, err := scanner.Header()
c.Assert(err, IsNil)
isError := false
var i uint32
for i = 0; i < count; i++ {
_, err := d.DecodeObject()
if err != nil {
isError = true
break
}
}
c.Assert(isError, Equals, true)
})
}
func (s *ReaderSuite) TestDecodeByType(c *C) {
ts := []plumbing.ObjectType{
plumbing.CommitObject,
plumbing.TagObject,
plumbing.TreeObject,
plumbing.BlobObject,
}
fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
for _, t := range ts {
storage := memory.NewStorage()
scanner := packfile.NewScanner(f.Packfile())
d, err := packfile.NewDecoderForType(scanner, storage, t)
c.Assert(err, IsNil)
// when the packfile is ref-delta based, the offsets are required
if f.Is("ref-delta") {
d.SetOffsets(getOffsetsFromIdx(f.Idx()))
}
defer d.Close()
_, count, err := scanner.Header()
c.Assert(err, IsNil)
var i uint32
for i = 0; i < count; i++ {
obj, err := d.DecodeObject()
c.Assert(err, IsNil)
if obj != nil {
c.Assert(obj.Type(), Equals, t)
}
}
}
})
}
func (s *ReaderSuite) TestDecodeByTypeConstructor(c *C) {
f := fixtures.Basic().ByTag("packfile").One()
storage := memory.NewStorage()
scanner := packfile.NewScanner(f.Packfile())
_, err := packfile.NewDecoderForType(scanner, storage, plumbing.OFSDeltaObject)
c.Assert(err, Equals, plumbing.ErrInvalidType)
_, err = packfile.NewDecoderForType(scanner, storage, plumbing.REFDeltaObject)
c.Assert(err, Equals, plumbing.ErrInvalidType)
_, err = packfile.NewDecoderForType(scanner, storage, plumbing.InvalidObject)
c.Assert(err, Equals, plumbing.ErrInvalidType)
}
func (s *ReaderSuite) TestDecodeMultipleTimes(c *C) {
f := fixtures.Basic().ByTag("packfile").One()
scanner := packfile.NewScanner(f.Packfile())
storage := memory.NewStorage()
d, err := packfile.NewDecoder(scanner, storage)
c.Assert(err, IsNil)
defer d.Close()
ch, err := d.Decode()
c.Assert(err, IsNil)
c.Assert(ch, Equals, f.PackfileHash)
ch, err = d.Decode()
c.Assert(err, Equals, packfile.ErrAlreadyDecoded)
c.Assert(ch, Equals, plumbing.ZeroHash)
}
func (s *ReaderSuite) TestDecodeInMemory(c *C) {
fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
scanner := packfile.NewScanner(f.Packfile())
d, err := packfile.NewDecoder(scanner, nil)
c.Assert(err, IsNil)
ch, err := d.Decode()
c.Assert(err, IsNil)
c.Assert(ch, Equals, f.PackfileHash)
})
}
type nonSeekableReader struct {
r io.Reader
}
func (nsr nonSeekableReader) Read(b []byte) (int, error) {
return nsr.r.Read(b)
}
func (s *ReaderSuite) TestDecodeNoSeekableWithTxStorer(c *C) {
fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
reader := nonSeekableReader{
r: f.Packfile(),
}
scanner := packfile.NewScanner(reader)
var storage storer.EncodedObjectStorer = memory.NewStorage()
_, isTxStorer := storage.(storer.Transactioner)
c.Assert(isTxStorer, Equals, true)
d, err := packfile.NewDecoder(scanner, storage)
c.Assert(err, IsNil)
defer d.Close()
ch, err := d.Decode()
c.Assert(err, IsNil)
c.Assert(ch, Equals, f.PackfileHash)
assertObjects(c, storage, expectedHashes)
})
}
func (s *ReaderSuite) TestDecodeNoSeekableWithoutTxStorer(c *C) {
fixtures.Basic().ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
reader := nonSeekableReader{
r: f.Packfile(),
}
scanner := packfile.NewScanner(reader)
var storage storer.EncodedObjectStorer
storage, _ = filesystem.NewStorage(memfs.New())
_, isTxStorer := storage.(storer.Transactioner)
c.Assert(isTxStorer, Equals, false)
d, err := packfile.NewDecoder(scanner, storage)
c.Assert(err, IsNil)
defer d.Close()
ch, err := d.Decode()
c.Assert(err, IsNil)
c.Assert(ch, Equals, f.PackfileHash)
assertObjects(c, storage, expectedHashes)
})
}
var expectedHashes = []string{
"918c48b83bd081e863dbe1b80f8998f058cd8294",
"af2d6a6954d532f8ffb47615169c8fdf9d383a1a",
"1669dce138d9b841a518c64b10914d88f5e488ea",
"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
"b8e471f58bcbca63b07bda20e428190409c2db47",
"35e85108805c84807bc66a02d91535e1e24b38b9",
"b029517f6300c2da0f4b651b8642506cd6aaf45d",
"32858aad3c383ed1ff0a0f9bdf231d54a00c9e88",
"d3ff53e0564a9f87d8e84b6e28e5060e517008aa",
"c192bd6a24ea1ab01d78686e417c8bdc7c3d197f",
"d5c0f4ab811897cadf03aec358ae60d21f91c50d",
"49c6bb89b17060d7b4deacb7b338fcc6ea2352a9",
"cf4aa3b38974fb7d81f367c0830f7d78d65ab86b",
"9dea2395f5403188298c1dabe8bdafe562c491e3",
"586af567d0bb5e771e49bdd9434f5e0fb76d25fa",
"9a48f23120e880dfbe41f7c9b7b708e9ee62a492",
"5a877e6a906a2743ad6e45d99c1793642aaf8eda",
"c8f1d8c61f9da76f4cb49fd86322b6e685dba956",
"a8d315b2b1c615d43042c3a62402b8a54288cf5c",
"a39771a7651f97faf5c72e08224d857fc35133db",
"880cd14280f4b9b6ed3986d6671f907d7cc2a198",
"fb72698cab7617ac416264415f13224dfd7a165e",
"4d081c50e250fa32ea8b1313cf8bb7c2ad7627fd",
"eba74343e2f15d62adedfd8c883ee0262b5c8021",
"c2d30fa8ef288618f65f6eed6e168e0d514886f4",
"8dcef98b1d52143e1e2dbc458ffe38f925786bf2",
"aa9b383c260e1d05fbbf6b30a02914555e20c725",
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
"dbd3641b371024f44d0e469a9c8f5457b0660de1",
"e8d3ffab552895c19b9fcf7aa264d277cde33881",
"7e59600739c96546163833214c36459e324bad0a",
}
func (s *ReaderSuite) TestDecodeCRCs(c *C) {
f := fixtures.Basic().ByTag("ofs-delta").One()
scanner := packfile.NewScanner(f.Packfile())
storage := memory.NewStorage()
d, err := packfile.NewDecoder(scanner, storage)
c.Assert(err, IsNil)
_, err = d.Decode()
c.Assert(err, IsNil)
var sum uint64
for _, crc := range d.CRCs() {
sum += uint64(crc)
}
c.Assert(int(sum), Equals, 78022211966)
}
func (s *ReaderSuite) TestReadObjectAt(c *C) {
f := fixtures.Basic().One()
scanner := packfile.NewScanner(f.Packfile())
d, err := packfile.NewDecoder(scanner, nil)
c.Assert(err, IsNil)
// when the packfile is ref-delta based, the offsets are required
if f.Is("ref-delta") {
offsets := getOffsetsFromIdx(f.Idx())
d.SetOffsets(offsets)
}
// the objects at reference 186, is a delta, so should be recall,
// without being read before.
obj, err := d.DecodeObjectAt(186)
c.Assert(err, IsNil)
c.Assert(obj.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
}
func (s *ReaderSuite) TestOffsets(c *C) {
f := fixtures.Basic().One()
scanner := packfile.NewScanner(f.Packfile())
d, err := packfile.NewDecoder(scanner, nil)
c.Assert(err, IsNil)
c.Assert(d.Offsets(), HasLen, 0)
_, err = d.Decode()
c.Assert(err, IsNil)
c.Assert(d.Offsets(), HasLen, 31)
}
func (s *ReaderSuite) TestSetOffsets(c *C) {
f := fixtures.Basic().One()
scanner := packfile.NewScanner(f.Packfile())
d, err := packfile.NewDecoder(scanner, nil)
c.Assert(err, IsNil)
h := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
d.SetOffsets(map[plumbing.Hash]int64{h: 42})
o := d.Offsets()
c.Assert(o, HasLen, 1)
c.Assert(o[h], Equals, int64(42))
}
func assertObjects(c *C, s storer.EncodedObjectStorer, expects []string) {
i, err := s.IterEncodedObjects(plumbing.AnyObject)
c.Assert(err, IsNil)
var count int
err = i.ForEach(func(plumbing.EncodedObject) error { count++; return nil })
c.Assert(err, IsNil)
c.Assert(count, Equals, len(expects))
for _, exp := range expects {
obt, err := s.EncodedObject(plumbing.AnyObject, plumbing.NewHash(exp))
c.Assert(err, IsNil)
c.Assert(obt.Hash().String(), Equals, exp)
}
}
func getOffsetsFromIdx(r io.Reader) map[plumbing.Hash]int64 {
idx := &idxfile.Idxfile{}
err := idxfile.NewDecoder(r).Decode(idx)
if err != nil {
panic(err)
}
offsets := make(map[plumbing.Hash]int64)
for _, e := range idx.Entries {
offsets[e.Hash] = int64(e.Offset)
}
return offsets
}
|