@@ -384,6 +384,31 @@ func Test_createRun(t *testing.T) {
384384 },
385385 expectedOut : "https://github.com/OWNER/REPO/pull/12\n " ,
386386 },
387+ {
388+ name : "same head and base branch should error" ,
389+ setup : func (opts * CreateOptions , t * testing.T ) func () {
390+ opts .TitleProvided = true
391+ opts .BodyProvided = true
392+ opts .Title = "my title"
393+ opts .Body = "my body"
394+ opts .HeadBranch = "master"
395+ return func () {}
396+ },
397+ wantErr : `head branch "master" is the same as base branch "master", cannot create a pull request` ,
398+ },
399+ {
400+ name : "same head and base branch with explicit base should error" ,
401+ setup : func (opts * CreateOptions , t * testing.T ) func () {
402+ opts .TitleProvided = true
403+ opts .BodyProvided = true
404+ opts .Title = "my title"
405+ opts .Body = "my body"
406+ opts .HeadBranch = "feature"
407+ opts .BaseBranch = "feature"
408+ return func () {}
409+ },
410+ wantErr : `head branch "feature" is the same as base branch "feature", cannot create a pull request` ,
411+ },
387412 {
388413 name : "dry-run-nontty-with-default-base" ,
389414 tty : false ,
@@ -2879,3 +2904,73 @@ func TestProjectsV1Deprecation(t *testing.T) {
28792904 })
28802905 })
28812906}
2907+
2908+ func Test_isSameRef (t * testing.T ) {
2909+ tests := []struct {
2910+ name string
2911+ refs creationRefs
2912+ expected bool
2913+ }{
2914+ {
2915+ name : "same branch in same repo" ,
2916+ refs : skipPushRefs {
2917+ qualifiedHeadRef : shared .NewQualifiedHeadRefWithoutOwner ("main" ),
2918+ baseRefs : baseRefs {
2919+ baseBranchName : "main" ,
2920+ },
2921+ },
2922+ expected : true ,
2923+ },
2924+ {
2925+ name : "different branches in same repo" ,
2926+ refs : skipPushRefs {
2927+ qualifiedHeadRef : shared .NewQualifiedHeadRefWithoutOwner ("feature" ),
2928+ baseRefs : baseRefs {
2929+ baseBranchName : "main" ,
2930+ },
2931+ },
2932+ expected : false ,
2933+ },
2934+ {
2935+ name : "same branch name in different repos (cross-repo PR)" ,
2936+ refs : skipPushRefs {
2937+ qualifiedHeadRef : shared .NewQualifiedHeadRef ("other-owner" , "main" ),
2938+ baseRefs : baseRefs {
2939+ baseBranchName : "main" ,
2940+ },
2941+ },
2942+ expected : false ,
2943+ },
2944+ {
2945+ name : "pushableRefs same branch same repo" ,
2946+ refs : pushableRefs {
2947+ headRepo : ghrepo .New ("OWNER" , "REPO" ),
2948+ headBranchName : "main" ,
2949+ baseRefs : baseRefs {
2950+ baseRepo : api .InitRepoHostname (& api.Repository {Name : "REPO" , Owner : api.RepositoryOwner {Login : "OWNER" }}, "github.com" ),
2951+ baseBranchName : "main" ,
2952+ },
2953+ },
2954+ expected : true ,
2955+ },
2956+ {
2957+ name : "pushableRefs same branch different repos (fork)" ,
2958+ refs : pushableRefs {
2959+ headRepo : ghrepo .New ("FORK-OWNER" , "REPO" ),
2960+ headBranchName : "main" ,
2961+ baseRefs : baseRefs {
2962+ baseRepo : api .InitRepoHostname (& api.Repository {Name : "REPO" , Owner : api.RepositoryOwner {Login : "OWNER" }}, "github.com" ),
2963+ baseBranchName : "main" ,
2964+ },
2965+ },
2966+ expected : false ,
2967+ },
2968+ }
2969+
2970+ for _ , tt := range tests {
2971+ t .Run (tt .name , func (t * testing.T ) {
2972+ result := isSameRef (tt .refs )
2973+ assert .Equal (t , tt .expected , result )
2974+ })
2975+ }
2976+ }
0 commit comments