2022-01-30 02:10:00 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
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),
|
2023-05-25 13:11:51 +02:00
|
|
|
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{
|
|
|
|
{
|
2023-05-25 13:11:51 +02:00
|
|
|
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)
|
|
|
|
}
|
2023-07-16 12:34:22 +02:00
|
|
|
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-08-25 15:50:05 +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{
|
2023-05-25 13:11:51 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-16 12:34:22 +02:00
|
|
|
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{
|
|
|
|
{
|
2023-05-25 13:11:51 +02:00
|
|
|
Label: self.c.Tr.SubmoduleStashAndReset,
|
2022-01-30 02:10:00 +02:00
|
|
|
OnPress: func() error {
|
|
|
|
return self.ResetSubmodule(submodule)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
menuItems = []*types.MenuItem{
|
|
|
|
{
|
2023-05-25 13:11:51 +02:00
|
|
|
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)
|
|
|
|
}
|
2023-07-16 12:34:22 +02:00
|
|
|
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-08-25 15:50:05 +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{
|
2023-05-25 13:11:51 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-16 12:34:22 +02:00
|
|
|
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 {
|
2023-07-09 13:09:52 +02:00
|
|
|
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
|
|
|
}
|