fix: crash when scan repositories under default clone dir (#434)

This commit is contained in:
leo 2024-09-02 07:54:46 +08:00
parent e4f95745ce
commit f586979ac5
No known key found for this signature in database

View file

@ -12,7 +12,7 @@ namespace SourceGit.ViewModels
{ {
get; get;
private set; private set;
} = string.Empty; }
public ScanRepositories(string rootDir) public ScanRepositories(string rootDir)
{ {
@ -28,7 +28,7 @@ namespace SourceGit.ViewModels
return Task.Run(() => return Task.Run(() =>
{ {
// If it is too fast, the panel will dispear very quickly, the we'll have a bad experience. // If it is too fast, the panel will disappear very quickly, then we'll have a bad experience.
Task.Delay(500).Wait(); Task.Delay(500).Wait();
var rootDir = new DirectoryInfo(RootDir); var rootDir = new DirectoryInfo(RootDir);
@ -42,15 +42,25 @@ namespace SourceGit.ViewModels
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
var normalizedRoot = rootDir.FullName.Replace("\\", "/"); var normalizedRoot = rootDir.FullName.Replace("\\", "/");
var prefixLen = normalizedRoot.EndsWith('/') ? normalizedRoot.Length : normalizedRoot.Length + 1;
foreach (var f in founded) foreach (var f in founded)
{ {
var fullpath = new DirectoryInfo(f); var parent = new DirectoryInfo(f).Parent!.FullName.Replace("\\", "/");
var relative = fullpath.Parent!.FullName.Replace("\\", "/").Substring(prefixLen); if (parent.Equals(normalizedRoot, StringComparison.Ordinal))
{
Preference.Instance.FindOrAddNodeByRepositoryPath(f, null, false);
}
else if (parent.StartsWith(normalizedRoot, StringComparison.Ordinal))
{
var relative = parent.Substring(normalizedRoot.Length).TrimStart('/');
var group = FindOrCreateGroupRecursive(Preference.Instance.RepositoryNodes, relative); var group = FindOrCreateGroupRecursive(Preference.Instance.RepositoryNodes, relative);
Preference.Instance.FindOrAddNodeByRepositoryPath(f, group, false); Preference.Instance.FindOrAddNodeByRepositoryPath(f, group, false);
} }
else
{
// Should not happen.
}
}
Welcome.Instance.Refresh(); Welcome.Instance.Refresh();
}); });
@ -61,7 +71,7 @@ namespace SourceGit.ViewModels
private void GetManagedRepositories(List<RepositoryNode> group, HashSet<string> repos) private void GetManagedRepositories(List<RepositoryNode> group, HashSet<string> repos)
{ {
foreach (RepositoryNode node in group) foreach (var node in group)
{ {
if (node.IsRepository) if (node.IsRepository)
repos.Add(node.Id); repos.Add(node.Id);