Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TF command-line tool: Compare local sourcecode files with shelved TFS files

I have the full name of the folder on my harddrive where my local files of a project are located. Now I need to get the latest corresponding files of this project from the TFS server.

My aim is to retrieve both versions and compare them (using C#).

What's the best way to get these files via Microsoft's TF command-line tool?

like image 594
RolandK Avatar asked Oct 08 '22 07:10

RolandK


1 Answers

What you're trying to do may already be built-in to tf.exe as the folderdiff command. This will show you the differences between your local source tree and the latest version on the server. For example:

tf folderdiff C:\MyTFSWorkspace\ /recursive

This functionality also exists in the TFS clients in both Visual Studio and Eclipse. Simply browse to the path in Source Control Explorer and select "Compare With..." That said, there are certainly reasons why this would be useful to have outside of

If this isn't quite what you're after though, I would suggest not trying to script tf.exe, but instead using the TFS SDK to talk to the server directly. While it's easy to get the latest version with tf.exe (updating your working folder), it is not easy to download the file to a temporary location for comparison.

Using the TFS SDK is both powerful and fairly straightforward. You should be able to connect to the server and download the temporary files fairly easily. This code snippet is untested, and assumes that your have your a workspace mapping at folderPath that you want to compare with the latest version on the server.

/* Some temporary directory to download the latest versions to, for comparing. */
String tempDir = @"C:\Temp\TFSLatestVersion";

/* Load the workspace information from the local workspace cache */
WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(folderPath);

/* Connect to the server */
TfsTeamProjectCollection projectCollection = new TfsTeamProjectCollection(WorkspaceInfo.ServerUri);
VersionControlServer vc = projectCollection.GetService<VersionControlServer>();

/* "Realize" the cached workspace - open the workspace based on the cached information */
Workspace workspace = vc.GetWorkspace(workspaceInfo);

/* Get the server path for the corresponding local items */
String folderServerPath = workspace.GetServerItemForLocalItem(folderPath);

/* Query all items that exist under the server path */
ItemSet items = vc.QueryItems(new ItemSpec(folderServerPath, RecursionType.Full),
    VersionSpec.Latest,
    DeletedState.NonDeleted,
    ItemType.Any,
    true);

foreach(Item item in items.Items)
{
    /* Figure out the item path relative to the folder we're looking at */
    String relativePath = item.ServerItem.Substring(folderServerPath.Length);

    /* Append the relative path to our folder's local path */
    String downloadPath = Path.Combine(folderPath, relativePath);

    /* Create the directory if necessary */
    String downloadParent = Directory.GetParent(downloadPath).FullName;
    if(! Directory.Exists(downloadParent))
    {
        Directory.CreateDirectory(downloadParent);
    }

    /* Download the item to the local folder */
    item.DownloadFile(downloadPath);
}

/* Launch your compare tool between folderPath and tempDir */
like image 52
Edward Thomson Avatar answered Oct 11 '22 23:10

Edward Thomson