mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-21 04:04:59 +00:00
feature: add a button to scan repositories under default clone dir (#427)
This commit is contained in:
parent
aab14784fc
commit
0d676fa3fb
9 changed files with 162 additions and 3 deletions
94
src/ViewModels/ScanRepositories.cs
Normal file
94
src/ViewModels/ScanRepositories.cs
Normal file
|
@ -0,0 +1,94 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Threading;
|
||||
|
||||
namespace SourceGit.ViewModels
|
||||
{
|
||||
public class ScanRepositories : Popup
|
||||
{
|
||||
public string RootDir
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
} = string.Empty;
|
||||
|
||||
public ScanRepositories(string rootDir)
|
||||
{
|
||||
GetManagedRepositories(Preference.Instance.RepositoryNodes, _managed);
|
||||
|
||||
RootDir = rootDir;
|
||||
View = new Views.ScanRepositories() { DataContext = this };
|
||||
}
|
||||
|
||||
public override Task<bool> Sure()
|
||||
{
|
||||
ProgressDescription = $"Scan repositories under '{RootDir}' ...";
|
||||
|
||||
return Task.Run(() =>
|
||||
{
|
||||
// If it is too fast, the panel will dispear very quickly, the we'll have a bad experience.
|
||||
Task.Delay(500).Wait();
|
||||
|
||||
var founded = new List<string>();
|
||||
GetUnmanagedRepositories(new DirectoryInfo(RootDir), founded, new EnumerationOptions()
|
||||
{
|
||||
AttributesToSkip = FileAttributes.Hidden | FileAttributes.System,
|
||||
IgnoreInaccessible = true,
|
||||
});
|
||||
|
||||
Dispatcher.UIThread.Invoke(() =>
|
||||
{
|
||||
foreach (var f in founded)
|
||||
Preference.Instance.FindOrAddNodeByRepositoryPath(f, null, false);
|
||||
Welcome.Instance.Refresh();
|
||||
});
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private void GetManagedRepositories(List<RepositoryNode> group, HashSet<string> repos)
|
||||
{
|
||||
foreach (RepositoryNode node in group)
|
||||
{
|
||||
if (node.IsRepository)
|
||||
repos.Add(node.Id);
|
||||
else
|
||||
GetManagedRepositories(node.SubNodes, repos);
|
||||
}
|
||||
}
|
||||
|
||||
private void GetUnmanagedRepositories(DirectoryInfo dir, List<string> outs, EnumerationOptions opts, int depth = 0)
|
||||
{
|
||||
var subdirs = dir.GetDirectories("*", opts);
|
||||
foreach (var subdir in subdirs)
|
||||
{
|
||||
SetProgressDescription($"Scanning {subdir.FullName}...");
|
||||
|
||||
var normalizedSelf = subdir.FullName.Replace("\\", "/");
|
||||
if (_managed.Contains(normalizedSelf))
|
||||
continue;
|
||||
|
||||
var gitDir = Path.Combine(subdir.FullName, ".git");
|
||||
if (Directory.Exists(gitDir) || File.Exists(gitDir))
|
||||
{
|
||||
var test = new Commands.QueryRepositoryRootPath(subdir.FullName).ReadToEnd();
|
||||
if (test.IsSuccess && !string.IsNullOrEmpty(test.StdOut))
|
||||
{
|
||||
var normalized = test.StdOut.Trim().Replace("\\", "/");
|
||||
if (!_managed.Contains(normalizedSelf))
|
||||
outs.Add(normalized);
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (depth < 8)
|
||||
GetUnmanagedRepositories(subdir, outs, opts, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private HashSet<string> _managed = new HashSet<string>();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue