1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/controllers/remotes_controller.go

194 lines
5.4 KiB
Go
Raw Normal View History

package controllers
import (
"github.com/jesseduffield/lazygit/pkg/commands"
"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 RemotesController struct {
2022-02-05 05:42:56 +02:00
baseController
2022-01-31 13:20:28 +02:00
c *types.ControllerCommon
2022-02-05 08:04:10 +02:00
context *context.RemotesContext
2022-01-31 13:20:28 +02:00
git *commands.GitCommand
setRemoteBranches func([]*models.RemoteBranch)
2022-01-31 13:20:28 +02:00
contexts *context.ContextTree
}
var _ types.IController = &RemotesController{}
func NewRemotesController(
2022-01-29 10:09:20 +02:00
c *types.ControllerCommon,
2022-02-05 08:04:10 +02:00
context *context.RemotesContext,
git *commands.GitCommand,
2022-01-31 13:20:28 +02:00
contexts *context.ContextTree,
setRemoteBranches func([]*models.RemoteBranch),
) *RemotesController {
return &RemotesController{
2022-02-05 05:42:56 +02:00
baseController: baseController{},
c: c,
git: git,
2022-01-31 13:20:28 +02:00
contexts: contexts,
context: context,
setRemoteBranches: setRemoteBranches,
}
}
2022-02-05 01:31:07 +02:00
func (self *RemotesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
},
2022-02-05 08:04:10 +02:00
// {
// Key: gocui.MouseLeft,
// Handler: func() error { return self.context.HandleClick(self.checkSelected(self.enter)) },
// },
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Branches.FetchRemote),
Handler: self.checkSelected(self.fetch),
Description: self.c.Tr.LcFetchRemote,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.add,
Description: self.c.Tr.LcAddNewRemote,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.remove),
Description: self.c.Tr.LcRemoveRemote,
},
{
2022-02-05 01:31:07 +02:00
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.checkSelected(self.edit),
Description: self.c.Tr.LcEditRemote,
},
}
2022-02-05 01:31:07 +02:00
return bindings
}
func (self *RemotesController) enter(remote *models.Remote) error {
// naive implementation: get the branches from the remote and render them to the list, change the context
self.setRemoteBranches(remote.Branches)
newSelectedLine := 0
if len(remote.Branches) == 0 {
newSelectedLine = -1
}
2022-01-31 13:20:28 +02:00
self.contexts.RemoteBranches.GetPanelState().SetSelectedLineIdx(newSelectedLine)
2022-01-31 13:20:28 +02:00
return self.c.PushContext(self.contexts.RemoteBranches)
}
func (self *RemotesController) add() error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.LcNewRemoteName,
HandleConfirm: func(remoteName string) error {
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.LcNewRemoteUrl,
HandleConfirm: func(remoteUrl string) error {
self.c.LogAction(self.c.Tr.Actions.AddRemote)
if err := self.git.Remote.AddRemote(remoteName, remoteUrl); err != nil {
return err
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.REMOTES}})
},
})
},
})
}
func (self *RemotesController) remove(remote *models.Remote) error {
2022-01-29 10:09:20 +02:00
return self.c.Ask(types.AskOpts{
Title: self.c.Tr.LcRemoveRemote,
Prompt: self.c.Tr.LcRemoveRemotePrompt + " '" + remote.Name + "'?",
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.RemoveRemote)
if err := self.git.Remote.RemoveRemote(remote.Name); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.REMOTES}})
},
})
}
func (self *RemotesController) edit(remote *models.Remote) error {
editNameMessage := utils.ResolvePlaceholderString(
self.c.Tr.LcEditRemoteName,
map[string]string{
"remoteName": remote.Name,
},
)
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
Title: editNameMessage,
InitialContent: remote.Name,
HandleConfirm: func(updatedRemoteName string) error {
if updatedRemoteName != remote.Name {
self.c.LogAction(self.c.Tr.Actions.UpdateRemote)
if err := self.git.Remote.RenameRemote(remote.Name, updatedRemoteName); err != nil {
return self.c.Error(err)
}
}
editUrlMessage := utils.ResolvePlaceholderString(
self.c.Tr.LcEditRemoteUrl,
map[string]string{
"remoteName": updatedRemoteName,
},
)
urls := remote.Urls
url := ""
if len(urls) > 0 {
url = urls[0]
}
2022-01-29 10:09:20 +02:00
return self.c.Prompt(types.PromptOpts{
Title: editUrlMessage,
InitialContent: url,
HandleConfirm: func(updatedRemoteUrl string) error {
self.c.LogAction(self.c.Tr.Actions.UpdateRemote)
if err := self.git.Remote.UpdateRemoteUrl(updatedRemoteName, updatedRemoteUrl); err != nil {
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.REMOTES}})
},
})
},
})
}
func (self *RemotesController) fetch(remote *models.Remote) error {
return self.c.WithWaitingStatus(self.c.Tr.FetchingRemoteStatus, func() error {
err := self.git.Sync.FetchRemote(remote.Name)
if err != nil {
_ = self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.REMOTES}})
})
}
func (self *RemotesController) checkSelected(callback func(*models.Remote) error) func() error {
return func() error {
2022-02-05 08:04:10 +02:00
file := self.context.GetSelected()
if file == nil {
return nil
}
return callback(file)
}
}
func (self *RemotesController) Context() types.Context {
2022-01-31 13:20:28 +02:00
return self.context
}