ObjectsWithPrefix returns the hashes of objects that have the given prefix.
(prefix []byte)
| 348 | |
| 349 | // ObjectsWithPrefix returns the hashes of objects that have the given prefix. |
| 350 | func (d *DotGit) ObjectsWithPrefix(prefix []byte) ([]plumbing.Hash, error) { |
| 351 | // Handle edge cases. |
| 352 | if len(prefix) < 1 { |
| 353 | return d.Objects() |
| 354 | } else if len(prefix) > len(plumbing.ZeroHash) { |
| 355 | return nil, nil |
| 356 | } |
| 357 | |
| 358 | if d.options.ExclusiveAccess { |
| 359 | err := d.genObjectList() |
| 360 | if err != nil { |
| 361 | return nil, err |
| 362 | } |
| 363 | |
| 364 | // Rely on d.objectList being sorted. |
| 365 | // Figure out the half-open interval defined by the prefix. |
| 366 | first := sort.Search(len(d.objectList), func(i int) bool { |
| 367 | // Same as plumbing.HashSlice.Less. |
| 368 | return bytes.Compare(d.objectList[i][:], prefix) >= 0 |
| 369 | }) |
| 370 | lim := len(d.objectList) |
| 371 | if limPrefix, overflow := incBytes(prefix); !overflow { |
| 372 | lim = sort.Search(len(d.objectList), func(i int) bool { |
| 373 | // Same as plumbing.HashSlice.Less. |
| 374 | return bytes.Compare(d.objectList[i][:], limPrefix) >= 0 |
| 375 | }) |
| 376 | } |
| 377 | return d.objectList[first:lim], nil |
| 378 | } |
| 379 | |
| 380 | // This is the slow path. |
| 381 | var objects []plumbing.Hash |
| 382 | var n int |
| 383 | err := d.ForEachObjectHash(func(hash plumbing.Hash) error { |
| 384 | n++ |
| 385 | if bytes.HasPrefix(hash[:], prefix) { |
| 386 | objects = append(objects, hash) |
| 387 | } |
| 388 | return nil |
| 389 | }) |
| 390 | if err != nil { |
| 391 | return nil, err |
| 392 | } |
| 393 | return objects, nil |
| 394 | } |
| 395 | |
| 396 | // Objects returns a slice with the hashes of objects found under the |
| 397 | // .git/objects/ directory. |