diff --git a/TGit/Commands/ContextMenuCommands.cs b/TGit/Commands/ContextMenuCommands.cs index 1ab3739..31645d0 100644 --- a/TGit/Commands/ContextMenuCommands.cs +++ b/TGit/Commands/ContextMenuCommands.cs @@ -8,18 +8,13 @@ namespace SamirBoulema.TGit.Commands { public class ContextMenuCommands { - private readonly ProcessHelper _processHelper; private readonly CommandHelper _commandHelper; - private readonly GitHelper _gitHelper; private readonly DTE _dte; private readonly OptionPageGrid _generalOptions; - public ContextMenuCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, - DTE dte, OptionPageGrid generalOptions) + public ContextMenuCommands(CommandHelper commandHelper, DTE dte, OptionPageGrid generalOptions) { - _processHelper = processHelper; _commandHelper = commandHelper; - _gitHelper = gitHelper; _dte = dte; _generalOptions = generalOptions; } @@ -47,19 +42,19 @@ private void ShowLogContextCommand(object sender, EventArgs e) var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:log /path:\"{currentFilePath}\" /closeonend:{_generalOptions.CloseOnEnd}"); + ProcessHelper.StartTortoiseGitProc($"/command:log /path:\"{currentFilePath}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void DiskBrowserContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; - _processHelper.Start(currentFilePath); + ProcessHelper.Start(currentFilePath); } private void RepoBrowserContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; - _processHelper.StartTortoiseGitProc($"/command:repobrowser /path:\"{currentFilePath}\""); + ProcessHelper.StartTortoiseGitProc($"/command:repobrowser /path:\"{currentFilePath}\""); } private void BlameContextCommand(object sender, EventArgs e) { @@ -67,49 +62,49 @@ private void BlameContextCommand(object sender, EventArgs e) int currentLineIndex = ((TextDocument)_dte.ActiveDocument.Object(string.Empty)).Selection.CurrentLine; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:blame /path:\"{currentFilePath}\" /line:{currentLineIndex}"); + ProcessHelper.StartTortoiseGitProc($"/command:blame /path:\"{currentFilePath}\" /line:{currentLineIndex}"); } private void MergeContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:merge /path:\"{currentFilePath}\""); + ProcessHelper.StartTortoiseGitProc($"/command:merge /path:\"{currentFilePath}\""); } private void PullContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:pull /path:\"{currentFilePath}\""); + ProcessHelper.StartTortoiseGitProc($"/command:pull /path:\"{currentFilePath}\""); } private void FetchContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:fetch /path:\"{currentFilePath}\""); + ProcessHelper.StartTortoiseGitProc($"/command:fetch /path:\"{currentFilePath}\""); } private void CommitContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:commit /path:\"{currentFilePath}\" /logmsg:\"{_gitHelper.GetCommitMessage(_generalOptions.CommitMessage, _dte)}\" /closeonend:{_generalOptions.CloseOnEnd}"); + ProcessHelper.StartTortoiseGitProc($"/command:commit /path:\"{currentFilePath}\" /logmsg:\"{GitHelper.GetCommitMessage(_generalOptions.CommitMessage, _dte)}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void RevertContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:revert /path:\"{currentFilePath}\""); + ProcessHelper.StartTortoiseGitProc($"/command:revert /path:\"{currentFilePath}\""); } private void DiffContextCommand(object sender, EventArgs e) { var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); - _processHelper.StartTortoiseGitProc($"/command:diff /path:\"{currentFilePath}\""); + ProcessHelper.StartTortoiseGitProc($"/command:diff /path:\"{currentFilePath}\""); } private void PrefDiffContextCommand(object sender, EventArgs e) @@ -118,14 +113,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/GitFlowMenuCommands.cs similarity index 72% rename from TGit/Commands/GitFlowCommands.cs rename to TGit/Commands/GitFlowMenuCommands.cs index 2a86a18..aa41be9 100644 --- a/TGit/Commands/GitFlowCommands.cs +++ b/TGit/Commands/GitFlowMenuCommands.cs @@ -3,30 +3,19 @@ using System; using System.IO; using System.Windows.Forms; -using EnvDTE; -using Microsoft.VisualStudio.Shell; namespace SamirBoulema.TGit.Commands { - public class GitFlowCommands + public class GitFlowMenuCommands { - private readonly ProcessHelper _processHelper; private readonly CommandHelper _commandHelper; - 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, - OleMenuCommandService mcs, DTE dte, OptionPageGrid options) + public GitFlowMenuCommands(CommandHelper commandHelper, OptionPageGrid options) { - _processHelper = processHelper; _commandHelper = commandHelper; - _gitHelper = gitHelper; _gitBin = FileHelper.GetMSysGit(); - _mcs = mcs; - _dte = dte; _options = options; } @@ -36,47 +25,46 @@ public void AddCommands() //Start/Finish Feature var startFeature = _commandHelper.CreateCommand(StartFeatureCommand, PkgCmdIDList.StartFeature); startFeature.BeforeQueryStatus += _commandHelper.GitFlow_BeforeQueryStatus; - _mcs.AddCommand(startFeature); + _commandHelper.AddCommand(startFeature); var finishFeature = _commandHelper.CreateCommand(FinishFeatureCommand, PkgCmdIDList.FinishFeature); finishFeature.BeforeQueryStatus += _commandHelper.Feature_BeforeQueryStatus; - _mcs.AddCommand(finishFeature); + _commandHelper.AddCommand(finishFeature); //Start/Finish Release var startRelease = _commandHelper.CreateCommand(StartReleaseCommand, PkgCmdIDList.StartRelease); startRelease.BeforeQueryStatus += _commandHelper.GitFlow_BeforeQueryStatus; - _mcs.AddCommand(startRelease); + _commandHelper.AddCommand(startRelease); var finishRelease = _commandHelper.CreateCommand(FinishReleaseCommand, PkgCmdIDList.FinishRelease); finishRelease.BeforeQueryStatus += _commandHelper.Release_BeforeQueryStatus; - _mcs.AddCommand(finishRelease); + _commandHelper.AddCommand(finishRelease); //Start/Finish Hotfix var startHotfix = _commandHelper.CreateCommand(StartHotfixCommand, PkgCmdIDList.StartHotfix); startHotfix.BeforeQueryStatus += _commandHelper.GitFlow_BeforeQueryStatus; - _mcs.AddCommand(startHotfix); + _commandHelper.AddCommand(startHotfix); var finishHotfix = _commandHelper.CreateCommand(FinishHotfixCommand, PkgCmdIDList.FinishHotfix); finishHotfix.BeforeQueryStatus += _commandHelper.Hotfix_BeforeQueryStatus; - _mcs.AddCommand(finishHotfix); + _commandHelper.AddCommand(finishHotfix); //Init var init = _commandHelper.CreateCommand(InitCommand, PkgCmdIDList.Init); init.BeforeQueryStatus += _commandHelper.GitHubFlow_BeforeQueryStatus; - _mcs.AddCommand(init); + _commandHelper.AddCommand(init); //GitHubFlow Commands //Start/Finish Feature _commandHelper.AddCommand(StartFeatureGitHubCommand, PkgCmdIDList.StartFeatureGitHub); var finishFeatureGitHub = _commandHelper.CreateCommand(FinishFeatureGitHubCommand, PkgCmdIDList.FinishFeatureGitHub); finishFeatureGitHub.BeforeQueryStatus += _commandHelper.Feature_BeforeQueryStatus; - _mcs.AddCommand(finishFeatureGitHub); + _commandHelper.AddCommand(finishFeatureGitHub); } private void InitCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; var flowDialog = new Flow(); if (flowDialog.ShowDialog() == DialogResult.OK) @@ -85,16 +73,16 @@ private void InitCommand(object sender, EventArgs e) * 2. Checkout develop branch (create if it doesn't exist, reset if it does) * 3. Push develop branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"config --add gitflow.branch.master {flowDialog.FlowOptions.MasterBranch}") + FormatCliCommand($"config --add gitflow.branch.develop {flowDialog.FlowOptions.DevelopBranch}") + FormatCliCommand($"config --add gitflow.prefix.feature {flowDialog.FlowOptions.FeaturePrefix}") + FormatCliCommand($"config --add gitflow.prefix.release {flowDialog.FlowOptions.ReleasePrefix}") + FormatCliCommand($"config --add gitflow.prefix.hotfix {flowDialog.FlowOptions.HotfixPrefix}") + - (_gitHelper.RemoteBranchExists(flowDialog.FlowOptions.DevelopBranch) ? + (GitHelper.RemoteBranchExists(flowDialog.FlowOptions.DevelopBranch) ? "echo." : FormatCliCommand($"checkout -b {flowDialog.FlowOptions.DevelopBranch}", false)), "Initializing GitFlow" @@ -104,21 +92,20 @@ private void InitCommand(object sender, EventArgs e) private void StartFeatureCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; var featureName = Interaction.InputBox("Feature Name:", "Start New Feature"); if (string.IsNullOrEmpty(featureName)) return; - var flowOptions = _gitHelper.GetFlowOptions(); + var flowOptions = GitHelper.GetFlowOptions(); /* 1. Switch to the develop branch * 2. Pull latest changes on develop * 3. Create and switch to a new branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"checkout {flowOptions.DevelopBranch}") + FormatCliCommand("pull") + FormatCliCommand($"checkout -b {flowOptions.FeaturePrefix}{featureName} {flowOptions.DevelopBranch}", false), @@ -128,8 +115,7 @@ private void StartFeatureCommand(object sender, EventArgs e) private void StartFeatureGitHubCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; var featureName = Interaction.InputBox("Feature Name:", "Start New Feature"); if (string.IsNullOrEmpty(featureName)) return; @@ -137,10 +123,10 @@ private void StartFeatureGitHubCommand(object sender, EventArgs e) * 2. Pull latest changes on master * 3. Create and switch to a new branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand("checkout master") + FormatCliCommand("pull") + FormatCliCommand($"checkout -b {featureName} master", false), @@ -150,11 +136,10 @@ private void StartFeatureGitHubCommand(object sender, EventArgs e) private void FinishFeatureCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - var featureBranch = _gitHelper.GetCurrentBranchName(false); - var featureName = _gitHelper.GetCurrentBranchName(true); - var flowOptions = _gitHelper.GetFlowOptions(); + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; + var featureBranch = GitHelper.GetCurrentBranchName(false); + var featureName = GitHelper.GetCurrentBranchName(true); + var flowOptions = GitHelper.GetFlowOptions(); /* 1. Switch to the develop branch * 2. Pull latest changes on develop @@ -163,10 +148,10 @@ private void FinishFeatureCommand(object sender, EventArgs e) * 5. Delete the local feature branch * 6. Delete the remote feature branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"checkout {flowOptions.DevelopBranch}") + FormatCliCommand("pull") + FormatCliCommand($"merge --no-ff {featureBranch}") + @@ -183,10 +168,9 @@ private string FormatCliCommand(string gitCommand, bool appendNextLine = true) private void FinishFeatureGitHubCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - var featureBranch = _gitHelper.GetCurrentBranchName(false); - var featureName = _gitHelper.GetCurrentBranchName(true); + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; + var featureBranch = GitHelper.GetCurrentBranchName(false); + var featureName = GitHelper.GetCurrentBranchName(true); /* 1. Switch to the master branch * 2. Pull latest changes on master @@ -195,10 +179,10 @@ private void FinishFeatureGitHubCommand(object sender, EventArgs e) * 5. Delete the local feature branch * 6. Delete the remote feature branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand("checkout master") + FormatCliCommand("pull") + FormatCliCommand($"merge --no-ff {featureBranch}") + @@ -209,21 +193,20 @@ private void FinishFeatureGitHubCommand(object sender, EventArgs e) private void StartReleaseCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; var releaseVersion = Interaction.InputBox("Release Version:", "Start New Release"); if (string.IsNullOrEmpty(releaseVersion)) return; - var flowOptions = _gitHelper.GetFlowOptions(); + var flowOptions = GitHelper.GetFlowOptions(); /* 1. Switch to the develop branch * 2. Pull latest changes on develop * 3. Create and switch to a new release branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"checkout {flowOptions.DevelopBranch}") + FormatCliCommand("pull") + FormatCliCommand($"checkout -b {flowOptions.ReleasePrefix}{releaseVersion} {flowOptions.DevelopBranch}", false), @@ -233,11 +216,10 @@ private void StartReleaseCommand(object sender, EventArgs e) private void FinishReleaseCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - var releaseBranch = _gitHelper.GetCurrentBranchName(false); - var releaseName = _gitHelper.GetCurrentBranchName(true); - var flowOptions = _gitHelper.GetFlowOptions(); + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; + var releaseBranch = GitHelper.GetCurrentBranchName(false); + var releaseName = GitHelper.GetCurrentBranchName(true); + var flowOptions = GitHelper.GetFlowOptions(); /* 1. Switch to the master branch * 2. Pull latest changes on master @@ -252,10 +234,10 @@ private void FinishReleaseCommand(object sender, EventArgs e) * 11. Delete the local release branch * 12. Delete the remote release branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"checkout {flowOptions.MasterBranch}") + FormatCliCommand("pull") + FormatCliCommand($"merge --no-ff {releaseBranch}") + @@ -273,21 +255,20 @@ private void FinishReleaseCommand(object sender, EventArgs e) private void StartHotfixCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; var hotfixVersion = Interaction.InputBox("Hotfix Version:", "Start New Hotfix"); if (string.IsNullOrEmpty(hotfixVersion)) return; - var flowOptions = _gitHelper.GetFlowOptions(); + var flowOptions = GitHelper.GetFlowOptions(); /* 1. Switch to the master branch * 2. Pull latest changes on master * 3. Create and switch to a new hotfix branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"checkout {flowOptions.MasterBranch}") + FormatCliCommand("pull") + FormatCliCommand($"checkout -b {flowOptions.HotfixPrefix}{hotfixVersion} {flowOptions.MasterBranch}", false), @@ -297,11 +278,10 @@ private void StartHotfixCommand(object sender, EventArgs e) private void FinishHotfixCommand(object sender, EventArgs e) { - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - var hotfixBranch = _gitHelper.GetCurrentBranchName(false); - var hotfixName = _gitHelper.GetCurrentBranchName(true); - var flowOptions = _gitHelper.GetFlowOptions(); + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; + var hotfixBranch = GitHelper.GetCurrentBranchName(false); + var hotfixName = GitHelper.GetCurrentBranchName(true); + var flowOptions = GitHelper.GetFlowOptions(); /* 1. Switch to the master branch * 2. Pull latest changes on master @@ -316,10 +296,10 @@ private void FinishHotfixCommand(object sender, EventArgs e) * 11. Delete the local hotfix branch * 12. Delete the remote hotfix branch */ - _processHelper.StartProcessGui( + ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{solutionDir}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"checkout {flowOptions.MasterBranch}") + FormatCliCommand("pull") + FormatCliCommand($"merge --no-ff {hotfixBranch}") + diff --git a/TGit/Commands/MainMenuCommands.cs b/TGit/Commands/MainMenuCommands.cs index 1ddb4c6..e1313f5 100644 --- a/TGit/Commands/MainMenuCommands.cs +++ b/TGit/Commands/MainMenuCommands.cs @@ -1,28 +1,20 @@ using EnvDTE; using SamirBoulema.TGit.Helpers; -using Microsoft.VisualStudio.Shell; using System; namespace SamirBoulema.TGit.Commands { public class MainMenuCommands { - private readonly ProcessHelper _processHelper; private readonly CommandHelper _commandHelper; - private readonly GitHelper _gitHelper; private readonly DTE _dte; private readonly OptionPageGrid _generalOptions; - private readonly OleMenuCommandService _mcs; - public MainMenuCommands(ProcessHelper processHelper, CommandHelper commandHelper, GitHelper gitHelper, - DTE dte, OptionPageGrid generalOptions, OleMenuCommandService mcs) + public MainMenuCommands(CommandHelper commandHelper, DTE dte, OptionPageGrid generalOptions) { - _processHelper = processHelper; _commandHelper = commandHelper; - _gitHelper = gitHelper; _dte = dte; _generalOptions = generalOptions; - _mcs = mcs; } public void AddCommands() @@ -31,9 +23,6 @@ public void AddCommands() _commandHelper.AddCommand(PullCommand, PkgCmdIDList.Pull); _commandHelper.AddCommand(FetchCommand, PkgCmdIDList.Fetch); _commandHelper.AddCommand(CommitCommand, PkgCmdIDList.Commit); - //OleMenuCommand commit = commandHelper.CreateCommand(CommitCommand, PkgCmdIDList.Commit); - //commit.BeforeQueryStatus += Diff_BeforeQueryStatus; - //mcs.AddCommand(commit); _commandHelper.AddCommand(PushCommand, PkgCmdIDList.Push); _commandHelper.AddCommand(ShowLogCommand, PkgCmdIDList.ShowLog); @@ -41,9 +30,9 @@ public void AddCommands() _commandHelper.AddCommand(RepoBrowserCommand, PkgCmdIDList.RepoBrowser); _commandHelper.AddCommand(CreateStashCommand, PkgCmdIDList.CreateStash); - OleMenuCommand applyStash = _commandHelper.CreateCommand(ApplyStashCommand, PkgCmdIDList.ApplyStash); + var applyStash = _commandHelper.CreateCommand(ApplyStashCommand, PkgCmdIDList.ApplyStash); applyStash.BeforeQueryStatus += _commandHelper.ApplyStash_BeforeQueryStatus; - _mcs.AddCommand(applyStash); + _commandHelper.AddCommand(applyStash); _commandHelper.AddCommand(BranchCommand, PkgCmdIDList.Branch); _commandHelper.AddCommand(SwitchCommand, PkgCmdIDList.Switch); @@ -55,120 +44,103 @@ public void AddCommands() _commandHelper.AddCommand(CleanupCommand, PkgCmdIDList.Cleanup); } - private void ShowChangesCommand(object sender, EventArgs e) + private void PreCommand() { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; + EnvHelper.GetFlowOptions(); + EnvHelper.GetBranchName(); + EnvHelper.GetStash(); FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:repostatus /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); + } + + private void ShowChangesCommand(object sender, EventArgs e) + { + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:repostatus /path:\"{EnvHelper.SolutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void PullCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:pull /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:pull /path:\"{EnvHelper.SolutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void FetchCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:fetch /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:fetch /path:\"{EnvHelper.SolutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void CommitCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc( - $"/command:commit /path:\"{solutionDir}\" /logmsg:\"{_gitHelper.GetCommitMessage(_generalOptions.CommitMessage, _dte)}\" /closeonend:{_generalOptions.CloseOnEnd}"); + PreCommand(); + ProcessHelper.StartTortoiseGitProc( + $"/command:commit /path:\"{EnvHelper.SolutionDir}\" /logmsg:\"{GitHelper.GetCommitMessage(_generalOptions.CommitMessage, _dte)}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void PushCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:push /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:push /path:\"{EnvHelper.SolutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void ShowLogCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:log /path:\"{solutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:log /path:\"{EnvHelper.SolutionDir}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void DiskBrowserCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.Start(solutionDir); + PreCommand(); + ProcessHelper.Start(EnvHelper.SolutionDir); } private void RepoBrowserCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:repobrowser /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:repobrowser /path:\"{EnvHelper.SolutionDir}\""); } private void CreateStashCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:stashsave /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:stashsave /path:\"{EnvHelper.SolutionDir}\""); } private void ApplyStashCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:reflog /ref:refs/stash /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:reflog /ref:refs/stash /path:\"{EnvHelper.SolutionDir}\""); } private void BranchCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:branch /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:branch /path:\"{EnvHelper.SolutionDir}\""); } + private void SwitchCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:switch /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:switch /path:\"{EnvHelper.SolutionDir}\""); } + private void MergeCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:merge /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:merge /path:\"{EnvHelper.SolutionDir}\""); } private void RevertCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - FileHelper.SaveAllFiles(_dte); - _processHelper.StartTortoiseGitProc($"/command:revert /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:revert /path:\"{EnvHelper.SolutionDir}\""); } private void CleanupCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:cleanup /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:cleanup /path:\"{EnvHelper.SolutionDir}\""); } private void ResolveCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:resolve /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:resolve /path:\"{EnvHelper.SolutionDir}\""); } private void SyncCommand(object sender, EventArgs e) { - string solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:sync /path:\"{solutionDir}\""); + PreCommand(); + ProcessHelper.StartTortoiseGitProc($"/command:sync /path:\"{EnvHelper.SolutionDir}\""); } } } diff --git a/TGit/Credentials.cs b/TGit/Credentials.cs index b40a1db..afbbc4d 100644 --- a/TGit/Credentials.cs +++ b/TGit/Credentials.cs @@ -1,5 +1,4 @@ -using EnvDTE; -using SamirBoulema.TGit.Helpers; +using SamirBoulema.TGit.Helpers; using System; using System.Windows.Forms; @@ -7,12 +6,9 @@ namespace SamirBoulema.TGit { public partial class Credentials : Form { - private readonly ProcessHelper _processHelper; - - public Credentials(DTE dte) + public Credentials() { InitializeComponent(); - _processHelper = new ProcessHelper(dte); emailTextBox.KeyDown += EmailTextBox_KeyDown; } @@ -31,8 +27,8 @@ private void cancelButton_Click(object sender, EventArgs e) private void okButton_Click(object sender, EventArgs e) { - _processHelper.StartProcessGit(string.Format("config user.name \"{0}\"", nameTextBox.Text)); - _processHelper.StartProcessGit(string.Format("config user.email \"{0}\"", emailTextBox.Text)); + ProcessHelper.StartProcessGit($"config user.name \"{nameTextBox.Text}\""); + ProcessHelper.StartProcessGit($"config user.email \"{emailTextBox.Text}\""); DialogResult = DialogResult.OK; Close(); } diff --git a/TGit/FlowOptions.cs b/TGit/FlowOptions.cs index b349f0b..3ea2d0d 100644 --- a/TGit/FlowOptions.cs +++ b/TGit/FlowOptions.cs @@ -1,4 +1,6 @@ -using System.Windows.Forms; +using System; +using System.Linq; +using System.Windows.Forms; namespace SamirBoulema.TGit { @@ -9,5 +11,43 @@ public class FlowOptions public string FeaturePrefix; public string ReleasePrefix; public string HotfixPrefix; + + public FlowOptions() + { + + } + + public FlowOptions(string input) + { + MasterBranch = string.Empty; + DevelopBranch = string.Empty; + FeaturePrefix = string.Empty; + ReleasePrefix = string.Empty; + HotfixPrefix = string.Empty; + + foreach (var line in input.Split(';')) + { + if (line.StartsWith("gitflow.branch.master")) + { + MasterBranch = line.Split(' ').Last(); + } + else if (line.StartsWith("gitflow.branch.develop")) + { + DevelopBranch = line.Split(' ').Last(); + } + else if (line.StartsWith("gitflow.prefix.feature")) + { + FeaturePrefix = line.Split(' ').Last(); + } + else if (line.StartsWith("gitflow.prefix.release")) + { + ReleasePrefix = line.Split(' ').Last(); + } + else if (line.StartsWith("gitflow.prefix.hotfix")) + { + HotfixPrefix = line.Split(' ').Last(); + } + } + } } } diff --git a/TGit/Helpers/CommandHelper.cs b/TGit/Helpers/CommandHelper.cs index 8273305..e8e5a99 100644 --- a/TGit/Helpers/CommandHelper.cs +++ b/TGit/Helpers/CommandHelper.cs @@ -6,15 +6,11 @@ namespace SamirBoulema.TGit.Helpers { public class CommandHelper { - private readonly ProcessHelper _processHelper; private readonly OleMenuCommandService _mcs; - private readonly TGitPackage _package; - public CommandHelper(ProcessHelper processHelper, OleMenuCommandService mcs, TGitPackage package) + public CommandHelper(OleMenuCommandService mcs) { - _processHelper = processHelper; _mcs = mcs; - _package = package; } public void AddCommand(EventHandler handler, uint commandId) @@ -22,6 +18,11 @@ public void AddCommand(EventHandler handler, uint commandId) _mcs.AddCommand(CreateCommand(handler, commandId)); } + public void AddCommand(MenuCommand command) + { + _mcs.AddCommand(command); + } + public OleMenuCommand CreateCommand(EventHandler handler, uint commandId) { var menuCommandId = new CommandID(GuidList.GuidTgitCmdSet, (int)commandId); @@ -37,53 +38,45 @@ 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) - { - ((OleMenuCommand)sender).Enabled = _processHelper.StartProcessGit("diff"); + ((OleMenuCommand) sender).Enabled = EnvHelper.HasStash; } public void Feature_BeforeQueryStatus(object sender, EventArgs e) - { - ((OleMenuCommand)sender).Visible = _package.HasSolutionDir() && _package.IsGitFlow; - ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _package.BranchName.StartsWith(_package.FlowOptions.FeaturePrefix); + { + ((OleMenuCommand)sender).Visible = EnvHelper.HasSolutionDir() && EnvHelper.IsGitFlow; + ((OleMenuCommand)sender).Enabled = EnvHelper.HasSolutionDir() && EnvHelper.BranchName.StartsWith(EnvHelper.FlowOptions.FeaturePrefix); } public void Hotfix_BeforeQueryStatus(object sender, EventArgs e) { - ((OleMenuCommand)sender).Visible = _package.HasSolutionDir() && _package.IsGitFlow; - ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _package.BranchName.StartsWith(_package.FlowOptions.HotfixPrefix); + ((OleMenuCommand)sender).Visible = EnvHelper.HasSolutionDir() && EnvHelper.IsGitFlow; + ((OleMenuCommand)sender).Enabled = EnvHelper.HasSolutionDir() && EnvHelper.BranchName.StartsWith(EnvHelper.FlowOptions.HotfixPrefix); } public void Release_BeforeQueryStatus(object sender, EventArgs e) { - ((OleMenuCommand)sender).Visible = _package.HasSolutionDir() && _package.IsGitFlow; - ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _package.BranchName.StartsWith(_package.FlowOptions.ReleasePrefix); + ((OleMenuCommand)sender).Visible = EnvHelper.HasSolutionDir() && EnvHelper.IsGitFlow; + ((OleMenuCommand)sender).Enabled = EnvHelper.HasSolutionDir() && EnvHelper.BranchName.StartsWith(EnvHelper.FlowOptions.ReleasePrefix); } public void Solution_BeforeQueryStatus(object sender, EventArgs e) { - ((OleMenuCommand) sender).Enabled = _package.HasSolutionDir(); + ((OleMenuCommand) sender).Enabled = EnvHelper.HasSolutionDir(); } public void SolutionVisibility_BeforeQueryStatus(object sender, EventArgs e) { - ((OleMenuCommand) sender).Visible = _package.HasSolutionDir(); + ((OleMenuCommand) sender).Visible = EnvHelper.HasSolutionDir(); } public void GitFlow_BeforeQueryStatus(object sender, EventArgs e) { - ((OleMenuCommand) sender).Visible = _package.HasSolutionDir() && _package.IsGitFlow; + ((OleMenuCommand) sender).Visible = EnvHelper.HasSolutionDir() && EnvHelper.IsGitFlow; } public void GitHubFlow_BeforeQueryStatus(object sender, EventArgs e) { - ((OleMenuCommand) sender).Visible = _package.HasSolutionDir() && !_package.IsGitFlow; + ((OleMenuCommand) sender).Visible = EnvHelper.HasSolutionDir() && !EnvHelper.IsGitFlow; } } } diff --git a/TGit/Helpers/EnvHelper.cs b/TGit/Helpers/EnvHelper.cs new file mode 100644 index 0000000..8bd3fcf --- /dev/null +++ b/TGit/Helpers/EnvHelper.cs @@ -0,0 +1,62 @@ +using EnvDTE; + +namespace SamirBoulema.TGit.Helpers +{ + public static class EnvHelper + { + public static FlowOptions FlowOptions; + public static bool IsGitFlow; + public static string BranchName; + public static string SolutionDir; + public static bool HasStash; + public static string TortoiseGitProc; + public static string Git; + + public static void GetFlowOptions() + { + FlowOptions = GitHelper.GetFlowOptions(); + IsGitFlow = !string.IsNullOrEmpty(FlowOptions.MasterBranch); + } + + public static void GetBranchName() + { + BranchName = GitHelper.GetCurrentBranchName(false); + } + + public static void GetSolutionDir(DTE dte) + { + SolutionDir = FileHelper.GetSolutionDir(dte); + } + + public static void GetStash() + { + HasStash = ProcessHelper.StartProcessGit("stash list"); + } + + public static void GetTortoiseGitProc() + { + TortoiseGitProc = FileHelper.GetTortoiseGitProc(); + } + + public static void GetGit() + { + Git = FileHelper.GetMSysGit(); + } + + public static bool HasSolutionDir() + { + return !string.IsNullOrEmpty(SolutionDir); + } + + public static void Clear() + { + FlowOptions = null; + IsGitFlow = false; + BranchName = string.Empty; + SolutionDir = string.Empty; + TortoiseGitProc = string.Empty; + HasStash = false; + Git = string.Empty; + } + } +} diff --git a/TGit/Helpers/GitHelper.cs b/TGit/Helpers/GitHelper.cs index 2250acb..00d232e 100644 --- a/TGit/Helpers/GitHelper.cs +++ b/TGit/Helpers/GitHelper.cs @@ -4,16 +4,9 @@ namespace SamirBoulema.TGit.Helpers { - public class GitHelper + public static class GitHelper { - private readonly ProcessHelper _processHelper; - - public GitHelper(ProcessHelper processHelper) - { - _processHelper = processHelper; - } - - public string GetCommitMessage(string commitMessageTemplate, DTE dte) + public static string GetCommitMessage(string commitMessageTemplate, DTE dte) { var commitMessage = commitMessageTemplate; commitMessage = commitMessage.Replace("$(BranchName)", GetCurrentBranchName(false)); @@ -30,53 +23,45 @@ public string GetCommitMessage(string commitMessageTemplate, DTE dte) return commitMessage; } - public string GetCurrentBranchName(bool trimPrefix) + public static string GetCurrentBranchName(bool trimPrefix) { - var flowOptions = GetFlowOptions(); - var branchName = _processHelper.StartProcessGitResult("symbolic-ref -q --short HEAD"); + var branchName = ProcessHelper.StartProcessGitResult("symbolic-ref -q --short HEAD"); if (branchName == null) return string.Empty; - if (branchName.StartsWith(flowOptions.FeaturePrefix) && trimPrefix) + if (branchName.StartsWith(EnvHelper.FlowOptions.FeaturePrefix) && trimPrefix) { - return branchName.Substring(flowOptions.FeaturePrefix.Length); + return branchName.Substring(EnvHelper.FlowOptions.FeaturePrefix.Length); } - if (branchName.StartsWith(flowOptions.ReleasePrefix) && trimPrefix) + if (branchName.StartsWith(EnvHelper.FlowOptions.ReleasePrefix) && trimPrefix) { - return branchName.Substring(flowOptions.ReleasePrefix.Length); + return branchName.Substring(EnvHelper.FlowOptions.ReleasePrefix.Length); } - if (branchName.StartsWith(flowOptions.HotfixPrefix) && trimPrefix) + if (branchName.StartsWith(EnvHelper.FlowOptions.HotfixPrefix) && trimPrefix) { - return branchName.Substring(flowOptions.HotfixPrefix.Length); + return branchName.Substring(EnvHelper.FlowOptions.HotfixPrefix.Length); } return branchName; } - public string GetSshSetup() + public static string GetSshSetup() { - var remoteOriginPuttyKeyfile = _processHelper.StartProcessGitResult("config --get remote.origin.puttykeyfile"); + var remoteOriginPuttyKeyfile = ProcessHelper.StartProcessGitResult("config --get remote.origin.puttykeyfile"); if (string.IsNullOrEmpty(remoteOriginPuttyKeyfile)) return string.Empty; - _processHelper.Start("pageant", remoteOriginPuttyKeyfile); + ProcessHelper.Start("pageant", remoteOriginPuttyKeyfile); return $"set GIT_SSH={FileHelper.GetTortoiseGitPlink()} && "; } - public FlowOptions GetFlowOptions() + public static FlowOptions GetFlowOptions() { - return new FlowOptions - { - MasterBranch = _processHelper.StartProcessGitResult("config --get gitflow.branch.master"), - DevelopBranch = _processHelper.StartProcessGitResult("config --get gitflow.branch.develop"), - FeaturePrefix = _processHelper.StartProcessGitResult("config --get gitflow.prefix.feature"), - ReleasePrefix = _processHelper.StartProcessGitResult("config --get gitflow.prefix.release"), - HotfixPrefix = _processHelper.StartProcessGitResult("config --get gitflow.prefix.hotfix") - }; + return new FlowOptions(ProcessHelper.StartProcessGitResult("config --get-regexp gitflow")); } - public bool RemoteBranchExists(string branch) + public static bool RemoteBranchExists(string branch) { - return _processHelper.StartProcessGit($"show-ref refs/remotes/origin/{branch}"); + return ProcessHelper.StartProcessGit($"show-ref refs/remotes/origin/{branch}"); } } } diff --git a/TGit/Helpers/ProcessHelper.cs b/TGit/Helpers/ProcessHelper.cs index 8add68c..cb1e8bd 100644 --- a/TGit/Helpers/ProcessHelper.cs +++ b/TGit/Helpers/ProcessHelper.cs @@ -1,5 +1,4 @@ -using EnvDTE; -using System; +using System; using System.Diagnostics; using System.Drawing; using System.Windows.Forms; @@ -7,21 +6,9 @@ namespace SamirBoulema.TGit.Helpers { - public class ProcessHelper + public static class ProcessHelper { - 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; - _tortoiseGitProc = FileHelper.GetTortoiseGitProc(); - _git = FileHelper.GetMSysGit(); - _stopwatch = new Stopwatch(); - } + private static OutputBox _outputBox; /// /// Execute a Git command and return true if output is non-empty @@ -29,10 +16,9 @@ public ProcessHelper(DTE dte) /// Git command to be executed /// Show an alert dialog when error output is non-empty /// True if output is non-empty - public bool StartProcessGit(string commands, bool showAlert = true) + public static bool StartProcessGit(string commands, bool showAlert = true) { - _solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(_solutionDir)) return false; + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return false; var output = string.Empty; var error = string.Empty; @@ -41,7 +27,7 @@ public bool StartProcessGit(string commands, bool showAlert = true) StartInfo = new ProcessStartInfo { FileName = "cmd.exe", - Arguments = $"/c cd /D \"{_solutionDir}\" && \"{_git}\" {commands}", + Arguments = $"/c cd /D \"{EnvHelper.SolutionDir}\" && \"{EnvHelper.Git}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, @@ -68,15 +54,15 @@ public bool StartProcessGit(string commands, bool showAlert = true) return false; } - public void StartTortoiseGitProc(string args) + public static void StartTortoiseGitProc(string args) { try { - Process.Start(_tortoiseGitProc, args); + Process.Start(EnvHelper.TortoiseGitProc, args); } catch (Exception e) { - MessageBox.Show(e.Message, $"{_tortoiseGitProc} not found", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(e.Message, $"{EnvHelper.TortoiseGitProc} not found", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -85,10 +71,9 @@ public void StartTortoiseGitProc(string args) /// /// Git command to be executed /// Git output - public string StartProcessGitResult(string commands) + public static string StartProcessGitResult(string commands) { - _solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(_solutionDir)) return string.Empty; + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return string.Empty; var output = string.Empty; var error = string.Empty; @@ -97,7 +82,7 @@ public string StartProcessGitResult(string commands) StartInfo = new ProcessStartInfo { FileName = "cmd.exe", - Arguments = $"/c cd /D \"{_solutionDir}\" && \"{_git}\" {commands}", + Arguments = $"/c cd /D \"{EnvHelper.SolutionDir}\" && \"{EnvHelper.Git}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, @@ -107,26 +92,26 @@ public string StartProcessGitResult(string commands) proc.Start(); while (!proc.StandardOutput.EndOfStream) { - output += proc.StandardOutput.ReadLine(); + output += proc.StandardOutput.ReadLine() + ";"; } while (!proc.StandardError.EndOfStream) { error += proc.StandardError.ReadLine(); } - return string.IsNullOrEmpty(output) ? error : output; + return string.IsNullOrEmpty(output) ? error : output.TrimEnd(';'); } - public string GitResult(string workingDir, string commands) + public static string GitResult(string workingDir, string commands) { - string output = string.Empty; - string error = string.Empty; + var output = string.Empty; + var error = string.Empty; var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "cmd.exe", - Arguments = $"/c cd /D \"{workingDir}\" && \"{_git}\" {commands}", + Arguments = $"/c cd /D \"{workingDir}\" && \"{EnvHelper.Git}\" {commands}", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, @@ -146,23 +131,23 @@ public string GitResult(string workingDir, string commands) return string.IsNullOrEmpty(output) ? error : output.TrimEnd(','); } - public void Start(string application) + public static void Start(string application) { Process.Start(application); } - public void Start(string application, string arguments) + public static void Start(string application, string arguments) { Process.Start(application, arguments); } - public Process StartProcessGui(string application, string args, string title, string branchName = "", + public static 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")) { - dialogResult = new Credentials(_dte).ShowDialog(); + dialogResult = new Credentials().ShowDialog(); } if (dialogResult == DialogResult.OK) @@ -180,7 +165,7 @@ public Process StartProcessGui(string application, string args, string title, st CreateNoWindow = true, FileName = application, Arguments = args, - WorkingDirectory = _solutionDir + WorkingDirectory = EnvHelper.SolutionDir }, EnableRaisingEvents = true }; @@ -190,7 +175,7 @@ public Process StartProcessGui(string application, string args, string title, st if (outputBox == null) { - _outputBox = new OutputBox(_dte, branchName, options); + _outputBox = new OutputBox(branchName, options); _outputBox.Show(); } else @@ -198,8 +183,6 @@ public Process StartProcessGui(string application, string args, string title, st _outputBox = outputBox; } - _stopwatch.Reset(); - _stopwatch.Start(); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); @@ -218,7 +201,7 @@ public Process StartProcessGui(string application, string args, string title, st return null; } - private void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine) + private static void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine) { if (string.IsNullOrEmpty(outLine.Data)) return; var process = sendingProcess as Process; @@ -240,15 +223,13 @@ private void OutputDataHandler(object sendingProcess, DataReceivedEventArgs outL } } - private void process_Exited(object sender, EventArgs e) + private static void process_Exited(object sender, EventArgs e) { var process = sender as Process; - if (process == null) return; - - _stopwatch.Stop(); + if (process == null) return; var exitCodeText = process.ExitCode == 0 ? "Succes" : "Error"; - var summaryText = $"{Environment.NewLine}{exitCodeText} ({_stopwatch.ElapsedMilliseconds} ms @ {process.StartTime})"; + var summaryText = $"{Environment.NewLine}{exitCodeText} ({(int)(process.ExitTime - process.StartTime).TotalMilliseconds} ms @ {process.ExitTime})"; _outputBox.BeginInvoke((Action) (() => _outputBox.richTextBox.AppendText( summaryText, diff --git a/TGit/OutputBox.cs b/TGit/OutputBox.cs index 14f8e09..637c09d 100644 --- a/TGit/OutputBox.cs +++ b/TGit/OutputBox.cs @@ -9,17 +9,11 @@ namespace SamirBoulema.TGit { public sealed partial class OutputBox : Form { - private readonly DTE _dte; - private readonly ProcessHelper _processHelper; - private readonly GitHelper _gitHelper; private readonly string _branchName; - public OutputBox(DTE dte, string branchName, OptionPageGrid options) + public OutputBox(string branchName, OptionPageGrid options) { InitializeComponent(); - _dte = dte; - _processHelper = new ProcessHelper(dte); - _gitHelper = new GitHelper(_processHelper); _branchName = branchName; richTextBox.TextChanged += textBox_TextChanged; @@ -31,7 +25,7 @@ public OutputBox(DTE dte, string branchName, OptionPageGrid options) } else { - remoteBranchCheckBox.Enabled = new GitHelper(_processHelper).RemoteBranchExists(branchName); + remoteBranchCheckBox.Enabled = GitHelper.RemoteBranchExists(branchName); } if (options != null) @@ -45,14 +39,13 @@ private void okButton_Click(object sender, EventArgs e) { if (localBranchCheckBox.Checked || remoteBranchCheckBox.Checked) { - var process = _processHelper.StartProcessGui( + var process = ProcessHelper.StartProcessGui( "cmd.exe", - $"/c cd \"{FileHelper.GetSolutionDir(_dte)}\" && " + - _gitHelper.GetSshSetup() + + $"/c cd \"{EnvHelper.SolutionDir}\" && " + + GitHelper.GetSshSetup() + FormatCliCommand($"branch -d {_branchName}") + (remoteBranchCheckBox.Checked ? FormatCliCommand($"push origin --delete {_branchName}", false) : "echo."), - "Deleting branches...", - string.Empty, this + "Deleting branches...", string.Empty, this ); process.WaitForExit(); okButton.Click += OkButton_Click_Close; @@ -80,17 +73,15 @@ private static string FormatCliCommand(string gitCommand, bool appendNextLine = private void ResolveButton_Click(object sender, EventArgs e) { okButton_Click(null, null); - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:resolve /path:\"{solutionDir}\""); + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; + ProcessHelper.StartTortoiseGitProc($"/command:resolve /path:\"{EnvHelper.SolutionDir}\""); } private void StashButton_Click(object sender, EventArgs e) { okButton_Click(null, null); - var solutionDir = FileHelper.GetSolutionDir(_dte); - if (string.IsNullOrEmpty(solutionDir)) return; - _processHelper.StartTortoiseGitProc($"/command:repostatus /path:\"{solutionDir}\""); + if (string.IsNullOrEmpty(EnvHelper.SolutionDir)) return; + ProcessHelper.StartTortoiseGitProc($"/command:repostatus /path:\"{EnvHelper.SolutionDir}\""); } private void textBox_TextChanged(object sender, EventArgs e) diff --git a/TGit/TGIT.csproj b/TGit/TGIT.csproj index fb8a1ad..2abf671 100644 --- a/TGit/TGIT.csproj +++ b/TGit/TGIT.csproj @@ -169,7 +169,7 @@ - + @@ -190,6 +190,7 @@ + Component diff --git a/TGit/TGITPackage.cs b/TGit/TGITPackage.cs index 09b8cd7..ffc20bf 100644 --- a/TGit/TGITPackage.cs +++ b/TGit/TGITPackage.cs @@ -18,15 +18,8 @@ namespace SamirBoulema.TGit public sealed class TGitPackage : Package { private DTE _dte; - private ProcessHelper _processHelper; private CommandHelper _commandHelper; - private GitHelper _gitHelper; - private SolutionEvents _events; - public string SolutionDir; - public bool IsGitFlow; - public FlowOptions FlowOptions; - public string BranchName; /// /// Initialization of the package; this method is called right after the package is sited, so this is the place @@ -38,24 +31,22 @@ protected override void Initialize() _dte = (DTE)GetService(typeof(DTE)); var options = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid)); - _processHelper = new ProcessHelper(_dte); - _gitHelper = new GitHelper(_processHelper); _events = _dte.Events.SolutionEvents; _events.Opened += SolutionEvents_Opened; - _events.AfterClosing += _events_AfterClosing; + _events.AfterClosing += SolutionEvents_AfterClosing; // 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); + _commandHelper = new CommandHelper(mcs); - new MainMenuCommands(_processHelper, _commandHelper, _gitHelper, _dte, options, mcs).AddCommands(); + new MainMenuCommands(_commandHelper, _dte, options).AddCommands(); - new ContextMenuCommands(_processHelper, _commandHelper, _gitHelper, _dte, options).AddCommands(); + new ContextMenuCommands(_commandHelper, _dte, options).AddCommands(); - new GitFlowCommands(_processHelper, _commandHelper, _gitHelper, mcs, _dte, options).AddCommands(); + new GitFlowMenuCommands(_commandHelper, options).AddCommands(); // Add all menus var tgitMenu = _commandHelper.CreateCommand(PkgCmdIDList.TGitMenu); @@ -84,24 +75,19 @@ protected override void Initialize() mcs.AddCommand(tgitGitHubFlowMenu); } - private void _events_AfterClosing() + private static void SolutionEvents_AfterClosing() { - SolutionDir = string.Empty; - BranchName = string.Empty; - IsGitFlow = false; + EnvHelper.Clear(); } public void SolutionEvents_Opened() { - SolutionDir = FileHelper.GetSolutionDir(_dte); - IsGitFlow = _processHelper.StartProcessGit("config --get gitflow.branch.master", false); - FlowOptions = _gitHelper.GetFlowOptions(); - BranchName = _gitHelper.GetCurrentBranchName(false); - } - - public bool HasSolutionDir() - { - return !string.IsNullOrEmpty(SolutionDir); - } + EnvHelper.GetTortoiseGitProc(); + EnvHelper.GetGit(); + EnvHelper.GetSolutionDir(_dte); + EnvHelper.GetFlowOptions(); + EnvHelper.GetBranchName(); + EnvHelper.GetStash(); + } } } diff --git a/TGit/VSPackage.resx b/TGit/VSPackage.resx index 7f2a943..9d1a87b 100644 --- a/TGit/VSPackage.resx +++ b/TGit/VSPackage.resx @@ -1,14 +1,4 @@  -