MCPcopy
hub / github.com/cli/cli / Resolver

Method Resolver

pkg/cmd/factory/remote_resolver.go:28–96  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26}
27
28func (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

Callers 6

Test_BaseRepoFunction · 0.95
Test_SmartBaseRepoFunction · 0.95
Test_OverrideBaseRepoFunction · 0.95
remotesFuncFunction · 0.95
Test_remoteResolverFunction · 0.95

Calls 12

TranslateRemotesFunction · 0.92
NewStringSetFunction · 0.92
DefaultFunction · 0.92
isHostEnvFunction · 0.85
AddValuesMethod · 0.80
FilterByHostsMethod · 0.80
HostsMethod · 0.65
AuthenticationMethod · 0.65
DefaultHostMethod · 0.65
ToSliceMethod · 0.65
ErrorfMethod · 0.65
HasEnvTokenMethod · 0.65

Tested by 5

Test_BaseRepoFunction · 0.76
Test_SmartBaseRepoFunction · 0.76
Test_OverrideBaseRepoFunction · 0.76
Test_remoteResolverFunction · 0.76