1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

Make Node.path private

This is in preparation for changing the meaning of path in the next commit.
This commit is contained in:
Stefan Haller
2025-02-28 21:15:15 +01:00
parent 3f4cb8bdda
commit 0b42cfb568
8 changed files with 106 additions and 105 deletions

View File

@ -41,7 +41,7 @@ func BuildTreeFromFiles(files []*models.File) *Node[models.File] {
}
newChild := &Node[models.File]{
Path: path,
path: path,
File: setFile,
}
curr.Children = append(curr.Children, newChild)
@ -83,14 +83,14 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFil
path := join(splitPath[:i+1])
for _, existingChild := range curr.Children {
if existingChild.Path == path {
if existingChild.path == path {
curr = existingChild
continue outer
}
}
newChild := &Node[models.CommitFile]{
Path: path,
path: path,
File: setFile,
}
curr.Children = append(curr.Children, newChild)

View File

@ -17,7 +17,7 @@ func TestBuildTreeFromFiles(t *testing.T) {
name: "no files",
files: []*models.File{},
expected: &Node[models.File]{
Path: "",
path: "",
Children: nil,
},
},
@ -32,18 +32,18 @@ func TestBuildTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
Path: "dir1",
path: "dir1",
Children: []*Node[models.File]{
{
File: &models.File{Path: "dir1/a"},
Path: "dir1/a",
path: "dir1/a",
},
{
File: &models.File{Path: "dir1/b"},
Path: "dir1/b",
path: "dir1/b",
},
},
},
@ -61,24 +61,24 @@ func TestBuildTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
Path: "dir1/dir3",
path: "dir1/dir3",
Children: []*Node[models.File]{
{
File: &models.File{Path: "dir1/dir3/a"},
Path: "dir1/dir3/a",
path: "dir1/dir3/a",
},
},
CompressionLevel: 1,
},
{
Path: "dir2/dir4",
path: "dir2/dir4",
Children: []*Node[models.File]{
{
File: &models.File{Path: "dir2/dir4/b"},
Path: "dir2/dir4/b",
path: "dir2/dir4/b",
},
},
CompressionLevel: 1,
@ -97,15 +97,15 @@ func TestBuildTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
File: &models.File{Path: "a"},
Path: "a",
path: "a",
},
{
File: &models.File{Path: "b"},
Path: "b",
path: "b",
},
},
},
@ -125,21 +125,21 @@ func TestBuildTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
// it is a little strange that we're not bubbling up our merge conflict
// here but we are technically still in tree mode and that's the rule
Children: []*Node[models.File]{
{
File: &models.File{Path: "a"},
Path: "a",
path: "a",
},
{
File: &models.File{Path: "b"},
Path: "b",
path: "b",
},
{
File: &models.File{Path: "z", HasMergeConflicts: true},
Path: "z",
path: "z",
},
},
},
@ -164,7 +164,7 @@ func TestBuildFlatTreeFromFiles(t *testing.T) {
name: "no files",
files: []*models.File{},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{},
},
},
@ -179,16 +179,16 @@ func TestBuildFlatTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
File: &models.File{Path: "dir1/a"},
Path: "dir1/a",
path: "dir1/a",
CompressionLevel: 0,
},
{
File: &models.File{Path: "dir1/b"},
Path: "dir1/b",
path: "dir1/b",
CompressionLevel: 0,
},
},
@ -205,16 +205,16 @@ func TestBuildFlatTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
File: &models.File{Path: "dir1/a"},
Path: "dir1/a",
path: "dir1/a",
CompressionLevel: 0,
},
{
File: &models.File{Path: "dir2/b"},
Path: "dir2/b",
path: "dir2/b",
CompressionLevel: 0,
},
},
@ -231,15 +231,15 @@ func TestBuildFlatTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
File: &models.File{Path: "a"},
Path: "a",
path: "a",
},
{
File: &models.File{Path: "b"},
Path: "b",
path: "b",
},
},
},
@ -273,31 +273,31 @@ func TestBuildFlatTreeFromFiles(t *testing.T) {
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
File: &models.File{Path: "c1", HasMergeConflicts: true},
Path: "c1",
path: "c1",
},
{
File: &models.File{Path: "c2", HasMergeConflicts: true},
Path: "c2",
path: "c2",
},
{
File: &models.File{Path: "b1", Tracked: true},
Path: "b1",
path: "b1",
},
{
File: &models.File{Path: "b2", Tracked: true},
Path: "b2",
path: "b2",
},
{
File: &models.File{Path: "a1", Tracked: false},
Path: "a1",
path: "a1",
},
{
File: &models.File{Path: "a2", Tracked: false},
Path: "a2",
path: "a2",
},
},
},
@ -322,7 +322,7 @@ func TestBuildTreeFromCommitFiles(t *testing.T) {
name: "no files",
files: []*models.CommitFile{},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: nil,
},
},
@ -337,18 +337,18 @@ func TestBuildTreeFromCommitFiles(t *testing.T) {
},
},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: []*Node[models.CommitFile]{
{
Path: "dir1",
path: "dir1",
Children: []*Node[models.CommitFile]{
{
File: &models.CommitFile{Path: "dir1/a"},
Path: "dir1/a",
path: "dir1/a",
},
{
File: &models.CommitFile{Path: "dir1/b"},
Path: "dir1/b",
path: "dir1/b",
},
},
},
@ -366,24 +366,24 @@ func TestBuildTreeFromCommitFiles(t *testing.T) {
},
},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: []*Node[models.CommitFile]{
{
Path: "dir1/dir3",
path: "dir1/dir3",
Children: []*Node[models.CommitFile]{
{
File: &models.CommitFile{Path: "dir1/dir3/a"},
Path: "dir1/dir3/a",
path: "dir1/dir3/a",
},
},
CompressionLevel: 1,
},
{
Path: "dir2/dir4",
path: "dir2/dir4",
Children: []*Node[models.CommitFile]{
{
File: &models.CommitFile{Path: "dir2/dir4/b"},
Path: "dir2/dir4/b",
path: "dir2/dir4/b",
},
},
CompressionLevel: 1,
@ -402,15 +402,15 @@ func TestBuildTreeFromCommitFiles(t *testing.T) {
},
},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: []*Node[models.CommitFile]{
{
File: &models.CommitFile{Path: "a"},
Path: "a",
path: "a",
},
{
File: &models.CommitFile{Path: "b"},
Path: "b",
path: "b",
},
},
},
@ -435,7 +435,7 @@ func TestBuildFlatTreeFromCommitFiles(t *testing.T) {
name: "no files",
files: []*models.CommitFile{},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: []*Node[models.CommitFile]{},
},
},
@ -450,16 +450,16 @@ func TestBuildFlatTreeFromCommitFiles(t *testing.T) {
},
},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: []*Node[models.CommitFile]{
{
File: &models.CommitFile{Path: "dir1/a"},
Path: "dir1/a",
path: "dir1/a",
CompressionLevel: 0,
},
{
File: &models.CommitFile{Path: "dir1/b"},
Path: "dir1/b",
path: "dir1/b",
CompressionLevel: 0,
},
},
@ -476,16 +476,16 @@ func TestBuildFlatTreeFromCommitFiles(t *testing.T) {
},
},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: []*Node[models.CommitFile]{
{
File: &models.CommitFile{Path: "dir1/a"},
Path: "dir1/a",
path: "dir1/a",
CompressionLevel: 0,
},
{
File: &models.CommitFile{Path: "dir2/b"},
Path: "dir2/b",
path: "dir2/b",
CompressionLevel: 0,
},
},
@ -502,15 +502,15 @@ func TestBuildFlatTreeFromCommitFiles(t *testing.T) {
},
},
expected: &Node[models.CommitFile]{
Path: "",
path: "",
Children: []*Node[models.CommitFile]{
{
File: &models.CommitFile{Path: "a"},
Path: "a",
path: "a",
},
{
File: &models.CommitFile{Path: "b"},
Path: "b",
path: "b",
},
},
},

