From 820f3d5cbb556f1c117906e4174f35ecf71e2ed5 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Wed, 30 Oct 2019 20:23:25 +1100 Subject: support split view in staging panel and staging ranges --- pkg/commands/git.go | 18 ++++++++++++++---- pkg/commands/git_test.go | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 14 deletions(-) (limited to 'pkg/commands') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 5e2e7e33c..6e86fe0b5 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -582,13 +582,13 @@ func (c *GitCommand) CheckRemoteBranchExists(branch *Branch) bool { } // Diff returns the diff of a file -func (c *GitCommand) Diff(file *File, plain bool) string { +func (c *GitCommand) Diff(file *File, plain bool, cached bool) string { cachedArg := "" trackedArg := "--" colorArg := "--color" split := strings.Split(file.Name, " -> ") // in case of a renamed file we get the new filename fileName := c.OSCommand.Quote(split[len(split)-1]) - if file.HasStagedChanges && !file.HasUnstagedChanges { + if cached { cachedArg = "--cached" } if !file.Tracked && !file.HasStagedChanges { @@ -605,7 +605,7 @@ func (c *GitCommand) Diff(file *File, plain bool) string { return s } -func (c *GitCommand) ApplyPatch(patch string) (string, error) { +func (c *GitCommand) ApplyPatch(patch string, reverse bool, cached bool) (string, error) { filename, err := c.OSCommand.CreateTempFile("patch", patch) if err != nil { c.Log.Error(err) @@ -614,7 +614,17 @@ func (c *GitCommand) ApplyPatch(patch string) (string, error) { defer func() { _ = c.OSCommand.Remove(filename) }() - return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git apply --cached %s", c.OSCommand.Quote(filename))) + reverseFlag := "" + if reverse { + reverseFlag = "--reverse" + } + + cachedFlag := "" + if cached { + cachedFlag = "--cached" + } + + return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git apply %s %s %s", cachedFlag, reverseFlag, c.OSCommand.Quote(filename))) } func (c *GitCommand) FastForward(branchName string) error { diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 51188f14d..c132de98a 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1539,6 +1539,7 @@ func TestGitCommandDiff(t *testing.T) { command func(string, ...string) *exec.Cmd file *File plain bool + cached bool } scenarios := []scenario{ @@ -1556,12 +1557,13 @@ func TestGitCommandDiff(t *testing.T) { Tracked: true, }, false, + false, }, { - "Default case", + "cached", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"diff", "--", "test.txt"}, args) + assert.EqualValues(t, []string{"diff", "--color", "--cached", "--", "test.txt"}, args) return exec.Command("echo") }, @@ -1570,22 +1572,23 @@ func TestGitCommandDiff(t *testing.T) { HasStagedChanges: false, Tracked: true, }, + false, true, }, { - "All changes staged", + "plain", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"diff", "--color", "--cached", "--", "test.txt"}, args) + assert.EqualValues(t, []string{"diff", "--", "test.txt"}, args) return exec.Command("echo") }, &File{ - Name: "test.txt", - HasStagedChanges: true, - HasUnstagedChanges: false, - Tracked: true, + Name: "test.txt", + HasStagedChanges: false, + Tracked: true, }, + true, false, }, { @@ -1602,6 +1605,7 @@ func TestGitCommandDiff(t *testing.T) { Tracked: false, }, false, + false, }, } @@ -1609,7 +1613,7 @@ func TestGitCommandDiff(t *testing.T) { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommand() gitCmd.OSCommand.command = s.command - gitCmd.Diff(s.file, s.plain) + gitCmd.Diff(s.file, s.plain, s.cached) }) } } @@ -1730,7 +1734,7 @@ func TestGitCommandApplyPatch(t *testing.T) { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommand() gitCmd.OSCommand.command = s.command - s.test(gitCmd.ApplyPatch("test")) + s.test(gitCmd.ApplyPatch("test", false, true)) }) } } -- cgit v1.2.3