|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace SourceGit.Models |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// One entry of the file layer: either a real file or a directory that git reported a |
| 11 | + /// file below. Because the directory list is derived from the file list, a directory |
| 12 | + /// with no visible file under it can never appear. |
| 13 | + /// </summary> |
| 14 | + public class RepositoryFileNode |
| 15 | + { |
| 16 | + public string Name { get; set; } = string.Empty; |
| 17 | + public string FullPath { get; set; } = string.Empty; |
| 18 | + public bool IsDirectory { get; set; } = false; |
| 19 | + public List<RepositoryFileNode> Children { get; } = new(); |
| 20 | + |
| 21 | + internal RepositoryFileNode FindChild(string name) |
| 22 | + { |
| 23 | + if (_lookup == null) |
| 24 | + _lookup = new Dictionary<string, RepositoryFileNode>(StringComparer.OrdinalIgnoreCase); |
| 25 | + |
| 26 | + return _lookup.TryGetValue(name, out var node) ? node : null; |
| 27 | + } |
| 28 | + |
| 29 | + internal void AddChild(RepositoryFileNode child) |
| 30 | + { |
| 31 | + if (_lookup == null) |
| 32 | + _lookup = new Dictionary<string, RepositoryFileNode>(StringComparer.OrdinalIgnoreCase); |
| 33 | + |
| 34 | + _lookup[child.Name] = child; |
| 35 | + Children.Add(child); |
| 36 | + } |
| 37 | + |
| 38 | + internal void SortRecursive() |
| 39 | + { |
| 40 | + Children.Sort((a, b) => |
| 41 | + { |
| 42 | + if (a.IsDirectory != b.IsDirectory) |
| 43 | + return a.IsDirectory ? -1 : 1; |
| 44 | + |
| 45 | + return string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase); |
| 46 | + }); |
| 47 | + |
| 48 | + foreach (var child in Children) |
| 49 | + { |
| 50 | + if (child.IsDirectory) |
| 51 | + child.SortRecursive(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private Dictionary<string, RepositoryFileNode> _lookup; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Reads the whole visible file list of a repository once and folds the paths into a |
| 60 | + /// directory tree. Nothing is truncated: the caller decides how deep to materialise |
| 61 | + /// view-model nodes, so a repository with thousands of files still costs one git |
| 62 | + /// process and no UI nodes until a branch is actually expanded. |
| 63 | + /// </summary> |
| 64 | + public static class RepositoryFileIndex |
| 65 | + { |
| 66 | + public static async Task<RepositoryFileNode> BuildAsync(string repoPath, CancellationToken cancellationToken) |
| 67 | + { |
| 68 | + var root = new RepositoryFileNode |
| 69 | + { |
| 70 | + Name = Path.GetFileName(repoPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)), |
| 71 | + FullPath = repoPath, |
| 72 | + IsDirectory = true, |
| 73 | + }; |
| 74 | + |
| 75 | + var command = new Commands.QueryRepositoryFiles(repoPath) { CancellationToken = cancellationToken }; |
| 76 | + var paths = await command.GetPathsAsync().ConfigureAwait(false); |
| 77 | + |
| 78 | + foreach (var path in paths) |
| 79 | + { |
| 80 | + var segments = path.Split('/'); |
| 81 | + var cursor = root; |
| 82 | + |
| 83 | + for (var i = 0; i < segments.Length; i++) |
| 84 | + { |
| 85 | + var segment = segments[i]; |
| 86 | + if (segment.Length == 0) |
| 87 | + continue; |
| 88 | + |
| 89 | + var isLeaf = i == segments.Length - 1; |
| 90 | + var existing = cursor.FindChild(segment); |
| 91 | + if (existing == null) |
| 92 | + { |
| 93 | + existing = new RepositoryFileNode |
| 94 | + { |
| 95 | + Name = segment, |
| 96 | + FullPath = Path.Combine(cursor.FullPath, segment), |
| 97 | + IsDirectory = !isLeaf, |
| 98 | + }; |
| 99 | + cursor.AddChild(existing); |
| 100 | + } |
| 101 | + else if (!isLeaf) |
| 102 | + { |
| 103 | + // The same directory can be reached by many files; the first one to |
| 104 | + // arrive may have been a leaf, so promote it once a real child appears. |
| 105 | + existing.IsDirectory = true; |
| 106 | + } |
| 107 | + |
| 108 | + cursor = existing; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + root.SortRecursive(); |
| 113 | + return root; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments