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

188 lines
5.5 KiB
Go
Raw Normal View History

2022-01-30 02:10:00 +02:00
package controllers
import (
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 03:32:27 +02:00
"github.com/jesseduffield/gocui"
2022-02-13 09:04:09 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-01-30 02:10:00 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/gui/types"
2023-02-19 04:11:33 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2022-01-30 02:10:00 +02:00
)
// splitting this action out into its own file because it's self-contained
2022-02-13 09:04:09 +02:00
type FilesRemoveController struct {
baseController
2023-03-23 09:47:29 +02:00
c *ControllerCommon
2022-02-13 09:04:09 +02:00
}
var _ types.IController = &FilesRemoveController{}
func NewFilesRemoveController(
2023-03-23 09:47:29 +02:00
common *ControllerCommon,
2022-02-13 09:04:09 +02:00
) *FilesRemoveController {
return &FilesRemoveController{
2023-03-23 09:47:29 +02:00
baseController: baseController{},
c: common,
2022-02-13 09:04:09 +02:00
}
}
func (self *FilesRemoveController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelectedFileNode(self.remove),
Description: self.c.Tr.ViewDiscardOptions,
2022-02-13 09:04:09 +02:00
OpensMenu: true,
},
}
return bindings
}
func (self *FilesRemoveController) remove(node *filetree.FileNode) error {
2022-01-30 02:10:00 +02:00
var menuItems []*types.MenuItem
if node.File == nil {
menuItems = []*types.MenuItem{
{
Label: self.c.Tr.DiscardAllChanges,
2022-01-30 02:10:00 +02:00
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.DiscardAllChangesInDirectory)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().WorkingTree.DiscardAllDirChanges(node); err != nil {
2022-01-30 02:10:00 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES, types.WORKTREES}})
2022-01-30 02:10:00 +02:00
},
Key: self.c.KeybindingsOpts().GetKey(self.c.UserConfig.Keybinding.Files.ConfirmDiscard),
2023-02-19 04:11:33 +02:00
Tooltip: utils.ResolvePlaceholderString(
self.c.Tr.DiscardAllTooltip,
map[string]string{
"path": node.GetPath(),
},
),
2022-01-30 02:10:00 +02:00
},
}
if node.GetHasStagedChanges() && node.GetHasUnstagedChanges() {
menuItems = append(menuItems, &types.MenuItem{
Label: self.c.Tr.DiscardUnstagedChanges,
2022-01-30 02:10:00 +02:00
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.DiscardUnstagedChangesInDirectory)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().WorkingTree.DiscardUnstagedDirChanges(node); err != nil {
2022-01-30 02:10:00 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES, types.WORKTREES}})
2022-01-30 02:10:00 +02:00
},
2023-02-19 04:11:33 +02:00
Key: 'u',
Tooltip: utils.ResolvePlaceholderString(
self.c.Tr.DiscardUnstagedTooltip,
map[string]string{
"path": node.GetPath(),
},
),
2022-01-30 02:10:00 +02:00
})
}
} else {
file := node.File
2023-03-23 04:04:57 +02:00
submodules := self.c.Model().Submodules
2022-01-30 02:10:00 +02:00
if file.IsSubmodule(submodules) {
submodule := file.SubmoduleConfig(submodules)
menuItems = []*types.MenuItem{
{
Label: self.c.Tr.SubmoduleStashAndReset,
2022-01-30 02:10:00 +02:00
OnPress: func() error {
return self.ResetSubmodule(submodule)
},
},
}
} else {
menuItems = []*types.MenuItem{
{
Label: self.c.Tr.DiscardAllChanges,
2022-01-30 02:10:00 +02:00
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.DiscardAllChangesInFile)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().WorkingTree.DiscardAllFileChanges(file); err != nil {
2022-01-30 02:10:00 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES, types.WORKTREES}})
2022-01-30 02:10:00 +02:00
},
Key: self.c.KeybindingsOpts().GetKey(self.c.UserConfig.Keybinding.Files.ConfirmDiscard),
2023-02-19 04:11:33 +02:00
Tooltip: utils.ResolvePlaceholderString(
self.c.Tr.DiscardAllTooltip,
map[string]string{
"path": node.GetPath(),
},
),
2022-01-30 02:10:00 +02:00
},
}
if file.HasStagedChanges && file.HasUnstagedChanges {
menuItems = append(menuItems, &types.MenuItem{
Label: self.c.Tr.DiscardUnstagedChanges,
2022-01-30 02:10:00 +02:00
OnPress: func() error {
self.c.LogAction(self.c.Tr.Actions.DiscardAllUnstagedChangesInFile)
2023-03-23 04:04:57 +02:00
if err := self.c.Git().WorkingTree.DiscardUnstagedFileChanges(file); err != nil {
2022-01-30 02:10:00 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES, types.WORKTREES}})
2022-01-30 02:10:00 +02:00
},
2023-02-19 04:11:33 +02:00
Key: 'u',
Tooltip: utils.ResolvePlaceholderString(
self.c.Tr.DiscardUnstagedTooltip,
map[string]string{
"path": node.GetPath(),
},
),
2022-01-30 02:10:00 +02:00
})
}
}
}
return self.c.Menu(types.CreateMenuOptions{Title: node.GetPath(), Items: menuItems})
}
2022-02-13 09:04:09 +02:00
func (self *FilesRemoveController) ResetSubmodule(submodule *models.SubmoduleConfig) error {
return self.c.WithWaitingStatus(self.c.Tr.ResettingSubmoduleStatus, func(gocui.Task) error {
2022-02-13 09:04:09 +02:00
self.c.LogAction(self.c.Tr.Actions.ResetSubmodule)
2023-03-23 09:47:29 +02:00
file := self.c.Helpers().WorkingTree.FileForSubmodule(submodule)
2022-02-13 09:04:09 +02:00
if file != nil {
2023-03-23 04:04:57 +02:00
if err := self.c.Git().WorkingTree.UnStageFile(file.Names(), file.Tracked); err != nil {
2022-02-13 09:04:09 +02:00
return self.c.Error(err)
}
}
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Submodule.Stash(submodule); err != nil {
2022-02-13 09:04:09 +02:00
return self.c.Error(err)
}
2023-03-23 04:04:57 +02:00
if err := self.c.Git().Submodule.Reset(submodule); err != nil {
2022-02-13 09:04:09 +02:00
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES, types.SUBMODULES}})
})
}
func (self *FilesRemoveController) checkSelectedFileNode(callback func(*filetree.FileNode) error) func() error {
return func() error {
2022-03-19 00:31:52 +02:00
node := self.context().GetSelected()
2022-02-13 09:04:09 +02:00
if node == nil {
return nil
}
return callback(node)
}
}
func (self *FilesRemoveController) Context() types.Context {
return self.context()
}
func (self *FilesRemoveController) context() *context.WorkingTreeContext {
2023-03-23 04:04:57 +02:00
return self.c.Contexts().Files
2022-02-13 09:04:09 +02:00
}