selectRepositoryMountCandidate will select the repo which has longest common prefix components as the candidate.
(refspec reference.Spec, sources map[string]string)
| 109 | // selectRepositoryMountCandidate will select the repo which has longest |
| 110 | // common prefix components as the candidate. |
| 111 | func selectRepositoryMountCandidate(refspec reference.Spec, sources map[string]string) string { |
| 112 | u, err := url.Parse("dummy://" + refspec.Locator) |
| 113 | if err != nil { |
| 114 | // NOTE: basically, it won't be error here |
| 115 | return "" |
| 116 | } |
| 117 | |
| 118 | source, target := u.Hostname(), strings.TrimPrefix(u.Path, "/") |
| 119 | repoLabel, ok := sources[distributionSourceLabelKey(source)] |
| 120 | if !ok || repoLabel == "" { |
| 121 | return "" |
| 122 | } |
| 123 | |
| 124 | n, match := 0, "" |
| 125 | components := strings.Split(target, "/") |
| 126 | for repo := range strings.SplitSeq(repoLabel, ",") { |
| 127 | // the target repo is not a candidate |
| 128 | if repo == target { |
| 129 | continue |
| 130 | } |
| 131 | |
| 132 | if l := commonPrefixComponents(components, repo); l >= n { |
| 133 | n, match = l, repo |
| 134 | } |
| 135 | } |
| 136 | return match |
| 137 | } |
| 138 | |
| 139 | func commonPrefixComponents(components []string, target string) int { |
| 140 | targetComponents := strings.Split(target, "/") |
searching dependent graphs…