Skip to content

Commit bb749f2

Browse files
committed
enhance: auto create group by relative path when scanning repositories under default clone dir (#427)
1 parent 0d676fa commit bb749f2

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/ViewModels/ScanRepositories.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Threading.Tasks;
45
using Avalonia.Threading;
@@ -30,17 +31,26 @@ public override Task<bool> Sure()
3031
// If it is too fast, the panel will dispear very quickly, the we'll have a bad experience.
3132
Task.Delay(500).Wait();
3233

34+
var rootDir = new DirectoryInfo(RootDir);
3335
var founded = new List<string>();
34-
GetUnmanagedRepositories(new DirectoryInfo(RootDir), founded, new EnumerationOptions()
36+
GetUnmanagedRepositories(rootDir, founded, new EnumerationOptions()
3537
{
3638
AttributesToSkip = FileAttributes.Hidden | FileAttributes.System,
3739
IgnoreInaccessible = true,
3840
});
3941

4042
Dispatcher.UIThread.Invoke(() =>
4143
{
44+
var normalizedRoot = rootDir.FullName.Replace("\\", "/");
45+
var prefixLen = normalizedRoot.EndsWith('/') ? normalizedRoot.Length : normalizedRoot.Length + 1;
46+
4247
foreach (var f in founded)
43-
Preference.Instance.FindOrAddNodeByRepositoryPath(f, null, false);
48+
{
49+
var relative = Path.GetDirectoryName(f).Substring(prefixLen);
50+
var group = FindOrCreateGroupRecursive(Preference.Instance.RepositoryNodes, relative);
51+
Preference.Instance.FindOrAddNodeByRepositoryPath(f, group, false);
52+
}
53+
4454
Welcome.Instance.Refresh();
4555
});
4656

@@ -89,6 +99,45 @@ private void GetUnmanagedRepositories(DirectoryInfo dir, List<string> outs, Enum
8999
}
90100
}
91101

102+
private RepositoryNode FindOrCreateGroupRecursive(List<RepositoryNode> collection, string path)
103+
{
104+
var idx = path.IndexOf('/');
105+
if (idx < 0)
106+
return FindOrCreateGroup(collection, path);
107+
108+
var name = path.Substring(0, idx);
109+
var tail = path.Substring(idx + 1);
110+
var parent = FindOrCreateGroup(collection, name);
111+
return FindOrCreateGroupRecursive(parent.SubNodes, tail);
112+
}
113+
114+
private RepositoryNode FindOrCreateGroup(List<RepositoryNode> collection, string name)
115+
{
116+
foreach (var node in collection)
117+
{
118+
if (node.Name.Equals(name, StringComparison.Ordinal))
119+
return node;
120+
}
121+
122+
var added = new RepositoryNode()
123+
{
124+
Id = Guid.NewGuid().ToString(),
125+
Name = name,
126+
IsRepository = false,
127+
IsExpanded = true,
128+
};
129+
collection.Add(added);
130+
collection.Sort((l, r) =>
131+
{
132+
if (l.IsRepository != r.IsRepository)
133+
return l.IsRepository ? 1 : -1;
134+
135+
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
136+
});
137+
138+
return added;
139+
}
140+
92141
private HashSet<string> _managed = new HashSet<string>();
93142
}
94143
}

0 commit comments

Comments
 (0)