public static async Task<string> GitResult(string workingDir, string commands) { var output = string.Empty; var error = string.Empty; var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c cd /D \"{workingDir}\" && \"{FileHelper.GetMSysGit()}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; proc.Start(); while (!proc.StandardOutput.EndOfStream) { output += await proc.StandardOutput.ReadLineAsync() + ","; } while (!proc.StandardError.EndOfStream) { error += await proc.StandardError.ReadLineAsync(); } 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 async Task<bool> StartProcessGit(string commands, bool showAlert = true) { var solutionDir = await FileHelper.GetSolutionDir(); if (string.IsNullOrEmpty(solutionDir)) { return false; } var output = string.Empty; var error = string.Empty; var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c cd /D \"{solutionDir}\" && \"{FileHelper.GetMSysGit()}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; proc.Start(); while (!proc.StandardOutput.EndOfStream) { output = await proc.StandardOutput.ReadLineAsync(); } while (!proc.StandardError.EndOfStream) { error += await proc.StandardError.ReadLineAsync(); } if (!string.IsNullOrEmpty(output)) { return true; } if (!string.IsNullOrEmpty(error) && showAlert) { await VS.MessageBox.ShowErrorAsync("TGit error", error); } return false; }
/// <summary> /// Get the path to the Git process /// </summary> /// <remarks>Cached for 1h</remarks> /// <returns></returns> public string GetGit() { if (_cache.Contains(CacheKeyEnum.Git.ToString())) { return(_cache.Get(CacheKeyEnum.Git.ToString()).ToString()); } var git = FileHelper.GetMSysGit(); if (!string.IsNullOrEmpty(git)) { _cache.Set(CacheKeyEnum.Git.ToString(), git, DateTimeOffset.Now.AddHours(1)); } return(git); }
public static void GetGit() => Git = FileHelper.GetMSysGit();
public static string FormatCliCommand(string gitCommand, bool appendNextLine = true) { var git = FileHelper.GetMSysGit(); return($"echo ^> {Path.GetFileNameWithoutExtension(git)} {gitCommand} && \"{git}\" {gitCommand}{(appendNextLine ? " && " : string.Empty)}"); }