MCPcopy Create free account
hub / github.com/go-git/go-git / ObjectsWithPrefix

Method ObjectsWithPrefix

storage/filesystem/dotgit/dotgit.go:350–394  ·  view source on GitHub ↗

ObjectsWithPrefix returns the hashes of objects that have the given prefix.

(prefix []byte)

Source from the content-addressed store, hash-verified

348
349// ObjectsWithPrefix returns the hashes of objects that have the given prefix.
350func (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.

Callers 2

HashesWithPrefixMethod · 0.80
testObjectsWithPrefixFunction · 0.80

Calls 5

ObjectsMethod · 0.95
genObjectListMethod · 0.95
ForEachObjectHashMethod · 0.95
incBytesFunction · 0.85
CompareMethod · 0.80

Tested by 1

testObjectsWithPrefixFunction · 0.64