/// <summary> /// Execute a Git command and return the output /// </summary> /// <param name="commands">Git command to be executed</param> /// <returns>Git output</returns> public static string StartProcessGitResult(EnvHelper envHelper, string commands) { if (string.IsNullOrEmpty(envHelper.GetSolutionDir())) return string.Empty; var output = string.Empty; var error = string.Empty; var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c cd /D \"{envHelper.GetSolutionDir()}\" && \"{envHelper.GetGit()}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; proc.Start(); while (!proc.StandardOutput.EndOfStream) { output += proc.StandardOutput.ReadLine() + ";"; } while (!proc.StandardError.EndOfStream) { error += proc.StandardError.ReadLine(); } return string.IsNullOrEmpty(output) ? error : output.TrimEnd(';'); }
/// <summary> /// Execute a Git command and return true if output is non-empty /// </summary> /// <param name="commands">Git command to be executed</param> /// <param name="showAlert">Show an alert dialog when error output is non-empty</param> /// <returns>True if output is non-empty</returns> public static bool StartProcessGit(EnvHelper envHelper, string commands, bool showAlert = true) { if (string.IsNullOrEmpty(envHelper.GetSolutionDir())) return false; var output = string.Empty; var error = string.Empty; var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c cd /D \"{envHelper.GetSolutionDir()}\" && \"{envHelper.GetGit()}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; proc.Start(); while (!proc.StandardOutput.EndOfStream) { output = proc.StandardOutput.ReadLine(); } while (!proc.StandardError.EndOfStream) { error += proc.StandardError.ReadLine(); } if (!string.IsNullOrEmpty(output)) { return true; } if (!string.IsNullOrEmpty(error) && showAlert) { MessageBox.Show(error, "TGit error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return false; }