|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Avalonia.Threading; |
| 5 | + |
| 6 | +namespace SourceGit.ViewModels |
| 7 | +{ |
| 8 | + public class ScanRepositories : Popup |
| 9 | + { |
| 10 | + public string RootDir |
| 11 | + { |
| 12 | + get; |
| 13 | + private set; |
| 14 | + } = string.Empty; |
| 15 | + |
| 16 | + public ScanRepositories(string rootDir) |
| 17 | + { |
| 18 | + GetManagedRepositories(Preference.Instance.RepositoryNodes, _managed); |
| 19 | + |
| 20 | + RootDir = rootDir; |
| 21 | + View = new Views.ScanRepositories() { DataContext = this }; |
| 22 | + } |
| 23 | + |
| 24 | + public override Task<bool> Sure() |
| 25 | + { |
| 26 | + ProgressDescription = $"Scan repositories under '{RootDir}' ..."; |
| 27 | + |
| 28 | + return Task.Run(() => |
| 29 | + { |
| 30 | + // If it is too fast, the panel will dispear very quickly, the we'll have a bad experience. |
| 31 | + Task.Delay(500).Wait(); |
| 32 | + |
| 33 | + var founded = new List<string>(); |
| 34 | + GetUnmanagedRepositories(new DirectoryInfo(RootDir), founded, new EnumerationOptions() |
| 35 | + { |
| 36 | + AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, |
| 37 | + IgnoreInaccessible = true, |
| 38 | + }); |
| 39 | + |
| 40 | + Dispatcher.UIThread.Invoke(() => |
| 41 | + { |
| 42 | + foreach (var f in founded) |
| 43 | + Preference.Instance.FindOrAddNodeByRepositoryPath(f, null, false); |
| 44 | + Welcome.Instance.Refresh(); |
| 45 | + }); |
| 46 | + |
| 47 | + return true; |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + private void GetManagedRepositories(List<RepositoryNode> group, HashSet<string> repos) |
| 52 | + { |
| 53 | + foreach (RepositoryNode node in group) |
| 54 | + { |
| 55 | + if (node.IsRepository) |
| 56 | + repos.Add(node.Id); |
| 57 | + else |
| 58 | + GetManagedRepositories(node.SubNodes, repos); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void GetUnmanagedRepositories(DirectoryInfo dir, List<string> outs, EnumerationOptions opts, int depth = 0) |
| 63 | + { |
| 64 | + var subdirs = dir.GetDirectories("*", opts); |
| 65 | + foreach (var subdir in subdirs) |
| 66 | + { |
| 67 | + SetProgressDescription($"Scanning {subdir.FullName}..."); |
| 68 | + |
| 69 | + var normalizedSelf = subdir.FullName.Replace("\\", "/"); |
| 70 | + if (_managed.Contains(normalizedSelf)) |
| 71 | + continue; |
| 72 | + |
| 73 | + var gitDir = Path.Combine(subdir.FullName, ".git"); |
| 74 | + if (Directory.Exists(gitDir) || File.Exists(gitDir)) |
| 75 | + { |
| 76 | + var test = new Commands.QueryRepositoryRootPath(subdir.FullName).ReadToEnd(); |
| 77 | + if (test.IsSuccess && !string.IsNullOrEmpty(test.StdOut)) |
| 78 | + { |
| 79 | + var normalized = test.StdOut.Trim().Replace("\\", "/"); |
| 80 | + if (!_managed.Contains(normalizedSelf)) |
| 81 | + outs.Add(normalized); |
| 82 | + |
| 83 | + continue; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (depth < 8) |
| 88 | + GetUnmanagedRepositories(subdir, outs, opts, depth + 1); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private HashSet<string> _managed = new HashSet<string>(); |
| 93 | + } |
| 94 | +} |
0 commit comments