summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiorgio Previtera <gprevitera@equalexperts.com>2019-07-13 14:50:52 +0100
committerJesse Duffield <jessedduffield@gmail.com>2019-07-27 11:05:23 +1000
commit504d5065756a8bdd95f8e0362fd59cb882e5adce (patch)
tree5ee45e2943cce80e45694d28c6ebfa352a3a6f92
parent823b436b5388bd7321f14d22f7555dc974e61dad (diff)
477 Add new `NeedReset` property to File and update tests
Use a boolean to determin if a file needs to be reset. We want to reset the file when discrading changes if there is a conflict.
-rw-r--r--pkg/commands/file.go1
-rw-r--r--pkg/commands/git.go9
-rw-r--r--pkg/commands/git_test.go113
3 files changed, 74 insertions, 49 deletions
diff --git a/pkg/commands/file.go b/pkg/commands/file.go
index 3c4e13f06..6d15356a2 100644
--- a/pkg/commands/file.go
+++ b/pkg/commands/file.go
@@ -12,6 +12,7 @@ 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 32382ba37..c24084e55 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -176,6 +176,8 @@ func (c *GitCommand) GetStatusFiles() []*File {
filename := c.OSCommand.Unquote(statusString[3:])
_, untracked := map[string]bool{"??": true, "A ": true, "AM": true}[change]
_, hasNoStagedChanges := map[string]bool{" ": true, "U": true, "?": true}[stagedChange]
+ hasMergeConflicts := change == "UU" || change == "AA" || change == "DU"
+ hasInlineMergeConflicts := change == "UU" || change == "AA"
file := &File{
Name: filename,
@@ -184,8 +186,9 @@ func (c *GitCommand) GetStatusFiles() []*File {
HasUnstagedChanges: unstagedChange != " ",
Tracked: !untracked,
Deleted: unstagedChange == "D" || stagedChange == "D",
- HasMergeConflicts: change == "UU" || change == "AA" || change == "DU",
- HasInlineMergeConflicts: change == "UU" || change == "AA",
+ HasMergeConflicts: hasMergeConflicts,
+ HasInlineMergeConflicts: hasInlineMergeConflicts,
+ NeedReset: !hasNoStagedChanges || hasMergeConflicts || hasInlineMergeConflicts,
Type: c.OSCommand.FileType(filename),
ShortStatus: change,
}
@@ -471,7 +474,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.HasStagedChanges || file.HasMergeConflicts || file.HasInlineMergeConflicts {
+ if file.NeedReset {
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 35e97b181..55b771109 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -356,56 +356,77 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
func(cmd string, args ...string) *exec.Cmd {
return exec.Command(
"echo",
- "MM file1.txt\nA file3.txt\nAM file2.txt\n?? file4.txt",
+ "MM file1.txt\nA file3.txt\nAM file2.txt\n?? file4.txt\nUU file5.txt",
)
},
func(files []*File) {
- assert.Len(t, files, 4)
+ assert.Len(t, files, 5)
expected := []*File{
{
- Name: "file1.txt",
- HasStagedChanges: true,
- HasUnstagedChanges: true,
- Tracked: true,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "MM file1.txt",
- Type: "other",
- ShortStatus: "MM",
+ Name: "file1.txt",
+ HasStagedChanges: true,
+ HasUnstagedChanges: true,
+ Tracked: true,
+ Deleted: false,
+ HasMergeConflicts: false,
+ HasInlineMergeConflicts: false,
+ NeedReset: true,
+ DisplayString: "MM file1.txt",
+ Type: "other",
+ ShortStatus: "MM",
},
{
- Name: "file3.txt",
- HasStagedChanges: true,
- HasUnstagedChanges: false,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "A file3.txt",
- Type: "other",
- ShortStatus: "A ",
+ Name: "file3.txt",
+ HasStagedChanges: true,
+ HasUnstagedChanges: false,
+ Tracked: false,
+ Deleted: false,
+ HasMergeConflicts: false,
+ HasInlineMergeConflicts: false,
+ NeedReset: true,
+ DisplayString: "A file3.txt",
+ Type: "other",
+ ShortStatus: "A ",
},
{
- Name: "file2.txt",
- HasStagedChanges: true,
- HasUnstagedChanges: true,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "AM file2.txt",
- Type: "other",
- ShortStatus: "AM",
+ Name: "file2.txt",
+ HasStagedChanges: true,
+ HasUnstagedChanges: true,
+ Tracked: false,
+ Deleted: false,
+ HasMergeConflicts: false,
+ HasInlineMergeConflicts: false,
+ NeedReset: true,
+ DisplayString: "AM file2.txt",
+ Type: "other",
+ ShortStatus: "AM",
},
{
- Name: "file4.txt",
- HasStagedChanges: false,
- HasUnstagedChanges: true,
- Tracked: false,
- Deleted: false,
- HasMergeConflicts: false,
- DisplayString: "?? file4.txt",
- Type: "other",
- ShortStatus: "??",
+ Name: "file4.txt",
+ HasStagedChanges: false,
+ HasUnstagedChanges: true,
+ Tracked: false,
+ Deleted: false,
+ HasMergeConflicts: false,
+ HasInlineMergeConflicts: false,
+ NeedReset: false,
+ DisplayString: "?? file4.txt",
+ Type: "other",
+ ShortStatus: "??",
+ },
+ {
+ Name: "file5.txt",
+ HasStagedChanges: false,
+ HasUnstagedChanges: true,
+ Tracked: true,
+ Deleted: false,
+ HasMergeConflicts: true,
+ HasInlineMergeConflicts: true,
+ NeedReset: true,
+ DisplayString: "UU file5.txt",
+ Type: "other",
+ ShortStatus: "UU",
},
}
@@ -1195,8 +1216,8 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
})
},
&File{
- Name: "test",
- HasStagedChanges: true,
+ Name: "test",
+ NeedReset: true,
},
func(string) error {
return nil
@@ -1296,9 +1317,9 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
})
},
&File{
- Name: "test",
- Tracked: true,
- HasStagedChanges: true,
+ Name: "test",
+ Tracked: true,
+ NeedReset: true,
},
func(string) error {
return nil
@@ -1322,9 +1343,9 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
})
},
&File{
- Name: "test",
- Tracked: false,
- HasStagedChanges: true,
+ Name: "test",
+ Tracked: false,
+ NeedReset: true,
},
func(filename string) error {
assert.Equal(t, "test", filename)