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

84 lines
2.4 KiB
Go
Raw Normal View History

2020-02-14 13:07:56 +02:00
package gui
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
}
2020-09-28 01:14:32 +02:00
var menuItems []*menuItem
2021-03-15 13:29:34 +02:00
if node.File == nil {
2020-09-28 01:14:32 +02:00
menuItems = []*menuItem{
{
2020-10-04 02:00:48 +02:00
displayString: gui.Tr.LcDiscardAllChanges,
2020-09-28 01:14:32 +02:00
onPress: func() error {
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.DiscardAllChangesInDirectory)
2022-01-08 05:10:01 +02:00
if err := gui.Git.WorkingTree.DiscardAllDirChanges(node); err != nil {
2020-09-28 01:14:32 +02:00
return gui.surfaceError(err)
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []RefreshableView{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() {
2020-09-28 01:14:32 +02:00
menuItems = append(menuItems, &menuItem{
2020-10-04 02:00:48 +02:00
displayString: gui.Tr.LcDiscardUnstagedChanges,
2020-09-28 01:14:32 +02:00
onPress: func() error {
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.DiscardUnstagedChangesInDirectory)
2022-01-08 05:10:01 +02:00
if err := gui.Git.WorkingTree.DiscardUnstagedDirChanges(node); err != nil {
2020-09-28 01:14:32 +02:00
return gui.surfaceError(err)
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []RefreshableView{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)
menuItems = []*menuItem{
{
displayString: gui.Tr.LcSubmoduleStashAndReset,
onPress: func() error {
return gui.handleResetSubmodule(submodule)
},
},
}
} else {
menuItems = []*menuItem{
{
displayString: gui.Tr.LcDiscardAllChanges,
onPress: func() error {
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.DiscardAllChangesInFile)
2022-01-08 05:10:01 +02:00
if err := gui.Git.WorkingTree.DiscardAllFileChanges(file); err != nil {
2021-03-15 13:29:34 +02:00
return gui.surfaceError(err)
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []RefreshableView{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 {
menuItems = append(menuItems, &menuItem{
displayString: gui.Tr.LcDiscardUnstagedChanges,
onPress: func() error {
2022-01-05 03:01:59 +02:00
gui.logAction(gui.Tr.Actions.DiscardAllUnstagedChangesInFile)
2022-01-08 05:10:01 +02:00
if err := gui.Git.WorkingTree.DiscardUnstagedFileChanges(file); err != nil {
2021-03-15 13:29:34 +02:00
return gui.surfaceError(err)
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []RefreshableView{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
return gui.createMenu(node.GetPath(), menuItems, createMenuOptions{showCancel: true})
2020-02-14 13:07:56 +02:00
}