diff --git a/TGit/Commands/ContextMenuCommands.cs b/TGit/Commands/ContextMenuCommands.cs index 61c005f..eef8754 100644 --- a/TGit/Commands/ContextMenuCommands.cs +++ b/TGit/Commands/ContextMenuCommands.cs @@ -46,26 +46,26 @@ public void AddCommands() private void ShowLogContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); _processHelper.StartTortoiseGitProc($"/command:log /path:\"{currentFilePath}\" /closeonend:{_generalOptions.CloseOnEnd}"); } private void DiskBrowserContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _processHelper.Start(currentFilePath); } private void RepoBrowserContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _processHelper.StartTortoiseGitProc($"/command:repobrowser /path:\"{currentFilePath}\""); } private void BlameContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; int currentLineIndex = ((TextDocument)_dte.ActiveDocument.Object(string.Empty)).Selection.CurrentLine; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); @@ -73,42 +73,42 @@ private void BlameContextCommand(object sender, EventArgs e) } private void MergeContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); _processHelper.StartTortoiseGitProc($"/command:merge /path:\"{currentFilePath}\""); } private void PullContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); _processHelper.StartTortoiseGitProc($"/command:pull /path:\"{currentFilePath}\""); } private void FetchContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); _processHelper.StartTortoiseGitProc($"/command:fetch /path:\"{currentFilePath}\""); } private void CommitContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + 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}"); } private void RevertContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); _processHelper.StartTortoiseGitProc($"/command:revert /path:\"{currentFilePath}\""); } private void DiffContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); _processHelper.StartTortoiseGitProc($"/command:diff /path:\"{currentFilePath}\""); @@ -116,7 +116,7 @@ private void DiffContextCommand(object sender, EventArgs e) private void PrefDiffContextCommand(object sender, EventArgs e) { - string currentFilePath = _dte.ActiveDocument.FullName; + var currentFilePath = _dte.ActiveDocument.FullName; if (string.IsNullOrEmpty(currentFilePath)) return; _dte.ActiveDocument.Save(); diff --git a/TGit/Helpers/CommandHelper.cs b/TGit/Helpers/CommandHelper.cs index 1109e41..280bf0c 100644 --- a/TGit/Helpers/CommandHelper.cs +++ b/TGit/Helpers/CommandHelper.cs @@ -7,14 +7,12 @@ namespace SamirBoulema.TGit.Helpers public class CommandHelper { private readonly ProcessHelper _processHelper; - private readonly GitHelper _gitHelper; private readonly OleMenuCommandService _mcs; private readonly TGitPackage _package; - public CommandHelper(ProcessHelper processHelper, GitHelper gitHelper, OleMenuCommandService mcs, TGitPackage package) + public CommandHelper(ProcessHelper processHelper, OleMenuCommandService mcs, TGitPackage package) { _processHelper = processHelper; - _gitHelper = gitHelper; _mcs = mcs; _package = package; } @@ -50,19 +48,19 @@ private void Diff_BeforeQueryStatus(object sender, EventArgs e) public void Feature_BeforeQueryStatus(object sender, EventArgs e) { ((OleMenuCommand)sender).Visible = _package.HasSolutionDir() && _package.IsGitFlow; - ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _gitHelper.IsFeatureBranch(); + ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _package.BranchName.StartsWith(_package.FlowOptions.FeaturePrefix); } public void Hotfix_BeforeQueryStatus(object sender, EventArgs e) { ((OleMenuCommand)sender).Visible = _package.HasSolutionDir() && _package.IsGitFlow; - ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _gitHelper.IsHotfixBranch(); + ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _package.BranchName.StartsWith(_package.FlowOptions.HotfixPrefix); } public void Release_BeforeQueryStatus(object sender, EventArgs e) { ((OleMenuCommand)sender).Visible = _package.HasSolutionDir() && _package.IsGitFlow; - ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _gitHelper.IsReleaseBranch(); + ((OleMenuCommand)sender).Enabled = _package.HasSolutionDir() && _package.BranchName.StartsWith(_package.FlowOptions.ReleasePrefix); } public void Solution_BeforeQueryStatus(object sender, EventArgs e) diff --git a/TGit/Helpers/GitHelper.cs b/TGit/Helpers/GitHelper.cs index 3b096e3..ad9542f 100644 --- a/TGit/Helpers/GitHelper.cs +++ b/TGit/Helpers/GitHelper.cs @@ -17,20 +17,11 @@ public GitHelper(FileHelper fileHelper, ProcessHelper processHelper) public string GetCommitMessage(string commitMessageTemplate, DTE dte) { - //var projectDir = _dte.Solution.Projects.Item(1).FullName; - //var projectFileName = _dte.Solution.Projects.Item(1).FileName; - - string commitMessage = commitMessageTemplate; + var commitMessage = commitMessageTemplate; commitMessage = commitMessage.Replace("$(BranchName)", GetCurrentBranchName(false)); commitMessage = commitMessage.Replace("$(FeatureName)", GetCurrentBranchName(true)); commitMessage = commitMessage.Replace("$(Configuration)", dte.Solution.SolutionBuild.ActiveConfiguration?.Name); - //commitMessage = commitMessage.Replace("$(Platform)", (string)_dte.Solution.Projects.Item(1).ConfigurationManager.PlatformNames); commitMessage = commitMessage.Replace("$(DevEnvDir)", (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7\\", dte.Version, "")); - //commitMessage = commitMessage.Replace("$(ProjectDir)", Path.GetDirectoryName(_dte.Solution.Projects.Item(1).FullName)); - //commitMessage = commitMessage.Replace("$(ProjectPath)", Path.GetFullPath(_dte.Solution.Projects.Item(1).FullName)); - //commitMessage = commitMessage.Replace("$(ProjectName)", _dte.Solution.Projects.Item(1).FullName); - //commitMessage = commitMessage.Replace("$(ProjectFileName)", _dte.Solution.Projects.Item(1).FileName); - //commitMessage = commitMessage.Replace("$(ProjectExt)", Path.GetExtension(_dte.Solution.Projects.Item(1).FileName)); commitMessage = commitMessage.Replace("$(SolutionDir)", Path.GetDirectoryName(dte.Solution.FullName)); commitMessage = commitMessage.Replace("$(SolutionPath)", Path.GetFullPath(dte.Solution.FullName)); commitMessage = commitMessage.Replace("$(SolutionName)", dte.Solution.FullName); @@ -64,16 +55,6 @@ public string GetCurrentBranchName(bool trimPrefix) return branchName; } - public bool IsGitFlow() - { - return _processHelper.StartProcessGit("config --get gitflow.branch.master", false); - } - - public bool IsGitHubFlow() - { - return !IsGitFlow(); - } - public string GetSshSetup() { var remoteOriginPuttyKeyfile = _processHelper.StartProcessGitResult("config --get remote.origin.puttykeyfile"); @@ -95,26 +76,6 @@ public FlowOptions GetFlowOptions() }; } - public string GetOption(string option) - { - return _processHelper.StartProcessGitResult($"config --get {option}"); - } - - public bool IsFeatureBranch() - { - return GetCurrentBranchName(false).StartsWith(GetOption("gitflow.prefix.feature")); - } - - public bool IsHotfixBranch() - { - return GetCurrentBranchName(false).StartsWith(GetOption("gitflow.prefix.hotfix")); - } - - public bool IsReleaseBranch() - { - return GetCurrentBranchName(false).StartsWith(GetOption("gitflow.prefix.release")); - } - public bool RemoteBranchExists(string branch) { return _processHelper.StartProcessGit($"show-ref refs/remotes/origin/{branch}"); diff --git a/TGit/TGITPackage.cs b/TGit/TGITPackage.cs index 573cead..084a902 100644 --- a/TGit/TGITPackage.cs +++ b/TGit/TGITPackage.cs @@ -24,8 +24,11 @@ public sealed class TGitPackage : Package private GitHelper _gitHelper; private SolutionEvents _events; + private WindowEvents _windowEvents; 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 @@ -43,12 +46,16 @@ protected override void Initialize() _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, _gitHelper, mcs, this); + _commandHelper = new CommandHelper(_processHelper, mcs, this); new MainMenuCommands(_processHelper, _commandHelper, _gitHelper, _fileHelper, _dte, generalOptions, mcs).AddCommands(); @@ -83,10 +90,24 @@ protected override void Initialize() mcs.AddCommand(tgitGitHubFlowMenu); } + private void WindowEvents_WindowActivated(Window GotFocus, Window LostFocus) + { + SolutionEvents_Opened(); + } + + private void _events_AfterClosing() + { + SolutionDir = string.Empty; + BranchName = string.Empty; + IsGitFlow = false; + } + private void SolutionEvents_Opened() { SolutionDir = _fileHelper.GetSolutionDir(); IsGitFlow = _processHelper.StartProcessGit("config --get gitflow.branch.master", false); + FlowOptions = _gitHelper.GetFlowOptions(); + BranchName = _gitHelper.GetCurrentBranchName(false); } public bool HasSolutionDir() diff --git a/TGit/source.extension.vsixmanifest b/TGit/source.extension.vsixmanifest index b7cc120..bf21faf 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