Skip to content

Commit 3ff1be0

Browse files
authored
Disallow creating custom patches when the diff context size is 0 (#4522)
- **PR Description** This is very similar to what we are doing for staging or discarding hunks in the Files panel (see #4235). Git doesn't allow applying patches with a zero context size (unless you use the --unidiff-zero option, which is discouraged). Fixes #4521.
2 parents bb64e3c + 0496e3a commit 3ff1be0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pkg/gui/controllers/commits_files_controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"errors"
5+
"fmt"
56
"path/filepath"
67
"strings"
78

@@ -12,6 +13,7 @@ import (
1213
"github.com/jesseduffield/lazygit/pkg/constants"
1314
"github.com/jesseduffield/lazygit/pkg/gui/context"
1415
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
16+
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
1517
"github.com/jesseduffield/lazygit/pkg/gui/types"
1618
"github.com/jesseduffield/lazygit/pkg/utils"
1719
"github.com/samber/lo"
@@ -380,6 +382,11 @@ func (self *CommitFilesController) openDiffTool(node *filetree.CommitFileNode) e
380382
}
381383

382384
func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.CommitFileNode) error {
385+
if self.c.AppState.DiffContextSize == 0 {
386+
return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage,
387+
keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView))
388+
}
389+
383390
toggle := func() error {
384391
return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error {
385392
if !self.c.Git().Patch.PatchBuilder.Active() {
@@ -471,6 +478,11 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode
471478
return self.handleToggleCommitFileDirCollapsed(node)
472479
}
473480

481+
if self.c.AppState.DiffContextSize == 0 {
482+
return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage,
483+
keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView))
484+
}
485+
474486
enterTheFile := func() error {
475487
if !self.c.Git().Patch.PatchBuilder.Active() {
476488
if err := self.startPatchBuilder(); err != nil {

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ type Actions struct {
984984
ResolveConflictByDeletingFile string
985985
NotEnoughContextToStage string
986986
NotEnoughContextToDiscard string
987+
NotEnoughContextForCustomPatch string
987988
IgnoreExcludeFile string
988989
IgnoreFileErr string
989990
ExcludeFile string
@@ -2031,6 +2032,7 @@ func EnglishTranslationSet() *TranslationSet {
20312032
ResolveConflictByDeletingFile: "Resolve by deleting file",
20322033
NotEnoughContextToStage: "Staging or unstaging changes is not possible with a diff context size of 0. Increase the context using '%s'.",
20332034
NotEnoughContextToDiscard: "Discarding changes is not possible with a diff context size of 0. Increase the context using '%s'.",
2035+
NotEnoughContextForCustomPatch: "Creating custom patches is not possible with a diff context size of 0. Increase the context using '%s'.",
20342036
IgnoreExcludeFile: "Ignore or exclude file",
20352037
IgnoreFileErr: "Cannot ignore .gitignore",
20362038
ExcludeFile: "Exclude file",

0 commit comments

Comments
 (0)