Skip to content

Commit 5dff9af

Browse files
authored
Disable staging and unstaging lines or hunks when the diff context size is 0 (#4235)
- **PR Description** 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 magit/magit#4222. Staging entire files by pressing space in the Files panel is still possible, of course. Fixes #4233.
2 parents c014168 + 437daf2 commit 5dff9af

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pkg/gui/controllers/staging_controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package controllers
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/jesseduffield/gocui"
78
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
89
"github.com/jesseduffield/lazygit/pkg/commands/patch"
10+
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
911
"github.com/jesseduffield/lazygit/pkg/gui/types"
1012
)
1113

@@ -185,10 +187,20 @@ func (self *StagingController) TogglePanel() error {
185187
}
186188

187189
func (self *StagingController) ToggleStaged() error {
190+
if self.c.AppState.DiffContextSize == 0 {
191+
return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage,
192+
keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView))
193+
}
194+
188195
return self.applySelectionAndRefresh(self.staged)
189196
}
190197

191198
func (self *StagingController) DiscardSelection() error {
199+
if self.c.AppState.DiffContextSize == 0 {
200+
return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToDiscard,
201+
keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView))
202+
}
203+
192204
reset := func() error { return self.applySelectionAndRefresh(true) }
193205

194206
if !self.staged && !self.c.UserConfig().Gui.SkipDiscardChangeWarning {

pkg/i18n/english.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ type Actions struct {
926926
UnstageFile string
927927
UnstageAllFiles string
928928
StageAllFiles string
929+
NotEnoughContextToStage string
930+
NotEnoughContextToDiscard string
929931
IgnoreExcludeFile string
930932
IgnoreFileErr string
931933
ExcludeFile string
@@ -1913,6 +1915,8 @@ func EnglishTranslationSet() *TranslationSet {
19131915
UnstageFile: "Unstage file",
19141916
UnstageAllFiles: "Unstage all files",
19151917
StageAllFiles: "Stage all files",
1918+
NotEnoughContextToStage: "Staging or unstaging changes is not possible with a diff context size of 0. Increase the context using '%s'.",
1919+
NotEnoughContextToDiscard: "Discarding changes is not possible with a diff context size of 0. Increase the context using '%s'.",
19161920
IgnoreExcludeFile: "Ignore or exclude file",
19171921
IgnoreFileErr: "Cannot ignore .gitignore",
19181922
ExcludeFile: "Exclude file",

0 commit comments

Comments
 (0)