1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-30 05:49:15 +02:00
lazygit/pkg/gui/controllers/remotes_controller.go

221 lines
6.1 KiB
Go
Raw Normal View History

package controllers
import (
"fmt"
"strings"
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 11:32:27 +10:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type RemotesController struct {
2022-02-05 14:42:56 +11:00
baseController
2023-03-23 18:47:29 +11:00
c *ControllerCommon
setRemoteBranches func([]*models.RemoteBranch)
}
var _ types.IController = &RemotesController{}
func NewRemotesController(
2023-03-23 18:47:29 +11:00
common *ControllerCommon,
setRemoteBranches func([]*models.RemoteBranch),
) *RemotesController {
return &RemotesController{
2022-02-05 14:42:56 +11:00
baseController: baseController{},
2023-03-23 18:47:29 +11:00
c: common,
setRemoteBranches: setRemoteBranches,
}
}
2022-02-05 10:31:07 +11:00
func (self *RemotesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
2022-02-05 10:31:07 +11:00
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
},
{
2022-02-05 10:31:07 +11:00
Key: opts.GetKey(opts.Config.Branches.FetchRemote),
Handler: self.checkSelected(self.fetch),
Description: self.c.Tr.FetchRemote,
},
{
2022-02-05 10:31:07 +11:00
Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.add,
Description: self.c.Tr.AddNewRemote,
},
{
2022-02-05 10:31:07 +11:00
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.remove),
Description: self.c.Tr.RemoveRemote,
},
{
2022-02-05 10:31:07 +11:00
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.checkSelected(self.edit),
Description: self.c.Tr.EditRemote,
},
}
2022-02-05 10:31:07 +11:00
return bindings
}
2023-03-23 13:04:57 +11:00
func (self *RemotesController) Context() types.Context {
return self.context()
}
func (self *RemotesController) context() *context.RemotesContext {
return self.c.Contexts().Remotes
}
func (self *RemotesController) GetOnRenderToMain() func() error {
return func() error {
2023-03-23 18:47:29 +11:00
return self.c.Helpers().Diff.WithDiffModeCheck(func() error {
var task types.UpdateTask
2023-03-23 13:04:57 +11:00
remote := self.context().GetSelected()
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 11:42:22 +11:00
func (self *RemotesController) GetOnClick() func() error {
return self.checkSelected(self.enter)
}
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
}
remoteBranchesContext := self.c.Contexts().RemoteBranches
remoteBranchesContext.SetSelectedLineIdx(newSelectedLine)
remoteBranchesContext.SetTitleRef(remote.Name)
remoteBranchesContext.SetParentContext(self.Context())
2022-03-26 14:44:30 +11:00
if err := self.c.PostRefreshUpdate(remoteBranchesContext); err != nil {
2022-03-26 14:44:30 +11:00
return err
}
return self.c.PushContext(remoteBranchesContext)
}
func (self *RemotesController) add() error {
2022-01-29 19:09:20 +11:00
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.NewRemoteName,
HandleConfirm: func(remoteName string) error {
2022-01-29 19:09:20 +11:00
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.NewRemoteUrl,
HandleConfirm: func(remoteUrl string) error {
self.c.LogAction(self.c.Tr.Actions.AddRemote)
2023-03-23 13:04:57 +11:00
if err := self.c.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 {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.RemoveRemote,
Prompt: self.c.Tr.RemoveRemotePrompt + " '" + remote.Name + "'?",
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.RemoveRemote)
2023-03-23 13:04:57 +11:00
if err := self.c.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.EditRemoteName,
map[string]string{
"remoteName": remote.Name,
},
)
2022-01-29 19:09:20 +11: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)
2023-03-23 13:04:57 +11:00
if err := self.c.Git().Remote.RenameRemote(remote.Name, updatedRemoteName); err != nil {
return self.c.Error(err)
}
}
editUrlMessage := utils.ResolvePlaceholderString(
self.c.Tr.EditRemoteUrl,
map[string]string{
"remoteName": updatedRemoteName,
},
)
urls := remote.Urls
url := ""
if len(urls) > 0 {
url = urls[0]
}
2022-01-29 19:09:20 +11:00
return self.c.Prompt(types.PromptOpts{
Title: editUrlMessage,
InitialContent: url,
HandleConfirm: func(updatedRemoteUrl string) error {
self.c.LogAction(self.c.Tr.Actions.UpdateRemote)
2023-03-23 13:04:57 +11:00
if err := self.c.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 {
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 11:32:27 +10:00
return self.c.WithWaitingStatus(self.c.Tr.FetchingRemoteStatus, func(task *gocui.Task) error {
err := self.c.Git().Sync.FetchRemote(task, 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 {
2023-03-23 13:04:57 +11:00
file := self.context().GetSelected()
if file == nil {
return nil
}
return callback(file)
}
}