diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 58472d871..e8dbca90d 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -62,7 +62,7 @@ func (self *CommitFilesController) GetKeybindings(opts types.KeybindingsOpts) [] { Key: opts.GetKey(opts.Config.Universal.Remove), Handler: self.withItems(self.discard), - GetDisabledReason: self.require(self.itemsSelected()), + GetDisabledReason: self.require(self.itemsSelected(self.canDiscardFileChanges)), Description: self.c.Tr.Discard, Tooltip: self.c.Tr.DiscardOldFileChangeTooltip, DisplayOnScreen: true, @@ -301,15 +301,6 @@ func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error } func (self *CommitFilesController) discard(selectedNodes []*filetree.CommitFileNode) error { - parentContext := self.c.Context().Current().GetParentContext() - if parentContext == nil || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY { - return errors.New(self.c.Tr.CanOnlyDiscardFromLocalCommits) - } - - if ok, err := self.c.Helpers().PatchBuilding.ValidateNormalWorkingTreeState(); !ok { - return err - } - prompt := lo.Ternary(self.c.Git().Patch.PatchBuilder.Active(), self.c.Tr.DiscardFileChangesPromptResetPatch, self.c.Tr.DiscardFileChangesPrompt) @@ -351,6 +342,25 @@ func (self *CommitFilesController) discard(selectedNodes []*filetree.CommitFileN return nil } +func (self *CommitFilesController) canDiscardFileChanges(nodes []*filetree.CommitFileNode) *types.DisabledReason { + parentContext := self.c.Context().Current().GetParentContext() + if parentContext == nil || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY { + return &types.DisabledReason{ + Text: self.c.Tr.CanOnlyDiscardFromLocalCommits, + ShowErrorInPanel: true, + } + } + + if self.c.Git().Status.WorkingTreeState().Any() { + return &types.DisabledReason{ + Text: self.c.Tr.CantPatchWhileRebasingError, + ShowErrorInPanel: true, + } + } + + return nil +} + func (self *CommitFilesController) open(node *filetree.CommitFileNode) error { return self.c.Helpers().Files.OpenFile(node.GetPath()) } diff --git a/pkg/gui/controllers/helpers/patch_building_helper.go b/pkg/gui/controllers/helpers/patch_building_helper.go index 72ece0a71..fd5136b8a 100644 --- a/pkg/gui/controllers/helpers/patch_building_helper.go +++ b/pkg/gui/controllers/helpers/patch_building_helper.go @@ -1,7 +1,6 @@ package helpers import ( - "errors" "fmt" "github.com/jesseduffield/lazygit/pkg/commands/patch" @@ -22,13 +21,6 @@ func NewPatchBuildingHelper( } } -func (self *PatchBuildingHelper) ValidateNormalWorkingTreeState() (bool, error) { - if self.c.Git().Status.WorkingTreeState().Any() { - return false, errors.New(self.c.Tr.CantPatchWhileRebasingError) - } - return true, nil -} - func (self *PatchBuildingHelper) ShowHunkStagingHint() { if !self.c.AppState.DidShowHunkStagingHint && self.c.UserConfig().Gui.UseHunkModeInStagingView { self.c.AppState.DidShowHunkStagingHint = true