summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/commands/git_commands')
-rw-r--r--pkg/commands/git_commands/working_tree.go21
-rw-r--r--pkg/commands/git_commands/working_tree_test.go43
2 files changed, 37 insertions, 27 deletions
diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go
index 084643532..06a64baa6 100644
--- a/pkg/commands/git_commands/working_tree.go
+++ b/pkg/commands/git_commands/working_tree.go
@@ -233,7 +233,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain
ignoreWhitespaceArg := ""
contextSize := self.UserConfig.Git.DiffContextSize
if cached {
- cachedArg = "--cached"
+ cachedArg = " --cached"
}
if !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached {
trackedArg = "--no-index -- /dev/null"
@@ -242,10 +242,10 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain
colorArg = "never"
}
if ignoreWhitespace {
- ignoreWhitespaceArg = "--ignore-all-space"
+ ignoreWhitespaceArg = " --ignore-all-space"
}
- cmdStr := fmt.Sprintf("git diff --submodule --no-ext-diff --unified=%d --color=%s %s %s %s %s", contextSize, colorArg, ignoreWhitespaceArg, cachedArg, trackedArg, quotedPath)
+ cmdStr := fmt.Sprintf("git diff --submodule --no-ext-diff --unified=%d --color=%s%s%s %s %s", contextSize, colorArg, ignoreWhitespaceArg, cachedArg, trackedArg, quotedPath)
return self.cmd.New(cmdStr).DontLog()
}
@@ -280,14 +280,14 @@ func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reve
reverseFlag := ""
if reverse {
- reverseFlag = " -R "
+ reverseFlag = " -R"
}
return self.cmd.
New(
fmt.Sprintf(
- "git diff --submodule --no-ext-diff --unified=%d --no-renames --color=%s %s %s %s -- %s",
- contextSize, colorArg, from, to, reverseFlag, self.cmd.Quote(fileName)),
+ "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)),
).
DontLog()
}
@@ -345,3 +345,12 @@ func (self *WorkingTreeCommands) ResetSoft(ref string) error {
func (self *WorkingTreeCommands) ResetMixed(ref string) error {
return self.cmd.New("git reset --mixed " + self.cmd.Quote(ref)).Run()
}
+
+// so that we don't have unnecessary space in our commands we use this helper function to prepend spaces to args so that in the format string we can go '%s%s%s' and if any args are missing we won't have gaps.
+func pad(str string) string {
+ if str == "" {
+ return ""
+ }
+
+ return " " + str
+}
diff --git a/pkg/commands/git_commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go
index ef3aedcf9..8b2c70714 100644
--- a/pkg/commands/git_commands/working_tree_test.go
+++ b/pkg/commands/git_commands/working_tree_test.go
@@ -15,7 +15,7 @@ import (
func TestWorkingTreeStageFile(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "add", "--", "test.txt"}, "", nil)
+ Expect(`git add -- "test.txt"`, "", nil)
instance := buildWorkingTreeCommands(commonDeps{runner: runner})
@@ -36,7 +36,7 @@ func TestWorkingTreeUnstageFile(t *testing.T) {
testName: "Remove an untracked file from staging",
reset: false,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "rm", "--cached", "--force", "--", "test.txt"}, "", nil),
+ Expect(`git rm --cached --force -- "test.txt"`, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
@@ -45,7 +45,7 @@ func TestWorkingTreeUnstageFile(t *testing.T) {
testName: "Remove a tracked file from staging",
reset: true,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "reset", "HEAD", "--", "test.txt"}, "", nil),
+ Expect(`git reset HEAD -- "test.txt"`, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
@@ -82,7 +82,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
},
removeFile: func(string) error { return nil },
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "reset", "--", "test"}, "", errors.New("error")),
+ Expect(`git reset -- "test"`, "", errors.New("error")),
expectedError: "error",
},
{
@@ -107,7 +107,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
},
removeFile: func(string) error { return nil },
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "checkout", "--", "test"}, "", errors.New("error")),
+ Expect(`git checkout -- "test"`, "", errors.New("error")),
expectedError: "error",
},
{
@@ -119,7 +119,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
},
removeFile: func(string) error { return nil },
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "checkout", "--", "test"}, "", nil),
+ Expect(`git checkout -- "test"`, "", nil),
expectedError: "",
},
{
@@ -131,8 +131,8 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
},
removeFile: func(string) error { return nil },
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "reset", "--", "test"}, "", nil).
- ExpectArgs([]string{"git", "checkout", "--", "test"}, "", nil),
+ Expect(`git reset -- "test"`, "", nil).
+ Expect(`git checkout -- "test"`, "", nil),
expectedError: "",
},
{
@@ -144,8 +144,8 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
},
removeFile: func(string) error { return nil },
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "reset", "--", "test"}, "", nil).
- ExpectArgs([]string{"git", "checkout", "--", "test"}, "", nil),
+ Expect(`git reset -- "test"`, "", nil).
+ Expect(`git checkout -- "test"`, "", nil),
expectedError: "",
},
{
@@ -161,7 +161,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
return nil
},
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "reset", "--", "test"}, "", nil),
+ Expect(`git reset -- "test"`, "", nil),
expectedError: "",
},
{
@@ -223,7 +223,7 @@ func TestWorkingTreeDiff(t *testing.T) {
ignoreWhitespace: false,
contextSize: 3,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--", "test.txt"}, expectedResult, nil),
+ Expect(`git diff --submodule --no-ext-diff --unified=3 --color=always -- "test.txt"`, expectedResult, nil),
},
{
testName: "cached",
@@ -237,7 +237,7 @@ func TestWorkingTreeDiff(t *testing.T) {
ignoreWhitespace: false,
contextSize: 3,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--cached", "--", "test.txt"}, expectedResult, nil),
+ Expect(`git diff --submodule --no-ext-diff --unified=3 --color=always --cached -- "test.txt"`, expectedResult, nil),
},
{
testName: "plain",
@@ -251,7 +251,7 @@ func TestWorkingTreeDiff(t *testing.T) {
ignoreWhitespace: false,
contextSize: 3,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=never", "--", "test.txt"}, expectedResult, nil),
+ Expect(`git diff --submodule --no-ext-diff --unified=3 --color=never -- "test.txt"`, expectedResult, nil),
},
{
testName: "File not tracked and file has no staged changes",
@@ -265,7 +265,7 @@ func TestWorkingTreeDiff(t *testing.T) {
ignoreWhitespace: false,
contextSize: 3,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--no-index", "--", "/dev/null", "test.txt"}, expectedResult, nil),
+ Expect(`git diff --submodule --no-ext-diff --unified=3 --color=always --no-index -- /dev/null "test.txt"`, expectedResult, nil),
},
{
testName: "Default case (ignore whitespace)",
@@ -279,7 +279,7 @@ func TestWorkingTreeDiff(t *testing.T) {
ignoreWhitespace: true,
contextSize: 3,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=3", "--color=always", "--ignore-all-space", "--", "test.txt"}, expectedResult, nil),
+ Expect(`git diff --submodule --no-ext-diff --unified=3 --color=always --ignore-all-space -- "test.txt"`, expectedResult, nil),
},
{
testName: "Show diff with custom context size",
@@ -293,7 +293,7 @@ func TestWorkingTreeDiff(t *testing.T) {
ignoreWhitespace: false,
contextSize: 17,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=17", "--color=always", "--", "test.txt"}, expectedResult, nil),
+ Expect(`git diff --submodule --no-ext-diff --unified=17 --color=always -- "test.txt"`, expectedResult, nil),
},
}
@@ -333,7 +333,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) {
plain: false,
contextSize: 3,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=3", "--no-renames", "--color=always", "1234567890", "0987654321", "--", "test.txt"}, expectedResult, nil),
+ 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",
@@ -343,7 +343,7 @@ func TestWorkingTreeShowFileDiff(t *testing.T) {
plain: false,
contextSize: 123,
runner: oscommands.NewFakeRunner(t).
- ExpectArgs([]string{"git", "diff", "--submodule", "--no-ext-diff", "--unified=123", "--no-renames", "--color=always", "1234567890", "0987654321", "--", "test.txt"}, expectedResult, nil),
+ Expect(`git diff --submodule --no-ext-diff --unified=123 --no-renames --color=always 1234567890 0987654321 -- "test.txt"`, expectedResult, nil),
},
}
@@ -416,8 +416,9 @@ func TestWorkingTreeApplyPatch(t *testing.T) {
expectFn := func(regexStr string, errToReturn error) func(cmdObj oscommands.ICmdObj) (string, error) {
return func(cmdObj oscommands.ICmdObj) (string, error) {
re := regexp.MustCompile(regexStr)
- matches := re.FindStringSubmatch(cmdObj.ToString())
- assert.Equal(t, 2, len(matches))
+ cmdStr := cmdObj.ToString()
+ matches := re.FindStringSubmatch(cmdStr)
+ assert.Equal(t, 2, len(matches), fmt.Sprintf("unexpected command: %s", cmdStr))
filename := matches[1]