Skip to content

[Bug]: link pushes a local branch named like a PR number before resolving that PR #650

Backlog

Description

@ediewilson

Preflight

  • I searched existing issues and did not find a duplicate
  • I reproduced this issue on the latest stable release (v0.3.0), via a failing unit test (below)

Describe the bug

In cmd/link.go, runLink calls pushBranchArgs (L139) before findExistingPRs (L145). pushBranchArgs pushes every argument for which git.BranchExists(arg) is true, and that includes a numeric argument that names an existing PR. Numbers are only resolved through FindPRByNumber later (L559-574). So when PR #N exists and a local branch is also named N, gh stack link N M pushes that local branch.

The documented behavior is that a number resolves to an existing PR first and falls back to a branch only when no PR exists. The push phase doesn't follow that. Possible effects: a link meant to change only metadata creates or updates a remote branch named N, or fails when that push is rejected. We don't claim a force-push.

Links, pinned to v0.3.0:

main is unchanged at these lines as of 2026-11-27.

Affected command: gh stack link · gh-stack version: 0.3.0 · GitHub CLI: 2.85.0 · OS: macOS 26.6 · Shell: zsh

Steps to reproduce

Reproduced with a unit test against v0.3.0 (2a49e67), using the existing newLinkGitMock and github.MockClient helpers in cmd/link_test.go. Setup: PRs #10 and #20 exist, local branch 10 exists, and the command runs link --base main 10 20. The test records every branch passed to git.Push:

func TestLink_NumericArgNamingExistingPR_DoesNotPushSameNamedLocalBranch(t *testing.T) {
	pushed := linkPushRecorder(t, []string{"10"}, map[int]bool{10: true, 20: true}, []string{"10", "20"})
	assert.Empty(t, pushed, "PR #10 exists, so arg \"10\" resolves to the PR; local branch \"10\" must not be pushed")
}

linkPushRecorder wraps newLinkGitMock(localBranches...) with a recording PushFn. Its FindPRByNumberFn returns a PR only for the given numbers. It then calls runLink(cfg, &linkOptions{base: "main"}, args). Two control tests pass on v0.3.0 and should keep passing:

  • A plain branch arg (feature) is pushed.
  • A number with no PR falls back to a branch and is pushed.

Happy to share the full test file or open a PR with it.

Real-world equivalent: two open PRs N and M, plus a local branch literally named N whose commits are unrelated to PR N's head. Run gh stack link --base main N M.

Expected behavior

An argument that resolves to an existing PR triggers no push. A number with no PR keeps the documented branch fallback. A plain branch name is still pushed, and PR URLs are never pushed.

Actual behavior

git.Push receives branch 10 before PR #10 is looked up.

Terminal output

=== RUN   TestLink_NumericArgNamingExistingPR_DoesNotPushSameNamedLocalBranch
        	Error:      	Should be empty, but was [10]
--- FAIL: TestLink_NumericArgNamingExistingPR_DoesNotPushSameNamedLocalBranch (0.00s)
=== RUN   TestLink_PlainBranchArg_IsPushed
--- PASS: TestLink_PlainBranchArg_IsPushed (0.00s)
=== RUN   TestLink_NumericArgWithNoPR_FallsBackToBranchAndIsPushed
--- PASS: TestLink_NumericArgWithNoPR_FallsBackToBranchAndIsPushed (0.00s)
FAIL	github.com/github/gh-stack/cmd	0.362s

Suggested fix

Resolve numeric arguments with FindPRByNumber before collecting the branches to push, and skip any argument that resolves to an existing PR. The test above, with its two controls, can serve as the regression test. In the meantime we work around it by passing PR URLs instead of numbers.


Disclosure: this issue was drafted by Claude (Anthropic's AI assistant, via Claude Code) on behalf of @ediewilson, who reviewed it and takes responsibility for it.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions