Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.8k
get_file_contents fetch refs improvements#1655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
almaleksia
merged 6 commits into
main
from
almaleksia/get_file_contents-fetch-refs-improvementsDec 22, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df21536
get_file_contents improvements
almaleksia cccf36e
Merge branch 'main' of https://github.com/github/github-mcp-server in…
almaleksia f76807b
Return custom errors when main ref is supplied
almaleksia 4bdf0a4
Remove test for short sha
almaleksia 0f9061b
Apply Copilot suggestion
almaleksia 0e1c6e7
Merge branch 'main' into almaleksia/get_file_contents-fetch-refs-impr…
almaleksia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1833,6 +1833,20 @@ func filterPaths(entries []*github.TreeEntry, path string, maxResults int) []str | ||
| return matchedPaths | ||
| } | ||
| // looksLikeSHA returns true if the string appears to be a Git commit SHA. | ||
| // A SHA is a 40-character hexadecimal string. | ||
| func looksLikeSHA(s string) bool { | ||
| if len(s) != 40 { | ||
almaleksia marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return false | ||
| } | ||
| for _, c := range s { | ||
| if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
| // resolveGitReference takes a user-provided ref and sha and resolves them into a | ||
| // definitive commit SHA and its corresponding fully-qualified reference. | ||
| // | ||
| @@ -1841,8 +1855,11 @@ func filterPaths(entries []*github.TreeEntry, path string, maxResults int) []str | ||
| // 1. If a specific commit `sha` is provided, it takes precedence and is used directly, | ||
| // and all reference resolution is skipped. | ||
| // | ||
| // 2. If no `sha` is provided, the function resolves the `ref` | ||
| // string into a fully-qualified format (e.g., "refs/heads/main") by trying | ||
| // 1a. If `sha` is empty but `ref` looks like a commit SHA (40 hexadecimal characters), | ||
| // it is returned as-is without any API calls or reference resolution. | ||
| // | ||
| // 2. If no `sha` is provided and `ref` does not look like a SHA, the function resolves | ||
| // the `ref` string into a fully-qualified format (e.g., "refs/heads/main") by trying | ||
| // the following steps in order: | ||
| // a). **Empty Ref:** If `ref` is empty, the repository's default branch is used. | ||
| // b). **Fully-Qualified:** If `ref` already starts with "refs/", it's considered fully | ||
| @@ -1865,6 +1882,11 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner | ||
| return &raw.ContentOpts{Ref: "", SHA: sha}, nil | ||
| } | ||
| // 1a) If sha is empty but ref looks like a SHA, return it without changes | ||
| if looksLikeSHA(ref) { | ||
| return &raw.ContentOpts{Ref: "", SHA: ref}, nil | ||
| } | ||
| originalRef := ref // Keep original ref for clearer error messages down the line. | ||
| // 2) If no SHA is provided, we try to resolve the ref into a fully-qualified format. | ||
| @@ -1905,8 +1927,12 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner | ||
| // The tag lookup also failed. Check if it was a 404 Not Found error. | ||
| ghErr2, isGhErr2 := err.(*github.ErrorResponse) | ||
| if isGhErr2 && ghErr2.Response.StatusCode == http.StatusNotFound { | ||
| if originalRef == "main" { | ||
| return nil, fmt.Errorf("could not find branch or tag 'main'. Some repositories use 'master' as the default branch name") | ||
| } | ||
almaleksia marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return nil, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef) | ||
| } | ||
| // The tag lookup failed for a different reason. | ||
| _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp, err) | ||
| return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err) | ||
| @@ -1922,6 +1948,9 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner | ||
| if reference == nil { | ||
| reference, resp, err = githubClient.Git.GetRef(ctx, owner, repo, ref) | ||
| if err != nil { | ||
| if ref == "refs/heads/main" { | ||
| return nil, fmt.Errorf("could not find branch 'main'. Some repositories use 'master' as the default branch name") | ||
| } | ||
| _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err) | ||
| return nil, fmt.Errorf("failed to get final reference for %q: %w", ref, err) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.