@@ -18,7 +18,7 @@ type Node struct {
1818
1919// BuildTree parses the output of `git status --porcelain` to construct a file tree.
2020func BuildTree (gitStatus string ) * Node {
21- root := & Node {name : repoRootNodeName }
21+ root := & Node {name : repoRootNodeName , path : "." }
2222
2323 lines := strings .Split (gitStatus , "\n " )
2424 if len (lines ) == 1 && lines [0 ] == "" {
@@ -30,30 +30,35 @@ func BuildTree(gitStatus string) *Node {
3030 continue
3131 }
3232 status := line [:2 ]
33- path := strings .TrimSpace (line [porcelainStatusPrefixLength :])
33+ fullPath := strings .TrimSpace (line [porcelainStatusPrefixLength :])
3434 isRenamed := false
3535
3636 if status [0 ] == 'R' || status [0 ] == 'C' {
37- parts := strings .Split (path , gitRenameDelimiter )
37+ parts := strings .Split (fullPath , gitRenameDelimiter )
3838 if len (parts ) == 2 {
39- path = parts [1 ]
39+ fullPath = parts [1 ]
4040 isRenamed = true
4141 }
4242 }
4343
44- parts := strings .Split (path , string (filepath .Separator ))
44+ parts := strings .Split (fullPath , string (filepath .Separator ))
4545 currentNode := root
4646 for i , part := range parts {
4747 childNode := currentNode .findChild (part )
4848 if childNode == nil {
49- childNode = & Node {name : part }
49+ // Construct path for the new node based on its parent
50+ nodePath := filepath .Join (currentNode .path , part )
51+ if currentNode .path == "." {
52+ nodePath = part
53+ }
54+ childNode = & Node {name : part , path : nodePath }
5055 currentNode .children = append (currentNode .children , childNode )
5156 }
5257 currentNode = childNode
5358
54- if i == len (parts )- 1 {
59+ if i == len (parts )- 1 { // Leaf node (file)
5560 currentNode .status = status
56- currentNode .path = path
61+ currentNode .path = fullPath // Overwrite with the full path from git
5762 currentNode .isRenamed = isRenamed
5863 }
5964 }
@@ -119,6 +124,7 @@ func (n *Node) compact() {
119124 for len (n .children ) == 1 && len (n .children [0 ].children ) > 0 {
120125 child := n .children [0 ]
121126 n .name = filepath .Join (n .name , child .name )
127+ n .path = child .path
122128 n .children = child .children
123129 }
124130}
@@ -132,14 +138,14 @@ func (n *Node) renderRecursive(prefix string, theme Theme) []string {
132138
133139 if len (child .children ) > 0 { // It's a directory
134140 displayName := dirExpandedIcon + child .name
135- lines = append (lines , fmt .Sprintf ("%s\t \t %s" , prefix , displayName ))
141+ lines = append (lines , fmt .Sprintf ("%s\t \t %s\t %s " , prefix , displayName , child . path ))
136142 lines = append (lines , child .renderRecursive (newPrefix , theme )... )
137143 } else { // It's a file.
138144 displayName := child .name
139145 if child .isRenamed {
140146 displayName = child .path
141147 }
142- lines = append (lines , fmt .Sprintf ("%s\t %s\t %s" , prefix , child .status , displayName ))
148+ lines = append (lines , fmt .Sprintf ("%s\t %s\t %s\t %s " , prefix , child .status , displayName , child . path ))
143149 }
144150 }
145151 return lines
0 commit comments