Skip to content

Commit

Permalink
Do not throw if process exits with 0
Browse files Browse the repository at this point in the history
- Ignore StandardError output
- Use it only as exception text on non-zero exit code

makes gitextensions#9056 less intercepting, fixes gitextensions#9462
  • Loading branch information
mstv committed Aug 12, 2021
1 parent d8ddc1f commit 945f370
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions GitCommands/Git/Executable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private sealed class ProcessWrapper : IProcess
private readonly ProcessOperation _logOperation;
private readonly bool _redirectInput;
private readonly bool _redirectOutput;
private readonly bool _throwOnErrorOutput;
private readonly bool _throwOnErrorExit;

private MemoryStream? _emptyStream;
private StreamReader? _emptyReader;
Expand All @@ -78,15 +78,15 @@ private sealed class ProcessWrapper : IProcess
bool redirectOutput,
Encoding? outputEncoding,
bool useShellExecute,
bool throwOnErrorOutput)
bool throwOnErrorExit)
{
Debug.Assert(redirectOutput == (outputEncoding is not null), "redirectOutput == (outputEncoding is not null)");
_redirectInput = redirectInput;
_redirectOutput = redirectOutput;
_throwOnErrorOutput = throwOnErrorOutput;
_throwOnErrorExit = throwOnErrorExit;

Encoding errorEncoding = outputEncoding;
if (throwOnErrorOutput)
if (throwOnErrorExit)
{
errorEncoding ??= Encoding.Default;
}
Expand All @@ -102,7 +102,7 @@ private sealed class ProcessWrapper : IProcess
CreateNoWindow = !createWindow,
RedirectStandardInput = redirectInput,
RedirectStandardOutput = redirectOutput,
RedirectStandardError = redirectOutput || throwOnErrorOutput,
RedirectStandardError = redirectOutput || throwOnErrorExit,
StandardOutputEncoding = outputEncoding,
StandardErrorEncoding = errorEncoding,
FileName = fileName,
Expand Down Expand Up @@ -150,20 +150,17 @@ private void OnProcessExit(object sender, EventArgs eventArgs)
int exitCode = _process.ExitCode;
_logOperation.LogProcessEnd(exitCode);

if (_throwOnErrorOutput)
if (_throwOnErrorExit && exitCode != 0)
{
string errorOutput = _process.StandardError.ReadToEnd().Trim();
if (exitCode != 0 || errorOutput.Length > 0)
{
ExternalOperationException ex
= new(command: _process.StartInfo.FileName,
_process.StartInfo.Arguments,
_process.StartInfo.WorkingDirectory,
exitCode,
new Exception(errorOutput));
_logOperation.LogProcessEnd(ex);
_exitTaskCompletionSource.TrySetException(ex);
}
ExternalOperationException ex
= new(command: _process.StartInfo.FileName,
_process.StartInfo.Arguments,
_process.StartInfo.WorkingDirectory,
exitCode,
new Exception(errorOutput));
_logOperation.LogProcessEnd(ex);
_exitTaskCompletionSource.TrySetException(ex);
}

_exitTaskCompletionSource.TrySetResult(exitCode);
Expand Down Expand Up @@ -210,12 +207,12 @@ public StreamReader StandardError
{
get
{
if (!_redirectOutput && !_throwOnErrorOutput)
if (!_redirectOutput && !_throwOnErrorExit)
{
throw new InvalidOperationException("Process was not created with redirected output.");
}

if (!_throwOnErrorOutput)
if (!_throwOnErrorExit)
{
return _process.StandardError;
}
Expand Down

0 comments on commit 945f370

Please sign in to comment.