| 1 |
package filesystem |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/hex" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"sync" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/go-git/go-git/v5/plumbing" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/format/idxfile" |
| 15 |
"github.com/go-git/go-git/v5/plumbing/format/objfile" |
| 16 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 17 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 18 |
"github.com/go-git/go-git/v5/storage/filesystem/dotgit" |
| 19 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 20 |
|
| 21 |
"github.com/go-git/go-billy/v5" |
| 22 |
) |
| 23 |
|
| 24 |
type ObjectStorage struct { |
| 25 |
options Options |
| 26 |
|
| 27 |
// objectCache is an object cache uses to cache delta's bases and also recently |
| 28 |
// loaded loose objects |
| 29 |
objectCache cache.Object |
| 30 |
|
| 31 |
dir *dotgit.DotGit |
| 32 |
index map[plumbing.Hash]idxfile.Index |
| 33 |
|
| 34 |
packList []plumbing.Hash |
| 35 |
packListIdx int |
| 36 |
packfiles map[plumbing.Hash]*packfile.Packfile |
| 37 |
} |
| 38 |
|
| 39 |
// NewObjectStorage creates a new ObjectStorage with the given .git directory and cache. |
| 40 |
func NewObjectStorage(dir *dotgit.DotGit, objectCache cache.Object) *ObjectStorage { |
| 41 |
return NewObjectStorageWithOptions(dir, objectCache, Options{}) |
| 42 |
} |
| 43 |
|
| 44 |
// NewObjectStorageWithOptions creates a new ObjectStorage with the given .git directory, cache and extra options |
| 45 |
func NewObjectStorageWithOptions(dir *dotgit.DotGit, objectCache cache.Object, ops Options) *ObjectStorage { |
| 46 |
return &ObjectStorage{ |
| 47 |
options: ops, |
| 48 |
objectCache: objectCache, |
| 49 |
dir: dir, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func (s *ObjectStorage) requireIndex() error { |
| 54 |
if s.index != nil { |
| 55 |
return nil |
| 56 |
} |
| 57 |
|
| 58 |
s.index = make(map[plumbing.Hash]idxfile.Index) |
| 59 |
packs, err := s.dir.ObjectPacks() |
| 60 |
if err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
|
| 64 |
for _, h := range packs { |
| 65 |
if err := s.loadIdxFile(h); err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
// Reindex indexes again all packfiles. Useful if git changed packfiles externally |
| 74 |
func (s *ObjectStorage) Reindex() { |
| 75 |
s.index = nil |
| 76 |
} |
| 77 |
|
| 78 |
func (s *ObjectStorage) loadIdxFile(h plumbing.Hash) (err error) { |
| 79 |
f, err := s.dir.ObjectPackIdx(h) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
|
| 84 |
defer ioutil.CheckClose(f, &err) |
| 85 |
|
| 86 |
idxf := idxfile.NewMemoryIndex() |
| 87 |
d := idxfile.NewDecoder(f) |
| 88 |
if err = d.Decode(idxf); err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
|
| 92 |
if !bytes.Equal(idxf.PackfileChecksum[:], h[:]) { |
| 93 |
return fmt.Errorf("%w: packfile mismatch: target is %q not %q", |
| 94 |
idxfile.ErrMalformedIdxFile, hex.EncodeToString(idxf.PackfileChecksum[:]), h.String()) |
| 95 |
} |
| 96 |
|
| 97 |
s.index[h] = idxf |
| 98 |
return err |
| 99 |
} |
| 100 |
|
| 101 |
func (s *ObjectStorage) NewEncodedObject() plumbing.EncodedObject { |
| 102 |
return &plumbing.MemoryObject{} |
| 103 |
} |
| 104 |
|
| 105 |
func (s *ObjectStorage) PackfileWriter() (io.WriteCloser, error) { |
| 106 |
if err := s.requireIndex(); err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
|
| 110 |
w, err := s.dir.NewObjectPack() |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
|
| 115 |
w.Notify = func(h plumbing.Hash, writer *idxfile.Writer) { |
| 116 |
index, err := writer.Index() |
| 117 |
if err == nil { |
| 118 |
s.index[h] = index |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return w, nil |
| 123 |
} |
| 124 |
|
| 125 |
// SetEncodedObject adds a new object to the storage. |
| 126 |
func (s *ObjectStorage) SetEncodedObject(o plumbing.EncodedObject) (h plumbing.Hash, err error) { |
| 127 |
if o.Type() == plumbing.OFSDeltaObject || o.Type() == plumbing.REFDeltaObject { |
| 128 |
return plumbing.ZeroHash, plumbing.ErrInvalidType |
| 129 |
} |
| 130 |
|
| 131 |
ow, err := s.dir.NewObject() |
| 132 |
if err != nil { |
| 133 |
return plumbing.ZeroHash, err |
| 134 |
} |
| 135 |
|
| 136 |
defer ioutil.CheckClose(ow, &err) |
| 137 |
|
| 138 |
or, err := o.Reader() |
| 139 |
if err != nil { |
| 140 |
return plumbing.ZeroHash, err |
| 141 |
} |
| 142 |
|
| 143 |
defer ioutil.CheckClose(or, &err) |
| 144 |
|
| 145 |
if err = ow.WriteHeader(o.Type(), o.Size()); err != nil { |
| 146 |
return plumbing.ZeroHash, err |
| 147 |
} |
| 148 |
|
| 149 |
if _, err = io.Copy(ow, or); err != nil { |
| 150 |
return plumbing.ZeroHash, err |
| 151 |
} |
| 152 |
|
| 153 |
return o.Hash(), err |
| 154 |
} |
| 155 |
|
| 156 |
// LazyWriter returns a lazy ObjectWriter that is bound to a DotGit file. |
| 157 |
// It first write the header passing on the object type and size, so |
| 158 |
// that the object contents can be written later, without the need to |
| 159 |
// create a MemoryObject and buffering its entire contents into memory. |
| 160 |
func (s *ObjectStorage) LazyWriter() (w io.WriteCloser, wh func(typ plumbing.ObjectType, sz int64) error, err error) { |
| 161 |
ow, err := s.dir.NewObject() |
| 162 |
if err != nil { |
| 163 |
return nil, nil, err |
| 164 |
} |
| 165 |
|
| 166 |
return ow, ow.WriteHeader, nil |
| 167 |
} |
| 168 |
|
| 169 |
// HasEncodedObject returns nil if the object exists, without actually |
| 170 |
// reading the object data from storage. |
| 171 |
func (s *ObjectStorage) HasEncodedObject(h plumbing.Hash) (err error) { |
| 172 |
// Check unpacked objects |
| 173 |
f, err := s.dir.Object(h) |
| 174 |
if err != nil { |
| 175 |
if !os.IsNotExist(err) { |
| 176 |
return err |
| 177 |
} |
| 178 |
// Fall through to check packed objects. |
| 179 |
} else { |
| 180 |
defer ioutil.CheckClose(f, &err) |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
// Check packed objects. |
| 185 |
if err := s.requireIndex(); err != nil { |
| 186 |
return err |
| 187 |
} |
| 188 |
_, _, offset := s.findObjectInPackfile(h) |
| 189 |
if offset == -1 { |
| 190 |
return plumbing.ErrObjectNotFound |
| 191 |
} |
| 192 |
return nil |
| 193 |
} |
| 194 |
|
| 195 |
func (s *ObjectStorage) encodedObjectSizeFromUnpacked(h plumbing.Hash) ( |
| 196 |
size int64, err error, |
| 197 |
) { |
| 198 |
f, err := s.dir.Object(h) |
| 199 |
if err != nil { |
| 200 |
if os.IsNotExist(err) { |
| 201 |
return 0, plumbing.ErrObjectNotFound |
| 202 |
} |
| 203 |
|
| 204 |
return 0, err |
| 205 |
} |
| 206 |
|
| 207 |
r, err := objfile.NewReader(f) |
| 208 |
if err != nil { |
| 209 |
return 0, err |
| 210 |
} |
| 211 |
defer ioutil.CheckClose(r, &err) |
| 212 |
|
| 213 |
_, size, err = r.Header() |
| 214 |
return size, err |
| 215 |
} |
| 216 |
|
| 217 |
func (s *ObjectStorage) packfile(idx idxfile.Index, pack plumbing.Hash) (*packfile.Packfile, error) { |
| 218 |
if p := s.packfileFromCache(pack); p != nil { |
| 219 |
return p, nil |
| 220 |
} |
| 221 |
|
| 222 |
f, err := s.dir.ObjectPack(pack) |
| 223 |
if err != nil { |
| 224 |
return nil, err |
| 225 |
} |
| 226 |
|
| 227 |
var p *packfile.Packfile |
| 228 |
if s.objectCache != nil { |
| 229 |
p = packfile.NewPackfileWithCache(idx, s.dir.Fs(), f, s.objectCache, s.options.LargeObjectThreshold) |
| 230 |
} else { |
| 231 |
p = packfile.NewPackfile(idx, s.dir.Fs(), f, s.options.LargeObjectThreshold) |
| 232 |
} |
| 233 |
|
| 234 |
return p, s.storePackfileInCache(pack, p) |
| 235 |
} |
| 236 |
|
| 237 |
func (s *ObjectStorage) packfileFromCache(hash plumbing.Hash) *packfile.Packfile { |
| 238 |
if s.packfiles == nil { |
| 239 |
if s.options.KeepDescriptors { |
| 240 |
s.packfiles = make(map[plumbing.Hash]*packfile.Packfile) |
| 241 |
} else if s.options.MaxOpenDescriptors > 0 { |
| 242 |
s.packList = make([]plumbing.Hash, s.options.MaxOpenDescriptors) |
| 243 |
s.packfiles = make(map[plumbing.Hash]*packfile.Packfile, s.options.MaxOpenDescriptors) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return s.packfiles[hash] |
| 248 |
} |
| 249 |
|
| 250 |
func (s *ObjectStorage) storePackfileInCache(hash plumbing.Hash, p *packfile.Packfile) error { |
| 251 |
if s.options.KeepDescriptors { |
| 252 |
s.packfiles[hash] = p |
| 253 |
return nil |
| 254 |
} |
| 255 |
|
| 256 |
if s.options.MaxOpenDescriptors <= 0 { |
| 257 |
return nil |
| 258 |
} |
| 259 |
|
| 260 |
// start over as the limit of packList is hit |
| 261 |
if s.packListIdx >= len(s.packList) { |
| 262 |
s.packListIdx = 0 |
| 263 |
} |
| 264 |
|
| 265 |
// close the existing packfile if open |
| 266 |
if next := s.packList[s.packListIdx]; !next.IsZero() { |
| 267 |
open := s.packfiles[next] |
| 268 |
delete(s.packfiles, next) |
| 269 |
if open != nil { |
| 270 |
if err := open.Close(); err != nil { |
| 271 |
return err |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
// cache newly open packfile |
| 277 |
s.packList[s.packListIdx] = hash |
| 278 |
s.packfiles[hash] = p |
| 279 |
s.packListIdx++ |
| 280 |
|
| 281 |
return nil |
| 282 |
} |
| 283 |
|
| 284 |
func (s *ObjectStorage) encodedObjectSizeFromPackfile(h plumbing.Hash) ( |
| 285 |
size int64, err error, |
| 286 |
) { |
| 287 |
if err := s.requireIndex(); err != nil { |
| 288 |
return 0, err |
| 289 |
} |
| 290 |
|
| 291 |
pack, _, offset := s.findObjectInPackfile(h) |
| 292 |
if offset == -1 { |
| 293 |
return 0, plumbing.ErrObjectNotFound |
| 294 |
} |
| 295 |
|
| 296 |
idx := s.index[pack] |
| 297 |
hash, err := idx.FindHash(offset) |
| 298 |
if err == nil { |
| 299 |
obj, ok := s.objectCache.Get(hash) |
| 300 |
if ok { |
| 301 |
return obj.Size(), nil |
| 302 |
} |
| 303 |
} else if err != nil && err != plumbing.ErrObjectNotFound { |
| 304 |
return 0, err |
| 305 |
} |
| 306 |
|
| 307 |
p, err := s.packfile(idx, pack) |
| 308 |
if err != nil { |
| 309 |
return 0, err |
| 310 |
} |
| 311 |
|
| 312 |
if !s.options.KeepDescriptors && s.options.MaxOpenDescriptors == 0 { |
| 313 |
defer ioutil.CheckClose(p, &err) |
| 314 |
} |
| 315 |
|
| 316 |
return p.GetSizeByOffset(offset) |
| 317 |
} |
| 318 |
|
| 319 |
// EncodedObjectSize returns the plaintext size of the given object, |
| 320 |
// without actually reading the full object data from storage. |
| 321 |
func (s *ObjectStorage) EncodedObjectSize(h plumbing.Hash) ( |
| 322 |
size int64, err error, |
| 323 |
) { |
| 324 |
size, err = s.encodedObjectSizeFromUnpacked(h) |
| 325 |
if err != nil && err != plumbing.ErrObjectNotFound { |
| 326 |
return 0, err |
| 327 |
} else if err == nil { |
| 328 |
return size, nil |
| 329 |
} |
| 330 |
|
| 331 |
return s.encodedObjectSizeFromPackfile(h) |
| 332 |
} |
| 333 |
|
| 334 |
// EncodedObject returns the object with the given hash, by searching for it in |
| 335 |
// the packfile and the git object directories. |
| 336 |
func (s *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) { |
| 337 |
var obj plumbing.EncodedObject |
| 338 |
var err error |
| 339 |
|
| 340 |
if s.index != nil { |
| 341 |
obj, err = s.getFromPackfile(h, false) |
| 342 |
if err == plumbing.ErrObjectNotFound { |
| 343 |
obj, err = s.getFromUnpacked(h) |
| 344 |
} |
| 345 |
} else { |
| 346 |
obj, err = s.getFromUnpacked(h) |
| 347 |
if err == plumbing.ErrObjectNotFound { |
| 348 |
obj, err = s.getFromPackfile(h, false) |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// If the error is still object not found, check if it's a shared object |
| 353 |
// repository. |
| 354 |
if err == plumbing.ErrObjectNotFound { |
| 355 |
dotgits, e := s.dir.Alternates() |
| 356 |
if e == nil { |
| 357 |
// Create a new object storage with the DotGit(s) and check for the |
| 358 |
// required hash object. Skip when not found. |
| 359 |
for _, dg := range dotgits { |
| 360 |
o := NewObjectStorage(dg, s.objectCache) |
| 361 |
enobj, enerr := o.EncodedObject(t, h) |
| 362 |
if enerr != nil { |
| 363 |
continue |
| 364 |
} |
| 365 |
return enobj, nil |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
if err != nil { |
| 371 |
return nil, err |
| 372 |
} |
| 373 |
|
| 374 |
if plumbing.AnyObject != t && obj.Type() != t { |
| 375 |
return nil, plumbing.ErrObjectNotFound |
| 376 |
} |
| 377 |
|
| 378 |
return obj, nil |
| 379 |
} |
| 380 |
|
| 381 |
// DeltaObject returns the object with the given hash, by searching for |
| 382 |
// it in the packfile and the git object directories. |
| 383 |
func (s *ObjectStorage) DeltaObject(t plumbing.ObjectType, |
| 384 |
h plumbing.Hash, |
| 385 |
) (plumbing.EncodedObject, error) { |
| 386 |
obj, err := s.getFromUnpacked(h) |
| 387 |
if err == plumbing.ErrObjectNotFound { |
| 388 |
obj, err = s.getFromPackfile(h, true) |
| 389 |
} |
| 390 |
|
| 391 |
if err != nil { |
| 392 |
return nil, err |
| 393 |
} |
| 394 |
|
| 395 |
if plumbing.AnyObject != t && obj.Type() != t { |
| 396 |
return nil, plumbing.ErrObjectNotFound |
| 397 |
} |
| 398 |
|
| 399 |
return obj, nil |
| 400 |
} |
| 401 |
|
| 402 |
func (s *ObjectStorage) getFromUnpacked(h plumbing.Hash) (obj plumbing.EncodedObject, err error) { |
| 403 |
f, err := s.dir.Object(h) |
| 404 |
if err != nil { |
| 405 |
if os.IsNotExist(err) { |
| 406 |
return nil, plumbing.ErrObjectNotFound |
| 407 |
} |
| 408 |
|
| 409 |
return nil, err |
| 410 |
} |
| 411 |
defer ioutil.CheckClose(f, &err) |
| 412 |
|
| 413 |
if cacheObj, found := s.objectCache.Get(h); found { |
| 414 |
return cacheObj, nil |
| 415 |
} |
| 416 |
|
| 417 |
r, err := objfile.NewReader(f) |
| 418 |
if err != nil { |
| 419 |
return nil, err |
| 420 |
} |
| 421 |
|
| 422 |
defer ioutil.CheckClose(r, &err) |
| 423 |
|
| 424 |
t, size, err := r.Header() |
| 425 |
if err != nil { |
| 426 |
return nil, err |
| 427 |
} |
| 428 |
|
| 429 |
if s.options.LargeObjectThreshold > 0 && size > s.options.LargeObjectThreshold { |
| 430 |
obj = dotgit.NewEncodedObject(s.dir, h, t, size) |
| 431 |
return obj, nil |
| 432 |
} |
| 433 |
|
| 434 |
obj = s.NewEncodedObject() |
| 435 |
|
| 436 |
obj.SetType(t) |
| 437 |
obj.SetSize(size) |
| 438 |
w, err := obj.Writer() |
| 439 |
if err != nil { |
| 440 |
return nil, err |
| 441 |
} |
| 442 |
|
| 443 |
defer ioutil.CheckClose(w, &err) |
| 444 |
|
| 445 |
bufp := copyBufferPool.Get().(*[]byte) |
| 446 |
buf := *bufp |
| 447 |
_, err = io.CopyBuffer(w, r, buf) |
| 448 |
copyBufferPool.Put(bufp) |
| 449 |
|
| 450 |
s.objectCache.Put(obj) |
| 451 |
|
| 452 |
return obj, err |
| 453 |
} |
| 454 |
|
| 455 |
var copyBufferPool = sync.Pool{ |
| 456 |
New: func() interface{} { |
| 457 |
b := make([]byte, 32*1024) |
| 458 |
return &b |
| 459 |
}, |
| 460 |
} |
| 461 |
|
| 462 |
// Get returns the object with the given hash, by searching for it in |
| 463 |
// the packfile. |
| 464 |
func (s *ObjectStorage) getFromPackfile(h plumbing.Hash, canBeDelta bool) ( |
| 465 |
plumbing.EncodedObject, error, |
| 466 |
) { |
| 467 |
if err := s.requireIndex(); err != nil { |
| 468 |
return nil, err |
| 469 |
} |
| 470 |
|
| 471 |
pack, hash, offset := s.findObjectInPackfile(h) |
| 472 |
if offset == -1 { |
| 473 |
return nil, plumbing.ErrObjectNotFound |
| 474 |
} |
| 475 |
|
| 476 |
idx := s.index[pack] |
| 477 |
p, err := s.packfile(idx, pack) |
| 478 |
if err != nil { |
| 479 |
return nil, err |
| 480 |
} |
| 481 |
|
| 482 |
if !s.options.KeepDescriptors && s.options.MaxOpenDescriptors == 0 { |
| 483 |
defer ioutil.CheckClose(p, &err) |
| 484 |
} |
| 485 |
|
| 486 |
if canBeDelta { |
| 487 |
return s.decodeDeltaObjectAt(p, offset, hash) |
| 488 |
} |
| 489 |
|
| 490 |
return s.decodeObjectAt(p, offset) |
| 491 |
} |
| 492 |
|
| 493 |
func (s *ObjectStorage) decodeObjectAt( |
| 494 |
p *packfile.Packfile, |
| 495 |
offset int64, |
| 496 |
) (plumbing.EncodedObject, error) { |
| 497 |
hash, err := p.FindHash(offset) |
| 498 |
if err == nil { |
| 499 |
obj, ok := s.objectCache.Get(hash) |
| 500 |
if ok { |
| 501 |
return obj, nil |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
if err != nil && err != plumbing.ErrObjectNotFound { |
| 506 |
return nil, err |
| 507 |
} |
| 508 |
|
| 509 |
return p.GetByOffset(offset) |
| 510 |
} |
| 511 |
|
| 512 |
func (s *ObjectStorage) decodeDeltaObjectAt( |
| 513 |
p *packfile.Packfile, |
| 514 |
offset int64, |
| 515 |
hash plumbing.Hash, |
| 516 |
) (plumbing.EncodedObject, error) { |
| 517 |
scan := p.Scanner() |
| 518 |
header, err := scan.SeekObjectHeader(offset) |
| 519 |
if err != nil { |
| 520 |
return nil, err |
| 521 |
} |
| 522 |
|
| 523 |
var base plumbing.Hash |
| 524 |
|
| 525 |
switch header.Type { |
| 526 |
case plumbing.REFDeltaObject: |
| 527 |
base = header.Reference |
| 528 |
case plumbing.OFSDeltaObject: |
| 529 |
base, err = p.FindHash(header.OffsetReference) |
| 530 |
if err != nil { |
| 531 |
return nil, err |
| 532 |
} |
| 533 |
default: |
| 534 |
return s.decodeObjectAt(p, offset) |
| 535 |
} |
| 536 |
|
| 537 |
obj := &plumbing.MemoryObject{} |
| 538 |
obj.SetType(header.Type) |
| 539 |
w, err := obj.Writer() |
| 540 |
if err != nil { |
| 541 |
return nil, err |
| 542 |
} |
| 543 |
|
| 544 |
if _, _, err := scan.NextObject(w); err != nil { |
| 545 |
return nil, err |
| 546 |
} |
| 547 |
|
| 548 |
return newDeltaObject(obj, hash, base, header.Length), nil |
| 549 |
} |
| 550 |
|
| 551 |
func (s *ObjectStorage) findObjectInPackfile(h plumbing.Hash) (plumbing.Hash, plumbing.Hash, int64) { |
| 552 |
for packfile, index := range s.index { |
| 553 |
offset, err := index.FindOffset(h) |
| 554 |
if err == nil { |
| 555 |
return packfile, h, offset |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
return plumbing.ZeroHash, plumbing.ZeroHash, -1 |
| 560 |
} |
| 561 |
|
| 562 |
// HashesWithPrefix returns all objects with a hash that starts with a prefix by searching for |
| 563 |
// them in the packfile and the git object directories. |
| 564 |
func (s *ObjectStorage) HashesWithPrefix(prefix []byte) ([]plumbing.Hash, error) { |
| 565 |
hashes, err := s.dir.ObjectsWithPrefix(prefix) |
| 566 |
if err != nil { |
| 567 |
return nil, err |
| 568 |
} |
| 569 |
|
| 570 |
seen := hashListAsMap(hashes) |
| 571 |
|
| 572 |
// TODO: This could be faster with some idxfile changes, |
| 573 |
// or diving into the packfile. |
| 574 |
if err := s.requireIndex(); err != nil { |
| 575 |
return nil, err |
| 576 |
} |
| 577 |
for _, index := range s.index { |
| 578 |
ei, err := index.Entries() |
| 579 |
if err != nil { |
| 580 |
return nil, err |
| 581 |
} |
| 582 |
for { |
| 583 |
e, err := ei.Next() |
| 584 |
if err == io.EOF { |
| 585 |
break |
| 586 |
} else if err != nil { |
| 587 |
return nil, err |
| 588 |
} |
| 589 |
if bytes.HasPrefix(e.Hash[:], prefix) { |
| 590 |
if _, ok := seen[e.Hash]; ok { |
| 591 |
continue |
| 592 |
} |
| 593 |
hashes = append(hashes, e.Hash) |
| 594 |
} |
| 595 |
} |
| 596 |
ei.Close() |
| 597 |
} |
| 598 |
|
| 599 |
return hashes, nil |
| 600 |
} |
| 601 |
|
| 602 |
// IterEncodedObjects returns an iterator for all the objects in the packfile |
| 603 |
// with the given type. |
| 604 |
func (s *ObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (storer.EncodedObjectIter, error) { |
| 605 |
objects, err := s.dir.Objects() |
| 606 |
if err != nil { |
| 607 |
return nil, err |
| 608 |
} |
| 609 |
|
| 610 |
seen := make(map[plumbing.Hash]struct{}) |
| 611 |
var iters []storer.EncodedObjectIter |
| 612 |
if len(objects) != 0 { |
| 613 |
iters = append(iters, &objectsIter{s: s, t: t, h: objects}) |
| 614 |
seen = hashListAsMap(objects) |
| 615 |
} |
| 616 |
|
| 617 |
packi, err := s.buildPackfileIters(t, seen) |
| 618 |
if err != nil { |
| 619 |
return nil, err |
| 620 |
} |
| 621 |
|
| 622 |
iters = append(iters, packi) |
| 623 |
return storer.NewMultiEncodedObjectIter(iters), nil |
| 624 |
} |
| 625 |
|
| 626 |
func (s *ObjectStorage) buildPackfileIters( |
| 627 |
t plumbing.ObjectType, |
| 628 |
seen map[plumbing.Hash]struct{}, |
| 629 |
) (storer.EncodedObjectIter, error) { |
| 630 |
if err := s.requireIndex(); err != nil { |
| 631 |
return nil, err |
| 632 |
} |
| 633 |
|
| 634 |
packs, err := s.dir.ObjectPacks() |
| 635 |
if err != nil { |
| 636 |
return nil, err |
| 637 |
} |
| 638 |
return &lazyPackfilesIter{ |
| 639 |
hashes: packs, |
| 640 |
open: func(h plumbing.Hash) (storer.EncodedObjectIter, error) { |
| 641 |
pack, err := s.dir.ObjectPack(h) |
| 642 |
if err != nil { |
| 643 |
return nil, err |
| 644 |
} |
| 645 |
return newPackfileIter( |
| 646 |
s.dir.Fs(), pack, t, seen, s.index[h], |
| 647 |
s.objectCache, s.options.KeepDescriptors, |
| 648 |
s.options.LargeObjectThreshold, |
| 649 |
) |
| 650 |
}, |
| 651 |
}, nil |
| 652 |
} |
| 653 |
|
| 654 |
// Close closes all opened files. |
| 655 |
func (s *ObjectStorage) Close() error { |
| 656 |
var firstError error |
| 657 |
if s.options.KeepDescriptors || s.options.MaxOpenDescriptors > 0 { |
| 658 |
for _, packfile := range s.packfiles { |
| 659 |
err := packfile.Close() |
| 660 |
if firstError == nil && err != nil { |
| 661 |
firstError = err |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
s.packfiles = nil |
| 667 |
s.dir.Close() |
| 668 |
|
| 669 |
return firstError |
| 670 |
} |
| 671 |
|
| 672 |
type lazyPackfilesIter struct { |
| 673 |
hashes []plumbing.Hash |
| 674 |
open func(h plumbing.Hash) (storer.EncodedObjectIter, error) |
| 675 |
cur storer.EncodedObjectIter |
| 676 |
} |
| 677 |
|
| 678 |
func (it *lazyPackfilesIter) Next() (plumbing.EncodedObject, error) { |
| 679 |
for { |
| 680 |
if it.cur == nil { |
| 681 |
if len(it.hashes) == 0 { |
| 682 |
return nil, io.EOF |
| 683 |
} |
| 684 |
h := it.hashes[0] |
| 685 |
it.hashes = it.hashes[1:] |
| 686 |
|
| 687 |
sub, err := it.open(h) |
| 688 |
if err == io.EOF { |
| 689 |
continue |
| 690 |
} else if err != nil { |
| 691 |
return nil, err |
| 692 |
} |
| 693 |
it.cur = sub |
| 694 |
} |
| 695 |
ob, err := it.cur.Next() |
| 696 |
if err == io.EOF { |
| 697 |
it.cur.Close() |
| 698 |
it.cur = nil |
| 699 |
continue |
| 700 |
} else if err != nil { |
| 701 |
return nil, err |
| 702 |
} |
| 703 |
return ob, nil |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
func (it *lazyPackfilesIter) ForEach(cb func(plumbing.EncodedObject) error) error { |
| 708 |
return storer.ForEachIterator(it, cb) |
| 709 |
} |
| 710 |
|
| 711 |
func (it *lazyPackfilesIter) Close() { |
| 712 |
if it.cur != nil { |
| 713 |
it.cur.Close() |
| 714 |
it.cur = nil |
| 715 |
} |
| 716 |
it.hashes = nil |
| 717 |
} |
| 718 |
|
| 719 |
type packfileIter struct { |
| 720 |
pack billy.File |
| 721 |
iter storer.EncodedObjectIter |
| 722 |
seen map[plumbing.Hash]struct{} |
| 723 |
|
| 724 |
// tells whether the pack file should be left open after iteration or not |
| 725 |
keepPack bool |
| 726 |
} |
| 727 |
|
| 728 |
// NewPackfileIter returns a new EncodedObjectIter for the provided packfile |
| 729 |
// and object type. Packfile and index file will be closed after they're |
| 730 |
// used. If keepPack is true the packfile won't be closed after the iteration |
| 731 |
// finished. |
| 732 |
func NewPackfileIter( |
| 733 |
fs billy.Filesystem, |
| 734 |
f billy.File, |
| 735 |
idxFile billy.File, |
| 736 |
t plumbing.ObjectType, |
| 737 |
keepPack bool, |
| 738 |
largeObjectThreshold int64, |
| 739 |
) (storer.EncodedObjectIter, error) { |
| 740 |
idx := idxfile.NewMemoryIndex() |
| 741 |
if err := idxfile.NewDecoder(idxFile).Decode(idx); err != nil { |
| 742 |
return nil, err |
| 743 |
} |
| 744 |
|
| 745 |
if err := idxFile.Close(); err != nil { |
| 746 |
return nil, err |
| 747 |
} |
| 748 |
|
| 749 |
seen := make(map[plumbing.Hash]struct{}) |
| 750 |
return newPackfileIter(fs, f, t, seen, idx, nil, keepPack, largeObjectThreshold) |
| 751 |
} |
| 752 |
|
| 753 |
func newPackfileIter( |
| 754 |
fs billy.Filesystem, |
| 755 |
f billy.File, |
| 756 |
t plumbing.ObjectType, |
| 757 |
seen map[plumbing.Hash]struct{}, |
| 758 |
index idxfile.Index, |
| 759 |
cache cache.Object, |
| 760 |
keepPack bool, |
| 761 |
largeObjectThreshold int64, |
| 762 |
) (storer.EncodedObjectIter, error) { |
| 763 |
var p *packfile.Packfile |
| 764 |
if cache != nil { |
| 765 |
p = packfile.NewPackfileWithCache(index, fs, f, cache, largeObjectThreshold) |
| 766 |
} else { |
| 767 |
p = packfile.NewPackfile(index, fs, f, largeObjectThreshold) |
| 768 |
} |
| 769 |
|
| 770 |
iter, err := p.GetByType(t) |
| 771 |
if err != nil { |
| 772 |
return nil, err |
| 773 |
} |
| 774 |
|
| 775 |
return &packfileIter{ |
| 776 |
pack: f, |
| 777 |
iter: iter, |
| 778 |
seen: seen, |
| 779 |
keepPack: keepPack, |
| 780 |
}, nil |
| 781 |
} |
| 782 |
|
| 783 |
func (iter *packfileIter) Next() (plumbing.EncodedObject, error) { |
| 784 |
for { |
| 785 |
obj, err := iter.iter.Next() |
| 786 |
if err != nil { |
| 787 |
return nil, err |
| 788 |
} |
| 789 |
|
| 790 |
if _, ok := iter.seen[obj.Hash()]; ok { |
| 791 |
continue |
| 792 |
} |
| 793 |
|
| 794 |
return obj, nil |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
func (iter *packfileIter) ForEach(cb func(plumbing.EncodedObject) error) error { |
| 799 |
for { |
| 800 |
o, err := iter.Next() |
| 801 |
if err != nil { |
| 802 |
if err == io.EOF { |
| 803 |
iter.Close() |
| 804 |
return nil |
| 805 |
} |
| 806 |
return err |
| 807 |
} |
| 808 |
|
| 809 |
if err := cb(o); err != nil { |
| 810 |
return err |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
func (iter *packfileIter) Close() { |
| 816 |
iter.iter.Close() |
| 817 |
if !iter.keepPack { |
| 818 |
_ = iter.pack.Close() |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
type objectsIter struct { |
| 823 |
s *ObjectStorage |
| 824 |
t plumbing.ObjectType |
| 825 |
h []plumbing.Hash |
| 826 |
} |
| 827 |
|
| 828 |
func (iter *objectsIter) Next() (plumbing.EncodedObject, error) { |
| 829 |
if len(iter.h) == 0 { |
| 830 |
return nil, io.EOF |
| 831 |
} |
| 832 |
|
| 833 |
obj, err := iter.s.getFromUnpacked(iter.h[0]) |
| 834 |
iter.h = iter.h[1:] |
| 835 |
|
| 836 |
if err != nil { |
| 837 |
return nil, err |
| 838 |
} |
| 839 |
|
| 840 |
if iter.t != plumbing.AnyObject && iter.t != obj.Type() { |
| 841 |
return iter.Next() |
| 842 |
} |
| 843 |
|
| 844 |
return obj, err |
| 845 |
} |
| 846 |
|
| 847 |
func (iter *objectsIter) ForEach(cb func(plumbing.EncodedObject) error) error { |
| 848 |
for { |
| 849 |
o, err := iter.Next() |
| 850 |
if err != nil { |
| 851 |
if err == io.EOF { |
| 852 |
return nil |
| 853 |
} |
| 854 |
return err |
| 855 |
} |
| 856 |
|
| 857 |
if err := cb(o); err != nil { |
| 858 |
return err |
| 859 |
} |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
func (iter *objectsIter) Close() { |
| 864 |
iter.h = []plumbing.Hash{} |
| 865 |
} |
| 866 |
|
| 867 |
func hashListAsMap(l []plumbing.Hash) map[plumbing.Hash]struct{} { |
| 868 |
m := make(map[plumbing.Hash]struct{}, len(l)) |
| 869 |
for _, h := range l { |
| 870 |
m[h] = struct{}{} |
| 871 |
} |
| 872 |
return m |
| 873 |
} |
| 874 |
|
| 875 |
func (s *ObjectStorage) ForEachObjectHash(fun func(plumbing.Hash) error) error { |
| 876 |
err := s.dir.ForEachObjectHash(fun) |
| 877 |
if err == storer.ErrStop { |
| 878 |
return nil |
| 879 |
} |
| 880 |
return err |
| 881 |
} |
| 882 |
|
| 883 |
func (s *ObjectStorage) LooseObjectTime(hash plumbing.Hash) (time.Time, error) { |
| 884 |
fi, err := s.dir.ObjectStat(hash) |
| 885 |
if err != nil { |
| 886 |
return time.Time{}, err |
| 887 |
} |
| 888 |
return fi.ModTime(), nil |
| 889 |
} |
| 890 |
|
| 891 |
func (s *ObjectStorage) DeleteLooseObject(hash plumbing.Hash) error { |
| 892 |
return s.dir.ObjectDelete(hash) |
| 893 |
} |
| 894 |
|
| 895 |
func (s *ObjectStorage) ObjectPacks() ([]plumbing.Hash, error) { |
| 896 |
return s.dir.ObjectPacks() |
| 897 |
} |
| 898 |
|
| 899 |
func (s *ObjectStorage) DeleteOldObjectPackAndIndex(h plumbing.Hash, t time.Time) error { |
| 900 |
return s.dir.DeleteOldObjectPackAndIndex(h, t) |
| 901 |
} |