2022-01-16 05:46:53 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-12-30 14:24:24 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
2022-12-30 14:24:24 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2022-01-16 05:46:53 +02:00
|
|
|
"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
|
2023-03-23 09:47:29 +02:00
|
|
|
c *ControllerCommon
|
2022-01-16 05:46:53 +02:00
|
|
|
|
|
|
|
setRemoteBranches func([]*models.RemoteBranch)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.IController = &RemotesController{}
|
|
|
|
|
|
|
|
func NewRemotesController(
|
2023-03-23 09:47:29 +02:00
|
|
|
common *ControllerCommon,
|
2022-01-16 05:46:53 +02:00
|
|
|
setRemoteBranches func([]*models.RemoteBranch),
|
|
|
|
) *RemotesController {
|
|
|
|
return &RemotesController{
|
2022-02-05 05:42:56 +02:00
|
|
|
baseController: baseController{},
|
2023-03-23 09:47:29 +02:00
|
|
|
c: common,
|
2022-01-16 05:46:53 +02:00
|
|
|
setRemoteBranches: setRemoteBranches,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
func (self *RemotesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
2022-01-16 05:46:53 +02:00
|
|
|
bindings := []*types.Binding{
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
2022-01-16 05:46:53 +02:00
|
|
|
Handler: self.checkSelected(self.enter),
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Branches.FetchRemote),
|
2022-01-16 05:46:53 +02:00
|
|
|
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),
|
2022-01-16 05:46:53 +02:00
|
|
|
Handler: self.add,
|
|
|
|
Description: self.c.Tr.LcAddNewRemote,
|
|
|
|
},
|
|
|
|
{
|
2022-02-05 01:31:07 +02:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.Remove),
|
2022-01-16 05:46:53 +02:00
|
|
|
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),
|
2022-01-16 05:46:53 +02:00
|
|
|
Handler: self.checkSelected(self.edit),
|
|
|
|
Description: self.c.Tr.LcEditRemote,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
return bindings
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
func (self *RemotesController) Context() types.Context {
|
|
|
|
return self.context()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemotesController) context() *context.RemotesContext {
|
|
|
|
return self.c.Contexts().Remotes
|
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
func (self *RemotesController) 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
|
2023-03-23 04:04:57 +02:00
|
|
|
remote := self.context().GetSelected()
|
2022-12-30 14:24:24 +02:00
|
|
|
if remote == nil {
|
|
|
|
task = types.NewRenderStringTask("No remotes")
|
|
|
|
} else {
|
|
|
|
task = types.NewRenderStringTask(fmt.Sprintf("%s\nUrls:\n%s", style.FgGreen.Sprint(remote.Name), strings.Join(remote.Urls, "\n")))
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.RenderToMainViews(types.RefreshMainOpts{
|
|
|
|
Pair: self.c.MainViewPairs().Normal,
|
|
|
|
Main: &types.ViewUpdateOpts{
|
|
|
|
Title: "Remote",
|
|
|
|
Task: task,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 02:42:22 +02:00
|
|
|
func (self *RemotesController) GetOnClick() func() error {
|
|
|
|
return self.checkSelected(self.enter)
|
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
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
|
|
|
|
}
|
2023-03-23 04:04:57 +02:00
|
|
|
self.c.Contexts().RemoteBranches.SetSelectedLineIdx(newSelectedLine)
|
|
|
|
self.c.Contexts().RemoteBranches.SetTitleRef(remote.Name)
|
2022-03-26 05:44:30 +02:00
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.PostRefreshUpdate(self.c.Contexts().RemoteBranches); err != nil {
|
2022-03-26 05:44:30 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-01-16 05:46:53 +02:00
|
|
|
|
2023-03-23 04:04:57 +02:00
|
|
|
return self.c.PushContext(self.c.Contexts().RemoteBranches)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemotesController) add() error {
|
2022-01-29 10:09:20 +02:00
|
|
|
return self.c.Prompt(types.PromptOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.LcNewRemoteName,
|
|
|
|
HandleConfirm: func(remoteName string) error {
|
2022-01-29 10:09:20 +02:00
|
|
|
return self.c.Prompt(types.PromptOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.LcNewRemoteUrl,
|
|
|
|
HandleConfirm: func(remoteUrl string) error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.AddRemote)
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.Git().Remote.AddRemote(remoteName, remoteUrl); err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.REMOTES}})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RemotesController) remove(remote *models.Remote) error {
|
2022-03-30 08:48:29 +02:00
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: self.c.Tr.LcRemoveRemote,
|
|
|
|
Prompt: self.c.Tr.LcRemoveRemotePrompt + " '" + remote.Name + "'?",
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.RemoveRemote)
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.Git().Remote.RemoveRemote(remote.Name); err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
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{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: editNameMessage,
|
|
|
|
InitialContent: remote.Name,
|
|
|
|
HandleConfirm: func(updatedRemoteName string) error {
|
|
|
|
if updatedRemoteName != remote.Name {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.UpdateRemote)
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.Git().Remote.RenameRemote(remote.Name, updatedRemoteName); err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
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{
|
2022-01-16 05:46:53 +02:00
|
|
|
Title: editUrlMessage,
|
|
|
|
InitialContent: url,
|
|
|
|
HandleConfirm: func(updatedRemoteUrl string) error {
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.UpdateRemote)
|
2023-03-23 04:04:57 +02:00
|
|
|
if err := self.c.Git().Remote.UpdateRemoteUrl(updatedRemoteName, updatedRemoteUrl); err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
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 {
|
2023-03-23 04:04:57 +02:00
|
|
|
err := self.c.Git().Sync.FetchRemote(remote.Name)
|
2022-01-16 05:46:53 +02:00
|
|
|
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 {
|
2023-03-23 04:04:57 +02:00
|
|
|
file := self.context().GetSelected()
|
2022-01-16 05:46:53 +02:00
|
|
|
if file == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(file)
|
|
|
|
}
|
|
|
|
}
|