diff options
author | Giorgio Previtera <gprevitera@equalexperts.com> | 2019-07-14 09:50:20 +0100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-07-27 11:05:23 +1000 |
commit | e83ef9858b70972b9c6b2efbbc4f5df7d8a5fe82 (patch) | |
tree | 02c327f08436cbfcd8acec05d44955b21bffb4fc | |
parent | 504d5065756a8bdd95f8e0362fd59cb882e5adce (diff) |
#477 Remove `NeedMerge` boolean
Instead of storing the status in a new variable, derive it from
the existing three fields
-rw-r--r-- | pkg/commands/file.go | 1 | ||||
-rw-r--r-- | pkg/commands/git.go | 3 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 77 |
3 files changed, 64 insertions, 17 deletions
diff --git a/pkg/commands/file.go b/pkg/commands/file.go index 6d15356a2..3c4e13f06 100644 --- a/pkg/commands/file.go +++ b/pkg/commands/file.go @@ -12,7 +12,6 @@ type File struct { Deleted bool HasMergeConflicts bool HasInlineMergeConflicts bool - NeedReset bool DisplayString string Type string // one of 'file', 'directory', and 'other' ShortStatus string // e.g. 'AD', ' A', 'M ', '??' diff --git a/pkg/commands/git.go b/pkg/commands/git.go index c24084e55..5aeaa08ee 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -188,7 +188,6 @@ func (c *GitCommand) GetStatusFiles() []*File { Deleted: unstagedChange == "D" || stagedChange == "D", HasMergeConflicts: hasMergeConflicts, HasInlineMergeConflicts: hasInlineMergeConflicts, - NeedReset: !hasNoStagedChanges || hasMergeConflicts || hasInlineMergeConflicts, Type: c.OSCommand.FileType(filename), ShortStatus: change, } @@ -474,7 +473,7 @@ func (c *GitCommand) RebaseMode() (string, error) { func (c *GitCommand) DiscardAllFileChanges(file *File) error { // if the file isn't tracked, we assume you want to delete it quotedFileName := c.OSCommand.Quote(file.Name) - if file.NeedReset { + if file.HasStagedChanges || file.HasMergeConflicts || file.HasInlineMergeConflicts { if err := c.OSCommand.RunCommand(fmt.Sprintf("git reset -- %s", quotedFileName)); err != nil { return err } diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 55b771109..5a043c4c7 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -371,7 +371,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, HasInlineMergeConflicts: false, - NeedReset: true, DisplayString: "MM file1.txt", Type: "other", ShortStatus: "MM", @@ -384,7 +383,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, HasInlineMergeConflicts: false, - NeedReset: true, DisplayString: "A file3.txt", Type: "other", ShortStatus: "A ", @@ -397,7 +395,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, HasInlineMergeConflicts: false, - NeedReset: true, DisplayString: "AM file2.txt", Type: "other", ShortStatus: "AM", @@ -410,7 +407,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: false, HasInlineMergeConflicts: false, - NeedReset: false, DisplayString: "?? file4.txt", Type: "other", ShortStatus: "??", @@ -423,7 +419,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) { Deleted: false, HasMergeConflicts: true, HasInlineMergeConflicts: true, - NeedReset: true, DisplayString: "UU file5.txt", Type: "other", ShortStatus: "UU", @@ -1216,8 +1211,8 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) { }) }, &File{ - Name: "test", - NeedReset: true, + Name: "test", + HasStagedChanges: true, }, func(string) error { return nil @@ -1299,7 +1294,34 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) { }, }, { - "Reset and checkout", + "Reset and checkout staged changes", + func() (func(string, ...string) *exec.Cmd, *[][]string) { + cmdsCalled := [][]string{} + return func(cmd string, args ...string) *exec.Cmd { + cmdsCalled = append(cmdsCalled, args) + + return exec.Command("echo") + }, &cmdsCalled + }, + func(cmdsCalled *[][]string, err error) { + assert.NoError(t, err) + assert.Len(t, *cmdsCalled, 2) + assert.EqualValues(t, *cmdsCalled, [][]string{ + {"reset", "--", "test"}, + {"checkout", "--", "test"}, + }) + }, + &File{ + Name: "test", + Tracked: true, + HasStagedChanges: true, + }, + func(string) error { + return nil + }, + }, + { + "Reset and checkout merge conflicts", func() (func(string, ...string) *exec.Cmd, *[][]string) { cmdsCalled := [][]string{} return func(cmd string, args ...string) *exec.Cmd { @@ -1317,9 +1339,36 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) { }) }, &File{ - Name: "test", - Tracked: true, - NeedReset: true, + Name: "test", + Tracked: true, + HasMergeConflicts: true, + }, + func(string) error { + return nil + }, + }, + { + "Reset and checkout inline merge conflicts", + func() (func(string, ...string) *exec.Cmd, *[][]string) { + cmdsCalled := [][]string{} + return func(cmd string, args ...string) *exec.Cmd { + cmdsCalled = append(cmdsCalled, args) + + return exec.Command("echo") + }, &cmdsCalled + }, + func(cmdsCalled *[][]string, err error) { + assert.NoError(t, err) + assert.Len(t, *cmdsCalled, 2) + assert.EqualValues(t, *cmdsCalled, [][]string{ + {"reset", "--", "test"}, + {"checkout", "--", "test"}, + }) + }, + &File{ + Name: "test", + Tracked: true, + HasInlineMergeConflicts: true, }, func(string) error { return nil @@ -1343,9 +1392,9 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) { }) }, &File{ - Name: "test", - Tracked: false, - NeedReset: true, + Name: "test", + Tracked: false, + HasStagedChanges: true, }, func(filename string) error { assert.Equal(t, "test", filename) |