-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibGit2Downloader.cs
More file actions
322 lines (279 loc) · 12.5 KB
/
LibGit2Downloader.cs
File metadata and controls
322 lines (279 loc) · 12.5 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using LibGit2Sharp;
using System.Net;
namespace RS.GitSubDirectoryDownloader
{
/// <summary>
/// LibGit2Sharp下载器 - 使用Git原生协议克隆仓库
/// LibGit2Sharp特点:
/// - libgit原生实现,性能优秀
/// - 支持完整Git操作(克隆、检出、历史等)
/// - 支持SSH和HTTPS协议
/// - 支持稀疏检出(需额外配置)
///
/// 注意:此实现会克隆整个仓库,然后复制指定目录
/// </summary>
public class LibGit2Downloader : IDirectoryDownloader
{
private readonly string? _username;
private readonly string? _password;
private readonly ProxyConfig _proxyConfig;
private readonly AccelerationConfig _accelerationConfig;
private Action<string>? _logCallback;
private static bool _sslConfigured = false;
private static readonly object _sslLock = new();
public LibGit2Downloader(string? username = null, string? password = null, ProxyConfig? proxyConfig = null, AccelerationConfig? accelerationConfig = null, Action<string>? logCallback = null)
{
_username = username;
_password = password;
_proxyConfig = proxyConfig ?? new ProxyConfig();
_accelerationConfig = accelerationConfig ?? new AccelerationConfig();
_logCallback = logCallback;
// 全局配置SSL(只执行一次)
ConfigureSsl();
if (_proxyConfig.IsTraditionalProxy)
{
Log($"LibGit2Sharp代理已配置: {_proxyConfig.ProxyAddress}");
}
else if (_accelerationConfig.IsEnabled)
{
Log($"LibGit2Sharp使用GH-Proxy: {_accelerationConfig.Prefix}");
}
if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
{
Log($"LibGit2Sharp认证已配置: {(username == "x-token-auth" ? "Token" : "用户名密码")}");
}
}
/// <summary>
/// 配置SSL验证 - 解决证书验证错误
/// </summary>
private static void ConfigureSsl()
{
lock (_sslLock)
{
if (_sslConfigured) return;
try
{
// 设置环境变量
Environment.SetEnvironmentVariable("GIT_SSL_NO_VERIFY", "1", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("LIBGIT2_SSL_CERTIFICATE_CHECK", "0", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("GIT_SSL_CAINFO", "", EnvironmentVariableTarget.Process);
// 尝试配置全局设置
try
{
GlobalSettings.SetConfigSearchPaths(ConfigurationLevel.Global, Path.GetTempPath());
}
catch { }
_sslConfigured = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"SSL配置失败: {ex.Message}");
}
}
}
private void Log(string message)
{
_logCallback?.Invoke(message);
}
/// <summary>
/// 下载目录 - 通过克隆仓库后复制指定目录
/// </summary>
public async Task DownloadDirectoryAsync(string owner, string repo, string remotePath, string localPath, string branch, List<string>? fileExtensions, IProgress<DownloadProgress>? progress, CancellationToken cancellationToken)
{
await Task.Run(() =>
{
var repoUrl = BuildRepoUrl(owner, repo);
var filterInfo = fileExtensions?.Count > 0 ? $" | 过滤后缀: {string.Join(", ", fileExtensions)}" : "";
Log($"LibGit2Sharp下载 - 仓库: {repoUrl}, 分支: {branch}, 目录: {remotePath}{filterInfo}");
var tempDir = Path.Combine(Path.GetTempPath(), $"GitDownload_{Guid.NewGuid():N}");
Directory.CreateDirectory(tempDir);
Log($"临时目录: {tempDir}");
try
{
Log("正在克隆仓库...");
var cloneOptions = CreateCloneOptions();
var clonedRepoPath = Repository.Clone(repoUrl, tempDir, cloneOptions);
Log($"克隆完成: {clonedRepoPath}");
// 切换到指定分支或commit
using var repository = new Repository(clonedRepoPath);
// 检查是否为commit SHA(7位或40位十六进制字符串)
bool isCommitSha = IsCommitSha(branch);
if (isCommitSha)
{
// 直接检出到指定commit
try
{
var commit = repository.Lookup<Commit>(branch);
if (commit != null)
{
Commands.Checkout(repository, commit);
Log($"已切换到Commit: {branch}");
}
else
{
Log($"警告: 未找到Commit {branch},使用默认分支");
}
}
catch (Exception ex)
{
Log($"警告: 切换到Commit {branch} 失败: {ex.Message},使用默认分支");
}
}
else
{
// 查找分支
var branchObj = repository.Branches[$"origin/{branch}"];
if (branchObj != null)
{
Commands.Checkout(repository, branchObj);
Log($"已切换到分支: {branch}");
}
else
{
Log($"警告: 未找到分支 {branch},使用默认分支");
}
}
var sourceDir = string.IsNullOrEmpty(remotePath)
? tempDir
: Path.Combine(tempDir, remotePath.TrimStart('/').Replace('/', Path.DirectorySeparatorChar));
if (!Directory.Exists(sourceDir))
{
Log($"错误: 在仓库中未找到指定目录: {remotePath}");
throw new DirectoryNotFoundException($"在仓库中未找到指定目录: {remotePath}");
}
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
CopyDirectory(sourceDir, localPath, fileExtensions ?? new List<string>(), progress, cancellationToken);
Log("下载完成!");
}
finally
{
try
{
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, true);
Log("已清理临时目录");
}
}
catch (Exception ex)
{
Log($"清理临时目录失败: {ex.Message}");
}
}
}, cancellationToken);
}
/// <summary>
/// 获取默认分支 - 需要额外API调用,这里返回默认值
/// </summary>
public Task<string> GetDefaultBranchAsync(string owner, string repo)
{
// LibGit2Sharp获取默认分支需要先克隆,这里返回常见默认值
return Task.FromResult("main");
}
/// <summary>
/// 构建仓库URL,支持GH-Proxy加速
/// </summary>
private string BuildRepoUrl(string owner, string repo)
{
var originalUrl = $"https://github.com/{owner}/{repo}.git";
if (_accelerationConfig.IsEnabled && !string.IsNullOrWhiteSpace(_accelerationConfig.Prefix))
{
// 使用GH-Proxy加速克隆
// 转换为: https://cdn.gh-proxy.org/https://github.com/user/repo.git
var acceleratedUrl = $"{_accelerationConfig.Prefix.TrimEnd('/')}/{originalUrl}";
Log($"使用GH-Proxy加速克隆: {acceleratedUrl}");
return acceleratedUrl;
}
return originalUrl;
}
/// <summary>
/// 检查字符串是否为commit SHA格式
/// 支持短SHA(7位)和完整SHA(40位)
/// </summary>
private static bool IsCommitSha(string input)
{
if (string.IsNullOrWhiteSpace(input)) return false;
// 短SHA(7位)或完整SHA(40位)
if (input.Length == 7 || input.Length == 40)
{
return input.All(c => char.IsLetterOrDigit(c) &&
(char.IsDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')));
}
return false;
}
/// <summary>
/// 创建克隆选项
/// </summary>
private CloneOptions CreateCloneOptions()
{
var options = new CloneOptions();
// SSL证书验证 - 跳过所有证书检查
options.FetchOptions.CertificateCheck = (certificate, host, valid) =>
{
Log($"SSL证书检查: {host} - 已跳过验证");
return true;
};
// 配置认证
if (!string.IsNullOrWhiteSpace(_username) && !string.IsNullOrWhiteSpace(_password))
{
options.FetchOptions.CredentialsProvider = (_url, _user, _cred) =>
new UsernamePasswordCredentials
{
Username = _username,
Password = _password
};
}
// 配置传统代理
if (_proxyConfig.IsTraditionalProxy && !string.IsNullOrWhiteSpace(_proxyConfig.ProxyAddress))
{
options.FetchOptions.ProxyOptions.Url = _proxyConfig.ProxyAddress;
}
return options;
}
/// <summary>
/// 复制目录
/// </summary>
private void CopyDirectory(string sourceDir, string destDir, List<string> fileExtensions, IProgress<DownloadProgress>? progress, CancellationToken cancellationToken)
{
var allFiles = Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories);
var files = fileExtensions.Count > 0
? allFiles.Where(f => fileExtensions.Contains(Path.GetExtension(f).ToLowerInvariant())).ToArray()
: allFiles;
var totalFiles = files.Length;
var copiedFiles = 0;
progress?.Report(new DownloadProgress { TotalFiles = totalFiles, Status = $"找到 {totalFiles} 个文件待复制" });
Log($"找到 {totalFiles} 个文件待复制");
if (totalFiles == 0)
{
Log("未找到匹配的文件");
return;
}
foreach (var file in files)
{
cancellationToken.ThrowIfCancellationRequested();
var relativePath = file.Substring(sourceDir.Length).TrimStart(Path.DirectorySeparatorChar);
var destFile = Path.Combine(destDir, relativePath);
var destFileDir = Path.GetDirectoryName(destFile);
if (!string.IsNullOrEmpty(destFileDir) && !Directory.Exists(destFileDir))
{
Directory.CreateDirectory(destFileDir);
}
File.Copy(file, destFile, true);
copiedFiles++;
if (copiedFiles % 10 == 0 || copiedFiles == totalFiles)
{
progress?.Report(new DownloadProgress
{
DownloadedFiles = copiedFiles,
TotalFiles = totalFiles,
CurrentFile = Path.GetFileName(file)
});
}
}
Log($"文件复制完成: {copiedFiles} 个文件");
}
}
}