2022-01-30 07:38:07 +02:00
|
|
|
package filetree
|
|
|
|
|
|
|
|
import (
|
2022-07-31 11:10:16 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2022-01-30 07:38:07 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ICommitFileTree interface {
|
2022-07-31 11:10:16 +02:00
|
|
|
ITree[models.CommitFile]
|
2022-01-30 07:38:07 +02:00
|
|
|
|
2022-03-19 00:31:52 +02:00
|
|
|
Get(index int) *CommitFileNode
|
2022-01-30 07:38:07 +02:00
|
|
|
GetFile(path string) *models.CommitFile
|
|
|
|
GetAllItems() []*CommitFileNode
|
|
|
|
GetAllFiles() []*models.CommitFile
|
2022-07-31 11:10:16 +02:00
|
|
|
GetRoot() *CommitFileNode
|
2022-01-30 07:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommitFileTree struct {
|
|
|
|
getFiles func() []*models.CommitFile
|
2022-07-31 11:10:16 +02:00
|
|
|
tree *Node[models.CommitFile]
|
2022-01-30 07:38:07 +02:00
|
|
|
showTree bool
|
|
|
|
log *logrus.Entry
|
2022-03-19 03:26:30 +02:00
|
|
|
collapsedPaths *CollapsedPaths
|
2022-01-30 07:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ ICommitFileTree = &CommitFileTree{}
|
|
|
|
|
|
|
|
func NewCommitFileTree(getFiles func() []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileTree {
|
|
|
|
return &CommitFileTree{
|
|
|
|
getFiles: getFiles,
|
|
|
|
log: log,
|
|
|
|
showTree: showTree,
|
2022-03-19 03:26:30 +02:00
|
|
|
collapsedPaths: NewCollapsedPaths(),
|
2022-01-30 07:38:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) ExpandToPath(path string) {
|
|
|
|
self.collapsedPaths.ExpandToPath(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) ToggleShowTree() {
|
|
|
|
self.showTree = !self.showTree
|
|
|
|
self.SetTree()
|
|
|
|
}
|
|
|
|
|
2022-03-19 00:31:52 +02:00
|
|
|
func (self *CommitFileTree) Get(index int) *CommitFileNode {
|
2022-01-30 07:38:07 +02:00
|
|
|
// need to traverse the three depth first until we get to the index.
|
2022-07-31 11:10:16 +02:00
|
|
|
return NewCommitFileNode(self.tree.GetNodeAtIndex(index+1, self.collapsedPaths)) // ignoring root
|
2022-01-30 07:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) GetIndexForPath(path string) (int, bool) {
|
|
|
|
index, found := self.tree.GetIndexForPath(path, self.collapsedPaths)
|
|
|
|
return index - 1, found
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) GetAllItems() []*CommitFileNode {
|
|
|
|
if self.tree == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-31 11:10:16 +02:00
|
|
|
// ignoring root
|
|
|
|
return slices.Map(self.tree.Flatten(self.collapsedPaths)[1:], func(node *Node[models.CommitFile]) *CommitFileNode {
|
|
|
|
return NewCommitFileNode(node)
|
|
|
|
})
|
2022-01-30 07:38:07 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 00:31:52 +02:00
|
|
|
func (self *CommitFileTree) Len() int {
|
2022-01-30 07:38:07 +02:00
|
|
|
return self.tree.Size(self.collapsedPaths) - 1 // ignoring root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) GetAllFiles() []*models.CommitFile {
|
|
|
|
return self.getFiles()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) SetTree() {
|
|
|
|
if self.showTree {
|
|
|
|
self.tree = BuildTreeFromCommitFiles(self.getFiles())
|
|
|
|
} else {
|
|
|
|
self.tree = BuildFlatTreeFromCommitFiles(self.getFiles())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) IsCollapsed(path string) bool {
|
|
|
|
return self.collapsedPaths.IsCollapsed(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) ToggleCollapsed(path string) {
|
|
|
|
self.collapsedPaths.ToggleCollapsed(path)
|
|
|
|
}
|
|
|
|
|
2022-07-31 11:10:16 +02:00
|
|
|
func (self *CommitFileTree) GetRoot() *CommitFileNode {
|
|
|
|
return NewCommitFileNode(self.tree)
|
2022-01-30 07:38:07 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:26:30 +02:00
|
|
|
func (self *CommitFileTree) CollapsedPaths() *CollapsedPaths {
|
2022-01-30 07:38:07 +02:00
|
|
|
return self.collapsedPaths
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) GetFile(path string) *models.CommitFile {
|
|
|
|
for _, file := range self.getFiles() {
|
|
|
|
if file.Name == path {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitFileTree) InTreeMode() bool {
|
|
|
|
return self.showTree
|
|
|
|
}
|