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

88 lines
2.6 KiB
Go
Raw Normal View History

2020-02-14 13:07:56 +02:00
package gui
2022-01-28 11:44:36 +02:00
import (
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
2021-03-15 13:29:34 +02:00
func (gui *Gui) handleCreateDiscardMenu() error {
node := gui.getSelectedFileNode()
2021-03-15 13:29:34 +02:00
if node == nil {
2020-02-14 13:07:56 +02:00
return nil
}
2022-01-29 10:09:20 +02:00
var menuItems []*types.MenuItem
2021-03-15 13:29:34 +02:00
if node.File == nil {
2022-01-29 10:09:20 +02:00
menuItems = []*types.MenuItem{
2020-09-28 01:14:32 +02:00
{
DisplayString: gui.c.Tr.LcDiscardAllChanges,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.DiscardAllChangesInDirectory)
if err := gui.git.WorkingTree.DiscardAllDirChanges(node); err != nil {
return gui.c.Error(err)
2020-09-28 01:14:32 +02:00
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
2020-09-28 01:14:32 +02:00
},
},
}
2020-02-14 13:07:56 +02:00
2021-03-15 13:29:34 +02:00
if node.GetHasStagedChanges() && node.GetHasUnstagedChanges() {
2022-01-29 10:09:20 +02:00
menuItems = append(menuItems, &types.MenuItem{
DisplayString: gui.c.Tr.LcDiscardUnstagedChanges,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.DiscardUnstagedChangesInDirectory)
if err := gui.git.WorkingTree.DiscardUnstagedDirChanges(node); err != nil {
return gui.c.Error(err)
2020-09-28 01:14:32 +02:00
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
2020-09-28 01:14:32 +02:00
},
})
}
2021-03-15 13:29:34 +02:00
} else {
file := node.File
submodules := gui.State.Submodules
if file.IsSubmodule(submodules) {
submodule := file.SubmoduleConfig(submodules)
2022-01-29 10:09:20 +02:00
menuItems = []*types.MenuItem{
2021-03-15 13:29:34 +02:00
{
DisplayString: gui.c.Tr.LcSubmoduleStashAndReset,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
return gui.Controllers.Files.ResetSubmodule(submodule)
2021-03-15 13:29:34 +02:00
},
},
}
} else {
2022-01-29 10:09:20 +02:00
menuItems = []*types.MenuItem{
2021-03-15 13:29:34 +02:00
{
DisplayString: gui.c.Tr.LcDiscardAllChanges,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.DiscardAllChangesInFile)
if err := gui.git.WorkingTree.DiscardAllFileChanges(file); err != nil {
return gui.c.Error(err)
2021-03-15 13:29:34 +02:00
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
2021-03-15 13:29:34 +02:00
},
},
}
2020-02-14 13:07:56 +02:00
2021-03-15 13:29:34 +02:00
if file.HasStagedChanges && file.HasUnstagedChanges {
2022-01-29 10:09:20 +02:00
menuItems = append(menuItems, &types.MenuItem{
DisplayString: gui.c.Tr.LcDiscardUnstagedChanges,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
gui.c.LogAction(gui.c.Tr.Actions.DiscardAllUnstagedChangesInFile)
if err := gui.git.WorkingTree.DiscardUnstagedFileChanges(file); err != nil {
return gui.c.Error(err)
2021-03-15 13:29:34 +02:00
}
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}})
2021-03-15 13:29:34 +02:00
},
})
}
}
2020-02-14 13:07:56 +02:00
}
2022-01-29 10:09:20 +02:00
return gui.c.Menu(types.CreateMenuOptions{Title: node.GetPath(), Items: menuItems})
2020-02-14 13:07:56 +02:00
}