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
|
package filesystem
import (
"io"
"os"
"srcd.works/go-git.v4/plumbing"
"srcd.works/go-git.v4/plumbing/format/idxfile"
"srcd.works/go-git.v4/plumbing/format/objfile"
"srcd.works/go-git.v4/plumbing/format/packfile"
"srcd.works/go-git.v4/plumbing/storer"
"srcd.works/go-git.v4/storage/filesystem/internal/dotgit"
"srcd.works/go-git.v4/storage/memory"
"srcd.works/go-billy.v1"
)
type ObjectStorage struct {
dir *dotgit.DotGit
index map[plumbing.Hash]idx
}
func newObjectStorage(dir *dotgit.DotGit) (ObjectStorage, error) {
s := ObjectStorage{
dir: dir,
index: make(map[plumbing.Hash]idx, 0),
}
return s, s.loadIdxFiles()
}
func (s *ObjectStorage) loadIdxFiles() error {
packs, err := s.dir.ObjectPacks()
if err != nil {
return err
}
for _, h := range packs {
if err := s.loadIdxFile(h); err != nil {
return err
}
}
return nil
}
func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) error {
idxfile, err := s.dir.ObjectPackIdx(h)
if err != nil {
return err
}
s.index[h] = make(idx)
return s.index[h].Decode(idxfile)
}
func (s *ObjectStorage) NewEncodedObject() plumbing.EncodedObject {
return &plumbing.MemoryObject{}
}
func (s *ObjectStorage) PackfileWriter() (io.WriteCloser, error) {
w, err := s.dir.NewObjectPack()
if err != nil {
return nil, err
}
w.Notify = func(h plumbing.Hash, idxfile idxfile.Idxfile) {
s.index[h] = make(idx)
for _, e := range idxfile.Entries {
s.index[h][e.Hash] = int64(e.Offset)
}
}
return w, nil
}
// SetEncodedObject adds a new object to the storage.
func (s *ObjectStorage) SetEncodedObject(o plumbing.EncodedObject) (plumbing.Hash, error) {
if o.Type() == plumbing.OFSDeltaObject || o.Type() == plumbing.REFDeltaObject {
return plumbing.ZeroHash, plumbing.ErrInvalidType
}
ow, err := s.dir.NewObject()
if err != nil {
return plumbing.ZeroHash, err
}
defer ow.Close()
or, err := o.Reader()
if err != nil {
return plumbing.ZeroHash, err
}
defer or.Close()
if err := ow.WriteHeader(o.Type(), o.Size()); err != nil {
return plumbing.ZeroHash, err
}
if _, err := io.Copy(ow, or); err != nil {
return plumbing.ZeroHash, err
}
return o.Hash(), nil
}
// EncodedObject returns the object with the given hash, by searching for it in
// the packfile and the git object directories.
func (s *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
obj, err := s.getFromUnpacked(h)
if err == plumbing.ErrObjectNotFound {
obj, err = s.getFromPackfile(h)
}
if err != nil {
return nil, err
}
if plumbing.AnyObject != t && obj.Type() != t {
return nil, plumbing.ErrObjectNotFound
}
return obj, nil
}
func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedObject, err error) {
f, err := s.dir.Object(h)
if err != nil {
if os.IsNotExist(err) {
return nil, plumbing.ErrObjectNotFound
}
return nil, err
}
defer f.Close()
obj = s.NewEncodedObject()
r, err := objfile.NewReader(f)
if err != nil {
return nil, err
}
defer r.Close()
t, size, err := r.Header()
if err != nil {
return nil, err
}
obj.SetType(t)
obj.SetSize(size)
w, err := obj.Writer()
if err != nil {
return nil, err
}
_, err = io.Copy(w, r)
return obj, err
}
// Get returns the object with the given hash, by searching for it in
// the packfile.
func (s *ObjectStorage) getFromPackfile(h plumbing.Hash) (plumbing.EncodedObject, error) {
pack, offset := s.findObjectInPackfile(h)
if offset == -1 {
return nil, plumbing.ErrObjectNotFound
}
f, err := s.dir.ObjectPack(pack)
if err != nil {
return nil, err
}
defer f.Close()
p := packfile.NewScanner(f)
d, err := packfile.NewDecoder(p, memory.NewStorage())
if err != nil {
return nil, err
}
d.SetOffsets(s.index[pack])
return d.DecodeObjectAt(offset)
}
func (s *ObjectStorage) findObjectInPackfile(h plumbing.Hash) (plumbing.Hash, int64) {
for packfile, index := range s.index {
if offset, ok := index[h]; ok {
return packfile, offset
}
}
return plumbing.ZeroHash, -1
}
// IterEncodedObjects returns an iterator for all the objects in the packfile
// with the given type.
func (s *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.EncodedObjectIter, error) {
objects, err := s.dir.Objects()
if err != nil {
return nil, err
}
seen := make(map[plumbing.Hash]bool, 0)
var iters []storer.EncodedObjectIter
if len(objects) != 0 {
iters = append(iters, &objectsIter{s: s, t: t, h: objects})
seen = hashListAsMap(objects)
}
packi, err := s.buildPackfileIters(t, seen)
if err != nil {
return nil, err
}
iters = append(iters, packi...)
return storer.NewMultiEncodedObjectIter(iters), nil
}
func (s *ObjectStorage) buildPackfileIters(
t plumbing.ObjectType, seen map[plumbing.Hash]bool) ([]storer.EncodedObjectIter, error) {
packs, err := s.dir.ObjectPacks()
if err != nil {
return nil, err
}
var iters []storer.EncodedObjectIter
for _, h := range packs {
pack, err := s.dir.ObjectPack(h)
if err != nil {
return nil, err
}
iter, err := newPackfileIter(pack, t, seen)
if err != nil {
return nil, err
}
iters = append(iters, iter)
}
return iters, nil
}
type idx map[plumbing.Hash]int64
func (i idx) Decode(r io.Reader) error {
idx := &idxfile.Idxfile{}
d := idxfile.NewDecoder(r)
if err := d.Decode(idx); err != nil {
return err
}
for _, e := range idx.Entries {
i[e.Hash] = int64(e.Offset)
}
return nil
}
type packfileIter struct {
f billy.File
d *packfile.Decoder
t plumbing.ObjectType
seen map[plumbing.Hash]bool
position uint32
total uint32
}
func NewPackfileIter(f billy.File, t plumbing.ObjectType) (storer.EncodedObjectIter, error) {
return newPackfileIter(f, t, make(map[plumbing.Hash]bool))
}
func newPackfileIter(f billy.File, t plumbing.ObjectType, seen map[plumbing.Hash]bool) (storer.EncodedObjectIter, error) {
s := packfile.NewScanner(f)
_, total, err := s.Header()
if err != nil {
return nil, err
}
d, err := packfile.NewDecoderForType(s, memory.NewStorage(), t)
if err != nil {
return nil, err
}
return &packfileIter{
f: f,
d: d,
t: t,
total: total,
seen: seen,
}, nil
}
func (iter *packfileIter) Next() (plumbing.EncodedObject, error) {
for {
if iter.position >= iter.total {
return nil, io.EOF
}
obj, err := iter.d.DecodeObject()
if err != nil {
return nil, err
}
iter.position++
if obj == nil {
continue
}
if iter.seen[obj.Hash()] {
return iter.Next()
}
return obj, nil
}
}
// ForEach is never called since is used inside of a MultiObjectIterator
func (iter *packfileIter) ForEach(cb func(plumbing.EncodedObject) error) error {
return nil
}
func (iter *packfileIter) Close() {
iter.f.Close()
iter.d.Close()
}
type objectsIter struct {
s *ObjectStorage
t plumbing.ObjectType
h []plumbing.Hash
}
func (iter *objectsIter) Next() (plumbing.EncodedObject, error) {
if len(iter.h) == 0 {
return nil, io.EOF
}
obj, err := iter.s.getFromUnpacked(iter.h[0])
iter.h = iter.h[1:]
if err != nil {
return nil, err
}
if iter.t != plumbing.AnyObject && iter.t != obj.Type() {
return iter.Next()
}
return obj, err
}
// ForEach is never called since is used inside of a MultiObjectIterator
func (iter *objectsIter) ForEach(cb func(plumbing.EncodedObject) error) error {
return nil
}
func (iter *objectsIter) Close() {
iter.h = []plumbing.Hash{}
}
func hashListAsMap(l []plumbing.Hash) map[plumbing.Hash]bool {
m := make(map[plumbing.Hash]bool, len(l))
for _, h := range l {
m[h] = true
}
return m
}
|