diff --git a/src/RepoCleaner/Git/External/GitHandler.cs b/src/RepoCleaner/Git/External/GitHandler.cs index 009029a..ba896ec 100644 --- a/src/RepoCleaner/Git/External/GitHandler.cs +++ b/src/RepoCleaner/Git/External/GitHandler.cs @@ -6,6 +6,8 @@ namespace Develix.RepoCleaner.Git.External; internal class GitHandler : IGitHandler { + private const string FormattedGitBranchArguments = "branch --format \"%(HEAD)\t%(refname)\t%(refname:short)\t%(upstream:track)\t%(authordate:unix)\t%(authorname)\" --all"; + public IReadOnlyList DeleteBranches(string repositoryPath, IEnumerable branches) { var results = new List(); @@ -61,26 +63,22 @@ private static Result DeleteBranch(string repositoryPath, Branch branch) var arguments = $"branch -D {branch.FriendlyName}"; var processStartInfo = GetGitProcessStartInfo(repositoryPath, arguments); - var process = Process.Start(processStartInfo) ?? throw new InvalidOperationException("The git process could not start"); - process.WaitForExit(); + var (_, errorLines, exitCode) = RunProcess(processStartInfo); - return process.ExitCode == 0 + return exitCode == 0 ? Result.Ok() - : Result.Fail(process.StandardError.ReadToEnd()); + : Result.Fail(string.Join(Environment.NewLine, errorLines)); } private static Result> GetBranches(string path) { - var processStartInfo = GetGitProcessStartInfo( - path, - "branch --format \"%(HEAD)\t%(refname)\t%(refname:short)\t%(upstream:track)\t%(authordate:unix)\t%(authorname)\" --all"); + var processStartInfo = GetGitProcessStartInfo(path, FormattedGitBranchArguments); - var process = Process.Start(processStartInfo) ?? throw new UnreachableException("The git process could not start"); - process.WaitForExit(); + var (outputLines, errorLines, exitCode) = RunProcess(processStartInfo); - return process.ExitCode != 0 - ? Result.Fail>(process.StandardError.ReadToEnd()) - : Result.Ok(ParseBranchOutput(process.StandardOutput)); + return exitCode == 0 + ? Result.Ok(ParseBranchOutput(outputLines)) + : Result.Fail>(string.Join(Environment.NewLine, errorLines)); } private static ProcessStartInfo GetGitProcessStartInfo(string path, string arguments) @@ -92,13 +90,33 @@ private static ProcessStartInfo GetGitProcessStartInfo(string path, string argum UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, - WorkingDirectory = path + WorkingDirectory = path, }; } - private static IEnumerable ParseBranchOutput(StreamReader standardOutput) + private static (List outputLines, List errorLines, int exitCode) RunProcess(ProcessStartInfo processStartInfo) + { + using var process = Process.Start(processStartInfo) ?? throw new UnreachableException("The git process could not start"); + List standardOutput = []; + List standardError = []; + process.OutputDataReceived += (s, e) => Add(e.Data, standardOutput); + process.ErrorDataReceived += (s, e) => Add(e.Data, standardError); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + process.WaitForExit(); + + return (standardOutput, standardError, process.ExitCode); + + static void Add(string? data, List outputLines) + { + if (data is not null) + outputLines.Add(data); + } + } + + private static IEnumerable ParseBranchOutput(IEnumerable outputLines) { - while (standardOutput.ReadLine() is { } line) + foreach (var line in outputLines) { var split = line .Split("\t") diff --git a/src/RepoCleaner/RepoCleaner.csproj b/src/RepoCleaner/RepoCleaner.csproj index 0353d26..da13d31 100644 --- a/src/RepoCleaner/RepoCleaner.csproj +++ b/src/RepoCleaner/RepoCleaner.csproj @@ -9,7 +9,7 @@ Develix.RepoCleaner win-x64 en - 1.8.0 + 1.8.1