From 6f98a18b2e97bb343552ec2f1468a23e7c9d4f36 Mon Sep 17 00:00:00 2001 From: "Samir L. Boulema" Date: Fri, 19 Aug 2016 17:50:20 +0200 Subject: [PATCH] #21, #22 --- TGit/Commands/ContextMenuCommands.cs | 8 ++-- TGit/Commands/GitFlowCommands.cs | 67 +++++++++++++--------------- TGit/Commands/MainMenuCommands.cs | 58 ++++++++++++------------ TGit/Helpers/CommandHelper.cs | 3 ++ TGit/Helpers/FileHelper.cs | 27 +++++------ TGit/Helpers/GitHelper.cs | 6 +-- TGit/Helpers/ProcessHelper.cs | 46 +++++++++++++------ TGit/OptionsGeneral.cs | 10 +++++ TGit/OutputBox.Designer.cs | 41 ++++++++++++++--- TGit/OutputBox.cs | 66 +++++++++++++++++++++++---- TGit/TGITPackage.cs | 27 ++++------- TGit/source.extension.vsixmanifest | 2 +- 12 files changed, 224 insertions(+), 137 deletions(-) diff --git a/TGit/Commands/ContextMenuCommands.cs b/TGit/Commands/ContextMenuCommands.cs index eef8754..1ab3739 100644 --- a/TGit/Commands/ContextMenuCommands.cs +++ b/TGit/Commands/ContextMenuCommands.cs @@ -10,18 +10,16 @@ public class ContextMenuCommands { private readonly ProcessHelper _processHelper; private readonly CommandHelper _commandHelper; - private readonly FileHelper _fileHelper; private readonly GitHelper _gitHelper; private readonly DTE _dte; private readonly OptionPageGrid _generalOptions; - public ContextMenuCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, FileHelper fileHelper, + public ContextMenuCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, DTE dte, OptionPageGrid generalOptions) { _processHelper = processHelper; _commandHelper = commandHelper; _gitHelper = gitHelper; - _fileHelper = fileHelper; _dte = dte; _generalOptions = generalOptions; } @@ -120,14 +118,14 @@ private void PrefDiffContextCommand(object sender, EventArgs e) if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - var revisions = _processHelper.GitResult(Path.GetDirectoryName(currentFilePath), $"log -2 --pretty=format:%h {_fileHelper.GetExactFileName(currentFilePath)}"); + var revisions = _processHelper.GitResult(Path.GetDirectoryName(currentFilePath), $"log -2 --pretty=format:%h {FileHelper.GetExactFileName(currentFilePath)}"); if (!revisions.Contains(",")) { MessageBox.Show("Could not determine the last committed revision!", "TGit", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { - _processHelper.StartTortoiseGitProc($"/command:diff /path:\"{_fileHelper.GetExactPathName(currentFilePath)}\" /startrev:{revisions.Split(',')[0]} /endrev:{revisions.Split(',')[1]}"); + _processHelper.StartTortoiseGitProc($"/command:diff /path:\"{FileHelper.GetExactPathName(currentFilePath)}\" /startrev:{revisions.Split(',')[0]} /endrev:{revisions.Split(',')[1]}"); } } } diff --git a/TGit/Commands/GitFlowCommands.cs b/TGit/Commands/GitFlowCommands.cs index a1a03f3..2a86a18 100644 --- a/TGit/Commands/GitFlowCommands.cs +++ b/TGit/Commands/GitFlowCommands.cs @@ -3,6 +3,7 @@ using System; using System.IO; using System.Windows.Forms; +using EnvDTE; using Microsoft.VisualStudio.Shell; namespace SamirBoulema.TGit.Commands @@ -11,19 +12,22 @@ public class GitFlowCommands { private readonly ProcessHelper _processHelper; private readonly CommandHelper _commandHelper; - private readonly FileHelper _fileHelper; private readonly GitHelper _gitHelper; private readonly string _gitBin; private readonly OleMenuCommandService _mcs; + private readonly DTE _dte; + private readonly OptionPageGrid _options; - public GitFlowCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, FileHelper fileHelper, OleMenuCommandService mcs) + public GitFlowCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, + OleMenuCommandService mcs, DTE dte, OptionPageGrid options) { _processHelper = processHelper; _commandHelper = commandHelper; _gitHelper = gitHelper; - _fileHelper = fileHelper; - _gitBin = fileHelper.GetMSysGit(); + _gitBin = FileHelper.GetMSysGit(); _mcs = mcs; + _dte = dte; + _options = options; } public void AddCommands() @@ -71,7 +75,7 @@ public void AddCommands() private void InitCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var flowDialog = new Flow(); @@ -100,7 +104,7 @@ private void InitCommand(object sender, EventArgs e) private void StartFeatureCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var featureName = Interaction.InputBox("Feature Name:", "Start New Feature"); if (string.IsNullOrEmpty(featureName)) return; @@ -124,13 +128,11 @@ private void StartFeatureCommand(object sender, EventArgs e) private void StartFeatureGitHubCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var featureName = Interaction.InputBox("Feature Name:", "Start New Feature"); if (string.IsNullOrEmpty(featureName)) return; - var flowOptions = _gitHelper.GetFlowOptions(); - /* 1. Switch to the master branch * 2. Pull latest changes on master * 3. Create and switch to a new branch @@ -139,16 +141,16 @@ private void StartFeatureGitHubCommand(object sender, EventArgs e) "cmd.exe", $"/c cd \"{solutionDir}\" && " + _gitHelper.GetSshSetup() + - FormatCliCommand($"checkout {flowOptions.MasterBranch}") + + FormatCliCommand("checkout master") + FormatCliCommand("pull") + - FormatCliCommand($"checkout -b {flowOptions.FeaturePrefix}{featureName} {flowOptions.MasterBranch}", false), + FormatCliCommand($"checkout -b {featureName} master", false), $"Starting feature {featureName}" ); } private void FinishFeatureCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var featureBranch = _gitHelper.GetCurrentBranchName(false); var featureName = _gitHelper.GetCurrentBranchName(true); @@ -168,10 +170,9 @@ private void FinishFeatureCommand(object sender, EventArgs e) FormatCliCommand($"checkout {flowOptions.DevelopBranch}") + FormatCliCommand("pull") + FormatCliCommand($"merge --no-ff {featureBranch}") + - FormatCliCommand($"push origin {flowOptions.DevelopBranch}") + - FormatCliCommand($"branch -d {featureBranch}") + - (_gitHelper.RemoteBranchExists(featureBranch) ? FormatCliCommand($"push origin --delete {featureBranch}", false) : "echo."), - $"Finishing feature {featureName}" + FormatCliCommand($"push origin {flowOptions.DevelopBranch}", false), + $"Finishing feature {featureName}", + featureBranch, null, _options ); } @@ -182,11 +183,10 @@ private string FormatCliCommand(string gitCommand, bool appendNextLine = true) private void FinishFeatureGitHubCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var featureBranch = _gitHelper.GetCurrentBranchName(false); var featureName = _gitHelper.GetCurrentBranchName(true); - var flowOptions = _gitHelper.GetFlowOptions(); /* 1. Switch to the master branch * 2. Pull latest changes on master @@ -199,18 +199,17 @@ private void FinishFeatureGitHubCommand(object sender, EventArgs e) "cmd.exe", $"/c cd \"{solutionDir}\" && " + _gitHelper.GetSshSetup() + - FormatCliCommand($"checkout {flowOptions.MasterBranch}") + + FormatCliCommand("checkout master") + FormatCliCommand("pull") + FormatCliCommand($"merge --no-ff {featureBranch}") + - FormatCliCommand($"push origin {flowOptions.MasterBranch}") + - FormatCliCommand($"branch -d {featureBranch}") + - (_gitHelper.RemoteBranchExists(featureBranch) ? FormatCliCommand($"push origin --delete {featureBranch}", false) : "echo."), - $"Finishing feature {featureName}"); + FormatCliCommand("push origin master", false), + $"Finishing feature {featureName}", + featureBranch, null, _options); } private void StartReleaseCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var releaseVersion = Interaction.InputBox("Release Version:", "Start New Release"); if (string.IsNullOrEmpty(releaseVersion)) return; @@ -234,7 +233,7 @@ private void StartReleaseCommand(object sender, EventArgs e) private void FinishReleaseCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var releaseBranch = _gitHelper.GetCurrentBranchName(false); var releaseName = _gitHelper.GetCurrentBranchName(true); @@ -266,16 +265,15 @@ private void FinishReleaseCommand(object sender, EventArgs e) FormatCliCommand($"merge --no-ff {releaseBranch}") + FormatCliCommand($"push origin {flowOptions.DevelopBranch}") + FormatCliCommand($"push origin {flowOptions.MasterBranch}") + - FormatCliCommand($"push origin {releaseName}") + - FormatCliCommand($"branch -d {releaseBranch}") + - (_gitHelper.RemoteBranchExists(releaseBranch) ? FormatCliCommand($"push origin --delete {releaseBranch}", false) : "echo."), - $"Finishing release {releaseName}" + FormatCliCommand($"push origin {releaseName}", false), + $"Finishing release {releaseName}", + releaseBranch, null, _options ); } private void StartHotfixCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var hotfixVersion = Interaction.InputBox("Hotfix Version:", "Start New Hotfix"); if (string.IsNullOrEmpty(hotfixVersion)) return; @@ -299,7 +297,7 @@ private void StartHotfixCommand(object sender, EventArgs e) private void FinishHotfixCommand(object sender, EventArgs e) { - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; var hotfixBranch = _gitHelper.GetCurrentBranchName(false); var hotfixName = _gitHelper.GetCurrentBranchName(true); @@ -331,10 +329,9 @@ private void FinishHotfixCommand(object sender, EventArgs e) FormatCliCommand($"merge --no-ff {hotfixBranch}") + FormatCliCommand($"push origin {flowOptions.DevelopBranch}") + FormatCliCommand($"push origin {flowOptions.MasterBranch}") + - FormatCliCommand($"push origin {hotfixName}") + - FormatCliCommand($"branch -d {hotfixBranch}") + - (_gitHelper.RemoteBranchExists(hotfixBranch) ? FormatCliCommand($"push origin --delete {hotfixBranch}", false) : "echo."), - $"Finishing hotfix {hotfixName}" + FormatCliCommand($"push origin {hotfixName}", false), + $"Finishing hotfix {hotfixName}", + hotfixBranch, null, _options ); } } diff --git a/TGit/Commands/MainMenuCommands.cs b/TGit/Commands/MainMenuCommands.cs index d6a4d9a..1ddb4c6 100644 --- a/TGit/Commands/MainMenuCommands.cs +++ b/TGit/Commands/MainMenuCommands.cs @@ -9,19 +9,17 @@ public class MainMenuCommands { private readonly ProcessHelper _processHelper; private readonly CommandHelper _commandHelper; - private readonly FileHelper _fileHelper; private readonly GitHelper _gitHelper; private readonly DTE _dte; private readonly OptionPageGrid _generalOptions; private readonly OleMenuCommandService _mcs; - public MainMenuCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, FileHelper fileHelper, + public MainMenuCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, DTE dte, OptionPageGrid generalOptions, OleMenuCommandService mcs) { _processHelper = processHelper; _commandHelper = commandHelper; _gitHelper = gitHelper; - _fileHelper = fileHelper; _dte = dte; _generalOptions = generalOptions; _mcs = mcs; @@ -59,116 +57,116 @@ public void AddCommands() private void ShowChangesCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:repostatus /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void PullCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:pull /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void FetchCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:fetch /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void CommitCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc( $"/command:commit /path:\"{solutionDir}\" /logmsg:\"{_gitHelper.GetCommitMessage(_generalOptions.CommitMessage, _dte)}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void PushCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:push /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void ShowLogCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:log /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void DiskBrowserCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.Start(solutionDir); } private void RepoBrowserCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:repobrowser /path:\"{solutionDir}\""); } private void CreateStashCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:stashsave /path:\"{solutionDir}\""); } private void ApplyStashCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:reflog /ref:refs/stash /path:\"{solutionDir}\""); } private void BranchCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:branch /path:\"{solutionDir}\""); } private void SwitchCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:switch /path:\"{solutionDir}\""); } private void MergeCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:merge /path:\"{solutionDir}\""); } private void RevertCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; - _fileHelper.SaveAllFiles(); + FileHelper.SaveAllFiles(_dte); _processHelper.StartTortoiseGitProc($"/command:revert /path:\"{solutionDir}\""); } private void CleanupCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:cleanup /path:\"{solutionDir}\""); } private void ResolveCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:resolve /path:\"{solutionDir}\""); } private void SyncCommand(object sender, EventArgs e) { - string solutionDir = _fileHelper.GetSolutionDir(); + string solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:sync /path:\"{solutionDir}\""); } diff --git a/TGit/Helpers/CommandHelper.cs b/TGit/Helpers/CommandHelper.cs index 280bf0c..8273305 100644 --- a/TGit/Helpers/CommandHelper.cs +++ b/TGit/Helpers/CommandHelper.cs @@ -38,6 +38,9 @@ public OleMenuCommand CreateCommand(uint commandId) public void ApplyStash_BeforeQueryStatus(object sender, EventArgs e) { ((OleMenuCommand)sender).Enabled = _processHelper.StartProcessGit("stash list"); + + // Update all settings once the TGit menu opens + _package.SolutionEvents_Opened(); } private void Diff_BeforeQueryStatus(object sender, EventArgs e) diff --git a/TGit/Helpers/FileHelper.cs b/TGit/Helpers/FileHelper.cs index 2de2571..51ee5c0 100644 --- a/TGit/Helpers/FileHelper.cs +++ b/TGit/Helpers/FileHelper.cs @@ -7,39 +7,32 @@ namespace SamirBoulema.TGit.Helpers { - public class FileHelper + public static class FileHelper { - private readonly DTE _dte; - - public FileHelper(DTE dte) - { - _dte = dte; - } - - public string GetTortoiseGitProc() + public static string GetTortoiseGitProc() { return (string) Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\TortoiseGit", "ProcPath", @"C:\Program Files\TortoiseGit\bin\TortoiseGitProc.exe"); } - public string GetTortoiseGitPlink() + public static string GetTortoiseGitPlink() { return (string)Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\TortoiseGit", "ProcPath", @"C:\Program Files\TortoiseGit\bin\TortoiseGitPlink.exe"); } - public string GetMSysGit() + public static string GetMSysGit() { var regPath = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\TortoiseGit", "MSysGit", @"C:\Program Files (x86)\Git\bin\git.exe"); return Path.Combine(regPath, "git.exe"); } - public void SaveAllFiles() + public static void SaveAllFiles(DTE dte) { - _dte.ExecuteCommand("File.SaveAll"); + dte.ExecuteCommand("File.SaveAll"); } - public string GetSolutionDir() + public static string GetSolutionDir(DTE dte) { - string fileName = _dte.Solution.FullName; + string fileName = dte.Solution.FullName; if (!string.IsNullOrEmpty(fileName)) { var path = Path.GetDirectoryName(fileName); @@ -73,7 +66,7 @@ private static string FindGitdir(string path) /// Get case sensitive path. /// http://stackoverflow.com/questions/325931/getting-actual-file-name-with-proper-casing-on-windows-with-net /// - public string GetExactFileName(string pathName) + public static string GetExactFileName(string pathName) { if (!(File.Exists(pathName) || Directory.Exists(pathName))) return pathName; @@ -87,7 +80,7 @@ public string GetExactFileName(string pathName) return di.Name.ToUpper(); } - public string GetExactPathName(string pathName) + public static string GetExactPathName(string pathName) { if (!(File.Exists(pathName) || Directory.Exists(pathName))) return pathName; diff --git a/TGit/Helpers/GitHelper.cs b/TGit/Helpers/GitHelper.cs index ad9542f..2250acb 100644 --- a/TGit/Helpers/GitHelper.cs +++ b/TGit/Helpers/GitHelper.cs @@ -6,12 +6,10 @@ namespace SamirBoulema.TGit.Helpers { public class GitHelper { - private readonly FileHelper _fileHelper; private readonly ProcessHelper _processHelper; - public GitHelper(FileHelper fileHelper, ProcessHelper processHelper) + public GitHelper(ProcessHelper processHelper) { - _fileHelper = fileHelper; _processHelper = processHelper; } @@ -61,7 +59,7 @@ public string GetSshSetup() if (string.IsNullOrEmpty(remoteOriginPuttyKeyfile)) return string.Empty; _processHelper.Start("pageant", remoteOriginPuttyKeyfile); - return $"set GIT_SSH={_fileHelper.GetTortoiseGitPlink()} && "; + return $"set GIT_SSH={FileHelper.GetTortoiseGitPlink()} && "; } public FlowOptions GetFlowOptions() diff --git a/TGit/Helpers/ProcessHelper.cs b/TGit/Helpers/ProcessHelper.cs index 713e6f4..8add68c 100644 --- a/TGit/Helpers/ProcessHelper.cs +++ b/TGit/Helpers/ProcessHelper.cs @@ -9,19 +9,17 @@ namespace SamirBoulema.TGit.Helpers { public class ProcessHelper { - private readonly FileHelper _fileHelper; - private string _solutionDir; - private readonly string _tortoiseGitProc, _git; private readonly DTE _dte; + private string _solutionDir; + private readonly string _tortoiseGitProc, _git; private OutputBox _outputBox; private readonly Stopwatch _stopwatch; public ProcessHelper(DTE dte) { _dte = dte; - _fileHelper = new FileHelper(dte); - _tortoiseGitProc = _fileHelper.GetTortoiseGitProc(); - _git = _fileHelper.GetMSysGit(); + _tortoiseGitProc = FileHelper.GetTortoiseGitProc(); + _git = FileHelper.GetMSysGit(); _stopwatch = new Stopwatch(); } @@ -33,7 +31,7 @@ public ProcessHelper(DTE dte) /// True if output is non-empty public bool StartProcessGit(string commands, bool showAlert = true) { - _solutionDir = _fileHelper.GetSolutionDir(); + _solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(_solutionDir)) return false; var output = string.Empty; @@ -89,7 +87,7 @@ public void StartTortoiseGitProc(string args) /// Git output public string StartProcessGitResult(string commands) { - _solutionDir = _fileHelper.GetSolutionDir(); + _solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(_solutionDir)) return string.Empty; var output = string.Empty; @@ -158,7 +156,8 @@ public void Start(string application, string arguments) Process.Start(application, arguments); } - public void StartProcessGui(string application, string args, string title) + public Process StartProcessGui(string application, string args, string title, string branchName = "", + OutputBox outputBox = null, OptionPageGrid options = null) { var dialogResult = DialogResult.OK; if (!StartProcessGit("config user.name") || !StartProcessGit("config user.email")) @@ -189,7 +188,15 @@ public void StartProcessGui(string application, string args, string title) process.OutputDataReceived += OutputDataHandler; process.ErrorDataReceived += OutputDataHandler; - _outputBox = new OutputBox(_dte); + if (outputBox == null) + { + _outputBox = new OutputBox(_dte, branchName, options); + _outputBox.Show(); + } + else + { + _outputBox = outputBox; + } _stopwatch.Reset(); _stopwatch.Start(); @@ -199,12 +206,16 @@ public void StartProcessGui(string application, string args, string title) _outputBox.Text = title; _outputBox.Show(); + + return process; } catch (Exception e) { MessageBox.Show(e.Message, $"{application} not found", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + + return null; } private void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine) @@ -215,9 +226,18 @@ private void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outL var text = outLine.Data + Environment.NewLine; - _outputBox.BeginInvoke((Action) (() => _outputBox.richTextBox.AppendText(text, text.StartsWith(">")))); - _outputBox.BeginInvoke((Action) (() => _outputBox.richTextBox.Select(_outputBox.richTextBox.TextLength - text.Length + 1, 0))); - _outputBox.BeginInvoke((Action) (() => _outputBox.richTextBox.ScrollToCaret())); + if (_outputBox.InvokeRequired) + { + _outputBox.BeginInvoke((Action) (() => _outputBox.richTextBox.AppendText(text, text.StartsWith(">")))); + _outputBox.BeginInvoke((Action) (() => _outputBox.richTextBox.Select(_outputBox.richTextBox.TextLength - text.Length + 1, 0))); + _outputBox.BeginInvoke((Action) (() => _outputBox.richTextBox.ScrollToCaret())); + } + else + { + _outputBox.richTextBox.AppendText(text, text.StartsWith(">")); + _outputBox.richTextBox.Select(_outputBox.richTextBox.TextLength - text.Length + 1, 0); + _outputBox.richTextBox.ScrollToCaret(); + } } private void process_Exited(object sender, EventArgs e) diff --git a/TGit/OptionsGeneral.cs b/TGit/OptionsGeneral.cs index c608091..4bf006f 100644 --- a/TGit/OptionsGeneral.cs +++ b/TGit/OptionsGeneral.cs @@ -26,5 +26,15 @@ public string CommitMessage [DisplayName(@"Close dialog after operation")] [Description("0: Close manually, 1: Auto-close if no further options are available, 2: Auto-close if no errors")] public int CloseOnEnd { get; set; } + + [Category("TGit")] + [DisplayName(@"Delete local branch")] + [Description("When finishing a feature delete the local branch by default")] + public bool DeleteLocalBranch { get; set; } + + [Category("TGit")] + [DisplayName(@"Delete remote branch")] + [Description("When finishing a feature delete the remote branch by default")] + public bool DeleteRemoteBranch { get; set; } } } diff --git a/TGit/OutputBox.Designer.cs b/TGit/OutputBox.Designer.cs index 6f7767e..3c09569 100644 --- a/TGit/OutputBox.Designer.cs +++ b/TGit/OutputBox.Designer.cs @@ -32,6 +32,8 @@ private void InitializeComponent() this.okButton = new System.Windows.Forms.Button(); this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); this.richTextBox = new System.Windows.Forms.RichTextBox(); + this.localBranchCheckBox = new System.Windows.Forms.CheckBox(); + this.remoteBranchCheckBox = new System.Windows.Forms.CheckBox(); this.flowLayoutPanel1.SuspendLayout(); this.SuspendLayout(); // @@ -39,7 +41,7 @@ private void InitializeComponent() // this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.okButton.Enabled = false; - this.okButton.Location = new System.Drawing.Point(401, 3); + this.okButton.Location = new System.Drawing.Point(272, 3); this.okButton.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3); this.okButton.Name = "okButton"; this.okButton.Size = new System.Drawing.Size(75, 23); @@ -50,11 +52,13 @@ private void InitializeComponent() // // flowLayoutPanel1 // + this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.flowLayoutPanel1.Controls.Add(this.okButton); this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft; - this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 292); + this.flowLayoutPanel1.Location = new System.Drawing.Point(141, 290); this.flowLayoutPanel1.Name = "flowLayoutPanel1"; - this.flowLayoutPanel1.Size = new System.Drawing.Size(476, 32); + this.flowLayoutPanel1.Size = new System.Drawing.Size(347, 32); this.flowLayoutPanel1.TabIndex = 4; // // richTextBox @@ -66,15 +70,39 @@ private void InitializeComponent() this.richTextBox.Location = new System.Drawing.Point(12, 12); this.richTextBox.Name = "richTextBox"; this.richTextBox.ReadOnly = true; - this.richTextBox.Size = new System.Drawing.Size(476, 274); + this.richTextBox.Size = new System.Drawing.Size(476, 263); this.richTextBox.TabIndex = 2; this.richTextBox.Text = ""; // + // localBranchCheckBox + // + this.localBranchCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.localBranchCheckBox.AutoSize = true; + this.localBranchCheckBox.Location = new System.Drawing.Point(12, 281); + this.localBranchCheckBox.Name = "localBranchCheckBox"; + this.localBranchCheckBox.Size = new System.Drawing.Size(118, 17); + this.localBranchCheckBox.TabIndex = 5; + this.localBranchCheckBox.Text = "Delete local branch"; + this.localBranchCheckBox.UseVisualStyleBackColor = true; + // + // remoteBranchCheckBox + // + this.remoteBranchCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.remoteBranchCheckBox.AutoSize = true; + this.remoteBranchCheckBox.Location = new System.Drawing.Point(12, 305); + this.remoteBranchCheckBox.Name = "remoteBranchCheckBox"; + this.remoteBranchCheckBox.Size = new System.Drawing.Size(128, 17); + this.remoteBranchCheckBox.TabIndex = 6; + this.remoteBranchCheckBox.Text = "Delete remote branch"; + this.remoteBranchCheckBox.UseVisualStyleBackColor = true; + // // OutputBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(500, 327); + this.ClientSize = new System.Drawing.Size(500, 334); + this.Controls.Add(this.remoteBranchCheckBox); + this.Controls.Add(this.localBranchCheckBox); this.Controls.Add(this.flowLayoutPanel1); this.Controls.Add(this.richTextBox); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow; @@ -84,6 +112,7 @@ private void InitializeComponent() this.Text = "TGit"; this.flowLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); + this.PerformLayout(); } @@ -92,5 +121,7 @@ private void InitializeComponent() public System.Windows.Forms.Button okButton; private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; public System.Windows.Forms.RichTextBox richTextBox; + private System.Windows.Forms.CheckBox localBranchCheckBox; + private System.Windows.Forms.CheckBox remoteBranchCheckBox; } } \ No newline at end of file diff --git a/TGit/OutputBox.cs b/TGit/OutputBox.cs index b37f1b7..dca15a9 100644 --- a/TGit/OutputBox.cs +++ b/TGit/OutputBox.cs @@ -1,6 +1,7 @@ using EnvDTE; using SamirBoulema.TGit.Helpers; using System; +using System.IO; using System.Linq; using System.Windows.Forms; @@ -8,27 +9,76 @@ namespace SamirBoulema.TGit { public sealed partial class OutputBox : Form { - private readonly FileHelper _fileHelper; + private readonly DTE _dte; private readonly ProcessHelper _processHelper; + private readonly GitHelper _gitHelper; + private readonly string _branchName; - public OutputBox(DTE dte) + public OutputBox(DTE dte, string branchName, OptionPageGrid options) { InitializeComponent(); - _fileHelper = new FileHelper(dte); + _dte = dte; _processHelper = new ProcessHelper(dte); + _gitHelper = new GitHelper(_processHelper); + _branchName = branchName; + + richTextBox.TextChanged += textBox_TextChanged; + + if (string.IsNullOrEmpty(branchName)) + { + localBranchCheckBox.Visible = false; + remoteBranchCheckBox.Visible = false; + } + else + { + remoteBranchCheckBox.Enabled = new GitHelper(_processHelper).RemoteBranchExists(branchName); + } + + if (options != null) + { + localBranchCheckBox.Checked = options.DeleteLocalBranch; + remoteBranchCheckBox.Checked = options.DeleteRemoteBranch; + } } private void okButton_Click(object sender, EventArgs e) { - Close(); - richTextBox.Clear(); - okButton.Enabled = false; + if (localBranchCheckBox.Checked || remoteBranchCheckBox.Checked) + { + var process = _processHelper.StartProcessGui( + "cmd.exe", + $"/c cd \"{FileHelper.GetSolutionDir(_dte)}\" && " + + _gitHelper.GetSshSetup() + + FormatCliCommand($"branch -d {_branchName}") + + (remoteBranchCheckBox.Checked ? FormatCliCommand($"push origin --delete {_branchName}", false) : "echo."), + "Deleting branches...", + string.Empty, this + ); + process.WaitForExit(); + if (process.ExitCode == 0) + { + Close(); + richTextBox.Clear(); + okButton.Enabled = false; + } + } + else + { + Close(); + richTextBox.Clear(); + okButton.Enabled = false; + } + } + + private static string FormatCliCommand(string gitCommand, bool appendNextLine = true) + { + return $"echo ^> {Path.GetFileNameWithoutExtension(FileHelper.GetMSysGit())} {gitCommand} && \"{FileHelper.GetMSysGit()}\" {gitCommand}{(appendNextLine ? " && " : string.Empty)}"; } private void ResolveButton_Click(object sender, EventArgs e) { okButton_Click(null, null); - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:resolve /path:\"{solutionDir}\""); } @@ -36,7 +86,7 @@ private void ResolveButton_Click(object sender, EventArgs e) private void StashButton_Click(object sender, EventArgs e) { okButton_Click(null, null); - var solutionDir = _fileHelper.GetSolutionDir(); + var solutionDir = FileHelper.GetSolutionDir(_dte); if (string.IsNullOrEmpty(solutionDir)) return; _processHelper.StartTortoiseGitProc($"/command:repostatus /path:\"{solutionDir}\""); } diff --git a/TGit/TGITPackage.cs b/TGit/TGITPackage.cs index 084a902..09b8cd7 100644 --- a/TGit/TGITPackage.cs +++ b/TGit/TGITPackage.cs @@ -17,14 +17,12 @@ namespace SamirBoulema.TGit // ReSharper disable once InconsistentNaming public sealed class TGitPackage : Package { - private DTE _dte; - private FileHelper _fileHelper; + private DTE _dte; private ProcessHelper _processHelper; private CommandHelper _commandHelper; private GitHelper _gitHelper; private SolutionEvents _events; - private WindowEvents _windowEvents; public string SolutionDir; public bool IsGitFlow; public FlowOptions FlowOptions; @@ -39,29 +37,25 @@ protected override void Initialize() base.Initialize(); _dte = (DTE)GetService(typeof(DTE)); - var generalOptions = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid)); - _fileHelper = new FileHelper(_dte); + var options = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid)); _processHelper = new ProcessHelper(_dte); - _gitHelper = new GitHelper(_fileHelper, _processHelper); + _gitHelper = new GitHelper(_processHelper); _events = _dte.Events.SolutionEvents; _events.Opened += SolutionEvents_Opened; _events.AfterClosing += _events_AfterClosing; - _windowEvents = _dte.Events.WindowEvents; - _windowEvents.WindowActivated += WindowEvents_WindowActivated; - // Add our command handlers for menu (commands must exist in the .vsct file) var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if (null == mcs) return; _commandHelper = new CommandHelper(_processHelper, mcs, this); - new MainMenuCommands(_processHelper, _commandHelper, _gitHelper, _fileHelper, _dte, generalOptions, mcs).AddCommands(); + new MainMenuCommands(_processHelper, _commandHelper, _gitHelper, _dte, options, mcs).AddCommands(); - new ContextMenuCommands(_processHelper, _commandHelper, _gitHelper, _fileHelper, _dte, generalOptions).AddCommands(); + new ContextMenuCommands(_processHelper, _commandHelper, _gitHelper, _dte, options).AddCommands(); - new GitFlowCommands(_processHelper, _commandHelper, _gitHelper, _fileHelper, mcs).AddCommands(); + new GitFlowCommands(_processHelper, _commandHelper, _gitHelper, mcs, _dte, options).AddCommands(); // Add all menus var tgitMenu = _commandHelper.CreateCommand(PkgCmdIDList.TGitMenu); @@ -90,11 +84,6 @@ protected override void Initialize() mcs.AddCommand(tgitGitHubFlowMenu); } - private void WindowEvents_WindowActivated(Window GotFocus, Window LostFocus) - { - SolutionEvents_Opened(); - } - private void _events_AfterClosing() { SolutionDir = string.Empty; @@ -102,9 +91,9 @@ private void _events_AfterClosing() IsGitFlow = false; } - private void SolutionEvents_Opened() + public void SolutionEvents_Opened() { - SolutionDir = _fileHelper.GetSolutionDir(); + SolutionDir = FileHelper.GetSolutionDir(_dte); IsGitFlow = _processHelper.StartProcessGit("config --get gitflow.branch.master", false); FlowOptions = _gitHelper.GetFlowOptions(); BranchName = _gitHelper.GetCurrentBranchName(false); diff --git a/TGit/source.extension.vsixmanifest b/TGit/source.extension.vsixmanifest index bf21faf..be91585 100644 --- a/TGit/source.extension.vsixmanifest +++ b/TGit/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + TGit Control TortoiseGit from within Visual Studio https://github.com/sboulema/TGit