From 437daf2f7444a3f8afb5e8db862a1fdb0cd89f66 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 5 Feb 2025 16:04:50 +0100 Subject: [PATCH] Disable staging and unstaging lines or hunks when the diff context size is 0 Git diff and patch doesn't work reliably with a context size of 0, so disable it in this case (and discarding changes as well). Magit does the same, see https://github.com/magit/magit/issues/4222. Staging entire files by pressing space in the Files panel is still possible, of course. --- pkg/gui/controllers/staging_controller.go | 12 ++++++++++++ pkg/i18n/english.go | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/pkg/gui/controllers/staging_controller.go b/pkg/gui/controllers/staging_controller.go index c3ea3ca24..f353cc215 100644 --- a/pkg/gui/controllers/staging_controller.go +++ b/pkg/gui/controllers/staging_controller.go @@ -1,11 +1,13 @@ package controllers import ( + "fmt" "strings" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/patch" + "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -185,10 +187,20 @@ func (self *StagingController) TogglePanel() error { } func (self *StagingController) ToggleStaged() error { + if self.c.AppState.DiffContextSize == 0 { + return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage, + keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) + } + return self.applySelectionAndRefresh(self.staged) } func (self *StagingController) DiscardSelection() error { + if self.c.AppState.DiffContextSize == 0 { + return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToDiscard, + keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) + } + reset := func() error { return self.applySelectionAndRefresh(true) } if !self.staged && !self.c.UserConfig().Gui.SkipDiscardChangeWarning { diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 28e126356..f92c40993 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -926,6 +926,8 @@ type Actions struct { UnstageFile string UnstageAllFiles string StageAllFiles string + NotEnoughContextToStage string + NotEnoughContextToDiscard string IgnoreExcludeFile string IgnoreFileErr string ExcludeFile string @@ -1913,6 +1915,8 @@ func EnglishTranslationSet() *TranslationSet { UnstageFile: "Unstage file", UnstageAllFiles: "Unstage all files", StageAllFiles: "Stage all files", + NotEnoughContextToStage: "Staging or unstaging changes is not possible with a diff context size of 0. Increase the context using '%s'.", + NotEnoughContextToDiscard: "Discarding changes is not possible with a diff context size of 0. Increase the context using '%s'.", IgnoreExcludeFile: "Ignore or exclude file", IgnoreFileErr: "Cannot ignore .gitignore", ExcludeFile: "Exclude file",