View File

@ -27,7 +27,7 @@ type CommitFileTree struct {
func (self *CommitFileTree) CollapseAll() {
dirPaths := lo.FilterMap(self.GetAllItems(), func(file *CommitFileNode, index int) (string, bool) {
return file.Path, !file.IsFile()
return file.path, !file.IsFile()
})
for _, path := range dirPaths {

View File

@ -148,12 +148,12 @@ func (self *CommitFileTreeViewModel) ToggleShowTree() {
if selectedNode == nil {
return
}
path := selectedNode.Path
path := selectedNode.path
if self.InTreeMode() {
self.ExpandToPath(path)
} else if len(selectedNode.Children) > 0 {
path = selectedNode.GetLeaves()[0].Path
path = selectedNode.GetLeaves()[0].path
}
index, found := self.GetIndexForPath(path)
@ -170,7 +170,7 @@ func (self *CommitFileTreeViewModel) CollapseAll() {
return
}
topLevelPath := strings.Split(selectedNode.Path, "/")[0]
topLevelPath := strings.Split(selectedNode.path, "/")[0]
index, found := self.GetIndexForPath(topLevelPath)
if found {
self.SetSelectedLineIdx(index)
@ -186,7 +186,7 @@ func (self *CommitFileTreeViewModel) ExpandAll() {
return
}
index, found := self.GetIndexForPath(selectedNode.Path)
index, found := self.GetIndexForPath(selectedNode.path)
if found {
self.SetSelectedLineIdx(index)
}

View File

@ -21,54 +21,54 @@ func TestCompress(t *testing.T) {
{
name: "leaf node",
root: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{File: &models.File{Path: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
{File: &models.File{Path: "test", ShortStatus: " M", HasStagedChanges: true}, path: "test"},
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{File: &models.File{Path: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"},
{File: &models.File{Path: "test", ShortStatus: " M", HasStagedChanges: true}, path: "test"},
},
},
},
{
name: "big example",
root: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
Path: "dir1",
path: "dir1",
Children: []*Node[models.File]{
{
File: &models.File{Path: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file2",
path: "dir1/file2",
},
},
},
{
Path: "dir2",
path: "dir2",
Children: []*Node[models.File]{
{
File: &models.File{Path: "file3", ShortStatus: " M", HasStagedChanges: true},
Path: "dir2/file3",
path: "dir2/file3",
},
{
File: &models.File{Path: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/file4",
path: "dir2/file4",
},
},
},
{
Path: "dir3",
path: "dir3",
Children: []*Node[models.File]{
{
Path: "dir3/dir3-1",
path: "dir3/dir3-1",
Children: []*Node[models.File]{
{
File: &models.File{Path: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir3/dir3-1/file5",
path: "dir3/dir3-1/file5",
},
},
},
@ -76,48 +76,48 @@ func TestCompress(t *testing.T) {
},
{
File: &models.File{Path: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "file1",
path: "file1",
},
},
},
expected: &Node[models.File]{
Path: "",
path: "",
Children: []*Node[models.File]{
{
Path: "dir1",
path: "dir1",
Children: []*Node[models.File]{
{
File: &models.File{Path: "file2", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir1/file2",
path: "dir1/file2",
},
},
},
{
Path: "dir2",
path: "dir2",
Children: []*Node[models.File]{
{
File: &models.File{Path: "file3", ShortStatus: " M", HasStagedChanges: true},
Path: "dir2/file3",
path: "dir2/file3",
},
{
File: &models.File{Path: "file4", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir2/file4",
path: "dir2/file4",
},
},
},
{
Path: "dir3/dir3-1",
path: "dir3/dir3-1",
CompressionLevel: 1,
Children: []*Node[models.File]{
{
File: &models.File{Path: "file5", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "dir3/dir3-1/file5",
path: "dir3/dir3-1/file5",
},
},
},
{
File: &models.File{Path: "file1", ShortStatus: "M ", HasUnstagedChanges: true},
Path: "file1",
path: "file1",
},
},
},

View File

@ -185,7 +185,7 @@ func (self *FileTree) ToggleCollapsed(path string) {
func (self *FileTree) CollapseAll() {
dirPaths := lo.FilterMap(self.GetAllItems(), func(file *FileNode, index int) (string, bool) {
return file.Path, !file.IsFile()
return file.path, !file.IsFile()
})
for _, path := range dirPaths {

View File

@ -103,7 +103,7 @@ func (self *FileTreeViewModel) SetTree() {
// for when you stage the old file of a rename and the new file is in a collapsed dir
for _, file := range newFiles {
if selectedNode != nil && selectedNode.Path != "" && file.PreviousPath == selectedNode.Path {
if selectedNode != nil && selectedNode.path != "" && file.PreviousPath == selectedNode.path {
self.ExpandToPath(file.Path)
}
}
@ -139,7 +139,7 @@ func (self *FileTreeViewModel) findNewSelectedIdx(prevNodes []*FileNode, currNod
if node.File != nil && node.File.IsRename() {
return node.File.Names()
} else {
return []string{node.Path}
return []string{node.path}
}
}
@ -151,7 +151,7 @@ func (self *FileTreeViewModel) findNewSelectedIdx(prevNodes []*FileNode, currNod
// If you started off with a rename selected, and now it's broken in two, we want you to jump to the new file, not the old file.
// This is because the new should be in the same position as the rename was meaning less cursor jumping
foundOldFileInRename := prevNode.File != nil && prevNode.File.IsRename() && node.Path == prevNode.File.PreviousPath
foundOldFileInRename := prevNode.File != nil && prevNode.File.IsRename() && node.path == prevNode.File.PreviousPath
foundNode := utils.StringArraysOverlap(paths, selectedPaths) && !foundOldFileInRename
if foundNode {
return idx
@ -178,12 +178,12 @@ func (self *FileTreeViewModel) ToggleShowTree() {
if selectedNode == nil {
return
}
path := selectedNode.Path
path := selectedNode.path
if self.InTreeMode() {
self.ExpandToPath(path)
} else if len(selectedNode.Children) > 0 {
path = selectedNode.GetLeaves()[0].Path
path = selectedNode.GetLeaves()[0].path
}
index, found := self.GetIndexForPath(path)
@ -200,7 +200,7 @@ func (self *FileTreeViewModel) CollapseAll() {
return
}
topLevelPath := strings.Split(selectedNode.Path, "/")[0]
topLevelPath := strings.Split(selectedNode.path, "/")[0]
index, found := self.GetIndexForPath(topLevelPath)
if found {
self.SetSelectedLineIdx(index)
@ -216,7 +216,7 @@ func (self *FileTreeViewModel) ExpandAll() {
return
}
index, found := self.GetIndexForPath(selectedNode.Path)
index, found := self.GetIndexForPath(selectedNode.path)
if found {
self.SetSelectedLineIdx(index)
}

View File

@ -20,7 +20,8 @@ type Node[T any] struct {
Children []*Node[T]
// path of the file/directory
Path string
// private; use GetPath() to access
path string
// rather than render a tree as:
// a/
@ -47,7 +48,7 @@ func (self *Node[T]) GetFile() *T {
}
func (self *Node[T]) GetPath() string {
return self.Path
return self.path
}
func (self *Node[T]) Sort() {
@ -89,7 +90,7 @@ func (self *Node[T]) SortChildren() {
return 1
}
return strings.Compare(a.Path, b.Path)
return strings.Compare(a.path, b.path)
})
// TODO: think about making this in-place
@ -159,7 +160,7 @@ func (self *Node[T]) EveryFile(test func(*T) bool) bool {
func (self *Node[T]) Flatten(collapsedPaths *CollapsedPaths) []*Node[T] {
result := []*Node[T]{self}
if len(self.Children) > 0 && !collapsedPaths.IsCollapsed(self.Path) {
if len(self.Children) > 0 && !collapsedPaths.IsCollapsed(self.path) {
result = append(result, lo.FlatMap(self.Children, func(child *Node[T], _ int) []*Node[T] {
return child.Flatten(collapsedPaths)
})...)
@ -185,7 +186,7 @@ func (self *Node[T]) getNodeAtIndexAux(index int, collapsedPaths *CollapsedPaths
return self, offset
}
if !collapsedPaths.IsCollapsed(self.Path) {
if !collapsedPaths.IsCollapsed(self.path) {
for _, child := range self.Children {
foundNode, offsetChange := child.getNodeAtIndexAux(index-offset, collapsedPaths)
offset += offsetChange
@ -201,11 +202,11 @@ func (self *Node[T]) getNodeAtIndexAux(index int, collapsedPaths *CollapsedPaths
func (self *Node[T]) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) {
offset := 0
if self.Path == path {
if self.path == path {
return offset, true
}
if !collapsedPaths.IsCollapsed(self.Path) {
if !collapsedPaths.IsCollapsed(self.path) {
for _, child := range self.Children {
offsetChange, found := child.GetIndexForPath(path, collapsedPaths)
offset += offsetChange + 1
@ -225,7 +226,7 @@ func (self *Node[T]) Size(collapsedPaths *CollapsedPaths) int {
output := 1
if !collapsedPaths.IsCollapsed(self.Path) {
if !collapsedPaths.IsCollapsed(self.path) {
for _, child := range self.Children {
output += child.Size(collapsedPaths)
}
@ -309,5 +310,5 @@ func (self *Node[T]) Description() string {
}
func (self *Node[T]) Name() string {
return path.Base(self.Path)
return path.Base(self.path)
}