MCPcopy Create free account
hub / github.com/github/gh-aw / resolveRefToSHAViaGit

Function resolveRefToSHAViaGit

pkg/parser/remote_fetch.go:453–504  ·  view source on GitHub ↗

resolveRefToSHAViaGit resolves a git ref to SHA using git ls-remote This is a fallback for when GitHub API authentication fails

(owner, repo, ref, host string)

Source from the content-addressed store, hash-verified

451// resolveRefToSHAViaGit resolves a git ref to SHA using git ls-remote
452// This is a fallback for when GitHub API authentication fails
453func resolveRefToSHAViaGit(owner, repo, ref, host string) (string, error) {
454 remoteLog.Printf("Attempting git ls-remote fallback for ref resolution: %s/%s@%s", owner, repo, ref)
455
456 var githubHost string
457 if host != "" {
458 githubHost = "https://" + host
459 } else {
460 githubHost = GetGitHubHostForRepo(owner, repo)
461 }
462 repoURL := fmt.Sprintf("%s/%s/%s.git", githubHost, owner, repo)
463
464 // Try to resolve the ref using git ls-remote
465 // Format: git ls-remote <repo> <ref>
466 cmd := exec.Command("git", "ls-remote", repoURL, ref)
467 output, err := cmd.Output()
468 if err != nil {
469 // If exact ref doesn't work, try with refs/heads/ and refs/tags/ prefixes
470 for _, prefix := range []string{"refs/heads/", "refs/tags/"} {
471 cmd = exec.Command("git", "ls-remote", repoURL, prefix+ref)
472 output, err = cmd.Output()
473 if err == nil && len(output) > 0 {
474 break
475 }
476 }
477
478 if err != nil {
479 return "", fmt.Errorf("failed to resolve ref via git ls-remote: %w", err)
480 }
481 }
482
483 // Parse the output: "<sha> <ref>"
484 lines := strings.Split(strings.TrimSpace(string(output)), "\n")
485 if len(lines) == 0 || lines[0] == "" {
486 return "", fmt.Errorf("no matching ref found for %s", ref)
487 }
488
489 // Extract SHA from the first line
490 parts := strings.Fields(lines[0])
491 if len(parts) < 1 {
492 return "", errors.New("invalid git ls-remote output format")
493 }
494
495 sha := parts[0]
496
497 // Validate it's a valid SHA
498 if len(sha) != 40 || !gitutil.IsHexString(sha) {
499 return "", fmt.Errorf("invalid SHA format from git ls-remote: %s", sha)
500 }
501
502 remoteLog.Printf("Successfully resolved ref via git ls-remote: %s/%s@%s -> %s", owner, repo, ref, sha)
503 return sha, nil
504}
505
506// resolveRefToSHA resolves a git ref (branch, tag, or SHA) to its commit SHA
507func resolveRefToSHA(owner, repo, ref, host string) (string, error) {

Callers 1

resolveRefToSHAFunction · 0.85

Calls 4

IsHexStringFunction · 0.92
GetGitHubHostForRepoFunction · 0.85
PrintfMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected