Skip to content

Commit dede117

Browse files
committed
[WIP] pkg/gui: Allow user to select remote and branch when creating a PR
When creating a PR against a selected branch (via O = "create pull request options"), the user will first be asked to select a remote (if there is more than one). After that, the suggestion area is populated with all remote branches at that origin - instead of all local ones. After all, creating a PR against a branch that doesn't exist on the remote won't work. Please note that the "PR is not filed against 'origin' remote" use cases (e.g. when contributing via a fork that is 'origin' to a GitHub project that is 'upstream'), is not working correctly yet. Fixes #1826.
1 parent e1e4e1b commit dede117

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

pkg/gui/controllers/branches_controller.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,26 @@ func (self *BranchesController) createPullRequestMenu(selectedBranch *models.Bra
728728
},
729729
},
730730
{
731+
// TODO: Replace with "Select remote and branch"?
731732
LabelColumns: fromToLabelColumns(branch.Name, self.c.Tr.SelectBranch),
732733
OnPress: func() error {
734+
if !branch.IsTrackingRemote() {
735+
return errors.New(self.c.Tr.PullRequestNoUpstream)
736+
}
737+
738+
if len(self.c.Model().Remotes) == 1 {
739+
toRemote := self.c.Model().Remotes[0].Name
740+
self.c.Log.Debugf("PR will target the only existing remote '%s'", toRemote)
741+
return self.promptForTargetBranchNameAndCreatePullRequest(branch, toRemote)
742+
}
743+
733744
self.c.Prompt(types.PromptOpts{
734-
Title: branch.Name + " →",
735-
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetRemoteBranchesSuggestionsFunc("/"),
736-
HandleConfirm: func(targetBranchName string) error {
737-
return self.createPullRequest(branch.Name, targetBranchName)
745+
Title: "Select Target Remote",
746+
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetRemoteSuggestionsFunc(),
747+
HandleConfirm: func(toRemote string) error {
748+
self.c.Log.Debugf("PR will target remote '%s'", toRemote)
749+
750+
return self.promptForTargetBranchNameAndCreatePullRequest(branch, toRemote)
738751
},
739752
})
740753

@@ -764,6 +777,19 @@ func (self *BranchesController) createPullRequestMenu(selectedBranch *models.Bra
764777
return self.c.Menu(types.CreateMenuOptions{Title: fmt.Sprint(self.c.Tr.CreatePullRequestOptions), Items: menuItems})
765778
}
766779

780+
func (self *BranchesController) promptForTargetBranchNameAndCreatePullRequest(fromBranch *models.Branch, toRemote string) error {
781+
self.c.Prompt(types.PromptOpts{
782+
Title: fmt.Sprintf("%s → %s/", fromBranch.UpstreamBranch, toRemote),
783+
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetRemoteBranchesForRemoteSuggestionsFunc(toRemote),
784+
HandleConfirm: func(toBranch string) error {
785+
self.c.Log.Debugf("PR will target branch '%s' on remote '%s'", toBranch, toRemote)
786+
return self.createPullRequest(fromBranch.UpstreamBranch, toBranch)
787+
},
788+
})
789+
790+
return nil
791+
}
792+
767793
func (self *BranchesController) createPullRequest(from string, to string) error {
768794
url, err := self.c.Helpers().Host.GetPullRequestURL(from, to)
769795
if err != nil {

pkg/gui/controllers/helpers/suggestions_helper.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,38 @@ func (self *SuggestionsHelper) getRemoteBranchNames(separator string) []string {
162162
})
163163
}
164164

165+
func (self *SuggestionsHelper) getRemoteBranchNamesForRemote(remoteName string) []string {
166+
for _, remote := range self.c.Model().Remotes {
167+
if remote.Name == remoteName {
168+
return lo.Map(remote.Branches, func(branch *models.RemoteBranch, _ int) string {
169+
return branch.Name
170+
})
171+
}
172+
}
173+
174+
return nil
175+
}
176+
177+
func (self *SuggestionsHelper) getRemoteBranchNamesWithoutRemotePrefix() []string {
178+
return lo.FlatMap(self.c.Model().Remotes, func(remote *models.Remote, _ int) []string {
179+
return lo.Map(remote.Branches, func(branch *models.RemoteBranch, _ int) string {
180+
return branch.Name
181+
})
182+
})
183+
}
184+
165185
func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion {
166186
return FilterFunc(self.getRemoteBranchNames(separator), self.c.UserConfig().Gui.UseFuzzySearch())
167187
}
168188

189+
func (self *SuggestionsHelper) GetRemoteBranchesForRemoteSuggestionsFunc(remoteName string) func(string) []*types.Suggestion {
190+
return FilterFunc(self.getRemoteBranchNamesForRemote(remoteName), self.c.UserConfig().Gui.UseFuzzySearch())
191+
}
192+
193+
func (self *SuggestionsHelper) GetRemoteBranchesWithoutRemotePrefixSuggestionsFunc() func(string) []*types.Suggestion {
194+
return FilterFunc(self.getRemoteBranchNamesWithoutRemotePrefix(), self.c.UserConfig().Gui.UseFuzzySearch())
195+
}
196+
169197
func (self *SuggestionsHelper) getTagNames() []string {
170198
return lo.Map(self.c.Model().Tags, func(tag *models.Tag, _ int) string {
171199
return tag.Name

0 commit comments

Comments
 (0)