2021-03-21 06:58:15 +02:00
|
|
|
package filetree
|
2020-11-15 01:45:55 +02:00
|
|
|
|
|
|
|
import (
|
2021-03-14 10:38:59 +02:00
|
|
|
"fmt"
|
2021-03-21 06:58:15 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2020-11-15 01:45:55 +02:00
|
|
|
)
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
type FileNode struct {
|
|
|
|
Children []*FileNode
|
2021-03-21 06:58:15 +02:00
|
|
|
File *models.File
|
2021-03-21 01:46:43 +02:00
|
|
|
Path string // e.g. '/path/to/mydir'
|
2021-03-21 06:01:13 +02:00
|
|
|
CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
// methods satisfying ListItem interface
|
2020-11-15 01:45:55 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) ID() string {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.GetPath()
|
2021-03-21 06:44:10 +02:00
|
|
|
}
|
2020-11-15 01:45:55 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) Description() string {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.GetPath()
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
// methods satisfying INode interface
|
2021-03-21 23:11:39 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) IsLeaf() bool {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.File != nil
|
2021-03-21 06:44:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetPath() string {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.Path
|
2021-03-21 23:11:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetChildren() []INode {
|
2021-03-30 14:56:59 +02:00
|
|
|
result := make([]INode, len(s.Children))
|
|
|
|
for i, child := range s.Children {
|
|
|
|
result[i] = child
|
2021-03-21 23:11:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
return result
|
2021-03-21 23:11:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) SetChildren(children []INode) {
|
|
|
|
castChildren := make([]*FileNode, len(children))
|
2021-03-30 14:56:59 +02:00
|
|
|
for i, child := range children {
|
2021-03-31 14:26:53 +02:00
|
|
|
castChildren[i] = child.(*FileNode)
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
s.Children = castChildren
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetCompressionLevel() int {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.CompressionLevel
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) SetCompressionLevel(level int) {
|
2021-03-30 14:56:59 +02:00
|
|
|
s.CompressionLevel = level
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
// methods utilising generic functions for INodes
|
2021-03-20 23:41:06 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) Sort() {
|
2021-03-30 14:56:59 +02:00
|
|
|
sortNode(s)
|
2021-03-20 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) ForEachFile(cb func(*models.File) error) error {
|
2021-03-30 14:56:59 +02:00
|
|
|
return forEachLeaf(s, func(n INode) error {
|
2021-03-31 14:26:53 +02:00
|
|
|
castNode := n.(*FileNode)
|
2021-03-30 14:56:59 +02:00
|
|
|
return cb(castNode.File)
|
|
|
|
})
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) Any(test func(node *FileNode) bool) bool {
|
2021-03-30 14:56:59 +02:00
|
|
|
return any(s, func(n INode) bool {
|
2021-03-31 14:26:53 +02:00
|
|
|
castNode := n.(*FileNode)
|
2021-03-30 14:56:59 +02:00
|
|
|
return test(castNode)
|
|
|
|
})
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode {
|
2021-03-30 14:56:59 +02:00
|
|
|
results := flatten(n, collapsedPaths)
|
2021-03-31 14:26:53 +02:00
|
|
|
nodes := make([]*FileNode, len(results))
|
2021-03-30 14:56:59 +02:00
|
|
|
for i, result := range results {
|
2021-03-31 14:26:53 +02:00
|
|
|
nodes[i] = result.(*FileNode)
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
return nodes
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileNode {
|
2021-04-03 04:43:43 +02:00
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 06:24:28 +02:00
|
|
|
result := getNodeAtIndex(node, index, collapsedPaths)
|
|
|
|
if result == nil {
|
|
|
|
// not sure how this can be nil: we probably are missing a mutex somewhere
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.(*FileNode)
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (node *FileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) {
|
2021-03-30 14:56:59 +02:00
|
|
|
return getIndexForPath(node, path, collapsedPaths)
|
2020-11-15 01:45:55 +02:00
|
|
|
}
|
2021-03-14 09:46:22 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (node *FileNode) Size(collapsedPaths map[string]bool) int {
|
2021-04-02 01:51:17 +02:00
|
|
|
if node == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
return size(node, collapsedPaths)
|
2021-03-14 09:46:22 +02:00
|
|
|
}
|
2021-03-14 10:38:59 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) Compress() {
|
2021-03-30 14:56:59 +02:00
|
|
|
// with these functions I try to only have type conversion code on the actual struct,
|
|
|
|
// but comparing interface values to nil is fraught with danger so I'm duplicating
|
|
|
|
// that code here.
|
2021-03-14 10:38:59 +02:00
|
|
|
if s == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
compressAux(s)
|
2021-03-14 10:38:59 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
// This ignores the root
|
2021-03-31 14:26:53 +02:00
|
|
|
func (node *FileNode) GetPathsMatching(test func(*FileNode) bool) []string {
|
2021-03-30 14:56:59 +02:00
|
|
|
return getPathsMatching(node, func(n INode) bool {
|
2021-03-31 14:26:53 +02:00
|
|
|
return test(n.(*FileNode))
|
2021-03-30 14:56:59 +02:00
|
|
|
})
|
|
|
|
}
|
2021-03-14 10:38:59 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetLeaves() []*FileNode {
|
2021-03-30 14:56:59 +02:00
|
|
|
leaves := getLeaves(s)
|
2021-03-31 14:26:53 +02:00
|
|
|
castLeaves := make([]*FileNode, len(leaves))
|
2021-03-30 14:56:59 +02:00
|
|
|
for i := range leaves {
|
2021-03-31 14:26:53 +02:00
|
|
|
castLeaves[i] = leaves[i].(*FileNode)
|
2021-03-14 10:38:59 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
return castLeaves
|
2021-03-14 11:23:06 +02:00
|
|
|
}
|
2021-03-15 13:29:34 +02:00
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
// extra methods
|
2021-03-15 13:29:34 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetHasUnstagedChanges() bool {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.AnyFile(func(file *models.File) bool { return file.HasUnstagedChanges })
|
|
|
|
}
|
2021-03-15 13:29:34 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetHasStagedChanges() bool {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.AnyFile(func(file *models.File) bool { return file.HasStagedChanges })
|
|
|
|
}
|
2021-03-15 13:29:34 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetHasInlineMergeConflicts() bool {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.AnyFile(func(file *models.File) bool { return file.HasInlineMergeConflicts })
|
2021-03-15 13:29:34 +02:00
|
|
|
}
|
2021-03-15 14:00:20 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) GetIsTracked() bool {
|
2021-03-30 14:56:59 +02:00
|
|
|
return s.AnyFile(func(file *models.File) bool { return file.Tracked })
|
2021-03-15 14:00:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) AnyFile(test func(file *models.File) bool) bool {
|
|
|
|
return s.Any(func(node *FileNode) bool {
|
2021-03-30 14:56:59 +02:00
|
|
|
return node.IsLeaf() && test(node.File)
|
|
|
|
})
|
2021-03-15 14:00:20 +02:00
|
|
|
}
|
2021-03-16 00:07:00 +02:00
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *FileNode) NameAtDepth(depth int) string {
|
2021-04-08 13:24:49 +02:00
|
|
|
splitName := split(s.Path)
|
|
|
|
name := join(splitName[depth:])
|
2021-03-21 01:46:43 +02:00
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
if s.File != nil && s.File.IsRename() {
|
2021-04-08 13:24:49 +02:00
|
|
|
splitPrevName := split(s.File.PreviousName)
|
2021-03-30 14:56:59 +02:00
|
|
|
|
|
|
|
prevName := s.File.PreviousName
|
|
|
|
// if the file has just been renamed inside the same directory, we can shave off
|
|
|
|
// the prefix for the previous path too. Otherwise we'll keep it unchanged
|
2021-04-08 13:24:49 +02:00
|
|
|
sameParentDir := len(splitName) == len(splitPrevName) && join(splitName[0:depth]) == join(splitPrevName[0:depth])
|
2021-03-30 14:56:59 +02:00
|
|
|
if sameParentDir {
|
2021-04-08 13:24:49 +02:00
|
|
|
prevName = join(splitPrevName[depth:])
|
2021-03-30 14:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s%s", prevName, " → ", name)
|
2021-03-21 01:46:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:56:59 +02:00
|
|
|
return name
|
2021-03-21 01:46:43 +02:00
|
|
|
}
|