Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve implementation of diff-file-tree #32768

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
14 changes: 14 additions & 0 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1432,11 +1432,13 @@ func buildTree(files []*DiffFile) []*FileTreeNode {
for _, node := range result {
if len(node.Children) > 0 {
mergedNode := mergeSingleChildDirs(node)
sortChildren(mergedNode)
roots = append(roots, mergedNode)
} else {
roots = append(roots, node)
}
}
sortChildren(&FileTreeNode{Children: roots})
return roots
}

Expand All @@ -1455,3 +1457,15 @@ func mergeSingleChildDirs(node *FileTreeNode) *FileTreeNode {
}
return node
}

func sortChildren(node *FileTreeNode) {
sort.Slice(node.Children, func(i, j int) bool {
if node.Children[i].IsFile == node.Children[j].IsFile {
return node.Children[i].Name < node.Children[j].Name
}
return !node.Children[i].IsFile
kerwin612 marked this conversation as resolved.
Show resolved Hide resolved
})
for _, child := range node.Children {
sortChildren(child)
}
}
Loading