123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
using System.ComponentModel;
using System.Text;
using CliWrap;
using Microsoft.Extensions.Options;
namespace Forge.Git;
public sealed record GitResult(int ExitCode, byte[] StdOut, string StdErr, bool Truncated)
{
public bool Ok => ExitCode == 0 && !Truncated;
public string Text => Encoding.UTF8.GetString(StdOut);
}
/// <summary>
/// The only place that starts git processes. Every process gets an isolated environment,
/// a timeout, and is killed when the caller cancels.
/// </summary>
public sealed class GitCli
{
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
/// <summary>Clone and push can legitimately run long; they still must not run forever.</summary>
public static readonly TimeSpan TransportTimeout = TimeSpan.FromHours(2);
public const long DefaultMaxOutputBytes = 32 * 1024 * 1024;
private readonly string _gitPath;
private readonly string _workDir;
/// <summary>The data directory; git processes run here and scratch files go below it.</summary>
public string WorkDirectory => _workDir;
private readonly Dictionary<string, string?> _environment;
public GitCli(IOptions<ForgeOptions> options)
{
var o = options.Value;
_gitPath = o.GitPath;
_workDir = o.DataRoot;
_environment = new Dictionary<string, string?>(StringComparer.Ordinal)
{
// Isolation: no system config, no config from the real home directory, never prompt.
["GIT_CONFIG_NOSYSTEM"] = "1",
["HOME"] = o.DataRoot,
["XDG_CONFIG_HOME"] = Path.Combine(o.DataRoot, ".config"),
["GIT_TERMINAL_PROMPT"] = "0",
// Paths from URLs are plain paths, never pathspec magic such as ":(glob)".
["GIT_LITERAL_PATHSPECS"] = "1",
["GIT_OPTIONAL_LOCKS"] = "0",
["LC_ALL"] = "C",
// Nothing inherited from the server's own environment may redirect git elsewhere.
["GIT_DIR"] = null,
["GIT_WORK_TREE"] = null,
["GIT_INDEX_FILE"] = null,
["GIT_OBJECT_DIRECTORY"] = null,
["GIT_ALTERNATE_OBJECT_DIRECTORIES"] = null,
["GIT_NAMESPACE"] = null,
["GIT_CONFIG_GLOBAL"] = null,
["GIT_CONFIG_PARAMETERS"] = null,
["GIT_CONFIG_COUNT"] = null,
["GIT_PROTOCOL"] = null,
};
}
/// <summary>Runs git and buffers stdout. Pass <paramref name="repoPath"/> as null for commands outside a repo.</summary>
public async Task<GitResult> RunAsync(
string? repoPath,
IEnumerable<string> args,
Stream? stdin = null,
CancellationToken ct = default,
TimeSpan? timeout = null,
long maxOutputBytes = DefaultMaxOutputBytes,
bool throwOnError = true,
IReadOnlyDictionary<string, string?>? extraEnvironment = null)
{
var fullArgs = BuildArgs(repoPath, args);
var limit = timeout ?? DefaultTimeout;
var environment = MergeEnvironment(extraEnvironment);
using var timeoutCts = new CancellationTokenSource(limit);
using var capCts = new CancellationTokenSource();
using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCts.Token, capCts.Token);
await using var stdout = new CappedBuffer(maxOutputBytes, capCts);
var stderr = new StringBuilder();
var command = Cli.Wrap(_gitPath)
.WithArguments(fullArgs)
.WithWorkingDirectory(_workDir)
.WithEnvironmentVariables(environment)
.WithStandardInputPipe(stdin is null ? PipeSource.Null : PipeSource.FromStream(stdin))
.WithStandardOutputPipe(PipeTarget.ToStream(stdout))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stderr))
.WithValidation(CommandResultValidation.None);
try
{
var result = await command.ExecuteAsync(linked.Token);
if (result.ExitCode != 0 && throwOnError && !stdout.Truncated)
{
throw new GitException(
$"git {Describe(fullArgs)} exited with code {result.ExitCode}: {stderr.ToString().Trim()}",
result.ExitCode,
stderr.ToString());
}
return new GitResult(result.ExitCode, stdout.ToArray(), stderr.ToString(), stdout.Truncated);
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested && stdout.Truncated)
{
// Output went over the cap, so the process was killed on purpose.
return new GitResult(-1, stdout.ToArray(), stderr.ToString(), Truncated: true);
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested && timeoutCts.IsCancellationRequested)
{
throw new GitTimeoutException($"git {Describe(fullArgs)} did not finish within {limit.TotalSeconds:0} s and was killed.");
}
}
/// <summary>Runs git with caller-supplied pipes, for output that must not be buffered.</summary>
public async Task<(int ExitCode, string StdErr)> StreamAsync(
string? repoPath,
IEnumerable<string> args,
PipeSource stdin,
PipeTarget stdout,
IReadOnlyDictionary<string, string?>? extraEnvironment = null,
TimeSpan? timeout = null,
CancellationToken ct = default)
{
var fullArgs = BuildArgs(repoPath, args);
var limit = timeout ?? DefaultTimeout;
var environment = MergeEnvironment(extraEnvironment);
using var timeoutCts = new CancellationTokenSource(limit);
using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCts.Token);
var stderr = new StringBuilder();
var command = Cli.Wrap(_gitPath)
.WithArguments(fullArgs)
.WithWorkingDirectory(_workDir)
.WithEnvironmentVariables(environment)
.WithStandardInputPipe(stdin)
.WithStandardOutputPipe(stdout)
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stderr))
.WithValidation(CommandResultValidation.None);
try
{
var result = await command.ExecuteAsync(linked.Token);
return (result.ExitCode, stderr.ToString());
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested && timeoutCts.IsCancellationRequested)
{
throw new GitTimeoutException($"git {Describe(fullArgs)} did not finish within {limit.TotalMinutes:0} min and was killed.");
}
}
/// <summary>Returns the installed git version, or throws with a message suitable for the startup log.</summary>
public async Task<Version> GetVersionAsync(CancellationToken ct = default)
{
GitResult result;
try
{
result = await RunAsync(null, ["--version"], ct: ct, throwOnError: false);
}
catch (Exception ex) when (ex is Win32Exception or FileNotFoundException or InvalidOperationException)
{
throw new GitException($"Could not start git ('{_gitPath}'): {ex.Message}", -1, "");
}
var version = result.ExitCode == 0 ? GitParsers.ParseVersion(result.Text) : null;
return version ?? throw new GitException(
$"'{_gitPath} --version' did not report a git version (exit code {result.ExitCode}): {result.Text.Trim()} {result.StdErr.Trim()}",
result.ExitCode,
result.StdErr);
}
private Dictionary<string, string?> MergeEnvironment(IReadOnlyDictionary<string, string?>? extra)
{
if (extra is not { Count: > 0 })
{
return _environment;
}
var merged = new Dictionary<string, string?>(_environment, StringComparer.Ordinal);
foreach (var (key, value) in extra)
{
merged[key] = value;
}
return merged;
}
private static List<string> BuildArgs(string? repoPath, IEnumerable<string> args)
{
var list = new List<string>();
if (repoPath is not null)
{
list.Add("--git-dir=" + repoPath);
}
// Print non-ASCII paths as UTF-8 instead of octal escapes.
list.Add("-c");
list.Add("core.quotePath=false");
list.AddRange(args);
return list;
}
private static string Describe(List<string> args)
{
var text = string.Join(' ', args.Where(a => !a.StartsWith("--git-dir=", StringComparison.Ordinal)));
return text.Length > 200 ? text[..200] + "…" : text;
}
/// <summary>Collects process output up to a cap, then cancels the run instead of growing without bound.</summary>
private sealed class CappedBuffer(long cap, CancellationTokenSource onOverflow) : Stream
{
private readonly MemoryStream _buffer = new();
public bool Truncated { get; private set; }
public byte[] ToArray() => _buffer.ToArray();
public override void Write(byte[] buffer, int offset, int count) => Write(buffer.AsSpan(offset, count));
public override void Write(ReadOnlySpan<byte> buffer)
{
var room = cap - _buffer.Length;
if (room >= buffer.Length)
{
_buffer.Write(buffer);
return;
}
if (room > 0)
{
_buffer.Write(buffer[..(int)room]);
}
if (!Truncated)
{
Truncated = true;
onOverflow.Cancel();
}
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
Write(buffer.AsSpan(offset, count));
return Task.CompletedTask;
}
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
Write(buffer.Span);
return ValueTask.CompletedTask;
}
public override void Flush()
{
}
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => _buffer.Length;
public override long Position
{
get => _buffer.Length;
set => throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
if (disposing)
{
_buffer.Dispose();
}
base.Dispose(disposing);
}
}
}