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

Function resolveExplicitRef

internal/skills/discovery/discovery.go:235–272  ·  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

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

Tested by

no test coverage detected