问题
I managed to get a custom button added to the Git history context menu thanks to the help offered here.
I'm continuing work at the same extension and am again stuck. Once the button I've added to the context menu is clicked I need to get a reference to the commit that is selected when it is clicked. The idea is that I then need to grab the code changes associate with that commit.
I've gotten as far as getting a reference to the ActiveWindow which has a caption of "History - master". Which makes me believe I'm close. However, ActiveWindow.Selection is null. So I'm not sure where to go next to get the selected commit.
This is what I'm using to get the ActiveWindow property.
EnvDTE80.DTE2 dte = ServiceProvider.GetService(typeof(DTE)) as EnvDTE80.DTE2;
Anyone know how to grab a reference to the selected commit? Then use that to grab information about the commit including the changed files?
My question looks similar to this one, but for Git instead of TFS.
Thanks in advance for the help!
回答1:
Took forever, but I finally managed to get at the selected commit. It involves reflection because a lot of the types used in the git extension are internal. There's got to be a better way to do this.
The IGitCommit that I'm able to retrieve doesn't have the changes for the commit populated. Hopefully getting the changes that are part of the commit isn't as challenging.
private IGitCommit GetSelectedCommit()
{
ThreadHelper.ThrowIfNotOnUIThread();
EnvDTE80.DTE2 dte = ServiceProvider.GetService(typeof(DTE)) as EnvDTE80.DTE2;
// GUID found via dte.ActiveWindow.ObjectKind
Guid gitHistoryWindowGuid = new Guid("116D2292-E37D-41CD-A077-EBACAC4C8CC4");
IVsUIShell vsUIShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
int toolWindowReturn = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFrameOnly, ref gitHistoryWindowGuid, out IVsWindowFrame vsWindowFrame);
WindowFrame windowFrame = (WindowFrame)vsWindowFrame;
ToolWindowView toolWindowView = (ToolWindowView)windowFrame.FrameView;
// panel is of innaccessible type Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.ContentHostingPanel
// so use base System.Windows.Controls.Grid
Grid contentHostingPanel = (Grid)toolWindowView.Content;
// Type Microsoft.VisualStudio.Platform.WindowManagement.Controls.GenericPaneContentPresenter is internal
// so use base ContentPresenter
ContentPresenter genericPaneContentPresenter = contentHostingPanel.Children[1] as ContentPresenter;
// Microsoft.VisualStudio.TeamFoundation.ToolWindowBase.ToolWindowBaseProxy is innaccessible
// so use base ContentPresenter
ContentPresenter toolWindowBaseProxy = (ContentPresenter)genericPaneContentPresenter.Content;
// Is of type Microsoft.TeamFoundation.Git.Controls.History.HistoryView,
// but this class is defined as internal so using base UserControl.
UserControl historyView = (UserControl)toolWindowBaseProxy.Content;
// Is of type Microsoft.TeamFoundation.Git.Controls.History.HistoryViewModel,
// but this class is defined as internal so using base Microsoft.TeamFoundation.MVVM.ViewModelBase.
ViewModelBase historyViewModel = (ViewModelBase)historyView.DataContext;
// Use reflection to get at properties of internal type HistoryViewModel and ObservableCollection<GitHistoryItem>
object gitHistoryItem = ((IList)historyViewModel.GetType().GetProperty("SelectedItems").GetValue(historyViewModel, null))[0];
IGitCommit gitCommit = (IGitCommit)gitHistoryItem.GetType().GetProperty("Commit").GetValue(gitHistoryItem, null);
return gitCommit;
}
来源:https://stackoverflow.com/questions/62504005/visual-studio-extension-get-reference-to-currently-selected-item-in-git-histor