forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanRepositories.cs
More file actions
193 lines (162 loc) · 6.76 KB
/
Copy pathScanRepositories.cs
File metadata and controls
193 lines (162 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class ScanRepositories : Popup
{
public bool UseCustomDir
{
get => _useCustomDir;
set => SetProperty(ref _useCustomDir, value);
}
public string CustomDir
{
get => _customDir;
set => SetProperty(ref _customDir, value);
}
public List<Models.ScanDir> ScanDirs
{
get;
}
[Required(ErrorMessage = "Scan directory is required!!!")]
public Models.ScanDir Selected
{
get => _selected;
set => SetProperty(ref _selected, value, true);
}
public ScanRepositories()
{
ScanDirs = new List<Models.ScanDir>();
var workspace = Preferences.Instance.GetActiveWorkspace();
if (!string.IsNullOrEmpty(workspace.DefaultCloneDir))
ScanDirs.Add(new Models.ScanDir(workspace.DefaultCloneDir, "Workspace"));
if (!string.IsNullOrEmpty(Preferences.Instance.GitDefaultCloneDir))
ScanDirs.Add(new Models.ScanDir(Preferences.Instance.GitDefaultCloneDir, "Global"));
if (ScanDirs.Count > 0)
_selected = ScanDirs[0];
else
_useCustomDir = true;
GetManagedRepositories(Preferences.Instance.RepositoryNodes, _managed);
}
public override async Task<bool> Sure()
{
var selectedDir = _useCustomDir ? _customDir : _selected?.Path;
if (string.IsNullOrEmpty(selectedDir))
{
App.RaiseException(null, "Missing root directory to scan!");
return false;
}
if (!Directory.Exists(selectedDir))
return true;
ProgressDescription = $"Scan repositories under '{selectedDir}' ...";
var minDelay = Task.Delay(500);
var rootDir = new DirectoryInfo(selectedDir);
var found = new List<string>();
await GetUnmanagedRepositoriesAsync(rootDir, found, new EnumerationOptions()
{
AttributesToSkip = FileAttributes.Hidden | FileAttributes.System,
IgnoreInaccessible = true,
});
// Make sure this task takes at least 0.5s to avoid the popup panel disappearing too quickly.
await minDelay;
var normalizedRoot = rootDir.FullName.Replace('\\', '/').TrimEnd('/');
foreach (var f in found)
{
var parent = new DirectoryInfo(f).Parent!.FullName.Replace('\\', '/').TrimEnd('/');
if (parent.Equals(normalizedRoot, StringComparison.Ordinal))
{
Preferences.Instance.FindOrAddNodeByRepositoryPath(f, null, false, false);
}
else if (parent.StartsWith(normalizedRoot, StringComparison.Ordinal))
{
var relative = parent.Substring(normalizedRoot.Length).TrimStart('/');
var group = FindOrCreateGroupRecursive(Preferences.Instance.RepositoryNodes, relative);
Preferences.Instance.FindOrAddNodeByRepositoryPath(f, group, false, false);
}
}
Preferences.Instance.AutoRemoveInvalidNode();
Preferences.Instance.Save();
Welcome.Instance.Refresh();
return true;
}
private void GetManagedRepositories(List<RepositoryNode> group, HashSet<string> repos)
{
foreach (var node in group)
{
if (node.IsRepository)
repos.Add(node.Id);
else
GetManagedRepositories(node.SubNodes, repos);
}
}
private async Task GetUnmanagedRepositoriesAsync(DirectoryInfo dir, List<string> outs, EnumerationOptions opts, int depth = 0)
{
var subdirs = dir.GetDirectories("*", opts);
foreach (var subdir in subdirs)
{
if (subdir.Name.StartsWith(".", StringComparison.Ordinal) ||
subdir.Name.Equals("node_modules", StringComparison.Ordinal))
continue;
ProgressDescription = $"Scanning {subdir.FullName}...";
var normalizedSelf = subdir.FullName.Replace('\\', '/').TrimEnd('/');
if (_managed.Contains(normalizedSelf))
continue;
var gitDir = Path.Combine(subdir.FullName, ".git");
if (Directory.Exists(gitDir) || File.Exists(gitDir))
{
var test = await new Commands.QueryRepositoryRootPath(subdir.FullName).GetResultAsync().ConfigureAwait(false);
if (test.IsSuccess && !string.IsNullOrEmpty(test.StdOut))
{
var normalized = test.StdOut.Trim().Replace('\\', '/').TrimEnd('/');
if (!_managed.Contains(normalized))
outs.Add(normalized);
}
continue;
}
var isBare = await new Commands.IsBareRepository(subdir.FullName).GetResultAsync().ConfigureAwait(false);
if (isBare)
{
outs.Add(normalizedSelf);
continue;
}
if (depth < 5)
await GetUnmanagedRepositoriesAsync(subdir, outs, opts, depth + 1);
}
}
private RepositoryNode FindOrCreateGroupRecursive(List<RepositoryNode> collection, string path)
{
RepositoryNode node = null;
foreach (var name in path.Split('/'))
{
node = FindOrCreateGroup(collection, name);
collection = node.SubNodes;
}
return node;
}
private RepositoryNode FindOrCreateGroup(List<RepositoryNode> collection, string name)
{
foreach (var node in collection)
{
if (node.Name.Equals(name, StringComparison.Ordinal))
return node;
}
var added = new RepositoryNode()
{
Id = Guid.NewGuid().ToString(),
Name = name,
IsRepository = false,
IsExpanded = true,
};
collection.Add(added);
Preferences.Instance.SortNodes(collection);
return added;
}
private HashSet<string> _managed = new();
private bool _useCustomDir = false;
private string _customDir = string.Empty;
private Models.ScanDir _selected = null;
}
}