1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03:00

#477 Remove NeedMerge boolean

Instead of storing the status in a new variable, derive it from
the existing three fields
This commit is contained in:
Giorgio Previtera
2019-07-14 09:50:20 +01:00
committed by Jesse Duffield
parent 504d506575
commit e83ef9858b
3 changed files with 64 additions and 17 deletions

View File

@ -12,7 +12,6 @@ type File struct {
Deleted bool Deleted bool
HasMergeConflicts bool HasMergeConflicts bool
HasInlineMergeConflicts bool HasInlineMergeConflicts bool
NeedReset bool
DisplayString string DisplayString string
Type string // one of 'file', 'directory', and 'other' Type string // one of 'file', 'directory', and 'other'
ShortStatus string // e.g. 'AD', ' A', 'M ', '??' ShortStatus string // e.g. 'AD', ' A', 'M ', '??'

View File

@ -188,7 +188,6 @@ func (c *GitCommand) GetStatusFiles() []*File {
Deleted: unstagedChange == "D" || stagedChange == "D", Deleted: unstagedChange == "D" || stagedChange == "D",
HasMergeConflicts: hasMergeConflicts, HasMergeConflicts: hasMergeConflicts,
HasInlineMergeConflicts: hasInlineMergeConflicts, HasInlineMergeConflicts: hasInlineMergeConflicts,
NeedReset: !hasNoStagedChanges || hasMergeConflicts || hasInlineMergeConflicts,
Type: c.OSCommand.FileType(filename), Type: c.OSCommand.FileType(filename),
ShortStatus: change, ShortStatus: change,
} }
@ -474,7 +473,7 @@ func (c *GitCommand) RebaseMode() (string, error) {
func (c *GitCommand) DiscardAllFileChanges(file *File) error { func (c *GitCommand) DiscardAllFileChanges(file *File) error {
// if the file isn't tracked, we assume you want to delete it // if the file isn't tracked, we assume you want to delete it
quotedFileName := c.OSCommand.Quote(file.Name) 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 { if err := c.OSCommand.RunCommand(fmt.Sprintf("git reset -- %s", quotedFileName)); err != nil {
return err return err
} }

View File

@ -371,7 +371,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false, HasInlineMergeConflicts: false,
NeedReset: true,
DisplayString: "MM file1.txt", DisplayString: "MM file1.txt",
Type: "other", Type: "other",
ShortStatus: "MM", ShortStatus: "MM",
@ -384,7 +383,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false, HasInlineMergeConflicts: false,
NeedReset: true,
DisplayString: "A file3.txt", DisplayString: "A file3.txt",
Type: "other", Type: "other",
ShortStatus: "A ", ShortStatus: "A ",
@ -397,7 +395,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false, HasInlineMergeConflicts: false,
NeedReset: true,
DisplayString: "AM file2.txt", DisplayString: "AM file2.txt",
Type: "other", Type: "other",
ShortStatus: "AM", ShortStatus: "AM",
@ -410,7 +407,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false, HasInlineMergeConflicts: false,
NeedReset: false,
DisplayString: "?? file4.txt", DisplayString: "?? file4.txt",
Type: "other", Type: "other",
ShortStatus: "??", ShortStatus: "??",
@ -423,7 +419,6 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Deleted: false, Deleted: false,
HasMergeConflicts: true, HasMergeConflicts: true,
HasInlineMergeConflicts: true, HasInlineMergeConflicts: true,
NeedReset: true,
DisplayString: "UU file5.txt", DisplayString: "UU file5.txt",
Type: "other", Type: "other",
ShortStatus: "UU", ShortStatus: "UU",
@ -1216,8 +1211,8 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
}) })
}, },
&File{ &File{
Name: "test", Name: "test",
NeedReset: true, HasStagedChanges: true,
}, },
func(string) error { func(string) error {
return nil return nil
@ -1299,7 +1294,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
}, },
}, },
{ {
"Reset and checkout", "Reset and checkout staged changes",
func() (func(string, ...string) *exec.Cmd, *[][]string) { func() (func(string, ...string) *exec.Cmd, *[][]string) {
cmdsCalled := [][]string{} cmdsCalled := [][]string{}
return func(cmd string, args ...string) *exec.Cmd { return func(cmd string, args ...string) *exec.Cmd {
@ -1317,9 +1312,63 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
}) })
}, },
&File{ &File{
Name: "test", Name: "test",
Tracked: true, Tracked: true,
NeedReset: 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 {
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,
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 { func(string) error {
return nil return nil
@ -1343,9 +1392,9 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
}) })
}, },
&File{ &File{
Name: "test", Name: "test",
Tracked: false, Tracked: false,
NeedReset: true, HasStagedChanges: true,
}, },
func(filename string) error { func(filename string) error {
assert.Equal(t, "test", filename) assert.Equal(t, "test", filename)