@@ -28,11 +28,12 @@ type progressIndicator interface {
2828}
2929
3030type finder struct {
31- baseRepoFn func () (ghrepo.Interface , error )
32- branchFn func () (string , error )
33- remotesFn func () (context.Remotes , error )
34- httpClient func () (* http.Client , error )
35- progress progressIndicator
31+ baseRepoFn func () (ghrepo.Interface , error )
32+ branchFn func () (string , error )
33+ remotesFn func () (context.Remotes , error )
34+ httpClient func () (* http.Client , error )
35+ branchConfig func (string ) git.BranchConfig
36+ progress progressIndicator
3637
3738 repo ghrepo.Interface
3839 prNumber int
@@ -47,11 +48,12 @@ func NewFinder(factory *cmdutil.Factory) PRFinder {
4748 }
4849
4950 return & finder {
50- baseRepoFn : factory .BaseRepo ,
51- branchFn : factory .Branch ,
52- remotesFn : factory .Remotes ,
53- httpClient : factory .HttpClient ,
54- progress : factory .IOStreams ,
51+ baseRepoFn : factory .BaseRepo ,
52+ branchFn : factory .Branch ,
53+ remotesFn : factory .Remotes ,
54+ httpClient : factory .HttpClient ,
55+ progress : factory .IOStreams ,
56+ branchConfig : git .ReadBranchConfig ,
5557 }
5658}
5759
@@ -79,7 +81,10 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err
7981 return nil , nil , errors .New ("Find error: no fields specified" )
8082 }
8183
82- _ = f .parseURL (opts .Selector )
84+ if repo , prNumber , err := f .parseURL (opts .Selector ); err == nil {
85+ f .prNumber = prNumber
86+ f .repo = repo
87+ }
8388
8489 if f .repo == nil {
8590 repo , err := f .baseRepoFn ()
@@ -90,8 +95,12 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err
9095 }
9196
9297 if opts .Selector == "" {
93- if err := f .parseCurrentBranch (); err != nil {
98+ if branch , prNumber , err := f .parseCurrentBranch (); err != nil {
9499 return nil , nil , err
100+ } else if prNumber > 0 {
101+ f .prNumber = prNumber
102+ } else {
103+ f .branchName = branch
95104 }
96105 } else if f .prNumber == 0 {
97106 if prNumber , err := strconv .Atoi (strings .TrimPrefix (opts .Selector , "#" )); err == nil {
@@ -129,44 +138,44 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err
129138
130139var pullURLRE = regexp .MustCompile (`^/([^/]+)/([^/]+)/pull/(\d+)` )
131140
132- func (f * finder ) parseURL (prURL string ) error {
141+ func (f * finder ) parseURL (prURL string ) (ghrepo. Interface , int , error ) {
133142 if prURL == "" {
134- return fmt .Errorf ("invalid URL: %q" , prURL )
143+ return nil , 0 , fmt .Errorf ("invalid URL: %q" , prURL )
135144 }
136145
137146 u , err := url .Parse (prURL )
138147 if err != nil {
139- return err
148+ return nil , 0 , err
140149 }
141150
142151 if u .Scheme != "https" && u .Scheme != "http" {
143- return fmt .Errorf ("invalid scheme: %s" , u .Scheme )
152+ return nil , 0 , fmt .Errorf ("invalid scheme: %s" , u .Scheme )
144153 }
145154
146155 m := pullURLRE .FindStringSubmatch (u .Path )
147156 if m == nil {
148- return fmt .Errorf ("not a pull request URL: %s" , prURL )
157+ return nil , 0 , fmt .Errorf ("not a pull request URL: %s" , prURL )
149158 }
150159
151- f . repo = ghrepo .NewWithHost (m [1 ], m [2 ], u .Hostname ())
152- f . prNumber , _ = strconv .Atoi (m [3 ])
153- return nil
160+ repo : = ghrepo .NewWithHost (m [1 ], m [2 ], u .Hostname ())
161+ prNumber , _ : = strconv .Atoi (m [3 ])
162+ return repo , prNumber , nil
154163}
155164
156165var prHeadRE = regexp .MustCompile (`^refs/pull/(\d+)/head$` )
157166
158- func (f * finder ) parseCurrentBranch () error {
167+ func (f * finder ) parseCurrentBranch () ( string , int , error ) {
159168 prHeadRef , err := f .branchFn ()
160169 if err != nil {
161- return err
170+ return "" , 0 , err
162171 }
163172
164- branchConfig := git . ReadBranchConfig (prHeadRef )
173+ branchConfig := f . branchConfig (prHeadRef )
165174
166175 // the branch is configured to merge a special PR head ref
167176 if m := prHeadRE .FindStringSubmatch (branchConfig .MergeRef ); m != nil {
168- f . prNumber , _ = strconv .Atoi (m [1 ])
169- return nil
177+ prNumber , _ : = strconv .Atoi (m [1 ])
178+ return "" , prNumber , nil
170179 }
171180
172181 var branchOwner string
@@ -193,8 +202,7 @@ func (f *finder) parseCurrentBranch() error {
193202 }
194203 }
195204
196- f .branchName = prHeadRef
197- return nil
205+ return prHeadRef , 0 , nil
198206}
199207
200208func findByNumber (httpClient * http.Client , repo ghrepo.Interface , number int , fields []string ) (* api.PullRequest , error ) {
0 commit comments