TF命令行工具:比较本地源代码文件和搁置的TFS文件

Rol*_*ndK 5 c# tfs command-line tf-cli

我在硬盘上有项目的本地文件所在的文件夹的全名。现在,我需要从TFS服务器获取该项目的最新对应文件。

我的目的是检索两个版本并进行比较(使用C#)。

通过Microsoft TF命令行工具获取这些文件的最佳方法是什么?

Edw*_*son 4

您想要执行的操作可能tf.exe已经作为命令内置了folderdiff。这将向您显示本地源代码树和服务器上最新版本之间的差异。例如:

tf folderdiff C:\MyTFSWorkspace\ /recursive
Run Code Online (Sandbox Code Playgroud)

此功能也存在于 Visual Studio 和 Eclipse 中的 TFS 客户端中。只需浏览到源代码管理资源管理器中的路径并选择“与...比较”也就是说,当然有理由说明为什么这在外部是有用的

如果这不完全是您想要的,我建议不要尝试编写脚本tf.exe,而是使用 TFS SDK 直接与服务器对话。虽然使用(更新工作文件夹)获取get最新版本很容易,但将文件下载到临时位置进行比较并不容易。tf.exe

使用 TFS SDK 功能强大且相当简单。您应该能够相当轻松地连接到服务器并下载临时文件。此代码片段未经测试,并假设您有一个工作区映射,folderPath您希望与服务器上的最新版本进行比较。

/* 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 */
Run Code Online (Sandbox Code Playgroud)