forked from dotnet/crank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGit.cs
54 lines (44 loc) · 2.57 KB
/
Git.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Crank.Agent
{
public static class Git
{
private static readonly TimeSpan CloneTimeout = TimeSpan.FromMinutes(2);
private static readonly TimeSpan CheckoutTimeout = TimeSpan.FromSeconds(30);
private static readonly TimeSpan SubModuleTimeout = TimeSpan.FromSeconds(30);
public static async Task<string> CloneAsync(string path, string repository, bool shallow = true, string branch = null, CancellationToken cancellationToken = default)
{
Log.WriteLine($"Cloning {repository} with branch '{branch}'");
var branchParam = string.IsNullOrEmpty(branch) ? string.Empty : $"-b {branch}";
var depth = shallow ? "--depth 1" : "";
var result = await RunGitCommandAsync(path, $"clone -c core.longpaths=true {depth} {branchParam} {repository}", CloneTimeout, retries: 5, cancellationToken: cancellationToken);
var match = Regex.Match(result.StandardError, @"'(.*)'");
if (match.Success && match.Groups.Count == 2)
{
return match.Groups[1].Value;
}
else
{
throw new InvalidOperationException("Could not parse directory from 'git clone' standard error");
}
}
public static Task CheckoutAsync(string path, string branchOrCommit, CancellationToken cancellationToken = default)
{
return RunGitCommandAsync(path, $"checkout {branchOrCommit}", CheckoutTimeout, retries: 5, cancellationToken: cancellationToken);
}
public static Task InitSubModulesAsync(string path, CancellationToken cancellationToken = default)
{
return RunGitCommandAsync(path, $"submodule update --init", SubModuleTimeout, retries: 5, cancellationToken: cancellationToken);
}
private static Task<ProcessResult> RunGitCommandAsync(string path, string command, TimeSpan? timeout, bool throwOnError = true, int retries = 0, CancellationToken cancellationToken = default)
{
return ProcessUtil.RetryOnExceptionAsync(retries, () => ProcessUtil.RunAsync("git", command, timeout, workingDirectory: path, throwOnError: throwOnError, captureOutput: true, captureError: true, cancellationToken: cancellationToken));
}
}
}