From f502f75e1f56bf8f34d3fdf42e934919cd045d85 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 18 Mar 2019 21:19:07 +1100 Subject: add more options for resetting files in the working tree --- pkg/commands/git.go | 19 ++++++++- pkg/commands/git_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 4 deletions(-) (limited to 'pkg/commands') 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()) + }) + } +} -- cgit v1.2.3