Skip to content

Commit f586979

Browse files
committed
fix: crash when scan repositories under default clone dir (#434)
1 parent e4f9574 commit f586979

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/ViewModels/ScanRepositories.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public string RootDir
1212
{
1313
get;
1414
private set;
15-
} = string.Empty;
15+
}
1616

1717
public ScanRepositories(string rootDir)
1818
{
@@ -28,7 +28,7 @@ public override Task<bool> Sure()
2828

2929
return Task.Run(() =>
3030
{
31-
// If it is too fast, the panel will dispear very quickly, the we'll have a bad experience.
31+
// If it is too fast, the panel will disappear very quickly, then we'll have a bad experience.
3232
Task.Delay(500).Wait();
3333

3434
var rootDir = new DirectoryInfo(RootDir);
@@ -42,14 +42,24 @@ public override Task<bool> Sure()
4242
Dispatcher.UIThread.Invoke(() =>
4343
{
4444
var normalizedRoot = rootDir.FullName.Replace("\\", "/");
45-
var prefixLen = normalizedRoot.EndsWith('/') ? normalizedRoot.Length : normalizedRoot.Length + 1;
46-
45+
4746
foreach (var f in founded)
4847
{
49-
var fullpath = new DirectoryInfo(f);
50-
var relative = fullpath.Parent!.FullName.Replace("\\", "/").Substring(prefixLen);
51-
var group = FindOrCreateGroupRecursive(Preference.Instance.RepositoryNodes, relative);
52-
Preference.Instance.FindOrAddNodeByRepositoryPath(f, group, false);
48+
var parent = new DirectoryInfo(f).Parent!.FullName.Replace("\\", "/");
49+
if (parent.Equals(normalizedRoot, StringComparison.Ordinal))
50+
{
51+
Preference.Instance.FindOrAddNodeByRepositoryPath(f, null, false);
52+
}
53+
else if (parent.StartsWith(normalizedRoot, StringComparison.Ordinal))
54+
{
55+
var relative = parent.Substring(normalizedRoot.Length).TrimStart('/');
56+
var group = FindOrCreateGroupRecursive(Preference.Instance.RepositoryNodes, relative);
57+
Preference.Instance.FindOrAddNodeByRepositoryPath(f, group, false);
58+
}
59+
else
60+
{
61+
// Should not happen.
62+
}
5363
}
5464

5565
Welcome.Instance.Refresh();
@@ -61,7 +71,7 @@ public override Task<bool> Sure()
6171

6272
private void GetManagedRepositories(List<RepositoryNode> group, HashSet<string> repos)
6373
{
64-
foreach (RepositoryNode node in group)
74+
foreach (var node in group)
6575
{
6676
if (node.IsRepository)
6777
repos.Add(node.Id);

0 commit comments

Comments
 (0)