| 79 | } |
| 80 | |
| 81 | func BuildTreeFromCommitFiles( |
| 82 | files []*models.CommitFile, |
| 83 | showRootItem bool, |
| 84 | cmp func(a, b *Node[models.CommitFile]) int, |
| 85 | ) *Node[models.CommitFile] { |
| 86 | root := &Node[models.CommitFile]{} |
| 87 | |
| 88 | var curr *Node[models.CommitFile] |
| 89 | for _, file := range files { |
| 90 | splitPath := SplitFileTreePath(file.Path, showRootItem) |
| 91 | curr = root |
| 92 | outer: |
| 93 | for i := range splitPath { |
| 94 | var setFile *models.CommitFile |
| 95 | isFile := i == len(splitPath)-1 |
| 96 | if isFile { |
| 97 | setFile = file |
| 98 | } |
| 99 | |
| 100 | path := join(splitPath[:i+1]) |
| 101 | |
| 102 | for _, existingChild := range curr.Children { |
| 103 | if existingChild.path == path { |
| 104 | curr = existingChild |
| 105 | continue outer |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if i == 0 && len(files) == 1 && len(splitPath) == 2 { |
| 110 | // skip the root item when there's only one file at top level; we don't need it in that case |
| 111 | continue outer |
| 112 | } |
| 113 | |
| 114 | newChild := &Node[models.CommitFile]{ |
| 115 | path: path, |
| 116 | File: setFile, |
| 117 | } |
| 118 | curr.Children = append(curr.Children, newChild) |
| 119 | |
| 120 | curr = newChild |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | root.Sort(cmp) |
| 125 | root.Compress() |
| 126 | |
| 127 | return root |
| 128 | } |
| 129 | |
| 130 | func BuildFlatTreeFromFiles( |
| 131 | files []*models.File, |