Visual Studio扩展-获取对Git历史记录中当前选定项目的引用 [英] Visual Studio extension - get reference to currently selected item in Git history

查看:186
本文介绍了Visual Studio扩展-获取对Git历史记录中当前选定项目的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于提供了我正在同一分机上继续工作,然后再次卡住.单击添加到上下文菜单中的按钮后,我需要获取对单击时选择的提交的引用.我的想法是,然后我需要获取与该提交相关联的代码更改.

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.

我已经获得对ActiveWindow的引用,该窗口的标题为"History-master".这让我相信我已经接近了.但是,ActiveWindow.Selection为null.因此,我不确定下一步该去哪里以获取选定的提交.

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.

这就是我用来获取ActiveWindow属性的方法.

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.

提前感谢您的帮助!

推荐答案

花了很长时间,但我终于设法达到了所选的提交.它涉及反射,因为git扩展中使用的许多类型都是内部类型.必须有一个更好的方法来做到这一点.

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.

我能够检索的IGitCommit没有填充提交的更改.希望获得作为提交的一部分的更改不会那么具有挑战性.

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;
    }

这篇关于Visual Studio扩展-获取对Git历史记录中当前选定项目的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