MCPcopy Create free account
hub / github.com/cli/cli / resolveExplicitRef

Function resolveExplicitRef

internal/skills/discovery/discovery.go:236–273  ·  view source on GitHub ↗

resolveExplicitRef resolves a user-supplied version string. It supports: - fully qualified refs: "refs/tags/v1.0" or "refs/heads/main" - short names: tried as branch first, then tag, then commit SHA - bare SHAs: resolved as commit SHA When a short name matches both a branch and a tag, the branch wi

(client *api.Client, host, owner, repo, ref string)

Source from the content-addressed store, hash-verified

234// The returned Ref is always a fully qualified ref (refs/heads/* or refs/tags/*)
235// unless the input resolves to a bare commit SHA.
236func resolveExplicitRef(client *api.Client, host, owner, repo, ref string) (*ResolvedRef, error) {
237 // Handle fully-qualified refs: resolve directly without ambiguity.
238 if after, ok := strings.CutPrefix(ref, "refs/tags/"); ok {
239 return resolveTagRef(client, host, owner, repo, after)
240 }
241 if after, ok := strings.CutPrefix(ref, "refs/heads/"); ok {
242 return resolveBranchRef(client, host, owner, repo, after)
243 }
244
245 // Short name: try branch first, then tag, then commit SHA.
246 // Only fall through on 404 (not found); surface other errors
247 // (403, 500, network) immediately to avoid masking real failures.
248 if resolved, err := resolveBranchRef(client, host, owner, repo, ref); err == nil {
249 return resolved, nil
250 } else if !isNotFound(err) {
251 return nil, err
252 }
253 if resolved, err := resolveTagRef(client, host, owner, repo, ref); err == nil {
254 return resolved, nil
255 } else if !isNotFound(err) {
256 return nil, err
257 }
258
259 commitPath, err := safeurl.JoinPath("repos", owner, repo, "commits", ref)
260 if err != nil {
261 return nil, err
262 }
263 var commitResp struct {
264 SHA string `json:"sha"`
265 }
266 if err := client.REST(host, "GET", commitPath.String(), nil, &commitResp); err == nil {
267 return &ResolvedRef{Ref: commitResp.SHA, SHA: commitResp.SHA}, nil
268 } else if !isNotFound(err) {
269 return nil, err
270 }
271
272 return nil, fmt.Errorf("ref %q not found as branch, tag, or commit in %s/%s", ref, owner, repo)
273}
274
275// resolveTagRef looks up a tag by short name and returns a fully qualified ref.
276// For annotated tags, the tag object is dereferenced to obtain the commit SHA.

Callers 1

ResolveRefFunction · 0.85

Calls 7

JoinPathFunction · 0.92
resolveTagRefFunction · 0.85
resolveBranchRefFunction · 0.85
isNotFoundFunction · 0.85
RESTMethod · 0.65
StringMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected