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