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

Function getHavesFromRef

remote.go:869–919  ·  view source on GitHub ↗

getHavesFromRef populates the given `haves` map with the given reference, and up to `maxHavesToVisitPerRef` ancestor commits.

(
	ref *plumbing.Reference,
	remoteRefs map[plumbing.Hash]bool,
	s storage.Storer,
	haves map[plumbing.Hash]bool,
	depth int,
)

Source from the content-addressed store, hash-verified

867// getHavesFromRef populates the given `haves` map with the given
868// reference, and up to `maxHavesToVisitPerRef` ancestor commits.
869func getHavesFromRef(
870 ref *plumbing.Reference,
871 remoteRefs map[plumbing.Hash]bool,
872 s storage.Storer,
873 haves map[plumbing.Hash]bool,
874 depth int,
875) error {
876 h := ref.Hash()
877 if haves[h] {
878 return nil
879 }
880
881 // No need to load the commit if we know the remote already
882 // has this hash.
883 if remoteRefs[h] {
884 haves[h] = true
885 return nil
886 }
887
888 commit, err := object.GetCommit(s, h)
889 if err != nil {
890 // Ignore the error if this isn't a commit.
891 haves[ref.Hash()] = true
892 return nil
893 }
894
895 // Until go-git supports proper commit negotiation during an
896 // upload pack request, include up to `maxHavesToVisitPerRef`
897 // commits from the history of each ref.
898 walker := object.NewCommitPreorderIter(commit, haves, nil)
899 toVisit := maxHavesToVisitPerRef
900 // But only need up to the requested depth
901 if depth > 0 && depth < maxHavesToVisitPerRef {
902 toVisit = depth
903 }
904 // It is safe to ignore any error here as we are just trying to find the references that we already have
905 // An example of a legitimate failure is we have a shallow clone and don't have the previous commit(s)
906 _ = walker.ForEach(func(c *object.Commit) error {
907 haves[c.Hash] = true
908 toVisit--
909 // If toVisit starts out at 0 (indicating there is no
910 // max), then it will be negative here and we won't stop
911 // early.
912 if toVisit == 0 || remoteRefs[c.Hash] {
913 return storer.ErrStop
914 }
915 return nil
916 })
917
918 return nil
919}
920
921func getHaves(
922 localRefs []*plumbing.Reference,

Callers 1

getHavesFunction · 0.85

Calls 4

GetCommitFunction · 0.92
NewCommitPreorderIterFunction · 0.92
HashMethod · 0.65
ForEachMethod · 0.65

Tested by

no test coverage detected