Skip to content

Add a resolver for getting the default branch - #608

Open
nickborromeo wants to merge 5 commits into
mainfrom
resolve-default-branch
Open

nickborromeo wants to merge 5 commits into
mainfrom
resolve-default-branch

Conversation

@nickborromeo

Copy link
Copy Markdown
Member

The current implementation only considers two paths

  • the result of symbolic-ref
  • a guess between master or main

This change adds a helper resolveDefaultBranch which will add more fallback steps to get the default branch

  • runs the symbolic-ref command
  • runs the ls-remote command to check remote refs
  • uses the GitHub API to get the default branch
  • guesses between main or master as the last fallback

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
Copilot AI max review requested due to automatic review settings October 31, 2026 21:29
@nickborromeo
nickborromeo requested a review from skarim as a code owner October 31, 2026 21:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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
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 local main or master.
  • Integrates the resolver into init and link, 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

Comment thread cmd/link.go
bottomBase := opts.base
if bottomBase == "" {
base, err := git.DefaultBranch()
base, err := resolveDefaultBranch(cfg, opts.remote)
Comment thread cmd/utils.go
Comment on lines +1934 to +1935
if resolved, err := git.ResolveRemote(branch); err == nil {
remote = resolved
Comment thread cmd/utils.go
Comment on lines +1941 to +1942
if client, err := cfg.GitHubClient(); err == nil {
if name, err := client.DefaultBranch(); err == nil && name != "" {
Comment thread internal/git/gitops.go
Comment on lines +254 to +255
cmd.Env = append(cmd.Environ(), "GIT_TERMINAL_PROMPT=0")
if out, err := cmd.Output(); err == nil {
Comment thread internal/git/gitops.go
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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants