1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-31 23:19:40 +02:00

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.
This commit is contained in:
Stefan Haller 2025-04-29 11:30:11 +02:00 committed by GitHub
commit 3ff1be0788
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package controllers
import ( import (
"errors" "errors"
"fmt"
"path/filepath" "path/filepath"
"strings" "strings"
@ -12,6 +13,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/constants"
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/filetree" "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/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo" "github.com/samber/lo"
@ -380,6 +382,11 @@ func (self *CommitFilesController) openDiffTool(node *filetree.CommitFileNode) e
} }
func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.CommitFileNode) error { 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 { toggle := func() error {
return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error { return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error {
if !self.c.Git().Patch.PatchBuilder.Active() { if !self.c.Git().Patch.PatchBuilder.Active() {
@ -471,6 +478,11 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode
return self.handleToggleCommitFileDirCollapsed(node) 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 { enterTheFile := func() error {
if !self.c.Git().Patch.PatchBuilder.Active() { if !self.c.Git().Patch.PatchBuilder.Active() {
if err := self.startPatchBuilder(); err != nil { if err := self.startPatchBuilder(); err != nil {

View File

@ -984,6 +984,7 @@ type Actions struct {
ResolveConflictByDeletingFile string ResolveConflictByDeletingFile string
NotEnoughContextToStage string NotEnoughContextToStage string
NotEnoughContextToDiscard string NotEnoughContextToDiscard string
NotEnoughContextForCustomPatch string
IgnoreExcludeFile string IgnoreExcludeFile string
IgnoreFileErr string IgnoreFileErr string
ExcludeFile string ExcludeFile string
@ -2031,6 +2032,7 @@ func EnglishTranslationSet() *TranslationSet {
ResolveConflictByDeletingFile: "Resolve by deleting file", 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'.", 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'.", 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", IgnoreExcludeFile: "Ignore or exclude file",
IgnoreFileErr: "Cannot ignore .gitignore", IgnoreFileErr: "Cannot ignore .gitignore",
ExcludeFile: "Exclude file", ExcludeFile: "Exclude file",