@@ -1177,6 +1177,51 @@ func TestLink_NumericArg_PRNotFound_TreatedAsBranch(t *testing.T) {
11771177 assert .Equal (t , []int {50 , 51 }, stackedPRs )
11781178}
11791179
1180+ func TestLink_NumericFirstArgIsLocalBranch_NotAddMode (t * testing.T ) {
1181+ // Branch "123" exists locally and stack #123 also exists. The numeric
1182+ // branch name must win over add mode, so the args form a new stack rather
1183+ // than appending #456 to the unrelated stack #123.
1184+ restore := git .SetOps (newLinkGitMock ("123" , "456" ))
1185+ defer restore ()
1186+
1187+ var createdPRs []int
1188+ cfg , _ , _ := config .NewTestConfig ()
1189+ cfg .GitHubClientOverride = & github.MockClient {
1190+ FindPRByNumberFn : func (int ) (* github.PullRequest , error ) { return nil , nil },
1191+ FindPRForBranchFn : func (branch string ) (* github.PullRequest , error ) {
1192+ switch branch {
1193+ case "123" :
1194+ return & github.PullRequest {Number : 50 , HeadRefName : "123" , BaseRefName : "main" , URL : "https://github.com/o/r/pull/50" }, nil
1195+ case "456" :
1196+ return & github.PullRequest {Number : 51 , HeadRefName : "456" , BaseRefName : "123" , URL : "https://github.com/o/r/pull/51" }, nil
1197+ }
1198+ return nil , nil
1199+ },
1200+ ListStacksFn : func () ([]github.RemoteStack , error ) {
1201+ return []github.RemoteStack {
1202+ linkRemoteStack (123 , linkPR (90 , "unrelated-a" ), linkPR (91 , "unrelated-b" )),
1203+ }, nil
1204+ },
1205+ AddToStackFn : func (int , []int ) (* github.RemoteStack , error ) {
1206+ t .Fatal ("AddToStack must not be called: a numeric branch name should not trigger add mode" )
1207+ return nil , nil
1208+ },
1209+ CreateStackFn : func (prNumbers []int ) (* github.RemoteStack , error ) {
1210+ createdPRs = prNumbers
1211+ return & github.RemoteStack {ID : 42 , Number : 42 }, nil
1212+ },
1213+ }
1214+
1215+ cmd := LinkCmd (cfg )
1216+ cmd .SetArgs ([]string {"123" , "456" })
1217+ cmd .SetOut (io .Discard )
1218+ cmd .SetErr (io .Discard )
1219+ err := cmd .Execute ()
1220+
1221+ assert .NoError (t , err )
1222+ assert .Equal (t , []int {50 , 51 }, createdPRs )
1223+ }
1224+
11801225func TestLink_FixesBaseBranches (t * testing.T ) {
11811226 restore := git .SetOps (newLinkGitMock ("feat-a" , "feat-b" ))
11821227 defer restore ()
0 commit comments