mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-17 01:42:45 +02:00
Fix some problems with patches if git diff
was customized with config (e.g. external
or noprefix
). (#3222)
- **PR Description** I encountered the problem that I couldn't extract changes into a new commit because I had difftastic as an external git tool configured. Add `diff.noprefix=false` config Option and also specify `--no-ext-diff` when doing the `git diff` after applying a patch. This fixes #3107. Though, there might be other config options that can cause problems, but fixing these common cases should be an improvement nevertheless.
This commit is contained in:
@ -239,6 +239,7 @@ func (self *CommitCommands) ShowCmdObj(sha string, filterPath string) oscommands
|
|||||||
|
|
||||||
extDiffCmd := self.UserConfig.Git.Paging.ExternalDiffCommand
|
extDiffCmd := self.UserConfig.Git.Paging.ExternalDiffCommand
|
||||||
cmdArgs := NewGitCmd("show").
|
cmdArgs := NewGitCmd("show").
|
||||||
|
Config("diff.noprefix=false").
|
||||||
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
|
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
|
||||||
ArgIfElse(extDiffCmd != "", "--ext-diff", "--no-ext-diff").
|
ArgIfElse(extDiffCmd != "", "--ext-diff", "--no-ext-diff").
|
||||||
Arg("--submodule").
|
Arg("--submodule").
|
||||||
|
@ -24,6 +24,7 @@ func NewCommitFileLoader(common *common.Common, cmd oscommands.ICmdObjBuilder) *
|
|||||||
// GetFilesInDiff get the specified commit files
|
// GetFilesInDiff get the specified commit files
|
||||||
func (self *CommitFileLoader) GetFilesInDiff(from string, to string, reverse bool) ([]*models.CommitFile, error) {
|
func (self *CommitFileLoader) GetFilesInDiff(from string, to string, reverse bool) ([]*models.CommitFile, error) {
|
||||||
cmdArgs := NewGitCmd("diff").
|
cmdArgs := NewGitCmd("diff").
|
||||||
|
Config("diff.noprefix=false").
|
||||||
Arg("--submodule").
|
Arg("--submodule").
|
||||||
Arg("--no-ext-diff").
|
Arg("--no-ext-diff").
|
||||||
Arg("--name-status").
|
Arg("--name-status").
|
||||||
|
@ -197,7 +197,7 @@ func TestCommitShowCmdObj(t *testing.T) {
|
|||||||
contextSize: 3,
|
contextSize: 3,
|
||||||
ignoreWhitespace: false,
|
ignoreWhitespace: false,
|
||||||
extDiffCmd: "",
|
extDiffCmd: "",
|
||||||
expected: []string{"-C", "/path/to/worktree", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890"},
|
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "Default case with filter path",
|
testName: "Default case with filter path",
|
||||||
@ -205,7 +205,7 @@ func TestCommitShowCmdObj(t *testing.T) {
|
|||||||
contextSize: 3,
|
contextSize: 3,
|
||||||
ignoreWhitespace: false,
|
ignoreWhitespace: false,
|
||||||
extDiffCmd: "",
|
extDiffCmd: "",
|
||||||
expected: []string{"-C", "/path/to/worktree", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--", "file.txt"},
|
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--", "file.txt"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "Show diff with custom context size",
|
testName: "Show diff with custom context size",
|
||||||
@ -213,7 +213,7 @@ func TestCommitShowCmdObj(t *testing.T) {
|
|||||||
contextSize: 77,
|
contextSize: 77,
|
||||||
ignoreWhitespace: false,
|
ignoreWhitespace: false,
|
||||||
extDiffCmd: "",
|
extDiffCmd: "",
|
||||||
expected: []string{"-C", "/path/to/worktree", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890"},
|
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "Show diff, ignoring whitespace",
|
testName: "Show diff, ignoring whitespace",
|
||||||
@ -221,7 +221,7 @@ func TestCommitShowCmdObj(t *testing.T) {
|
|||||||
contextSize: 77,
|
contextSize: 77,
|
||||||
ignoreWhitespace: true,
|
ignoreWhitespace: true,
|
||||||
extDiffCmd: "",
|
extDiffCmd: "",
|
||||||
expected: []string{"-C", "/path/to/worktree", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890", "--ignore-all-space"},
|
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--no-ext-diff", "--submodule", "--color=always", "--unified=77", "--stat", "--decorate", "-p", "1234567890", "--ignore-all-space"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "Show diff with external diff command",
|
testName: "Show diff with external diff command",
|
||||||
@ -229,7 +229,7 @@ func TestCommitShowCmdObj(t *testing.T) {
|
|||||||
contextSize: 3,
|
contextSize: 3,
|
||||||
ignoreWhitespace: false,
|
ignoreWhitespace: false,
|
||||||
extDiffCmd: "difft --color=always",
|
extDiffCmd: "difft --color=always",
|
||||||
expected: []string{"-C", "/path/to/worktree", "-c", "diff.external=difft --color=always", "show", "--ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890"},
|
expected: []string{"-C", "/path/to/worktree", "-c", "diff.external=difft --color=always", "-c", "diff.noprefix=false", "show", "--ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands {
|
|||||||
func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj {
|
func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj {
|
||||||
return self.cmd.New(
|
return self.cmd.New(
|
||||||
NewGitCmd("diff").
|
NewGitCmd("diff").
|
||||||
|
Config("diff.noprefix=false").
|
||||||
Arg("--submodule", "--no-ext-diff", "--color").
|
Arg("--submodule", "--no-ext-diff", "--color").
|
||||||
Arg(diffArgs...).
|
Arg(diffArgs...).
|
||||||
Dir(self.repoPaths.worktreePath).
|
Dir(self.repoPaths.worktreePath).
|
||||||
@ -24,6 +25,7 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj {
|
|||||||
|
|
||||||
func (self *DiffCommands) internalDiffCmdObj(diffArgs ...string) *GitCommandBuilder {
|
func (self *DiffCommands) internalDiffCmdObj(diffArgs ...string) *GitCommandBuilder {
|
||||||
return NewGitCmd("diff").
|
return NewGitCmd("diff").
|
||||||
|
Config("diff.noprefix=false").
|
||||||
Arg("--no-ext-diff", "--no-color").
|
Arg("--no-ext-diff", "--no-color").
|
||||||
Arg(diffArgs...).
|
Arg(diffArgs...).
|
||||||
Dir(self.repoPaths.worktreePath)
|
Dir(self.repoPaths.worktreePath)
|
||||||
@ -87,6 +89,7 @@ func (self *DiffCommands) OpenDiffToolCmdObj(opts DiffToolCmdOptions) oscommands
|
|||||||
func (self *DiffCommands) DiffIndexCmdObj(diffArgs ...string) oscommands.ICmdObj {
|
func (self *DiffCommands) DiffIndexCmdObj(diffArgs ...string) oscommands.ICmdObj {
|
||||||
return self.cmd.New(
|
return self.cmd.New(
|
||||||
NewGitCmd("diff-index").
|
NewGitCmd("diff-index").
|
||||||
|
Config("diff.noprefix=false").
|
||||||
Arg("--submodule", "--no-ext-diff", "--no-color", "--patch").
|
Arg("--submodule", "--no-ext-diff", "--no-color", "--patch").
|
||||||
Arg(diffArgs...).ToArgv(),
|
Arg(diffArgs...).ToArgv(),
|
||||||
)
|
)
|
||||||
|
@ -321,7 +321,11 @@ func (self *PatchCommands) PullPatchIntoNewCommit(
|
|||||||
// only some lines of a range of adjacent added lines. To solve this, we
|
// only some lines of a range of adjacent added lines. To solve this, we
|
||||||
// get the diff of HEAD and the original commit and then apply that.
|
// get the diff of HEAD and the original commit and then apply that.
|
||||||
func (self *PatchCommands) diffHeadAgainstCommit(commit *models.Commit) (string, error) {
|
func (self *PatchCommands) diffHeadAgainstCommit(commit *models.Commit) (string, error) {
|
||||||
cmdArgs := NewGitCmd("diff").Arg("HEAD.." + commit.Sha).ToArgv()
|
cmdArgs := NewGitCmd("diff").
|
||||||
|
Config("diff.noprefix=false").
|
||||||
|
Arg("--no-ext-diff").
|
||||||
|
Arg("HEAD.." + commit.Sha).
|
||||||
|
ToArgv()
|
||||||
|
|
||||||
return self.cmd.New(cmdArgs).RunWithOutput()
|
return self.cmd.New(cmdArgs).RunWithOutput()
|
||||||
}
|
}
|
||||||
|
@ -291,6 +291,7 @@ func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reve
|
|||||||
useExtDiff := extDiffCmd != "" && !plain
|
useExtDiff := extDiffCmd != "" && !plain
|
||||||
|
|
||||||
cmdArgs := NewGitCmd("diff").
|
cmdArgs := NewGitCmd("diff").
|
||||||
|
Config("diff.noprefix=false").
|
||||||
ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
|
ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
|
||||||
ArgIfElse(useExtDiff, "--ext-diff", "--no-ext-diff").
|
ArgIfElse(useExtDiff, "--ext-diff", "--no-ext-diff").
|
||||||
Arg("--submodule").
|
Arg("--submodule").
|
||||||
|
@ -348,7 +348,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) {
|
|||||||
ignoreWhitespace: false,
|
ignoreWhitespace: false,
|
||||||
contextSize: 3,
|
contextSize: 3,
|
||||||
runner: oscommands.NewFakeRunner(t).
|
runner: oscommands.NewFakeRunner(t).
|
||||||
ExpectGitArgs([]string{"-C", "/path/to/worktree", "diff", "--no-ext-diff", "--submodule", "--unified=3", "--no-renames", "--color=always", "1234567890", "0987654321", "--", "test.txt"}, expectedResult, nil),
|
ExpectGitArgs([]string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "diff", "--no-ext-diff", "--submodule", "--unified=3", "--no-renames", "--color=always", "1234567890", "0987654321", "--", "test.txt"}, expectedResult, nil),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "Show diff with custom context size",
|
testName: "Show diff with custom context size",
|
||||||
@ -359,7 +359,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) {
|
|||||||
ignoreWhitespace: false,
|
ignoreWhitespace: false,
|
||||||
contextSize: 123,
|
contextSize: 123,
|
||||||
runner: oscommands.NewFakeRunner(t).
|
runner: oscommands.NewFakeRunner(t).
|
||||||
ExpectGitArgs([]string{"-C", "/path/to/worktree", "diff", "--no-ext-diff", "--submodule", "--unified=123", "--no-renames", "--color=always", "1234567890", "0987654321", "--", "test.txt"}, expectedResult, nil),
|
ExpectGitArgs([]string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "diff", "--no-ext-diff", "--submodule", "--unified=123", "--no-renames", "--color=always", "1234567890", "0987654321", "--", "test.txt"}, expectedResult, nil),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "Default case (ignore whitespace)",
|
testName: "Default case (ignore whitespace)",
|
||||||
@ -370,7 +370,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) {
|
|||||||
ignoreWhitespace: true,
|
ignoreWhitespace: true,
|
||||||
contextSize: 3,
|
contextSize: 3,
|
||||||
runner: oscommands.NewFakeRunner(t).
|
runner: oscommands.NewFakeRunner(t).
|
||||||
ExpectGitArgs([]string{"-C", "/path/to/worktree", "diff", "--no-ext-diff", "--submodule", "--unified=3", "--no-renames", "--color=always", "1234567890", "0987654321", "--ignore-all-space", "--", "test.txt"}, expectedResult, nil),
|
ExpectGitArgs([]string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "diff", "--no-ext-diff", "--submodule", "--unified=3", "--no-renames", "--color=always", "1234567890", "0987654321", "--ignore-all-space", "--", "test.txt"}, expectedResult, nil),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package patch_building
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/config"
|
||||||
|
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||||
|
)
|
||||||
|
|
||||||
|
var MoveToIndexWorksEvenIfNoprefixIsSet = NewIntegrationTest(NewIntegrationTestArgs{
|
||||||
|
Description: "Moving a patch to the index works even if diff.noprefix or diff.external are set",
|
||||||
|
ExtraCmdArgs: []string{},
|
||||||
|
Skip: false,
|
||||||
|
SetupConfig: func(config *config.AppConfig) {},
|
||||||
|
SetupRepo: func(shell *Shell) {
|
||||||
|
shell.CreateFileAndAdd("file1", "file1 content\n")
|
||||||
|
shell.Commit("first commit")
|
||||||
|
|
||||||
|
// Test that this works even if custom diff options are set
|
||||||
|
shell.SetConfig("diff.noprefix", "true")
|
||||||
|
shell.SetConfig("diff.external", "echo")
|
||||||
|
},
|
||||||
|
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||||
|
t.Views().Commits().
|
||||||
|
Focus().
|
||||||
|
Lines(
|
||||||
|
Contains("first commit").IsSelected(),
|
||||||
|
).
|
||||||
|
PressEnter()
|
||||||
|
|
||||||
|
t.Views().CommitFiles().
|
||||||
|
IsFocused().
|
||||||
|
Lines(
|
||||||
|
Contains("file1").IsSelected(),
|
||||||
|
).
|
||||||
|
PressPrimaryAction()
|
||||||
|
|
||||||
|
t.Views().PatchBuildingSecondary().Content(Contains("+file1 content"))
|
||||||
|
|
||||||
|
t.Common().SelectPatchOption(Contains("Move patch out into index"))
|
||||||
|
|
||||||
|
t.Views().CommitFiles().IsFocused().
|
||||||
|
Lines(
|
||||||
|
Equals("(none)"),
|
||||||
|
)
|
||||||
|
|
||||||
|
t.Views().Files().
|
||||||
|
Lines(
|
||||||
|
Contains("A").Contains("file1"),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
@ -204,6 +204,7 @@ var tests = []*components.IntegrationTest{
|
|||||||
patch_building.MoveToIndexPartOfAdjacentAddedLines,
|
patch_building.MoveToIndexPartOfAdjacentAddedLines,
|
||||||
patch_building.MoveToIndexPartial,
|
patch_building.MoveToIndexPartial,
|
||||||
patch_building.MoveToIndexWithConflict,
|
patch_building.MoveToIndexWithConflict,
|
||||||
|
patch_building.MoveToIndexWorksEvenIfNoprefixIsSet,
|
||||||
patch_building.MoveToLaterCommit,
|
patch_building.MoveToLaterCommit,
|
||||||
patch_building.MoveToLaterCommitPartialHunk,
|
patch_building.MoveToLaterCommitPartialHunk,
|
||||||
patch_building.MoveToNewCommit,
|
patch_building.MoveToNewCommit,
|
||||||
|
Reference in New Issue
Block a user