Get Tfs Shelveset file contents at the command prompt?

限于喜欢 提交于 2019-11-29 07:08:38

It is possible to construct a small command-line application that uses the TFS SDK, which returns the list of files contained in a given shelveset.
The sample below assumes knowledge of the Shelveset name & it's owner:

using System;
using System.IO;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace ShelvesetDetails
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri tfsUri = (args.Length < 1) ? new Uri("TFS_URI") : new Uri(args[0]);

            TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);

            ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
                new[] { CatalogResourceTypes.ProjectCollection },
                false, CatalogQueryOptions.None);

            CatalogNode collectionNode = collectionNodes[0];

            Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
            TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);

            var vcServer = teamProjectCollection.GetService<VersionControlServer>();

            Shelveset[] shelves = vcServer.QueryShelvesets(
                "SHELVESET_NAME", "SHELVESET_OWNER");
            Shelveset shelveset = shelves[0];

            PendingSet[] sets = vcServer.QueryShelvedChanges(shelveset);
            foreach (PendingSet set in sets)
            {
                PendingChange[] changes = set.PendingChanges;
                foreach (PendingChange change in changes)
                {
                    Console.WriteLine(change.FileName);
                }
            }
        }
    }
}

Invoking this console app & catching the outcome during execution of the powershell should be possible.

tf status /shelveset:name

will list out the content of the named shelveset (you can also supplier an owner: see tf help status).

With the TFS PowerToy's PowerShell snapin:

Get-TfsPendingChange -Shelveset name

for the same information.

Try:

tfpt review /shelveset:shelvesetName;userName

You may also need to add on the server option so something like:

tfpt review /shelveset:Code Review;jim /sever:company-source

I think this is what you are looking for.

This is what I ended up with, based on pentelif's code and the technique in the article at http://akutz.wordpress.com/2010/11/03/get-msi/ linked in my comment.

function Get-TfsShelvesetItems
{
    [CmdletBinding()]
    param
    (
        [string] $ShelvesetName = $(throw "-ShelvesetName must be specified."),
        [string] $ShelvesetOwner = "$env:USERDOMAIN\$env:USERNAME",
        [string] $ServerUri = $(throw "-ServerUri must be specified."),
        [string] $Collection = $(throw "-Collection must be specified.")
    )

    $getShelvesetItemsClassDefinition = @'
    public IEnumerable<PendingChange> GetShelvesetItems(string shelvesetName, string shelvesetOwner, string tfsUriString, string tfsCollectionName)
    {
        Uri tfsUri = new Uri(tfsUriString);
        TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
        ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren( new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None);
        CatalogNode collectionNode = collectionNodes.Where(node => node.Resource.DisplayName == tfsCollectionName).SingleOrDefault();
        Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
        TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);
        var vcServer = teamProjectCollection.GetService<VersionControlServer>();
        var changes = new List<PendingChange>();
        foreach (Shelveset shelveset in vcServer.QueryShelvesets(shelvesetName, shelvesetOwner))
        {
            foreach (PendingSet set in vcServer.QueryShelvedChanges(shelveset))
            {
                foreach ( PendingChange change in set.PendingChanges )
                {
                    changes.Add(change);
                }
            }
        }
        return changes.Count == 0 ? null : changes;
    }
'@;

    $getShelvesetItemsType = Add-Type `
        -MemberDefinition $getShelvesetItemsClassDefinition `
        -Name "ShelvesetItemsAPI" `
        -Namespace "PowerShellTfs" `
        -Language CSharpVersion3 `
        -UsingNamespace System.IO, `
                        System.Linq, `
                        System.Collections.ObjectModel, `
                        System.Collections.Generic, `
                        Microsoft.TeamFoundation.Client, `
                        Microsoft.TeamFoundation.Framework.Client, `
                        Microsoft.TeamFoundation.Framework.Common, `
                        Microsoft.TeamFoundation.VersionControl.Client `
        -ReferencedAssemblies "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll", `
                                "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Common.dll", `
                                "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.VersionControl.Client.dll" `
        -PassThru;

    # Initialize an instance of the class.
    $getShelvesetItems = New-Object -TypeName "PowerShellTfs.ShelvesetItemsAPI";

    # Emit the pending changes to the pipeline.
    $getShelvesetItems.GetShelvesetItems($ShelvesetName, $ShelvesetOwner, $ServerUri, $Collection);
}
Greg W.

Spent a few days trying to do this as well, this always popped up on google so here is what I found to help future generations:

To get the contents of the shelveset (at least with Team Explorer Everywhere),
use the command: tf difference /shelveset:<Shelveset name>

That will print out the contents of the shelveset and give filenames in the form :
<Changetype>: <server file path>; C<base change number>
Shelved Change: <server file path again>;<shelveset name>

So if your file is contents/test.txt
in the shelveset shelve1 (with base revision 1), you will see :
edit: $/contents/file.txt;C1
Shelved Change: $/contents/file.txt;shelve1

After that, using the tf print command
(or view if not using TEE) on $/contents/file.txt;shelve1 should get you the contents :

tf print $/contents/file.txt;shelve1

Shows you what is in the file.txt in shelveset shelve1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!