1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00
This commit is contained in:
Jesse Duffield
2021-03-21 15:58:15 +11:00
parent 9e67f74ca3
commit d5639e6e95
9 changed files with 61 additions and 57 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/mgutz/str"
)
@ -137,12 +138,12 @@ func (c *GitCommand) DiscardAllFileChanges(file *models.File) error {
return c.DiscardUnstagedFileChanges(file)
}
func (c *GitCommand) DiscardAllDirChanges(node *models.FileChangeNode) error {
func (c *GitCommand) DiscardAllDirChanges(node *filetree.FileChangeNode) error {
// this could be more efficient but we would need to handle all the edge cases
return node.ForEachFile(c.DiscardAllFileChanges)
}
func (c *GitCommand) DiscardUnstagedDirChanges(node *models.FileChangeNode) error {
func (c *GitCommand) DiscardUnstagedDirChanges(node *filetree.FileChangeNode) error {
if err := c.RemoveUntrackedDirFiles(node); err != nil {
return err
}
@ -155,9 +156,9 @@ func (c *GitCommand) DiscardUnstagedDirChanges(node *models.FileChangeNode) erro
return nil
}
func (c *GitCommand) RemoveUntrackedDirFiles(node *models.FileChangeNode) error {
func (c *GitCommand) RemoveUntrackedDirFiles(node *filetree.FileChangeNode) error {
untrackedFilePaths := node.GetPathsMatching(
func(n *models.FileChangeNode) bool { return n.File != nil && !n.File.GetIsTracked() },
func(n *filetree.FileChangeNode) bool { return n.File != nil && !n.File.GetIsTracked() },
)
for _, path := range untrackedFilePaths {

View File

@ -21,6 +21,14 @@ type File struct {
ShortStatus string // e.g. 'AD', ' A', 'M ', '??'
}
// sometimes we need to deal with either a node (which contains a file) or an actual file
type IFileChange interface {
GetHasUnstagedChanges() bool
GetHasStagedChanges() bool
GetIsTracked() bool
GetPath() string
}
const RENAME_SEPARATOR = " -> "
func (f *File) IsRename() bool {

View File

@ -1,9 +0,0 @@
package models
// sometimes we need to deal with either a node (which contains a file) or an actual file
type IFileChange interface {
GetHasUnstagedChanges() bool
GetHasStagedChanges() bool
GetIsTracked() bool
GetPath() string
}

View File

@ -1,282 +0,0 @@
package models
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
type FileChangeNode struct {
Children []*FileChangeNode
File *File
Path string // e.g. '/path/to/mydir'
CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode
}
func (s *FileChangeNode) GetHasUnstagedChanges() bool {
return s.AnyFile(func(file *File) bool { return file.HasUnstagedChanges })
}
func (s *FileChangeNode) GetHasStagedChanges() bool {
return s.AnyFile(func(file *File) bool { return file.HasStagedChanges })
}
func (s *FileChangeNode) GetHasInlineMergeConflicts() bool {
return s.AnyFile(func(file *File) bool { return file.HasInlineMergeConflicts })
}
func (s *FileChangeNode) AnyFile(test func(file *File) bool) bool {
return s.Any(func(node *FileChangeNode) bool {
return node.IsLeaf() && test(node.File)
})
}
func (s *FileChangeNode) Any(test func(node *FileChangeNode) bool) bool {
if test(s) {
return true
}
for _, child := range s.Children {
if test(child) {
return true
}
}
return false
}
func (s *FileChangeNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileChangeNode {
node, _ := s.getNodeAtIndexAux(index, collapsedPaths)
return node
}
func (s *FileChangeNode) getNodeAtIndexAux(index int, collapsedPaths map[string]bool) (*FileChangeNode, int) {
offset := 1
if index == 0 {
return s, offset
}
if !collapsedPaths[s.GetPath()] {
for _, child := range s.Children {
node, offsetChange := child.getNodeAtIndexAux(index-offset, collapsedPaths)
offset += offsetChange
if node != nil {
return node, offset
}
}
}
return nil, offset
}
func (s *FileChangeNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) {
return s.getIndexForPathAux(path, collapsedPaths)
}
func (s *FileChangeNode) getIndexForPathAux(path string, collapsedPaths map[string]bool) (int, bool) {
offset := 0
if s.Path == path {
return offset, true
}
if !collapsedPaths[s.GetPath()] {
for _, child := range s.Children {
offsetChange, found := child.getIndexForPathAux(path, collapsedPaths)
offset += offsetChange + 1
if found {
return offset, true
}
}
}
return offset, false
}
func (s *FileChangeNode) IsLeaf() bool {
return s.File != nil
}
func (s *FileChangeNode) Size(collapsedPaths map[string]bool) int {
output := 1
if !collapsedPaths[s.GetPath()] {
for _, child := range s.Children {
output += child.Size(collapsedPaths)
}
}
return output
}
func (s *FileChangeNode) Flatten(collapsedPaths map[string]bool) []*FileChangeNode {
arr := []*FileChangeNode{s}
if !collapsedPaths[s.GetPath()] {
for _, child := range s.Children {
arr = append(arr, child.Flatten(collapsedPaths)...)
}
}
return arr
}
func (s *FileChangeNode) Sort() {
s.sortChildren()
for _, child := range s.Children {
child.Sort()
}
}
func (s *FileChangeNode) sortChildren() {
if s.IsLeaf() {
return
}
sortedChildren := make([]*FileChangeNode, len(s.Children))
copy(sortedChildren, s.Children)
sort.Slice(sortedChildren, func(i, j int) bool {
if !sortedChildren[i].IsLeaf() && sortedChildren[j].IsLeaf() {
return true
}
if sortedChildren[i].IsLeaf() && !sortedChildren[j].IsLeaf() {
return false
}
return sortedChildren[i].Path < sortedChildren[j].Path
})
// TODO: think about making this in-place
s.Children = sortedChildren
}
// returns true if any descendant file is tracked
func (s *FileChangeNode) GetIsTracked() bool {
if s.File != nil {
return s.File.GetIsTracked()
}
for _, child := range s.Children {
if child.GetIsTracked() {
return true
}
}
return false
}
func (s *FileChangeNode) GetPath() string {
return s.Path
}
func (s *FileChangeNode) Compress() {
if s == nil {
return
}
s.compressAux()
}
func (s *FileChangeNode) compressAux() *FileChangeNode {
if s.IsLeaf() {
return s
}
for i := range s.Children {
for s.Children[i].HasExactlyOneChild() {
prevCompressionLevel := s.Children[i].CompressionLevel
grandchild := s.Children[i].Children[0]
s.Children[i] = grandchild
s.Children[i].CompressionLevel = prevCompressionLevel + 1
}
}
for i := range s.Children {
s.Children[i] = s.Children[i].compressAux()
}
return s
}
func (s *FileChangeNode) HasExactlyOneChild() bool {
return len(s.Children) == 1
}
// This ignores the root
func (s *FileChangeNode) GetPathsMatching(test func(*FileChangeNode) bool) []string {
paths := []string{}
if test(s) {
paths = append(paths, s.GetPath())
}
for _, child := range s.Children {
paths = append(paths, child.GetPathsMatching(test)...)
}
return paths
}
func (s *FileChangeNode) ID() string {
return s.GetPath()
}
func (s *FileChangeNode) Description() string {
return s.GetPath()
}
func (s *FileChangeNode) ForEachFile(cb func(*File) error) error {
if s.File != nil {
if err := cb(s.File); err != nil {
return err
}
}
for _, child := range s.Children {
if err := child.ForEachFile(cb); err != nil {
return err
}
}
return nil
}
func (s *FileChangeNode) GetLeaves() []*FileChangeNode {
if s.IsLeaf() {
return []*FileChangeNode{s}
}
output := []*FileChangeNode{}
for _, child := range s.Children {
output = append(output, child.GetLeaves()...)
}
return output
}
func (s *FileChangeNode) NameAtDepth(depth int) string {
splitName := strings.Split(s.Path, string(os.PathSeparator))
name := filepath.Join(splitName[depth:]...)
if s.File != nil && s.File.IsRename() {
splitPrevName := strings.Split(s.File.PreviousName, string(os.PathSeparator))
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
sameParentDir := len(splitName) == len(splitPrevName) && filepath.Join(splitName[0:depth]...) == filepath.Join(splitPrevName[0:depth]...)
if sameParentDir {
prevName = filepath.Join(splitPrevName[depth:]...)
}
return fmt.Sprintf("%s%s%s", prevName, " → ", name)
}
return name
}

View File

@ -1,124 +0,0 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompress(t *testing.T) {
scenarios := []struct {
name string
root *FileChangeNode
expected *FileChangeNode
}{
{
name: "nil node",
root: nil,
expected: nil,
},
{
name: "leaf node",
root: &FileChangeNode{
Path: "",
Children: []*FileChangeNode{
{File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
},
},
expected: &FileChangeNode{
Path: "",
Children: []*FileChangeNode{
{File: &File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
},
},
},
{
name: "big example",
root: &FileChangeNode{
Path: "",
Children: []*FileChangeNode{
{
Path: "dir1",
Children: []*FileChangeNode{
{
File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file2",
},
},
},
{
Path: "dir2",
Children: []*FileChangeNode{
{
File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
Path: "dir2/file3",
},
{
File: &File{Name: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/file4",
},
},
},
{
Path: "dir3",
Children: []*FileChangeNode{
{
Path: "dir3/dir3-1",
Children: []*FileChangeNode{
{
File: &File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir3/dir3-1/file5",
},
},
},
},
},
{
File: &File{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "file1",
},
},
},
expected: &FileChangeNode{
Path: "",
Children: []*FileChangeNode{
{
Path: "dir1/file2",
File: &File{Name: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
CompressionLevel: 1,
},
{
Path: "dir2",
Children: []*FileChangeNode{
{
File: &File{Name: "file3", ShortStatus: " M", HasStagedChanges: true},
Path: "dir2/file3",
},
{
File: &File{Name: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/file4",
},
},
},
{
Path: "dir3/dir3-1/file5",
File: &File{Name: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
CompressionLevel: 2,
},
{
File: &File{Name: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "file1",
},
},
},
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
s.root.Compress()
assert.EqualValues(t, s.expected, s.root)
})
}
}