summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorstk <stk@ableton.com>2023-02-03 20:20:20 +0100
committerstk <stk@ableton.com>2023-02-07 12:14:29 +0100
commit5bb619821925cb653864f073587850a54fc2e6e1 (patch)
tree8636691ea584fc050741b56bfda9f5a7afa81d2b /pkg/commands
parentbbaeab68e1f106b5412d85dc2bd8d4aec8a6b258 (diff)
Allow ignoring whitespace in diff in commits panel
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git.go7
-rw-r--r--pkg/commands/git_commands/commit.go9
-rw-r--r--pkg/commands/git_commands/commit_test.go38
-rw-r--r--pkg/commands/git_commands/working_tree.go19
-rw-r--r--pkg/commands/git_commands/working_tree_test.go54
5 files changed, 82 insertions, 45 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index ad3831b01..e2a31c009 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -118,7 +118,12 @@ func NewGitCommandAux(
rebaseCommands := git_commands.NewRebaseCommands(gitCommon, commitCommands, workingTreeCommands)
stashCommands := git_commands.NewStashCommands(gitCommon, fileLoader, workingTreeCommands)
// TODO: have patch manager take workingTreeCommands in its entirety
- patchManager := patch.NewPatchManager(cmn.Log, workingTreeCommands.ApplyPatch, workingTreeCommands.ShowFileDiff)
+ patchManager := patch.NewPatchManager(cmn.Log, workingTreeCommands.ApplyPatch,
+ func(from string, to string, reverse bool, filename string, plain bool) (string, error) {
+ // TODO: make patch manager take Gui.IgnoreWhitespaceInDiffView into
+ // account. For now we just pass false.
+ return workingTreeCommands.ShowFileDiff(from, to, reverse, filename, plain, false)
+ })
patchCommands := git_commands.NewPatchCommands(gitCommon, rebaseCommands, commitCommands, statusCommands, stashCommands, patchManager)
bisectCommands := git_commands.NewBisectCommands(gitCommon)
diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go
index 2cba7c265..0a162a6b6 100644
--- a/pkg/commands/git_commands/commit.go
+++ b/pkg/commands/git_commands/commit.go
@@ -149,14 +149,19 @@ func (self *CommitCommands) AmendHeadCmdObj() oscommands.ICmdObj {
return self.cmd.New("git commit --amend --no-edit --allow-empty")
}
-func (self *CommitCommands) ShowCmdObj(sha string, filterPath string) oscommands.ICmdObj {
+func (self *CommitCommands) ShowCmdObj(sha string, filterPath string, ignoreWhitespace bool) oscommands.ICmdObj {
contextSize := self.UserConfig.Git.DiffContextSize
filterPathArg := ""
if filterPath != "" {
filterPathArg = fmt.Sprintf(" -- %s", self.cmd.Quote(filterPath))
}
+ ignoreWhitespaceArg := ""
+ if ignoreWhitespace {
+ ignoreWhitespaceArg = " --ignore-all-space"
+ }
- cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --no-renames --stat -p %s%s", self.UserConfig.Git.Paging.ColorArg, contextSize, sha, filterPathArg)
+ cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --no-renames --stat -p %s%s%s",
+ self.UserConfig.Git.Paging.ColorArg, contextSize, sha, ignoreWhitespaceArg, filterPathArg)
return self.cmd.New(cmdStr).DontLog()
}
diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go
index ac129098d..713c9fffa 100644
--- a/pkg/commands/git_commands/commit_test.go
+++ b/pkg/commands/git_commands/commit_test.go
@@ -177,30 +177,34 @@ func TestCommitCreateFixupCommit(t *testing.T) {
func TestCommitShowCmdObj(t *testing.T) {
type scenario struct {
- testName string
- filterPath string
- contextSize int
- expected string
+ testName string
+ filterPath string
+ contextSize int
+ ignoreWhitespace bool
+ expected string
}
scenarios := []scenario{
{
- testName: "Default case without filter path",
- filterPath: "",
- contextSize: 3,
- expected: "git show --submodule --color=always --unified=3 --no-renames --stat -p 1234567890",
+ testName: "Default case without filter path",
+ filterPath: "",
+ contextSize: 3,
+ ignoreWhitespace: false,
+ expected: "git show --submodule --color=always --unified=3 --no-renames --stat -p 1234567890",
},
{
- testName: "Default case with filter path",
- filterPath: "file.txt",
- contextSize: 3,
- expected: `git show --submodule --color=always --unified=3 --no-renames --stat -p 1234567890 -- "file.txt"`,
+ testName: "Default case with filter path",
+ filterPath: "file.txt",
+ contextSize: 3,
+ ignoreWhitespace: true,
+ expected: `git show --submodule --color=always --unified=3 --no-renames --stat -p 1234567890 --ignore-all-space -- "file.txt"`,
},
{
- testName: "Show diff with custom context size",
- filterPath: "",
- contextSize: 77,
- expected: "git show --submodule --color=always --unified=77 --no-renames --stat -p 1234567890",
+ testName: "Show diff with custom context size",
+ filterPath: "",
+ contextSize: 77,
+ ignoreWhitespace: false,
+ expected: "git show --submodule --color=always --unified=77 --no-renames --stat -p 1234567890",
},
}
@@ -212,7 +216,7 @@ func TestCommitShowCmdObj(t *testing.T) {
instance := buildCommitCommands(commonDeps{userConfig: userConfig})
- cmdStr := instance.ShowCmdObj("1234567890", s.filterPath).ToString()
+ cmdStr := instance.ShowCmdObj("1234567890", s.filterPath, s.ignoreWhitespace).ToString()
assert.Equal(t, s.expected, cmdStr)
})
}
diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go
index 7e08c348b..b7ebe0d30 100644
--- a/pkg/commands/git_commands/working_tree.go
+++ b/pkg/commands/git_commands/working_tree.go
@@ -287,11 +287,15 @@ func (self *WorkingTreeCommands) SaveTemporaryPatch(patch string) (string, error
// ShowFileDiff get the diff of specified from and to. Typically this will be used for a single commit so it'll be 123abc^..123abc
// but when we're in diff mode it could be any 'from' to any 'to'. The reverse flag is also here thanks to diff mode.
-func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bool, fileName string, plain bool) (string, error) {
- return self.ShowFileDiffCmdObj(from, to, reverse, fileName, plain).RunWithOutput()
+func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bool, fileName string, plain bool,
+ ignoreWhitespace bool,
+) (string, error) {
+ return self.ShowFileDiffCmdObj(from, to, reverse, fileName, plain, ignoreWhitespace).RunWithOutput()
}
-func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) oscommands.ICmdObj {
+func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool,
+ ignoreWhitespace bool,
+) oscommands.ICmdObj {
colorArg := self.UserConfig.Git.Paging.ColorArg
contextSize := self.UserConfig.Git.DiffContextSize
if plain {
@@ -303,11 +307,16 @@ func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reve
reverseFlag = " -R"
}
+ ignoreWhitespaceFlag := ""
+ if ignoreWhitespace {
+ ignoreWhitespaceFlag = " --ignore-all-space"
+ }
+
return self.cmd.
New(
fmt.Sprintf(
- "git diff --submodule --no-ext-diff --unified=%d --no-renames --color=%s%s%s%s -- %s",
- contextSize, colorArg, pad(from), pad(to), reverseFlag, self.cmd.Quote(fileName)),
+ "git diff --submodule --no-ext-diff --unified=%d --no-renames --color=%s%s%s%s%s -- %s",
+ contextSize, colorArg, pad(from), pad(to), reverseFlag, ignoreWhitespaceFlag, self.cmd.Quote(fileName)),
).
DontLog()
}
diff --git a/pkg/commands/git_commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go
index 049961541..ca2881efe 100644
--- a/pkg/commands/git_commands/working_tree_test.go
+++ b/pkg/commands/git_commands/working_tree_test.go
@@ -323,38 +323,52 @@ func TestWorkingTreeDiff(t *testing.T) {
func TestWorkingTreeShowFileDiff(t *testing.T) {
type scenario struct {
- testName string
- from string
- to string
- reverse bool
- plain bool
- contextSize int
- runner *oscommands.FakeCmdObjRunner
+ testName string
+ from string
+ to string
+ reverse bool
+ plain bool
+ ignoreWhitespace bool
+ contextSize int
+ runner *oscommands.FakeCmdObjRunner
}
const expectedResult = "pretend this is an actual git diff"
scenarios := []scenario{
{
- testName: "Default case",
- from: "1234567890",
- to: "0987654321",
- reverse: false,
- plain: false,
- contextSize: 3,
+ testName: "Default case",
+ from: "1234567890",
+ to: "0987654321",
+ reverse: false,
+ plain: false,
+ ignoreWhitespace: false,
+ contextSize: 3,
runner: oscommands.NewFakeRunner(t).
Expect(`git diff --submodule --no-ext-diff --unified=3 --no-renames --color=always 1234567890 0987654321 -- "test.txt"`, expectedResult, nil),
},
{
- testName: "Show diff with custom context size",
- from: "1234567890",
- to: "0987654321",
- reverse: false,
- plain: false,
- contextSize: 123,
+ testName: "Show diff with custom context size",
+ from: "1234567890",
+ to: "0987654321",
+ reverse: false,
+ plain: false,
+ ignoreWhitespace: false,
+ contextSize: 123,
runner: oscommands.NewFakeRunner(t).
Expect(`git diff --submodule --no-ext-diff --unified=123 --no-renames --color=always 1234567890 0987654321 -- "test.txt"`, expectedResult, nil),
},
+ {
+ testName: "Default case (ignore whitespace)",
+ from: "1234567890",
+ to: "0987654321",
+ reverse: false,
+ plain: false,
+ ignoreWhitespace: true,
+ contextSize: 3,
+ runner: oscommands.NewFakeRunner(t).
+ Expect(`git diff --submodule --no-ext-diff --unified=3 --no-renames --color=always 1234567890 0987654321 --ignore-all-space -- "test.txt"`, expectedResult, nil),
+ },
}
for _, s := range scenarios {
@@ -365,7 +379,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) {
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig})
- result, err := instance.ShowFileDiff(s.from, s.to, s.reverse, "test.txt", s.plain)
+ result, err := instance.ShowFileDiff(s.from, s.to, s.reverse, "test.txt", s.plain, s.ignoreWhitespace)
assert.NoError(t, err)
assert.Equal(t, expectedResult, result)
s.runner.CheckForMissingCalls()