Skip to content

Commit 64874bb

Browse files
committed
error handling of git cmd execution improved
1 parent f2ab674 commit 64874bb

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

GitWorkTree/Helpers/GitHelper.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using EnvDTE;
2+
using Microsoft.VisualStudio.Threading;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
7+
using System.Text;
68
using System.Text.RegularExpressions;
79
using System.Threading.Tasks;
810
using Path = System.IO.Path;
@@ -27,7 +29,6 @@ public static string ToGitCommandExecutableFormat(this string branchName) => Reg
2729
private static async Task<bool> ExecuteAsync(GitCommandArgs gitCommandArgs, Action<string> outputHandler = null)
2830
{
2931
LoggingHelper outputWindow = LoggingHelper.Instance;
30-
bool isError = false;
3132

3233
if (gitCommandArgs == null || string.IsNullOrEmpty(gitCommandArgs.WorkingDirectory))
3334
{
@@ -57,50 +58,52 @@ private static async Task<bool> ExecuteAsync(GitCommandArgs gitCommandArgs, Acti
5758
};
5859

5960
using var process = new System.Diagnostics.Process { StartInfo = startInfo };
60-
var outputDataReceivedTask = new TaskCompletionSource<bool>();
61-
var errorDataReceivedTask = new TaskCompletionSource<bool>();
61+
var outputBuilder = new StringBuilder();
62+
var errorBuilder = new StringBuilder();
6263

63-
DataReceivedEventHandler gitOutputHandler = (sender, e) =>
64+
process.OutputDataReceived += (s, e) =>
6465
{
6566
if (e.Data != null)
6667
{
67-
isError = isError || Regex.IsMatch(e.Data, @"\b(error|fatal|failed|rejected|conflict)\b", RegexOptions.IgnoreCase);
68-
if (!isError) outputHandler?.Invoke(e.Data);
69-
else outputWindow?.WriteToOutputWindowAsync(e.Data);
68+
outputBuilder.AppendLine(e.Data);
69+
outputHandler?.Invoke(e.Data);
7070
}
7171
};
7272

73-
process.OutputDataReceived += gitOutputHandler;
74-
process.ErrorDataReceived += gitOutputHandler;
75-
76-
process.EnableRaisingEvents = true;
77-
78-
process.Exited += (sender, e) =>
73+
process.ErrorDataReceived += (s, e) =>
7974
{
80-
outputDataReceivedTask.TrySetResult(true);
81-
errorDataReceivedTask.TrySetResult(true);
75+
if (e.Data != null)
76+
{
77+
errorBuilder.AppendLine(e.Data);
78+
outputWindow?.WriteToOutputWindowAsync(e.Data);
79+
}
8280
};
8381

8482
process.Start();
8583
process.BeginOutputReadLine();
8684
process.BeginErrorReadLine();
8785

88-
await Task.WhenAll(outputDataReceivedTask.Task, errorDataReceivedTask.Task);
86+
await process.WaitForExitAsync();
87+
88+
bool isError = process.ExitCode != 0;
89+
90+
if (isError && errorBuilder.Length == 0)
91+
outputWindow?.WriteToOutputWindowAsync("Git failed but no error output captured.", true);
92+
93+
var result = isError ? "failed" : "completed";
94+
outputWindow.WriteToOutputWindowAsync($"Command execution - {result}");
95+
8996
return !isError;
9097
}
9198
catch (Exception ex)
9299
{
93100
outputWindow?.WriteToOutputWindowAsync($"An error occurred during Git command execution: {ex.Message}", true);
94101
return false;
95102
}
96-
finally
97-
{
98-
var result = isError ? "failed" : "completed";
99-
outputWindow.WriteToOutputWindowAsync($"Command execution - {result}");
100-
}
101103
}
102104

103105

106+
104107
public static async Task<List<string>> GetWorkTreePathsAsync(string repositoryPath)
105108
{
106109
List<string> workTreePaths = new List<string>();

0 commit comments

Comments
 (0)