diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-02-18 15:34:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-18 15:34:09 +0100 |
commit | 43020184373abb624971bb81aebd34ea214addee (patch) | |
tree | ff71e43fdf84b59c3efc83b291a955ae5a88f76d | |
parent | a2ff2e6dd92c7b1bcfa41798548d3cd4f439dfa8 (diff) | |
parent | 236f42879ce9174600c9944f136e0fe266287413 (diff) |
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.
-rw-r--r-- | pkg/commands/git_commands/commit.go | 1 | ||||
-rw-r--r-- | pkg/commands/git_commands/commit_file_loader.go | 1 | ||||
-rw-r--r-- | pkg/commands/git_commands/commit_test.go | 10 | ||||
-rw-r--r-- | pkg/commands/git_commands/diff.go | 3 | ||||
-rw-r--r-- | pkg/commands/git_commands/patch.go | 6 | ||||
-rw-r--r-- | pkg/commands/git_commands/working_tree.go | 1 | ||||
-rw-r--r-- | pkg/commands/git_commands/working_tree_test.go | 6 | ||||
-rw-r--r-- | pkg/integration/tests/patch_building/move_to_index_works_even_if_noprefix_is_set.go | 50 | ||||
-rw-r--r-- | pkg/integration/tests/test_list.go | 1 |
9 files changed, 70 insertions, 9 deletions
diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index dfb0b4085..12806dafc 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -239,6 +239,7 @@ func (self *CommitCommands) ShowCmdObj(sha string, filterPath string) oscommands extDiffCmd := self.UserConfig.Git.Paging.ExternalDiffCommand cmdArgs := NewGitCmd("show"). + Config("diff.noprefix=false"). ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd). ArgIfElse(extDiffCmd != "", "--ext-diff", "--no-ext-diff"). Arg("--submodule"). diff --git a/pkg/commands/git_commands/commit_file_loader.go b/pkg/commands/git_commands/commit_file_loader.go index 7abdc74c5..68faf31ca 100644 --- a/pkg/commands/git_commands/commit_file_loader.go +++ b/pkg/commands/git_commands/commit_file_loader.go @@ -24,6 +24,7 @@ func NewCommitFileLoader(common *common.Common, cmd oscommands.ICmdObjBuilder) * // GetFilesInDiff get the specified commit files func (self *CommitFileLoader) GetFilesInDiff(from string, to string, reverse bool) ([]*models.CommitFile, error) { cmdArgs := NewGitCmd("diff"). + Config("diff.noprefix=false"). Arg("--submodule"). Arg("--no-ext-diff"). Arg("--name-status"). diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index a2b674eeb..34b064d67 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -197,7 +197,7 @@ func TestCommitShowCmdObj(t *testing.T) { contextSize: 3, ignoreWhitespace: false, 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", @@ -205,7 +205,7 @@ func TestCommitShowCmdObj(t *testing.T) { contextSize: 3, ignoreWhitespace: false, 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", @@ -213,7 +213,7 @@ func TestCommitShowCmdObj(t *testing.T) { contextSize: 77, ignoreWhitespace: false, 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", @@ -221,7 +221,7 @@ func TestCommitShowCmdObj(t *testing.T) { contextSize: 77, ignoreWhitespace: true, 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", @@ -229,7 +229,7 @@ func TestCommitShowCmdObj(t *testing.T) { contextSize: 3, ignoreWhitespace: false, 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"}, }, } diff --git a/pkg/commands/git_commands/diff.go b/pkg/commands/git_commands/diff.go index 73b30bc48..9f81bb91d 100644 --- a/pkg/commands/git_commands/diff.go +++ b/pkg/commands/git_commands/diff.go @@ -15,6 +15,7 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands { func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj { return self.cmd.New( NewGitCmd("diff"). + Config("diff.noprefix=false"). Arg("--submodule", "--no-ext-diff", "--color"). Arg(diffArgs...). Dir(self.repoPaths.worktreePath). @@ -24,6 +25,7 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj { func (self *DiffCommands) internalDiffCmdObj(diffArgs ...string) *GitCommandBuilder { return NewGitCmd("diff"). + Config("diff.noprefix=false"). Arg("--no-ext-diff", "--no-color"). Arg(diffArgs...). Dir(self.repoPaths.worktreePath) @@ -87,6 +89,7 @@ func (self *DiffCommands) OpenDiffToolCmdObj(opts DiffToolCmdOptions) oscommands func (self *DiffCommands) DiffIndexCmdObj(diffArgs ...string) oscommands.ICmdObj { return self.cmd.New( NewGitCmd("diff-index"). + Config("diff.noprefix=false"). Arg("--submodule", "--no-ext-diff", "--no-color", "--patch"). Arg(diffArgs...).ToArgv(), ) diff --git a/pkg/commands/git_commands/patch.go b/pkg/commands/git_commands/patch.go index 749e5dc22..c632e35ae 100644 --- a/pkg/commands/git_commands/patch.go +++ b/pkg/commands/git_commands/patch.go @@ -321,7 +321,11 @@ func (self *PatchCommands) PullPatchIntoNewCommit( // 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. 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() } diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 054a272d4..2bb82578d 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -291,6 +291,7 @@ func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reve useExtDiff := extDiffCmd != "" && !plain cmdArgs := NewGitCmd("diff"). + Config("diff.noprefix=false"). ConfigIf(useExtDiff, "diff.external="+extDiffCmd). ArgIfElse(useExtDiff, "--ext-diff", "--no-ext-diff"). Arg("--submodule"). diff --git a/pkg/commands/git_commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go index 51fa88b7f..08255e247 100644 --- a/pkg/commands/git_commands/working_tree_test.go +++ b/pkg/commands/git_commands/working_tree_test.go @@ -348,7 +348,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) { ignoreWhitespace: false, contextSize: 3, 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", @@ -359,7 +359,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) { ignoreWhitespace: false, contextSize: 123, 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)", @@ -370,7 +370,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) { ignoreWhitespace: true, contextSize: 3, 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), }, } diff --git a/pkg/integration/tests/patch_building/move_to_index_works_even_if_noprefix_is_set.go b/pkg/integration/tests/patch_building/move_to_index_works_even_if_noprefix_is_set.go new file mode 100644 index 000000000..391223c60 --- /dev/null +++ b/pkg/integration/tests/patch_building/move_to_index_works_even_if_noprefix_is_set.go @@ -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"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 3abc41853..b770b953d 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -204,6 +204,7 @@ var tests = []*components.IntegrationTest{ patch_building.MoveToIndexPartOfAdjacentAddedLines, patch_building.MoveToIndexPartial, patch_building.MoveToIndexWithConflict, + patch_building.MoveToIndexWorksEvenIfNoprefixIsSet, patch_building.MoveToLaterCommit, patch_building.MoveToLaterCommitPartialHunk, patch_building.MoveToNewCommit, |