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
51 changes: 7 additions & 44 deletions src/Stack/Git/GitClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics;
using System.Text;
using Octopus.Shellfish;
using Spectre.Console;
using Stack.Infrastructure;

Expand Down Expand Up @@ -334,50 +332,15 @@ private string ExecuteGitCommandAndReturnOutput(
bool captureStandardError = false,
Func<int, Exception?>? exceptionHandler = null)
{
if (settings.Verbose)
logger.Debug($"git {command}");

var infoBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();

var result = ShellExecutor.ExecuteCommand(
return ProcessHelpers.ExecuteProcessAndReturnOutput(
"git",
command,
settings.WorkingDirectory ?? ".",
(_) => { },
(info) => infoBuilder.AppendLine(info),
(error) => errorBuilder.AppendLine(error));

if (result != 0)
{
logger.Error(Markup.Escape(errorBuilder.ToString()));
if (exceptionHandler != null)
{
var exception = exceptionHandler(result);
if (exception != null)
{
throw exception;
}
}
else
{
throw new Exception($"Failed to execute git command: {command}. Exit code: {result}. Error: {errorBuilder}.");
}
}

if (settings.Verbose && infoBuilder.Length > 0)
{
logger.Debug(Markup.Escape(infoBuilder.ToString()));
}

var output = infoBuilder.ToString();

if (captureStandardError)
{
output += $"{Environment.NewLine}{errorBuilder}";
}

return output;
settings.WorkingDirectory,
logger,
settings.Verbose,
captureStandardError,
exceptionHandler
);
}

private void ExecuteGitCommand(
Expand Down
61 changes: 8 additions & 53 deletions src/Stack/Git/GitHubClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Octopus.Shellfish;
using Spectre.Console;
using Stack.Infrastructure;

Expand Down Expand Up @@ -114,67 +113,23 @@ public void OpenPullRequest(GitHubPullRequest pullRequest)

private string ExecuteGitHubCommandAndReturnOutput(string command)
{
if (settings.Verbose)
logger.Debug($"gh {command}");

var infoBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();

var result = ShellExecutor.ExecuteCommand(
return ProcessHelpers.ExecuteProcessAndReturnOutput(
"gh",
command,
settings.WorkingDirectory ?? ".",
(_) => { },
(info) => infoBuilder.AppendLine(info),
(error) => errorBuilder.AppendLine(error));

if (result != 0)
{
logger.Error(Markup.Escape(errorBuilder.ToString()));
throw new Exception("Failed to execute gh command.");
}

if (settings.Verbose && infoBuilder.Length > 0)
{
logger.Debug(Markup.Escape(infoBuilder.ToString()));
}

return infoBuilder.ToString();
settings.WorkingDirectory,
logger,
settings.Verbose,
false,
null
);
}

private void ExecuteGitHubCommand(string command)
{
if (settings.Verbose)
logger.Debug($"gh {command}");

ExecuteGitHubCommandInternal(command);
}

private void ExecuteGitHubCommandInternal(string command)
{
var infoBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();

var result = ShellExecutor.ExecuteCommand(
"gh",
command,
settings.WorkingDirectory ?? ".",
(_) => { },
(info) => infoBuilder.AppendLine(info),
(error) => errorBuilder.AppendLine(error));

if (result != 0)
{
logger.Error($"{errorBuilder}");
throw new Exception("Failed to execute gh command.");
}
else
{
if (infoBuilder.Length > 0)
{
logger.Debug(Markup.Escape(infoBuilder.ToString()));
}
}
ExecuteGitHubCommandAndReturnOutput(command);
}

private string Sanitize(string value)
Expand Down
86 changes: 86 additions & 0 deletions src/Stack/Git/ProcessHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Diagnostics;
using System.Text;
using Spectre.Console;
using Stack.Infrastructure;

namespace Stack.Git;

public static class ProcessHelpers
{
public static string ExecuteProcessAndReturnOutput(
string fileName,
string command,
string? workingDirectory,
ILogger logger,
bool verbose = false,
bool captureStandardError = false,
Func<int, Exception?>? exceptionHandler = null)
{
if (verbose)
logger.Debug($"{fileName} {command}");

var infoBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();

var psi = new ProcessStartInfo
{
FileName = fileName,
Arguments = command,
WorkingDirectory = workingDirectory ?? ".",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

using var process = Process.Start(psi);
if (process is null) return string.Empty;

process.OutputDataReceived += (_, e) =>
{
if (e.Data is null) return;
infoBuilder.AppendLine(e.Data);
};
process.ErrorDataReceived += (_, e) =>
{
if (e.Data is null) return;
errorBuilder.AppendLine(e.Data);
};
process.BeginErrorReadLine();
process.BeginOutputReadLine();

process.WaitForExit();
int result = process.ExitCode;

if (result != 0)
{
logger.Error(Markup.Escape(errorBuilder.ToString()));
if (exceptionHandler != null)
{
var exception = exceptionHandler(result);
if (exception != null)
{
throw exception;
}
}
else
{
throw new Exception($"Failed to execute command: {fileName} {command}. Exit code: {result}. Error: {errorBuilder}.");
}
}

if (verbose && infoBuilder.Length > 0)
{
logger.Debug(Markup.Escape(infoBuilder.ToString()));
}

var output = infoBuilder.ToString();

if (captureStandardError)
{
output += $"{Environment.NewLine}{errorBuilder}";
}

return output;
}
}
1 change: 0 additions & 1 deletion src/Stack/Stack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="3.0.0-beta.96" />
<PackageReference Include="morelinq" Version="4.4.0" />
<PackageReference Include="Octopus.Shellfish" Version="0.2.2130" />
<PackageReference Include="Spectre.Console.Cli" Version="0.50.0" />
</ItemGroup>

Expand Down