Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fallback to default branch in get_file_contents when main doesn't exist#1669

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
almaleksia wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromalmaleksia/auto-resolve-default-branch

Conversation

@almaleksia
Copy link
Contributor

Summary

Follow up for#1655
When LLM passesmain as ref toget_file_contents and it doesn't exist - auto-resolve default repo branch instead.

Why

When main branch is passed we assume the intent to get file content from the default branch to avoid unnecessary failure and as a result - unnecessary requests to LLM.

The note that branch was changed is added in the success message.

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
  • New tool added

Prompts tested (tool changes only)

Security / limits

  • No security or limits impact
  • Auth / permissions considered
  • Data exposure, filtering, or token/size limits considered

Lint & tests

  • Linted locally with./script/lint
  • Tested locally with./script/test

Docs

  • Not needed
  • Updated (README / docs / examples)

@almaleksiaalmaleksia requested a review froma team as acode ownerDecember 22, 2025 11:42
CopilotAI review requested due to automatic review settingsDecember 22, 2025 11:42
Copy link
Contributor

CopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Pull request overview

This PR implements automatic fallback to the default branch when the LLM passes "main" as the ref parameter toget_file_contents and that branch doesn't exist. The change helps avoid unnecessary failures and reduces round-trips to the LLM by auto-resolving to the repository's default branch. A note is added to the success message to inform the user that the ref was changed.

Key changes:

  • AddedresolveDefaultBranch helper function to fetch the repository's default branch reference
  • ModifiedresolveGitReference to fallback to the default branch when "main" doesn't exist
  • Updated success messages inGetFileContents to include a note when fallback occurs

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

FileDescription
pkg/github/repositories.goAddedresolveDefaultBranch function and fallback logic inresolveGitReference; added success note when ref is changed
pkg/github/repositories_test.goAdded test case to verify fallback behavior and success note message

Comment on lines +1991 to +1995
deferfunc() {_=resp.Body.Close() }()
iferr!=nil {
_,_=ghErrors.NewGitHubAPIErrorToCtx(ctx,"failed to get default branch reference",resp,err)
returnnil,fmt.Errorf("failed to get default branch reference: %w",err)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The defer statement to close the response body is placed before the error check. If the API call fails and returns a nil response, this will cause a nil pointer dereference panic when the deferred function executes. The defer should be placed after the error check, following the pattern used elsewhere in this file (e.g., line 1158).

Suggested change
deferfunc() {_=resp.Body.Close() }()
iferr!=nil {
_,_=ghErrors.NewGitHubAPIErrorToCtx(ctx,"failed to get default branch reference",resp,err)
returnnil,fmt.Errorf("failed to get default branch reference: %w",err)
}
iferr!=nil {
_,_=ghErrors.NewGitHubAPIErrorToCtx(ctx,"failed to get default branch reference",resp,err)
returnnil,fmt.Errorf("failed to get default branch reference: %w",err)
}
deferfunc() {_=resp.Body.Close() }()

Copilot uses AI. Check for mistakes.
iferr!=nil {
_,_=ghErrors.NewGitHubAPIErrorToCtx(ctx,"failed to get repository info",resp,err)
returnnil,fmt.Errorf("failed to get repository info: %w",err)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The response body from the Repositories.Get call is not being closed, which could lead to a resource leak. Add a defer statement to close resp.Body after the error check, similar to the pattern at line 1158 in this file.

Suggested change
}
}
deferfunc() {_=resp.Body.Close() }()

Copilot uses AI. Check for mistakes.
Comment on lines +754 to +755
if!strings.HasSuffix(rawOpts.Ref,originalRef) {
successNote=fmt.Sprintf(" Note: the provided ref '%s' does not exist, default branch '%s' was used instead.",originalRef,rawOpts.Ref)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The logic to determine if a fallback occurred uses HasSuffix, which could produce false negatives. If the default branch name happens to end with the original ref value (e.g., originalRef is "main" and the default branch is "whatevermain"), the note won't be shown even though a fallback occurred. Consider using a more precise check, such as comparing if rawOpts.Ref equals the expected ref format, or store a boolean flag during resolution to indicate when a fallback happens.

Suggested change
if!strings.HasSuffix(rawOpts.Ref,originalRef) {
successNote=fmt.Sprintf(" Note: the provided ref '%s' does not exist, default branch '%s' was used instead.",originalRef,rawOpts.Ref)
iforiginalRef!=""&&rawOpts.Ref!="" {
sameAsOriginal:=rawOpts.Ref==originalRef||rawOpts.Ref==fmt.Sprintf("refs/heads/%s",originalRef)
if!sameAsOriginal {
successNote=fmt.Sprintf(" Note: the provided ref '%s' does not exist, default branch '%s' was used instead.",originalRef,rawOpts.Ref)
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@tommaso-morotommaso-moro left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Great improvement! I love the idea of falling back to the default branch whenmain doesn't exist.

The only concern I have here is about maintainability. Specifically, here:

if !strings.HasSuffix(rawOpts.Ref, originalRef) {successNote = fmt.Sprintf(" Note: the provided ref '%s' does not exist, default branch '%s' was used instead.", originalRef, rawOpts.Ref)}

We are assuming that if!strings.HasSuffix(rawOpts.Ref, originalRef) is true then itmust be due to the fact that we used the fallback logic. This might work today because we know the only case where the resolved ref wouldn't end with the original ref is the fallback, but it introduces implicit coupling: for example, if we ever changeresolveGitReference to transform refs in other ways (e.g. lowercasing) the suffix check could fail even when no fallback was used.

And I think we might run into weird edge cases too with the current logic: for example if a user passesmain and the default branch issomething-main the fallback would be used, but no note would be generated even though a fallback occurred.

Given this I wonder if we should make the logic here more explicit. For example theresolveGitReference function could return an explicit signal when a fallback has been used. That way we could do an explicit check likeif result.FallbackOccurred orif result.FallbackRef != "" rather than inferring the fallback from the string comparisons.

What do you think?


// main branch ref passed in ref parameter but it doesn't exist - default branch was used
varsuccessNotestring
if!strings.HasSuffix(rawOpts.Ref,originalRef) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

To me this seems like a fragile check, it doesn't account for edge cases (e.g. if the user passesmain and the default branch is calledsomething-main) and I am worried that if we changeresolveGitReference we need to update this check here too as it seems quite coupled to the internal implementation ofresolveGitReference. Wdyt?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@tommaso-morotommaso-morotommaso-moro left review comments

Copilot code reviewCopilotCopilot left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@almaleksia@tommaso-moro

[8]ページ先頭

©2009-2025 Movatter.jp