/// <summary> /// Returns the <see cref="ProcessStartInfo"/> for the git <see cref="Process"/> /// </summary> /// <param name="submodule">The <see cref="Submodule"/> for the given <paramref name="submoduleCommand"/>, /// use <c>null</c> for all submodules</param> /// <param name="submoduleCommand">The <see cref="SubmoduleCommand"/> for the git <see cref="Process"/></param> /// <returns>The <see cref="ProcessStartInfo"/> for the git <see cref="Process"/></returns> internal static ProcessStartInfo GetProcessStartInfo(Submodule submodule, SubmoduleCommand submoduleCommand) { return(new ProcessStartInfo("git.exe") { Arguments = GetArguments(submodule, submoduleCommand), CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false }); }
/// <summary> /// Start git.exe with the given arguments /// </summary> /// <param name="submoduleCommand">The <see cref="SubmoduleCommand"/> for this argument</param> /// <param name="submodule">[Optional] The <see cref="Submodule"/> for this argument, /// use <c>null</c> for all submodules</param> internal void DoStartGit(SubmoduleCommand submoduleCommand, Submodule submodule = null) { Task.Run(() => { Model.GitCounter++; CanExecuteCommand(false); WriteToOutputWindow(Category.EmptyLine, null); SetPathForGitProcess((submoduleCommand == SubmoduleCommand.OnePullOriginMaster) || (submoduleCommand == SubmoduleCommand.OneBranchList) ? submodule : null); var gitStartInfo = GitHelper.GetProcessStartInfo(submodule, submoduleCommand); WriteToOutputWindow(Category.Debug, string.Format("Start Git with the follow arguments: {0}", gitStartInfo.Arguments)); using (var process = Process.Start(gitStartInfo)) { if (process == null) { WriteToOutputWindow(Category.Error, "Can't start Git process"); CanExecuteCommand(true); Model.WaitingTimer.Set(); return; } string consoleOutput; using (var reader = process.StandardOutput) { consoleOutput = reader.ReadToEnd().TrimEnd(); } if (process.ExitCode == 0) { AnalyzeConsoleOutput(consoleOutput, submoduleCommand); return; } WriteToOutputWindow(Category.Error, string.Format("Error on Git process with command: {0}", gitStartInfo.Arguments)); WriteToOutputWindow(Category.Error, string.Format("Git process end with errorcode: {0}", process.ExitCode)); using (var reader = process.StandardError) { WriteToOutputWindow(Category.Error, reader.ReadToEnd().TrimEnd()); CanExecuteCommand(true); Model.WaitingTimer.Set(); } if (submodule != null) { submodule.ChangeHealthStatus(HealthStatus.Error); return; } if (Model.ListOfSubmodules == null) { return; } foreach (var module in Model.ListOfSubmodules) { module.ChangeHealthStatus(HealthStatus.Error); } } }); }
/// <summary> /// Analyze the console output, based on the given <see cref="SubmoduleCommand"/> /// </summary> /// <param name="consoleOutput">The output of the console to analyze</param> /// <param name="submoduleCommand">The <see cref="SubmoduleCommand"/> for the analyze</param> internal void AnalyzeConsoleOutput(string consoleOutput, SubmoduleCommand submoduleCommand) { // TODO: cleanup this switch case switch (submoduleCommand) { case SubmoduleCommand.OtherGitVersion: WriteToOutputWindow(Category.Debug, "Finished Git process with no error"); if (string.IsNullOrEmpty(consoleOutput)) { WriteToOutputWindow(Category.Error, "Can't get version number from git"); return; } var versionNumberString = consoleOutput.Split(' ').LastOrDefault(); if (string.IsNullOrEmpty(versionNumberString)) { WriteToOutputWindow(Category.Error, "Can't parse version number from git"); return; } Model.GitVersion = versionNumberString; Model.GitIsPresent = true; Model.WaitingTimer.Set(); CanExecuteCommand(true); return; case SubmoduleCommand.OtherBranchList: WriteToOutputWindow(Category.Debug, "Finished Git process with no error"); Model.ListOfBranches = new Collection <string>(); Model.CountOfBranches = "Branch (of 0)"; Model.CurrentBranch = string.Empty; if (string.IsNullOrEmpty(consoleOutput)) { WriteToOutputWindow(Category.Error, "Can't get branch name"); return; } Model.ListOfBranches = consoleOutput.Split('\n').Select(found => found.TrimStart('*', ' ', '(').TrimEnd(')')); Model.CountOfBranches = string.Format("Branch (of {0})", Model.ListOfBranches.Count()); var branch = consoleOutput.Split('\n').FirstOrDefault(found => found.StartsWith("*", StringComparison.Ordinal)); if (string.IsNullOrEmpty(branch)) { WriteToOutputWindow(Category.Error, "Can't parse branch name"); return; } Model.CurrentBranch = branch.TrimStart('*', ' '); Model.WaitingTimer.Set(); return; case SubmoduleCommand.OneBranchList: var currentDirectory = Directory.GetCurrentDirectory(); var submodule = Model.ListOfSubmodules.FirstOrDefault(found => currentDirectory.EndsWith(found.Name, StringComparison.Ordinal)); if (submodule == null) { WriteToOutputWindow(Category.Error, "Can't found submodule for branch list"); Model.WaitingTimer.Set(); return; } if (string.IsNullOrEmpty(consoleOutput)) { WriteToOutputWindow(Category.Error, "Can't get branch list for submodule"); return; } submodule.ListOfBranches = consoleOutput.Split('\n').Select(found => found.TrimStart('*', ' ', '(').TrimEnd(')')); submodule.CountOfBranches = string.Format("Branch (of {0}):", submodule.ListOfBranches.Count()); var submoduleBranch = consoleOutput.Split('\n').FirstOrDefault(found => found.StartsWith("*", StringComparison.Ordinal)); if (string.IsNullOrEmpty(submoduleBranch)) { WriteToOutputWindow(Category.Error, "Can't parse branch name"); return; } submodule.CurrentBranch = submoduleBranch.TrimStart('*', ' ', '(').TrimEnd(')'); Model.WaitingTimer.Set(); CanExecuteCommand(true); return; case SubmoduleCommand.OnePullOriginMaster: WriteToOutputWindow(Category.Debug, "Finished Git process with no error"); Model.WaitingTimer.Set(); return; case SubmoduleCommand.AllDeinit: case SubmoduleCommand.AllDeinitForce: case SubmoduleCommand.AllFetch: case SubmoduleCommand.AllInit: case SubmoduleCommand.AllPullOriginMaster: case SubmoduleCommand.AllUpdate: case SubmoduleCommand.AllUpdateForce: case SubmoduleCommand.OneDeinit: case SubmoduleCommand.OneDeinitForce: case SubmoduleCommand.OneInit: case SubmoduleCommand.OneStatus: case SubmoduleCommand.OneUpdate: case SubmoduleCommand.OneUpdateForce: WriteToOutputWindow(Category.Debug, "Finished Git process with no error"); DoStartGit(SubmoduleCommand.AllStatus); return; default: WriteToOutputWindow(Category.Debug, "Finished Git process with no error"); DoStartGit(SubmoduleCommand.AllStatus); return; case SubmoduleCommand.AllStatus: if (string.IsNullOrEmpty(consoleOutput)) { WriteToOutputWindow(Category.Debug, "No submodules found"); CanExecuteCommand(true); return; } var tempList = new List <Submodule>(); var splitedAnswer = consoleOutput.Split('\n').Where(found => !string.IsNullOrEmpty(found)).ToList(); WriteToOutputWindow(Category.Debug, "Console output from Git process:"); WriteToOutputWindow(Category.Debug, string.Empty.PadRight(40, '-')); foreach (var line in splitedAnswer) { WriteToOutputWindow(Category.Debug, line); } WriteToOutputWindow(Category.Debug, string.Empty.PadRight(40, '-')); WriteToOutputWindow(Category.Debug, string.Format("Git console output have {0} lines, that should be {0} submodules", splitedAnswer.Count)); tempList.AddRange(splitedAnswer.Select(found => new Submodule(Model.CurrentSolutionPath, found))); Model.ListOfSubmodules = tempList; if (splitedAnswer.Count == Model.ListOfSubmodules.Count()) { WriteToOutputWindow(Category.Debug, string.Format("Count of Submodules: {0}", Model.ListOfSubmodules.Count())); } else { WriteToOutputWindow(Category.Error, string.Format("Count of Submodules {0} are not identical with count of lines {1}", Model.ListOfSubmodules.Count(), splitedAnswer.Count)); } CanExecuteCommand(true); DoCollectBranchList(); break; } // TODO: cleanup this switch case }
/// <summary> /// Returns a argument <see cref="string"/> for Git based on the given <see cref="Submodule"/> /// and <see cref="SubmoduleCommand"/> /// </summary> /// <param name="submodule">The <see cref="Submodule"/> for this argument, use <c>null</c> for all submodules</param> /// <param name="submoduleCommand">The <see cref="SubmoduleCommand"/> for this argument</param> /// <returns>Argument <see cref="string"/> for Git</returns> internal static string GetArguments(Submodule submodule, SubmoduleCommand submoduleCommand) { var submoduleName = (submodule != null) && !string.IsNullOrEmpty(submodule.Name) ? submodule.Name : string.Empty; switch (submoduleCommand) { case SubmoduleCommand.AllFetch: return("fetch --recurse-submodules"); case SubmoduleCommand.AllStatus: return("submodule status"); case SubmoduleCommand.AllInit: return("submodule init"); case SubmoduleCommand.AllDeinit: return("submodule deinit --all"); case SubmoduleCommand.AllDeinitForce: return("submodule deinit --all --force"); case SubmoduleCommand.AllUpdate: return("submodule update"); case SubmoduleCommand.AllUpdateForce: return("submodule update --force"); case SubmoduleCommand.AllPullOriginMaster: return("FOREACH command is still broken under windows"); case SubmoduleCommand.OneStatus: return("submodule status " + submoduleName); case SubmoduleCommand.OneInit: return("submodule init " + submoduleName); case SubmoduleCommand.OneDeinit: return("submodule deinit " + submoduleName); case SubmoduleCommand.OneDeinitForce: return("submodule deinit --force " + submoduleName); case SubmoduleCommand.OneUpdate: return("submodule update " + submoduleName); case SubmoduleCommand.OneUpdateForce: return("submodule update --force " + submoduleName); case SubmoduleCommand.OnePullOriginMaster: return("pull origin master"); case SubmoduleCommand.OtherGitVersion: return("--version"); case SubmoduleCommand.OneBranchList: case SubmoduleCommand.OtherBranchList: return("branch"); default: return(string.Empty); } }