summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-03-18 21:19:07 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-03-23 13:26:17 +1100
commitf502f75e1f56bf8f34d3fdf42e934919cd045d85 (patch)
tree27236dda4b1bd011d43e0bc1efb2e642371c6c6d /pkg/commands
parentff97ef7b94b5ccfda1d8b98b0a7e21a779309fcc (diff)
add more options for resetting files in the working tree
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git.go19
-rw-r--r--pkg/commands/git_test.go103
2 files changed, 118 insertions, 4 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index e674dc720..dbd9087c2 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -217,11 +217,11 @@ func includesInt(list []int, a int) bool {
// ResetAndClean removes all unstaged changes and removes all untracked files
func (c *GitCommand) ResetAndClean() error {
- if err := c.OSCommand.RunCommand("git reset --hard HEAD"); err != nil {
+ if err := c.ResetHardHead(); err != nil {
return err
}
- return c.OSCommand.RunCommand("git clean -fd")
+ return c.RemoveUntrackedFiles()
}
func (c *GitCommand) GetCurrentBranchUpstreamDifferenceCount() (string, string) {
@@ -890,3 +890,18 @@ func (c *GitCommand) DiscardOldFileChanges(commits []*Commit, commitIndex int, f
// continue
return c.GenericMerge("rebase", "continue")
}
+
+// DiscardAnyUnstagedFileChanges discards any unstages file changes via `git checkout -- .`
+func (c *GitCommand) DiscardAnyUnstagedFileChanges() error {
+ return c.OSCommand.RunCommand("git checkout -- .")
+}
+
+// RemoveUntrackedFiles runs `git clean -fd`
+func (c *GitCommand) RemoveUntrackedFiles() error {
+ return c.OSCommand.RunCommand("git clean -fd")
+}
+
+// ResetHardHead runs `git reset --hard HEAD`
+func (c *GitCommand) ResetHardHead() error {
+ return c.OSCommand.RunCommand("git reset --hard HEAD")
+}
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index 614ad527b..aa1785af8 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -1933,8 +1933,8 @@ func TestGitCommandGetCommitFiles(t *testing.T) {
}
}
-// TestGitCommandDiscardUnstagedChanges is a function.
-func TestGitCommandDiscardUnstagedChanges(t *testing.T) {
+// TestGitCommandDiscardUnstagedFileChanges is a function.
+func TestGitCommandDiscardUnstagedFileChanges(t *testing.T) {
type scenario struct {
testName string
file *File
@@ -1967,3 +1967,102 @@ func TestGitCommandDiscardUnstagedChanges(t *testing.T) {
})
}
}
+
+// TestGitCommandDiscardAnyUnstagedFileChanges is a function.
+func TestGitCommandDiscardAnyUnstagedFileChanges(t *testing.T) {
+ type scenario struct {
+ testName string
+ command func(string, ...string) *exec.Cmd
+ test func(error)
+ }
+
+ scenarios := []scenario{
+ {
+ "valid case",
+ test.CreateMockCommand(t, []*test.CommandSwapper{
+ {
+ Expect: `git checkout -- .`,
+ Replace: "echo",
+ },
+ }),
+ func(err error) {
+ assert.NoError(t, err)
+ },
+ },
+ }
+
+ gitCmd := NewDummyGitCommand()
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.DiscardAnyUnstagedFileChanges())
+ })
+ }
+}
+
+// TestGitCommandRemoveUntrackedFiles is a function.
+func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
+ type scenario struct {
+ testName string
+ command func(string, ...string) *exec.Cmd
+ test func(error)
+ }
+
+ scenarios := []scenario{
+ {
+ "valid case",
+ test.CreateMockCommand(t, []*test.CommandSwapper{
+ {
+ Expect: `git clean -fd`,
+ Replace: "echo",
+ },
+ }),
+ func(err error) {
+ assert.NoError(t, err)
+ },
+ },
+ }
+
+ gitCmd := NewDummyGitCommand()
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.RemoveUntrackedFiles())
+ })
+ }
+}
+
+// TestGitCommandResetHardHead is a function.
+func TestGitCommandResetHardHead(t *testing.T) {
+ type scenario struct {
+ testName string
+ command func(string, ...string) *exec.Cmd
+ test func(error)
+ }
+
+ scenarios := []scenario{
+ {
+ "valid case",
+ test.CreateMockCommand(t, []*test.CommandSwapper{
+ {
+ Expect: `git reset --hard HEAD`,
+ Replace: "echo",
+ },
+ }),
+ func(err error) {
+ assert.NoError(t, err)
+ },
+ },
+ }
+
+ gitCmd := NewDummyGitCommand()
+
+ for _, s := range scenarios {
+ t.Run(s.testName, func(t *testing.T) {
+ gitCmd.OSCommand.command = s.command
+ s.test(gitCmd.ResetHardHead())
+ })
+ }
+}