From 0496e3af503e1ab02c9b8fc0d011a49f44960b5a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 29 Apr 2025 10:21:18 +0200 Subject: [PATCH] Disallow creating custom patches when the diff context size is 0 This is very similar to what we are doing for staging or discarding hunks in the Files panel. Git doesn't allow applying patches with a zero context size (unless you use the --unidiff-zero option, which is discouraged). --- pkg/gui/controllers/commits_files_controller.go | 12 ++++++++++++ pkg/i18n/english.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index e1818789c..8beeb6ff2 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -2,6 +2,7 @@ package controllers import ( "errors" + "fmt" "path/filepath" "strings" @@ -12,6 +13,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/filetree" + "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" @@ -380,6 +382,11 @@ func (self *CommitFilesController) openDiffTool(node *filetree.CommitFileNode) e } func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.CommitFileNode) error { + if self.c.AppState.DiffContextSize == 0 { + return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage, + keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) + } + toggle := func() error { return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error { if !self.c.Git().Patch.PatchBuilder.Active() { @@ -471,6 +478,11 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode return self.handleToggleCommitFileDirCollapsed(node) } + if self.c.AppState.DiffContextSize == 0 { + return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage, + keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) + } + enterTheFile := func() error { if !self.c.Git().Patch.PatchBuilder.Active() { if err := self.startPatchBuilder(); err != nil { diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index baf3e20de..a85f7202f 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -984,6 +984,7 @@ type Actions struct { ResolveConflictByDeletingFile string NotEnoughContextToStage string NotEnoughContextToDiscard string + NotEnoughContextForCustomPatch string IgnoreExcludeFile string IgnoreFileErr string ExcludeFile string @@ -2031,6 +2032,7 @@ func EnglishTranslationSet() *TranslationSet { ResolveConflictByDeletingFile: "Resolve by deleting file", 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'.", + NotEnoughContextForCustomPatch: "Creating custom patches 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",