GitLabgitlab-org
cli
main
1package glrepo
3import (
4 "context"
5 "errors"
6 "fmt"
7 "sort"
8 "strings"
10 "github.com/hashicorp/go-multierror"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
14 "gitlab.com/gitlab-org/cli/internal/api"
15 "gitlab.com/gitlab-org/cli/internal/config"
16 "gitlab.com/gitlab-org/cli/internal/git"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18)
20// cap the number of git remotes looked up, since the user might have an
21// unusually large number of git remotes
22const maxRemotesForLookup = 5
24var (
25 resolverBaseRepoQuestion = "Which should be the base repository (used for e.g. querying issues) for this directory?"
26 resolverHeadRepoQuestion = "Which should be the head repository (where branches are pushed) for this directory?"
27)
29func ResolveRemotesToRepos(remotes Remotes, client *gitlab.Client, defaultHostname string, cfg config.Config) (*ResolvedRemotes, error) {
30 sort.Stable(remotes)
32 result := &ResolvedRemotes{
33 remotes: remotes,
34 apiClient: client,
35 defaultHostname: defaultHostname,
36 cfg: cfg,
37 }
39 return result, nil
40}
42func resolveNetwork(result *ResolvedRemotes) error {
43 // Loop over at most 5 (maxRemotesForLookup)
44 var errs error
45 anySuccess := false
46 for i := 0; i < len(result.remotes) && i < maxRemotesForLookup; i++ {
47 networkResult, err := api.GetProject(result.apiClient, result.remotes[i].FullName())
48 if err == nil {
49 result.network = append(result.network, *networkResult)
50 anySuccess = true
51 } else {
52 errs = multierror.Append(errs, fmt.Errorf("%s: %w", result.remotes[i].FullName(), err))
53 }
54 }
55 if anySuccess {
56 return nil
57 }
58 return errs
59}
61type ResolvedRemotes struct {
62 remotes Remotes
63 network []gitlab.Project
64 apiClient *gitlab.Client
65 defaultHostname string
66 cfg config.Config
67}
69func (r *ResolvedRemotes) BaseRepo(ctx context.Context, ios *iostreams.IOStreams) (Interface, error) {
70 // if any of the remotes already has a resolution, respect that
71 for _, remote := range r.remotes {
72 // Check new ResolvedBase field first
73 if remote.ResolvedBase == "base" {
74 return remote, nil
75 } else if after, ok := strings.CutPrefix(remote.ResolvedBase, "base:"); ok {
76 repo, err := FromFullName(after, r.defaultHostname, r.cfg)
77 if err != nil {
78 return nil, err
79 }
80 return NewWithHost(repo.RepoOwner(), repo.RepoName(), remote.RepoHost()), nil
81 }
83 // Fall back to legacy Resolved field for backward compatibility
84 if remote.Resolved == "base" {
85 return remote, nil
86 } else if after, ok := strings.CutPrefix(remote.Resolved, "base:"); ok {
87 repo, err := FromFullName(after, r.defaultHostname, r.cfg)
88 if err != nil {
89 return nil, err
90 }
91 return NewWithHost(repo.RepoOwner(), repo.RepoName(), remote.RepoHost()), nil
92 } else if remote.Resolved != "" && !strings.HasPrefix(remote.Resolved, "head") {
93 // Backward compatibility kludge for remote-less resolutions created before
94 // BaseRepo started creating resolutions prefixed with `base:`
95 repo, err := FromFullName(remote.Resolved, r.defaultHostname, r.cfg)
96 if err != nil {
97 return nil, err
98 }
99 // Rewrite resolution, ignore the error as this will keep working
100 // in the future we might add a warning that we couldn't rewrite
101 // it for compatibility
102 _ = git.SetRemoteResolution(remote.Name, "base:"+remote.Resolved)
104 return NewWithHost(repo.RepoOwner(), repo.RepoName(), remote.RepoHost()), nil
105 }
106 }
108 if !ios.PromptEnabled() {
109 // we cannot prompt, so just resort to the 1st remote
110 return r.remotes[0], nil
111 }
113 // from here on, consult the API
114 if r.network == nil {
115 err := resolveNetwork(r)
116 if err != nil {
117 return nil, err
118 }
119 if len(r.network) == 0 {
120 return nil, errors.New("no GitLab Projects found from remotes")
121 }
122 }
124 var repoNames []string
125 repoMap := map[string]*gitlab.Project{}
126 add := func(r *gitlab.Project) {
127 // Prefer PathWithNamespace from API over parsing URLs to avoid
128 // issues with split-host setups where config is keyed differently
129 fn := r.PathWithNamespace
130 if fn == "" {
131 fn, _ = FullNameFromURL(r.HTTPURLToRepo)
132 }
133 if _, ok := repoMap[fn]; !ok {
134 repoMap[fn] = r
135 repoNames = append(repoNames, fn)
136 }
137 }
139 for i := range r.network {
140 if r.network[i].ForkedFromProject != nil {
141 fProject, _ := api.GetProject(r.apiClient, r.network[i].ForkedFromProject.PathWithNamespace)
142 add(fProject)
143 }
144 add(&r.network[i])
145 }
147 baseName := repoNames[0]
148 if len(repoNames) > 1 {
149 err := ios.Select(ctx, &baseName, resolverBaseRepoQuestion, repoNames)
150 if err != nil {
151 return nil, err
152 }
153 }
155 // determine corresponding git remote
156 selectedRepo := repoMap[baseName]
157 selectedRepoInfo, _ := FromFullName(selectedRepo.HTTPURLToRepo, r.defaultHostname, r.cfg)
158 resolution := "base"
159 remote, _ := r.RemoteForRepo(selectedRepoInfo)
160 if remote == nil {
161 remote = r.remotes[0]
162 // Prefer PathWithNamespace from API to avoid URL parsing issues
163 resolution = selectedRepo.PathWithNamespace
164 if resolution == "" {
165 resolution, _ = FullNameFromURL(selectedRepo.HTTPURLToRepo)
166 }
167 resolution = "base:" + resolution
168 }
170 // cache the result to git config
171 err := git.SetRemoteResolution(remote.Name, resolution)
173 // Create the final repo object using the remote's host, not the API host
174 remoteHost := remote.RepoHost()
175 if remoteHost == "" {
176 return nil, fmt.Errorf("remote %s has invalid or empty host", remote.Name)
177 }
178 finalRepo := NewWithHost(selectedRepoInfo.RepoOwner(), selectedRepoInfo.RepoName(), remoteHost)
179 return finalRepo, err
180}
182func (r *ResolvedRemotes) HeadRepo(ctx context.Context, ios *iostreams.IOStreams) (Interface, error) {
183 // if any of the remotes already has a resolution, respect that
184 for _, remote := range r.remotes {
185 // Check new ResolvedHead field first
186 if remote.ResolvedHead == "head" {
187 return remote, nil
188 } else if after, ok := strings.CutPrefix(remote.ResolvedHead, "head:"); ok {
189 repo, err := FromFullName(after, r.defaultHostname, r.cfg)
190 if err != nil {
191 return nil, err
192 }
193 return NewWithHost(repo.RepoOwner(), repo.RepoName(), remote.RepoHost()), nil
194 }
196 // Fall back to legacy Resolved field for backward compatibility
197 if remote.Resolved == "head" {
198 return remote, nil
199 } else if after, ok := strings.CutPrefix(remote.Resolved, "head:"); ok {
200 repo, err := FromFullName(after, r.defaultHostname, r.cfg)
201 if err != nil {
202 return nil, err
203 }
204 return NewWithHost(repo.RepoOwner(), repo.RepoName(), remote.RepoHost()), nil
205 }
206 }
208 // from here on, consult the API
209 if r.network == nil {
210 err := resolveNetwork(r)
211 if err != nil {
212 return nil, err
213 }
214 if len(r.network) == 0 {
215 return nil, errors.New("no GitLab Projects found from remotes")
216 }
217 }
219 var repoNames []string
220 repoMap := map[string]*gitlab.Project{}
221 add := func(r *gitlab.Project) {
222 // Prefer PathWithNamespace from API over parsing URLs to avoid
223 // issues with split-host setups where config is keyed differently
224 fn := r.PathWithNamespace
225 if fn == "" {
226 fn, _ = FullNameFromURL(r.HTTPURLToRepo)
227 }
228 if _, ok := repoMap[fn]; !ok {
229 repoMap[fn] = r
230 repoNames = append(repoNames, fn)
231 }
232 }
234 for i := range r.network {
235 if r.network[i].ForkedFromProject != nil {
236 fProject, _ := api.GetProject(r.apiClient, r.network[i].ForkedFromProject.PathWithNamespace)
237 add(fProject)
238 }
239 add(&r.network[i])
240 }
242 headName := repoNames[0]
243 if len(repoNames) > 1 {
244 if !ios.PromptEnabled() {
245 // We cannot prompt so get the first repo that is a fork
246 for _, repo := range repoNames {
247 if repoMap[repo].ForkedFromProject != nil {
248 selectedRepoInfo, _ := FromFullName(repoMap[repo].HTTPURLToRepo, r.defaultHostname, r.cfg)
249 remote, err := r.RemoteForRepo(selectedRepoInfo)
250 if err != nil {
251 continue
252 }
253 return remote, nil
254 }
255 }
256 // No fork with a local remote
257 return r.remotes[0], nil
258 }
260 err := ios.Select(ctx, &headName, resolverHeadRepoQuestion, repoNames)
261 if err != nil {
262 return nil, err
263 }
264 }
266 // determine corresponding git remote
267 selectedRepo := repoMap[headName]
268 selectedRepoInfo, _ := FromFullName(selectedRepo.HTTPURLToRepo, r.defaultHostname, r.cfg)
269 resolution := "head"
270 remote, _ := r.RemoteForRepo(selectedRepoInfo)
271 if remote == nil {
272 remote = r.remotes[0]
273 // Prefer PathWithNamespace from API to avoid URL parsing issues
274 resolution = selectedRepo.PathWithNamespace
275 if resolution == "" {
276 resolution, _ = FullNameFromURL(selectedRepo.HTTPURLToRepo)
277 }
278 resolution = "head:" + resolution
279 }
281 // cache the result to git config
282 err := git.SetRemoteResolution(remote.Name, resolution)
284 // Create the final repo object using the remote's host, not the API host
285 remoteHost := remote.RepoHost()
286 if remoteHost == "" {
287 return nil, fmt.Errorf("remote %s has invalid or empty host", remote.Name)
288 }
289 finalRepo := NewWithHost(selectedRepoInfo.RepoOwner(), selectedRepoInfo.RepoName(), remoteHost)
290 return finalRepo, err
291}
293// RemoteForRepo finds the git remote that points to a repository
294func (r *ResolvedRemotes) RemoteForRepo(repo Interface) (*Remote, error) {
295 for _, remote := range r.remotes {
296 if IsSame(remote, repo) {
297 return remote, nil
298 }
299 }
300 return nil, errors.New("not found")
301}
Diagnostics6 git calls · 37 ms
CallStatusSizeTime037 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=6 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0200314 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=314 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=93a3fb44940eceae81f0a58fd4df1d50fbd7ccb5 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=314 git.pkt.payload.max=287 git.sideband.band1.bytes=287 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=287 git.pack.trailer=0a0d545706824b859dbb83ba13169780ef407392Open pack map
fetch2002.2 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=24 git.response.bytes=2299 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=ca4f2b3561e1002a6a2978348d7ab747d4062b88 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2299 git.pkt.payload.max=2272 git.sideband.band1.bytes=2272 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2c531dc764afc
- Request
- 346ada5e…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format