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

80 lines
2.3 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 {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.DiscardAllChangesInDirectory).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 {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.DiscardUnstagedChangesInDirectory).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 {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.DiscardAllChangesInFile).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 {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.DiscardAllUnstagedChangesInFile).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
}