2022-02-26 10:06:22 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RemoteBranchesController struct {
|
|
|
|
baseController
|
2023-03-23 09:47:29 +02:00
|
|
|
c *ControllerCommon
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.IController = &RemoteBranchesController{}
|
|
|
|
|
|
|
|
func NewRemoteBranchesController(
|
2023-03-23 09:47:29 +02:00
|
|
|
common *ControllerCommon,
|
2022-02-26 10:06:22 +02:00
|
|
|
) *RemoteBranchesController {
|
|
|
|
return &RemoteBranchesController{
|
2023-03-23 09:47:29 +02:00
|
|
|
baseController: baseController{},
|
|
|
|
c: common,
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
|
|
return []*types.Binding{
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.Select),
|
|
|
|
// gonna use the exact same handler as the 'n' keybinding because everybody wants this to happen when they checkout a remote branch
|
|
|
|
Handler: self.checkSelected(self.newLocalBranch),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.Checkout,
|
2022-02-26 10:06:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.New),
|
|
|
|
Handler: self.checkSelected(self.newLocalBranch),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.NewBranch,
|
2022-02-26 10:06:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Branches.MergeIntoCurrentBranch),
|
|
|
|
Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.merge)),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.MergeIntoCurrentBranch,
|
2022-02-26 10:06:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Branches.RebaseBranch),
|
|
|
|
Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.rebase)),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.RebaseBranch,
|
2022-02-26 10:06:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.Remove),
|
|
|
|
Handler: self.checkSelected(self.delete),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.DeleteBranch,
|
2022-02-26 10:06:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Branches.SetUpstream),
|
|
|
|
Handler: self.checkSelected(self.setAsUpstream),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.SetAsUpstream,
|
2022-02-26 10:06:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Universal.Return),
|
|
|
|
Handler: self.escape,
|
|
|
|
Description: self.c.Tr.ReturnToRemotesList,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: opts.GetKey(opts.Config.Commits.ViewResetOptions),
|
|
|
|
Handler: self.checkSelected(self.createResetMenu),
|
2023-05-25 13:11:51 +02:00
|
|
|
Description: self.c.Tr.ViewResetOptions,
|
2022-02-26 10:06:22 +02:00
|
|
|
OpensMenu: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
func (self *RemoteBranchesController) GetOnRenderToMain() func() error {
|
|
|
|
return func() error {
|
2023-03-23 09:47:29 +02:00
|
|
|
return self.c.Helpers().Diff.WithDiffModeCheck(func() error {
|
2022-12-30 14:24:24 +02:00
|
|
|
var task types.UpdateTask
|
|
|
|
remoteBranch := self.context().GetSelected()
|
|
|
|
if remoteBranch == nil {
|
|
|
|
task = types.NewRenderStringTask("No branches for this remote")
|
|
|
|
} else {
|
2023-03-23 04:04:57 +02:00
|
|
|
cmdObj := self.c.Git().Branch.GetGraphCmdObj(remoteBranch.FullRefName())
|
2022-12-30 14:24:24 +02:00
|
|
|
task = types.NewRunCommandTask(cmdObj.GetCmd())
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.RenderToMainViews(types.RefreshMainOpts{
|
|
|
|
Pair: self.c.MainViewPairs().Normal,
|
|
|
|
Main: &types.ViewUpdateOpts{
|
|
|
|
Title: "Remote Branch",
|
|
|
|
Task: task,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:06:22 +02:00
|
|
|
func (self *RemoteBranchesController) Context() types.Context {
|
|
|
|
return self.context()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) context() *context.RemoteBranchesContext {
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.Contexts().RemoteBranches
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) checkSelected(callback func(*models.RemoteBranch) error) func() error {
|
|
|
|
return func() error {
|
|
|
|
selectedItem := self.context().GetSelected()
|
|
|
|
if selectedItem == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(selectedItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) escape() error {
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.PushContext(self.c.Contexts().Remotes)
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) delete(selectedBranch *models.RemoteBranch) error {
|
|
|
|
message := fmt.Sprintf("%s '%s'?", self.c.Tr.DeleteRemoteBranchMessage, selectedBranch.FullName())
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-02-26 10:06:22 +02:00
|
|
|
Title: self.c.Tr.DeleteRemoteBranch,
|
|
|
|
Prompt: message,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.DeletingStatus, func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.DeleteRemoteBranch)
|
2023-03-23 04:04:57 +02:00
|
|
|
err := self.c.Git().Remote.DeleteRemoteBranch(selectedBranch.RemoteName, selectedBranch.Name)
|
2022-02-26 10:06:22 +02:00
|
|
|
if err != nil {
|
|
|
|
_ = self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.REMOTES}})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) merge(selectedBranch *models.RemoteBranch) error {
|
2023-03-23 09:47:29 +02:00
|
|
|
return self.c.Helpers().MergeAndRebase.MergeRefIntoCheckedOutBranch(selectedBranch.FullName())
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) rebase(selectedBranch *models.RemoteBranch) error {
|
2023-03-23 09:47:29 +02:00
|
|
|
return self.c.Helpers().MergeAndRebase.RebaseOntoRef(selectedBranch.FullName())
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) createResetMenu(selectedBranch *models.RemoteBranch) error {
|
2023-03-23 09:47:29 +02:00
|
|
|
return self.c.Helpers().Refs.CreateGitResetMenu(selectedBranch.FullName())
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) setAsUpstream(selectedBranch *models.RemoteBranch) error {
|
2023-03-23 09:47:29 +02:00
|
|
|
checkedOutBranch := self.c.Helpers().Refs.GetCheckedOutRef()
|
2022-02-26 10:06:22 +02:00
|
|
|
|
|
|
|
message := utils.ResolvePlaceholderString(
|
|
|
|
self.c.Tr.SetUpstreamMessage,
|
|
|
|
map[string]string{
|
|
|
|
"checkedOut": checkedOutBranch.Name,
|
|
|
|
"selected": selectedBranch.FullName(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-02-26 10:06:22 +02:00
|
|
|
Title: self.c.Tr.SetUpstreamTitle,
|
|
|
|
Prompt: message,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.SetBranchUpstream)
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.Git().Branch.SetUpstream(selectedBranch.RemoteName, selectedBranch.Name, checkedOutBranch.Name); err != nil {
|
2022-02-26 10:06:22 +02:00
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.REMOTES}})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemoteBranchesController) newLocalBranch(selectedBranch *models.RemoteBranch) error {
|
|
|
|
// will set to the remote's branch name without the remote name
|
|
|
|
nameSuggestion := strings.SplitAfterN(selectedBranch.RefName(), "/", 2)[1]
|
|
|
|
|
2023-03-23 09:47:29 +02:00
|
|
|
return self.c.Helpers().Refs.NewBranch(selectedBranch.RefName(), selectedBranch.RefName(), nameSuggestion)
|
2022-02-26 10:06:22 +02:00
|
|
|
}
|