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

Function resolveExplicitRef

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

262// The returned Ref is always a fully qualified ref (refs/heads/* or refs/tags/*)
263// unless the input resolves to a bare commit SHA.
264func resolveExplicitRef(client *api.Client, host, owner, repo, ref string) (*ResolvedRef, error) {
265 // Handle fully-qualified refs: resolve directly without ambiguity.
266 if after, ok := strings.CutPrefix(ref, "refs/tags/"); ok {
267 return resolveTagRef(client, host, owner, repo, after)
268 }
269 if after, ok := strings.CutPrefix(ref, "refs/heads/"); ok {
270 return resolveBranchRef(client, host, owner, repo, after)
271 }
272
273 // Short name: try branch first, then tag, then commit SHA.
274 // Only fall through on 404 (not found); surface other errors
275 // (403, 500, network) immediately to avoid masking real failures.
276 if resolved, err := resolveBranchRef(client, host, owner, repo, ref); err == nil {
277 return resolved, nil
278 } else if !isNotFound(err) {
279 return nil, err
280 }
281 if resolved, err := resolveTagRef(client, host, owner, repo, ref); err == nil {
282 return resolved, nil
283 } else if !isNotFound(err) {
284 return nil, err
285 }
286
287 commitPath, err := safeurl.JoinPath("repos", owner, repo, "commits", ref)
288 if err != nil {
289 return nil, err
290 }
291 var commitResp struct {
292 SHA string `json:"sha"`
293 }
294 if err := client.REST(host, "GET", commitPath.String(), nil, &commitResp); err == nil {
295 return &ResolvedRef{Ref: commitResp.SHA, SHA: commitResp.SHA}, nil
296 } else if !isNotFound(err) {
297 return nil, err
298 }
299
300 return nil, fmt.Errorf("ref %q not found as branch, tag, or commit in %s/%s", ref, owner, repo)
301}
302
303// resolveTagRef looks up a tag by short name and returns a fully qualified ref.
304// 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