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