如何以编程方式确定哪些项目包含待处理的待办事项?

时间:2014-10-02 20:19:32

标签: visual-studio tfs

我可以查询PendingSet和候选PendingSet,似乎没有办法区分待处理的挂起更改和排除挂起的更改。

我在所包含的更改中包含一个文件,以便通过Visual Studio Team Explorer签入。

使用Tfs dll查询我得到112个待处理的,以及145个CandidatePending:

var tfsServer = Macros.TfsModule.GetTfsServerFromEnvironment();

var tfs =new Macros.TFS(tfsServer,"Development", null);
var vcs= tfs.Tfs.GetService<Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer>();
var searchBase=vcs.GetItem("$/");
//vcs.Dump();
var workspace = vcs.GetWorkspace(Environment.ExpandEnvironmentVariables("%devroot%"));

// vcs.QueryWorkspaces(null,null,null).Dump("workspaces");
var itemSpecs = new []{new ItemSpec(searchBase.ServerItem,RecursionType.Full)};

vcs.QueryPendingSets(itemSpecs, workspace.Name, workspace.OwnerName, false, true)
    .Select(ps=>new {Pending=ps.PendingChanges.Select(pc=>new{ pc.LocalOrServerItem,PendingChange=Util.OnDemand("PendingChange",()=> pc)}), ps.CandidatePendingChanges})
    .First()
    .Dump();

workspace.Dump();

112值与包含+排除在一起。 465值与检测到的add(s)值对齐。

但是我完全无法弄清楚目前包含哪些更改。我已经尝试过Tfs dll查询,EnvDte几个小时。

我可以以编程方式获取当前包含的更改列表(更好的是,更改包含/排除列表)吗?

1 个答案:

答案 0 :(得分:2)

在我看来,您要求的功能(获取/修改包含和排除的更改集)是&#34;待定更改&#34;窗口本身,不是TFS API的一部分。有关如何与&#34;待更改&#34;进行互动的示例,请参阅this post;窗口(部分通过反射)。也许这是进一步探索的重要起点。

修改

我刚刚进一步探讨了这个问题并提出了以下代码。它主要使用接口 Microsoft.TeamFoundation.VersionControl.Controls.PendingChanges.IPendingChangesDataProvider ,它是程序集内部的 Microsoft.TeamFoundation.VersionControl.Controls.dll

using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.TeamFoundation.Controls;
using Microsoft.TeamFoundation.Controls.WPF.TeamExplorer;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace PrettyFlyForANamespace
{
    public class PendingChangesInclusion
    {
        private readonly Action<IList<PendingChange>> _includeChanges;
        private readonly Action<IList<PendingChange>> _excludeChanges;
        private readonly Func<IList<PendingChange>> _getIncludedChanges;
        private readonly Func<IList<PendingChange>> _getExcludedChanges;

        public IList<PendingChange> IncludedChanges
        {
            get
            {
                return _getIncludedChanges();
            }
        }

        public IList<PendingChange> ExcludedChanges
        {
            get
            {
                return _getExcludedChanges();
            }
        }

        public PendingChangesInclusion(ITeamExplorer teamExplorer)
        {
            var pendingChangesPage = (TeamExplorerPageBase)teamExplorer.NavigateToPage(new Guid(TeamExplorerPageIds.PendingChanges), null);

            var model = pendingChangesPage.Model;
            var p = model.GetType().GetProperty("DataProvider", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

            var dataProvider = p.GetValue(model); // IPendingChangesDataProvider is internal;
            var dataProviderType = dataProvider.GetType();

            p = dataProviderType.GetProperty("IncludedChanges", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            var m = p.GetMethod;
            _getIncludedChanges = (Func<IList<PendingChange>>)m.CreateDelegate(typeof(Func<IList<PendingChange>>), dataProvider);

            p = dataProviderType.GetProperty("ExcludedChanges", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            m = p.GetMethod;
            _getExcludedChanges = (Func<IList<PendingChange>>)m.CreateDelegate(typeof(Func<IList<PendingChange>>), dataProvider);

            m = dataProviderType.GetMethod("IncludeChanges", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            _includeChanges = (Action<IList<PendingChange>>)m.CreateDelegate(typeof(Action<IList<PendingChange>>), dataProvider);

            m = dataProviderType.GetMethod("ExcludeChanges", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            _excludeChanges = (Action<IList<PendingChange>>)m.CreateDelegate(typeof(Action<IList<PendingChange>>), dataProvider);
        }

        public void IncludeChanges(IList<PendingChange> changes)
        {
            _includeChanges(changes);
        }

        public void ExcludeChanges(IList<PendingChange> changes)
        {
            _excludeChanges(changes);
        }
    }
}

如果要从VS Package对象中调用上面的代码,可以使用以下代码:

var teamExplorer = (ITeamExplorer)GetService(typeof(ITeamExplorer));
var psi = new PendingChangesInclusion(teamExplorer);

// To see if this works, include all excluded pending changes.
psi.IncludeChanges(psi.ExcludedChanges);

您需要使用&#34;浏览&#34;包含以下程序集。对话框:

  • C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ ReferenceAssemblies \ v2.0 \ Microsoft.TeamFoundation.Client.dll
  • C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ ReferenceAssemblies \ v4.5 \ Microsoft.TeamFoundation.Controls.dll
  • C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ ReferenceAssemblies \ v2.0 \ Microsoft.TeamFoundation.VersionControl.Client.dll