summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-10-30 20:23:25 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-11-05 19:22:01 +1100
commit820f3d5cbb556f1c117906e4174f35ecf71e2ed5 (patch)
tree591c593fff6636707d06860cc7b918a92c573de0 /pkg/commands
parent081598d98944cdb95bfa649812565127c0592f5e (diff)
support split view in staging panel and staging ranges
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git.go18
-rw-r--r--pkg/commands/git_test.go24
2 files changed, 28 insertions, 14 deletions
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))
})
}
}