Skip to content

Commit 19b947d

Browse files
authored
fix(filetree): use forward-slash path separator for git diff paths (#121) (#128)
1 parent 7b12b79 commit 19b947d

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

pkg/ui/panes/filetree/filetree.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package filetree
22

33
import (
4-
"os"
5-
"path/filepath"
4+
"path"
65
"strings"
76

87
"charm.land/bubbles/v2/key"
@@ -241,8 +240,13 @@ func buildFullFileTree(files []*gitdiff.File, cfg config.Config) *tree.Node {
241240
subTree := t
242241

243242
name := filenode.GetFileName(file)
244-
dir := filepath.Dir(name)
245-
parts := strings.Split(dir, string(os.PathSeparator))
243+
// git diff always emits forward-slash paths regardless of host OS, so
244+
// use the path package (forward-slash) rather than filepath /
245+
// os.PathSeparator. On Windows the latter would split on `\\` and
246+
// never find the `/` separators, leaving every file as a flat root
247+
// child with no directory hierarchy. See #121.
248+
dir := path.Dir(name)
249+
parts := strings.Split(dir, "/")
246250
existingPath := ""
247251

248252
// walk the tree to find existing path
@@ -252,7 +256,7 @@ func buildFullFileTree(files []*gitdiff.File, cfg config.Config) *tree.Node {
252256
for _, child := range children {
253257
if dir, ok := child.GivenValue().(*dirnode.DirNode); ok && dir.Name == part {
254258
subTree = child
255-
existingPath = existingPath + part + string(os.PathSeparator)
259+
existingPath = existingPath + part + "/"
256260
found = true
257261
// found a part of the path, continue to the subtree
258262
break
@@ -265,7 +269,7 @@ func buildFullFileTree(files []*gitdiff.File, cfg config.Config) *tree.Node {
265269

266270
// path does not exist from this point, need to create it
267271
leftover := strings.TrimPrefix(name, existingPath)
268-
parts = strings.Split(leftover, string(os.PathSeparator))
272+
parts = strings.Split(leftover, "/")
269273
for i, part := range parts {
270274
var c *tree.Node
271275
if i == len(parts)-1 {
@@ -277,7 +281,7 @@ func buildFullFileTree(files []*gitdiff.File, cfg config.Config) *tree.Node {
277281
} else {
278282
dirNode := dirnode.DirNode{
279283
Name: part,
280-
FullPath: filepath.Join(existingPath, filepath.Join(parts[:i]...), part),
284+
FullPath: path.Join(existingPath, path.Join(parts[:i]...), part),
281285
}
282286
c = tree.Root(&dirNode)
283287
subTree.Child(c)
@@ -341,8 +345,8 @@ func collapseTree(t *tree.Node) *tree.Node {
341345
}
342346

343347
newDir := dirnode.DirNode{
344-
FullPath: filepath.Join(rootDir.FullPath, dir.Name),
345-
Name: filepath.Join(rootDir.Name, dir.Name),
348+
FullPath: path.Join(rootDir.FullPath, dir.Name),
349+
Name: path.Join(rootDir.Name, dir.Name),
346350
}
347351
children := make([]any, 0)
348352
for _, c := range child.ChildNodes() {

0 commit comments

Comments
 (0)