Repository navigation
Add a resolver for getting the default branch - #608
Open
nickborromeo wants to merge 5 commits into
Open
nickborromeo wants to merge 5 commits into
nickborromeo wants to merge 5 commits into
Conversation
The current implementation only considers two paths - the result of symbolic-ref - a guess between `master` or `main` This change adds another step to see if a local pointer is missing. This function will be used in the resolveDefaultBranch which adds more steps as fallbacks.
This function will go through different checks to get the default branch - call the DefaultBranch method - call the GitHub API to get the default branch - guess if its `main` or `master` if both of the earlier steps fail
Changes the references of DefaultBranch to take in the new parameter for this function
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Default-branch resolution can block on authentication prompts or select an incorrect trunk or PR base because remote and repository identities are not consistently aligned.
5 open issues
Resolve PR base from the PR repository, not the push remote · New Ignore local-upstream sentinel when resolving remote default branch · New Use the selected remote’s repository for the API fallback · New Disable askpass and SSH prompts for noninteractive remote lookup · New Preserve Unicode whitespace in remote default branch names · New
What changed in this PR
This PR improves default-branch discovery for gh stack, allowing stack initialization and PR linking to handle repositories without a cached remote HEAD or a conventional default-branch name.
Changes:
- Adds a fallback chain: cached remote HEAD,
ls-remote, GitHub API, then localmainormaster. - Integrates the resolver into
initandlink, with updated interfaces and mocks. - Adds resolution tests and updates interface documentation.
| File | Description |
|---|---|
internal/github/mock_client.go |
Adds a mock default-branch lookup. |
internal/github/github.go |
Fetches the repository’s default branch through the API. |
internal/github/client_interface.go |
Exposes the default-branch lookup. |
internal/git/mock_ops.go |
Makes default-branch mocks remote-aware. |
internal/git/gitops.go |
Queries the selected remote’s cached HEAD or remote symbolic HEAD. |
internal/git/gitops_test.go |
Tests remote HEAD discovery and cached-pointer precedence. |
internal/git/git.go |
Updates the wrapper and adds an unresolved-default sentinel. |
cmd/utils.go |
Implements the shared fallback resolver. |
cmd/utils_test.go |
Tests remote selection and fallback order. |
cmd/link.go |
Uses the resolver for the bottom PR’s default base. |
cmd/link_test.go |
Adapts default-base mocks to the new signature. |
cmd/init.go |
Uses the resolver to select the default trunk. |
cmd/init_test.go |
Updates initialization mocks for remote-aware lookup. |
cmd/add_test.go |
Updates mocks used when adding initializes a stack. |
AGENTS.md |
Documents the expanded GitHub client interface. |
.github/copilot-instructions.md |
Updates the documented interface method count. |
Review effort: Max
| bottomBase := opts.base | ||
| if bottomBase == "" { | ||
| base, err := git.DefaultBranch() | ||
| base, err := resolveDefaultBranch(cfg, opts.remote) |
Comment on lines
+1934
to
+1935
| if resolved, err := git.ResolveRemote(branch); err == nil { | ||
| remote = resolved |
Comment on lines
+1941
to
+1942
| if client, err := cfg.GitHubClient(); err == nil { | ||
| if name, err := client.DefaultBranch(); err == nil && name != "" { |
Comment on lines
+254
to
+255
| cmd.Env = append(cmd.Environ(), "GIT_TERMINAL_PROMPT=0") | ||
| if out, err := cmd.Output(); err == nil { |
Comment on lines
+268
to
271
| fields := strings.Fields(line) | ||
| if len(fields) == 3 && fields[0] == "ref:" && fields[2] == "HEAD" { | ||
| return strings.TrimPrefix(fields[1], "refs/heads/") | ||
| } |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

The current implementation only considers two paths
masterormainThis change adds a helper
resolveDefaultBranchwhich will add more fallback steps to get the default branchsymbolic-refcommandls-remotecommand to check remote refsmainormasteras the last fallback