Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions src/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public enum EditorType
public string Args { get; set; } = string.Empty;
public bool RaiseError { get; set; } = true;
public bool TraitErrorAsOutput { get; set; } = false;
public bool Cancelable { get; } = false;

public Command(bool cancelable = false)
{
Cancelable = cancelable;
}

public bool Exec()
{
Expand Down Expand Up @@ -109,12 +115,22 @@ public bool Exec()
return false;
}

proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
_proc = proc;

int exitCode;
try
{
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();

int exitCode = proc.ExitCode;
proc.Close();
exitCode = proc.ExitCode;
proc.Close();
}
finally
{
_proc = null;
}

if (!isCancelled && exitCode != 0)
{
Expand Down Expand Up @@ -150,17 +166,37 @@ public ReadToEndResult ReadToEnd()
};
}

var rs = new ReadToEndResult()
_proc = proc;

try
{
StdOut = proc.StandardOutput.ReadToEnd(),
StdErr = proc.StandardError.ReadToEnd(),
};
var rs = new ReadToEndResult()
{
StdOut = proc.StandardOutput.ReadToEnd(),
StdErr = proc.StandardError.ReadToEnd(),
};

proc.WaitForExit();
rs.IsSuccess = proc.ExitCode == 0;
proc.Close();
proc.WaitForExit();
rs.IsSuccess = proc.ExitCode == 0;
proc.Close();

return rs;
return rs;
}
finally
{
_proc = null;
}
}

public void TryCancel()
{
if (!this.Cancelable)
throw new Exception("Command is not cancelable!");

if (_proc is null)
return;

Native.OS.TerminateSafely(_proc);
}

protected virtual void OnReadline(string line)
Expand Down Expand Up @@ -226,5 +262,7 @@ private ProcessStartInfo CreateGitStartInfo()

[GeneratedRegex(@"\d+%")]
private static partial Regex REG_PROGRESS();

private Process _proc = null;
}
}
15 changes: 15 additions & 0 deletions src/Native/Linux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

using Avalonia;
Expand All @@ -11,6 +12,14 @@ namespace SourceGit.Native
[SupportedOSPlatform("linux")]
internal class Linux : OS.IBackend
{
private enum SIGNAL : int
{
TERM = 15
}

[DllImport("c")]
private static extern int kill(int pid, int sig);

public void SetupApp(AppBuilder builder)
{
builder.With(new X11PlatformOptions() { EnableIme = true });
Expand Down Expand Up @@ -97,6 +106,12 @@ public void OpenWithDefaultEditor(string file)
}
}

public void TerminateSafely(Process process)
{
if (kill(process.Id, (int)SIGNAL.TERM) == 0)
process.WaitForExit();
}

private string FindExecutable(string filename)
{
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
Expand Down
15 changes: 15 additions & 0 deletions src/Native/MacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

using Avalonia;
Expand All @@ -11,6 +12,14 @@ namespace SourceGit.Native
[SupportedOSPlatform("macOS")]
internal class MacOS : OS.IBackend
{
private enum SIGNAL : int
{
TERM = 15
}

[DllImport("System")]
private static extern int kill(int pid, int sig);

public void SetupApp(AppBuilder builder)
{
builder.With(new MacOSPlatformOptions()
Expand Down Expand Up @@ -88,5 +97,11 @@ public void OpenWithDefaultEditor(string file)
{
Process.Start("open", $"\"{file}\"");
}

public void TerminateSafely(Process process)
{
if (kill(process.Id, (int)SIGNAL.TERM) == 0)
process.WaitForExit();
}
}
}
7 changes: 7 additions & 0 deletions src/Native/OS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface IBackend
void OpenInFileManager(string path, bool select);
void OpenBrowser(string url);
void OpenWithDefaultEditor(string file);

void TerminateSafely(Process process);
}

public static string DataDir
Expand Down Expand Up @@ -168,6 +170,11 @@ public static void OpenWithDefaultEditor(string file)
_backend.OpenWithDefaultEditor(file);
}

public static void TerminateSafely(Process process)
{
_backend.TerminateSafely(process);
}

private static void UpdateGitVersion()
{
if (string.IsNullOrEmpty(_gitExecutable) || !File.Exists(_gitExecutable))
Expand Down
27 changes: 27 additions & 0 deletions src/Native/Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ internal struct MARGINS
public int cyBottomHeight;
}

private enum CTRL_EVENT : int
{
CTRL_C = 0
}

[DllImport("ntdll.dll")]
private static extern int RtlGetVersion(ref RTL_OSVERSIONINFOEX lpVersionInformation);

Expand All @@ -53,6 +58,12 @@ internal struct MARGINS
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);

[DllImport("kernel32.dll")]
public static extern bool SetConsoleCtrlHandler(IntPtr handlerRoutine, bool add);

[DllImport("kernel32.dll")]
private static extern bool GenerateConsoleCtrlEvent(int dwCtrlEvent, int dwProcessGroupId);

public void SetupApp(AppBuilder builder)
{
// Fix drop shadow issue on Windows 10
Expand Down Expand Up @@ -212,6 +223,22 @@ public void OpenWithDefaultEditor(string file)
Process.Start(start);
}

public void TerminateSafely(Process process)
{
if (SetConsoleCtrlHandler(IntPtr.Zero, true))
{
try
{
if (GenerateConsoleCtrlEvent((int)CTRL_EVENT.CTRL_C, process.Id))
process.WaitForExit();
}
finally
{
SetConsoleCtrlHandler(IntPtr.Zero, false);
}
}
}

private void FixWindowFrameOnWin10(Window w)
{
var platformHandle = w.TryGetPlatformHandle();
Expand Down
Loading