summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorDerTeta <derteta@gmx.de>2021-08-11 22:33:29 +0200
committerJesse Duffield <jessedduffield@gmail.com>2021-12-06 22:37:28 +1100
commitca88620e8fd68df4cec86007679665ccdda5c0e7 (patch)
treeeb0e1341db7ba6fbc37f7e682e297d79afddb00c /pkg/commands
parent9feaf5d70f7075199c4b517f7fe171318d951d5d (diff)
Use `DiffContextSize` in `WorkTreeFileDiffCmdStr`
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/files.go3
-rw-r--r--pkg/commands/files_test.go35
2 files changed, 32 insertions, 6 deletions
diff --git a/pkg/commands/files.go b/pkg/commands/files.go
index 07c4c4d28..bf692595a 100644
--- a/pkg/commands/files.go
+++ b/pkg/commands/files.go
@@ -207,6 +207,7 @@ func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFile, plain bool, cache
colorArg := c.colorArg()
quotedPath := c.OSCommand.Quote(node.GetPath())
ignoreWhitespaceArg := ""
+ contextSize := c.Config.GetUserConfig().Git.DiffContextSize
if cached {
cachedArg = "--cached"
}
@@ -220,7 +221,7 @@ func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFile, plain bool, cache
ignoreWhitespaceArg = "--ignore-all-space"
}
- return fmt.Sprintf("git diff --submodule --no-ext-diff --color=%s %s %s %s %s", colorArg, ignoreWhitespaceArg, cachedArg, trackedArg, quotedPath)
+ return fmt.Sprintf("git diff --submodule --no-ext-diff --unified=%d --color=%s %s %s %s %s", contextSize, colorArg, ignoreWhitespaceArg, cachedArg, trackedArg, quotedPath)
}
func (c *GitCommand) ApplyPatch(patch string, flags ...string) error {
diff --git a/pkg/commands/files_test.go b/pkg/commands/files_test.go
index f72b47ca0..acbdde8cc 100644
--- a/pkg/commands/files_test.go
+++ b/pkg/commands/files_test.go
@@ -317,6 +317,7 @@ func TestGitCommandDiff(t *testing.T) {
plain bool
cached bool
ignoreWhitespace bool
+ contextSize int
}
scenarios := []scenario{
@@ -324,7 +325,7 @@ func TestGitCommandDiff(t *testing.T) {
"Default case",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--color=always", "--", "test.txt"}, args)
+ assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--", "test.txt"}, args)
return secureexec.Command("echo")
},
@@ -336,12 +337,13 @@ func TestGitCommandDiff(t *testing.T) {
false,
false,
false,
+ 3,
},
{
"cached",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--color=always", "--cached", "--", "test.txt"}, args)
+ assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--cached", "--", "test.txt"}, args)
return secureexec.Command("echo")
},
@@ -353,12 +355,13 @@ func TestGitCommandDiff(t *testing.T) {
false,
true,
false,
+ 3,
},
{
"plain",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--color=never", "--", "test.txt"}, args)
+ assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=never", "--", "test.txt"}, args)
return secureexec.Command("echo")
},
@@ -370,12 +373,13 @@ func TestGitCommandDiff(t *testing.T) {
true,
false,
false,
+ 3,
},
{
"File not tracked and file has no staged changes",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--color=always", "--no-index", "--", "/dev/null", "test.txt"}, args)
+ assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--no-index", "--", "/dev/null", "test.txt"}, args)
return secureexec.Command("echo")
},
@@ -387,12 +391,13 @@ func TestGitCommandDiff(t *testing.T) {
false,
false,
false,
+ 3,
},
{
"Default case (ignore whitespace)",
func(cmd string, args ...string) *exec.Cmd {
assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--color=always", "--ignore-all-space", "--", "test.txt"}, args)
+ assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--ignore-all-space", "--", "test.txt"}, args)
return secureexec.Command("echo")
},
@@ -404,6 +409,25 @@ func TestGitCommandDiff(t *testing.T) {
false,
false,
true,
+ 3,
+ },
+ {
+ "Show diff with custom context size",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+ assert.EqualValues(t, []string{"diff", "--submodule", "--no-ext-diff", "--unified=17", "--color=always", "--", "test.txt"}, args)
+
+ return secureexec.Command("echo")
+ },
+ &models.File{
+ Name: "test.txt",
+ HasStagedChanges: false,
+ Tracked: true,
+ },
+ false,
+ false,
+ false,
+ 17,
},
}
@@ -411,6 +435,7 @@ func TestGitCommandDiff(t *testing.T) {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.OSCommand.Command = s.command
+ gitCmd.Config.GetUserConfig().Git.DiffContextSize = s.contextSize
gitCmd.WorktreeFileDiff(s.file, s.plain, s.cached, s.ignoreWhitespace)
})
}