1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-27 12:32:37 +02:00

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.
This commit is contained in:
Giorgio Previtera 2019-07-13 14:50:52 +01:00 committed by Jesse Duffield
parent 823b436b53
commit 504d506575
3 changed files with 74 additions and 49 deletions

View File

@ -12,6 +12,7 @@ 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

@ -176,6 +176,8 @@ func (c *GitCommand) GetStatusFiles() []*File {
filename := c.OSCommand.Unquote(statusString[3:]) filename := c.OSCommand.Unquote(statusString[3:])
_, untracked := map[string]bool{"??": true, "A ": true, "AM": true}[change] _, untracked := map[string]bool{"??": true, "A ": true, "AM": true}[change]
_, hasNoStagedChanges := map[string]bool{" ": true, "U": true, "?": true}[stagedChange] _, hasNoStagedChanges := map[string]bool{" ": true, "U": true, "?": true}[stagedChange]
hasMergeConflicts := change == "UU" || change == "AA" || change == "DU"
hasInlineMergeConflicts := change == "UU" || change == "AA"
file := &File{ file := &File{
Name: filename, Name: filename,
@ -184,8 +186,9 @@ func (c *GitCommand) GetStatusFiles() []*File {
HasUnstagedChanges: unstagedChange != " ", HasUnstagedChanges: unstagedChange != " ",
Tracked: !untracked, Tracked: !untracked,
Deleted: unstagedChange == "D" || stagedChange == "D", Deleted: unstagedChange == "D" || stagedChange == "D",
HasMergeConflicts: change == "UU" || change == "AA" || change == "DU", HasMergeConflicts: hasMergeConflicts,
HasInlineMergeConflicts: change == "UU" || change == "AA", HasInlineMergeConflicts: hasInlineMergeConflicts,
NeedReset: !hasNoStagedChanges || hasMergeConflicts || hasInlineMergeConflicts,
Type: c.OSCommand.FileType(filename), Type: c.OSCommand.FileType(filename),
ShortStatus: change, ShortStatus: change,
} }
@ -471,7 +474,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.HasStagedChanges || file.HasMergeConflicts || file.HasInlineMergeConflicts { if file.NeedReset {
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

@ -356,11 +356,11 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
return exec.Command( return exec.Command(
"echo", "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) { func(files []*File) {
assert.Len(t, files, 4) assert.Len(t, files, 5)
expected := []*File{ expected := []*File{
{ {
@ -370,6 +370,8 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Tracked: true, Tracked: true,
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false,
NeedReset: true,
DisplayString: "MM file1.txt", DisplayString: "MM file1.txt",
Type: "other", Type: "other",
ShortStatus: "MM", ShortStatus: "MM",
@ -381,6 +383,8 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Tracked: false, Tracked: false,
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false,
NeedReset: true,
DisplayString: "A file3.txt", DisplayString: "A file3.txt",
Type: "other", Type: "other",
ShortStatus: "A ", ShortStatus: "A ",
@ -392,6 +396,8 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Tracked: false, Tracked: false,
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false,
NeedReset: true,
DisplayString: "AM file2.txt", DisplayString: "AM file2.txt",
Type: "other", Type: "other",
ShortStatus: "AM", ShortStatus: "AM",
@ -403,10 +409,25 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
Tracked: false, Tracked: false,
Deleted: false, Deleted: false,
HasMergeConflicts: false, HasMergeConflicts: false,
HasInlineMergeConflicts: false,
NeedReset: false,
DisplayString: "?? file4.txt", DisplayString: "?? file4.txt",
Type: "other", Type: "other",
ShortStatus: "??", 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",
},
} }
assert.EqualValues(t, expected, files) assert.EqualValues(t, expected, files)
@ -1196,7 +1217,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
}, },
&File{ &File{
Name: "test", Name: "test",
HasStagedChanges: true, NeedReset: true,
}, },
func(string) error { func(string) error {
return nil return nil
@ -1298,7 +1319,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
&File{ &File{
Name: "test", Name: "test",
Tracked: true, Tracked: true,
HasStagedChanges: true, NeedReset: true,
}, },
func(string) error { func(string) error {
return nil return nil
@ -1324,7 +1345,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
&File{ &File{
Name: "test", Name: "test",
Tracked: false, Tracked: false,
HasStagedChanges: true, NeedReset: true,
}, },
func(filename string) error { func(filename string) error {
assert.Equal(t, "test", filename) assert.Equal(t, "test", filename)