| 26 | } |
| 27 | |
| 28 | func (rr *remoteResolver) Resolver() func() (context.Remotes, error) { |
| 29 | return func() (context.Remotes, error) { |
| 30 | if rr.cachedRemotes != nil || rr.remotesError != nil { |
| 31 | return rr.cachedRemotes, rr.remotesError |
| 32 | } |
| 33 | |
| 34 | gitRemotes, err := rr.readRemotes() |
| 35 | if err != nil { |
| 36 | rr.remotesError = err |
| 37 | return nil, err |
| 38 | } |
| 39 | if len(gitRemotes) == 0 { |
| 40 | rr.remotesError = errors.New("no git remotes found") |
| 41 | return nil, rr.remotesError |
| 42 | } |
| 43 | |
| 44 | sshTranslate := rr.urlTranslator |
| 45 | if sshTranslate == nil { |
| 46 | sshTranslate = ssh.NewTranslator() |
| 47 | } |
| 48 | resolvedRemotes := context.TranslateRemotes(gitRemotes, sshTranslate) |
| 49 | |
| 50 | cfg, err := rr.getConfig() |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | authedHosts := cfg.Authentication().Hosts() |
| 56 | if len(authedHosts) == 0 { |
| 57 | return nil, errors.New("could not find any host configurations") |
| 58 | } |
| 59 | defaultHost, src := cfg.Authentication().DefaultHost() |
| 60 | |
| 61 | // Use set to dedupe list of hosts |
| 62 | hostsSet := set.NewStringSet() |
| 63 | hostsSet.AddValues(authedHosts) |
| 64 | hostsSet.AddValues([]string{defaultHost, ghinstance.Default()}) |
| 65 | hosts := hostsSet.ToSlice() |
| 66 | |
| 67 | // Sort remotes |
| 68 | sort.Sort(resolvedRemotes) |
| 69 | |
| 70 | rr.cachedRemotes = resolvedRemotes.FilterByHosts(hosts) |
| 71 | |
| 72 | // Filter again by default host if one is set |
| 73 | // For config file default host fallback to cachedRemotes if none match |
| 74 | // For environment default host (GH_HOST) do not fallback to cachedRemotes if none match |
| 75 | if src != "default" { |
| 76 | filteredRemotes := rr.cachedRemotes.FilterByHosts([]string{defaultHost}) |
| 77 | if isHostEnv(src) || len(filteredRemotes) > 0 { |
| 78 | rr.cachedRemotes = filteredRemotes |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if len(rr.cachedRemotes) == 0 { |
| 83 | if isHostEnv(src) { |
| 84 | rr.remotesError = fmt.Errorf("none of the git remotes configured for this repository correspond to the %s environment variable. Try adding a matching remote or unsetting the variable", src) |
| 85 | return nil, rr.remotesError |