Skip to content

Commit 4bf11ea

Browse files
committed
Add free-standing function IsWorkingTreeDirty
The long story: I want to call this function from RefsHelper; however, I can't make WorkingTreeHelper a field of RefsHelper because RefsHelper is already a field in WorkingTreeHelper, so that would be a circular dependency. The shorter story: there's really little reason to have to instantiate a helper object in order to call a simple function like this. Long term I would like to get to a state where a lot more of these helper functions are free-standing, and you pass in the data they need. While at it, simplify the implementation of AnyStagedFiles and AnyTrackedFiles to one-liners.
1 parent 9d88d6a commit 4bf11ea

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

pkg/gui/controllers/helpers/working_tree_helper.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/jesseduffield/lazygit/pkg/config"
1111
"github.com/jesseduffield/lazygit/pkg/gui/context"
1212
"github.com/jesseduffield/lazygit/pkg/gui/types"
13+
"github.com/samber/lo"
1314
)
1415

1516
type WorkingTreeHelper struct {
@@ -34,25 +35,27 @@ func NewWorkingTreeHelper(
3435
}
3536

3637
func (self *WorkingTreeHelper) AnyStagedFiles() bool {
37-
for _, file := range self.c.Model().Files {
38-
if file.HasStagedChanges {
39-
return true
40-
}
41-
}
42-
return false
38+
return AnyStagedFiles(self.c.Model().Files)
39+
}
40+
41+
func AnyStagedFiles(files []*models.File) bool {
42+
return lo.SomeBy(files, func(f *models.File) bool { return f.HasStagedChanges })
4343
}
4444

4545
func (self *WorkingTreeHelper) AnyTrackedFiles() bool {
46-
for _, file := range self.c.Model().Files {
47-
if file.Tracked {
48-
return true
49-
}
50-
}
51-
return false
46+
return AnyTrackedFiles(self.c.Model().Files)
47+
}
48+
49+
func AnyTrackedFiles(files []*models.File) bool {
50+
return lo.SomeBy(files, func(f *models.File) bool { return f.Tracked })
5251
}
5352

5453
func (self *WorkingTreeHelper) IsWorkingTreeDirty() bool {
55-
return self.AnyStagedFiles() || self.AnyTrackedFiles()
54+
return IsWorkingTreeDirty(self.c.Model().Files)
55+
}
56+
57+
func IsWorkingTreeDirty(files []*models.File) bool {
58+
return AnyStagedFiles(files) || AnyTrackedFiles(files)
5659
}
5760

5861
func (self *WorkingTreeHelper) FileForSubmodule(submodule *models.SubmoduleConfig) *models.File {

0 commit comments

Comments
 (0)