2022-05-20 01:00:25 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
{
|
|
|
|
public partial class Command
|
|
|
|
{
|
|
|
|
public class CancelToken
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
public bool Requested { get; set; } = false;
|
|
|
|
}
|
2022-05-20 01:00:25 -07:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public class ReadToEndResult
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
public bool IsSuccess { get; set; }
|
2024-02-05 23:08:37 -08:00
|
|
|
public string StdOut { get; set; }
|
|
|
|
public string StdErr { get; set; }
|
2022-05-20 01:00:25 -07:00
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
public string Context { get; set; } = string.Empty;
|
|
|
|
public CancelToken Cancel { get; set; } = null;
|
|
|
|
public string WorkingDirectory { get; set; } = null;
|
|
|
|
public string Args { get; set; } = string.Empty;
|
|
|
|
public bool RaiseError { get; set; } = true;
|
2022-05-20 01:00:25 -07:00
|
|
|
public bool TraitErrorAsOutput { get; set; } = false;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool Exec()
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
var start = new ProcessStartInfo();
|
2024-02-20 19:29:28 -08:00
|
|
|
start.FileName = Native.OS.GitInstallPath;
|
2022-05-20 01:00:25 -07:00
|
|
|
start.Arguments = "--no-pager -c core.quotepath=off " + Args;
|
|
|
|
start.UseShellExecute = false;
|
|
|
|
start.CreateNoWindow = true;
|
|
|
|
start.RedirectStandardOutput = true;
|
|
|
|
start.RedirectStandardError = true;
|
|
|
|
start.StandardOutputEncoding = Encoding.UTF8;
|
|
|
|
start.StandardErrorEncoding = Encoding.UTF8;
|
|
|
|
|
2024-03-19 00:43:47 -07:00
|
|
|
// Force using en_US.UTF-8 locale to avoid GCM crash
|
|
|
|
if (OperatingSystem.IsLinux())
|
|
|
|
{
|
|
|
|
start.Environment.Add("LANG", "en_US.UTF-8");
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
if (!string.IsNullOrEmpty(WorkingDirectory)) start.WorkingDirectory = WorkingDirectory;
|
2022-05-20 01:00:25 -07:00
|
|
|
|
|
|
|
var errs = new List<string>();
|
|
|
|
var proc = new Process() { StartInfo = start };
|
|
|
|
var isCancelled = false;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
proc.OutputDataReceived += (_, e) =>
|
|
|
|
{
|
|
|
|
if (Cancel != null && Cancel.Requested)
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
isCancelled = true;
|
|
|
|
proc.CancelErrorRead();
|
|
|
|
proc.CancelOutputRead();
|
2024-02-05 23:08:37 -08:00
|
|
|
if (!proc.HasExited) proc.Kill(true);
|
2022-05-20 01:00:25 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
if (e.Data != null) OnReadline(e.Data);
|
2022-05-20 01:00:25 -07:00
|
|
|
};
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
proc.ErrorDataReceived += (_, e) =>
|
|
|
|
{
|
|
|
|
if (Cancel != null && Cancel.Requested)
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
isCancelled = true;
|
|
|
|
proc.CancelErrorRead();
|
|
|
|
proc.CancelOutputRead();
|
2024-02-05 23:08:37 -08:00
|
|
|
if (!proc.HasExited) proc.Kill(true);
|
2022-05-20 01:00:25 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(e.Data)) return;
|
|
|
|
if (TraitErrorAsOutput) OnReadline(e.Data);
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
// Ignore progress messages
|
2023-10-11 21:02:41 -07:00
|
|
|
if (e.Data.StartsWith("remote: Enumerating objects:", StringComparison.Ordinal)) return;
|
2022-05-20 01:00:25 -07:00
|
|
|
if (e.Data.StartsWith("remote: Counting objects:", StringComparison.Ordinal)) return;
|
2023-10-11 21:02:41 -07:00
|
|
|
if (e.Data.StartsWith("remote: Compressing objects:", StringComparison.Ordinal)) return;
|
|
|
|
if (e.Data.StartsWith("Filtering content:", StringComparison.Ordinal)) return;
|
2024-03-16 02:09:27 -07:00
|
|
|
if (_progressRegex().IsMatch(e.Data)) return;
|
2022-05-20 01:00:25 -07:00
|
|
|
errs.Add(e.Data);
|
|
|
|
};
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
try
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
proc.Start();
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
if (RaiseError)
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
App.RaiseException(Context, e.Message);
|
|
|
|
});
|
|
|
|
}
|
2022-05-20 01:00:25 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc.BeginOutputReadLine();
|
|
|
|
proc.BeginErrorReadLine();
|
|
|
|
proc.WaitForExit();
|
|
|
|
|
|
|
|
int exitCode = proc.ExitCode;
|
|
|
|
proc.Close();
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!isCancelled && exitCode != 0 && errs.Count > 0)
|
|
|
|
{
|
|
|
|
if (RaiseError)
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
App.RaiseException(Context, string.Join("\n", errs));
|
|
|
|
});
|
|
|
|
}
|
2022-05-20 01:00:25 -07:00
|
|
|
return false;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public ReadToEndResult ReadToEnd()
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
var start = new ProcessStartInfo();
|
2024-02-20 19:29:28 -08:00
|
|
|
start.FileName = Native.OS.GitInstallPath;
|
2022-05-20 01:00:25 -07:00
|
|
|
start.Arguments = "--no-pager -c core.quotepath=off " + Args;
|
|
|
|
start.UseShellExecute = false;
|
|
|
|
start.CreateNoWindow = true;
|
|
|
|
start.RedirectStandardOutput = true;
|
|
|
|
start.RedirectStandardError = true;
|
|
|
|
start.StandardOutputEncoding = Encoding.UTF8;
|
|
|
|
start.StandardErrorEncoding = Encoding.UTF8;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
if (!string.IsNullOrEmpty(WorkingDirectory)) start.WorkingDirectory = WorkingDirectory;
|
2022-05-20 01:00:25 -07:00
|
|
|
|
|
|
|
var proc = new Process() { StartInfo = start };
|
2024-03-17 18:37:06 -07:00
|
|
|
try
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
proc.Start();
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
return new ReadToEndResult()
|
|
|
|
{
|
2022-05-20 01:00:25 -07:00
|
|
|
IsSuccess = false,
|
2024-02-05 23:08:37 -08:00
|
|
|
StdOut = string.Empty,
|
|
|
|
StdErr = e.Message,
|
2022-05-20 01:00:25 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
var rs = new ReadToEndResult()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
StdOut = proc.StandardOutput.ReadToEnd(),
|
|
|
|
StdErr = proc.StandardError.ReadToEnd(),
|
|
|
|
};
|
2022-05-20 01:00:25 -07:00
|
|
|
|
|
|
|
proc.WaitForExit();
|
|
|
|
rs.IsSuccess = proc.ExitCode == 0;
|
|
|
|
proc.Close();
|
|
|
|
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
protected virtual void OnReadline(string line) { }
|
2022-10-19 00:20:58 -07:00
|
|
|
|
2024-03-16 02:09:27 -07:00
|
|
|
[GeneratedRegex(@"\d+%")]
|
|
|
|
private static partial Regex _progressRegex();
|
2022-05-20 01:00:25 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|