1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-21 19:48:58 +02:00
This commit is contained in:
Jesse Duffield 2022-01-08 16:02:56 +11:00
parent 16c9b5404d
commit ccf90466fa
5 changed files with 49 additions and 35 deletions

View File

@ -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
}

View File

@ -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]

View File

@ -55,7 +55,7 @@ func TestGetBool(t *testing.T) {
fake := NewFakeGitConfig(s.mockResponses)
real := NewCachedGitConfig(
func(cmd *exec.Cmd) (string, error) {
assert.Equal(t, "git config --get --null commit.gpgsign", strings.Join(cmd.Args, " "))
assert.Equal(t, "config --get --null commit.gpgsign", strings.Join(cmd.Args[1:], " "))
return fake.Get("commit.gpgsign"), nil
},
utils.NewDummyLog(),
@ -92,7 +92,7 @@ func TestGet(t *testing.T) {
fake := NewFakeGitConfig(s.mockResponses)
real := NewCachedGitConfig(
func(cmd *exec.Cmd) (string, error) {
assert.Equal(t, "git config --get --null commit.gpgsign", strings.Join(cmd.Args, " "))
assert.Equal(t, "config --get --null commit.gpgsign", strings.Join(cmd.Args[1:], " "))
return fake.Get("commit.gpgsign"), nil
},
utils.NewDummyLog(),
@ -107,7 +107,7 @@ func TestGet(t *testing.T) {
real := NewCachedGitConfig(
func(cmd *exec.Cmd) (string, error) {
count++
assert.Equal(t, "git config --get --null commit.gpgsign", strings.Join(cmd.Args, " "))
assert.Equal(t, "config --get --null commit.gpgsign", strings.Join(cmd.Args[1:], " "))
return "blah", nil
},
utils.NewDummyLog(),

View File

@ -4,7 +4,6 @@ import (
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
@ -19,7 +18,6 @@ type FileLoader struct {
*common.Common
cmd oscommands.ICmdObjBuilder
config FileLoaderConfig
gitConfig git_config.IGitConfig
getFileType func(string) string
}

View File

@ -3,7 +3,6 @@ package loaders
import (
"testing"
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
@ -189,12 +188,11 @@ func TestFileGetStatusFiles(t *testing.T) {
s := s
t.Run(s.testName, func(t *testing.T) {
cmd := oscommands.NewDummyCmdObjBuilder(s.runner)
gitConfig := git_config.NewFakeGitConfig(map[string]string{"status.showUntrackedFiles": "yes"})
loader := &FileLoader{
Common: utils.NewDummyCommon(),
cmd: cmd,
gitConfig: gitConfig,
config: &FakeFileLoaderConfig{showUntrackedFiles: "yes"},
getFileType: func(string) string { return "file" },
}
@ -202,3 +200,11 @@ func TestFileGetStatusFiles(t *testing.T) {
})
}
}
type FakeFileLoaderConfig struct {
showUntrackedFiles string
}
func (self *FakeFileLoaderConfig) GetShowUntrackedFiles() string {
return self.showUntrackedFiles
}