如何比较TFS项目的源代码

时间:2009-07-23 22:18:00

标签: version-control tfs

我们有许多最近添加到TFS版本控制的遗留项目。现在我们遇到的问题是,我们的每个项目都包含已经相互修改和维护的公共代码的副本。

有没有办法将TFS中的一个解决方案的源与同一TFS服务器上的另一个解决方案中的源进行比较? - 我可以指定要比较的文件夹和文件。

由于解决方案的规模和性质,目前无法重新构建存储库或对解决方案进行重大重构。

我也对任何可能使此过程自动化的API感兴趣。

谢谢!

1 个答案:

答案 0 :(得分:3)

C:\ TEMP> tf folderdiff /?

TF - Team Foundation Version Control Tool
Copyright (c) Microsoft Corporation.  All rights reserved.

Displays a visual representation of the differences between files in two server
folders, in a server folder and a local folder, or in two local folders.

tf folderdiff [sourcePath] targetPath [/recursive] [/noprompt]
              [/server:serverName:port] [/filter:filter]
              [/filterLocalPathsOnly]
              [/view:same,different,sourceOnly,targetOnly]

C:\ temp中> tf folderdiff 1 2 / noprompt

===========================================================================
Items That Exist Only in C:\temp\1
===========================================================================

C:\temp\1\baz

===========================================================================
Items That Exist Only in C:\temp\2
===========================================================================

C:\temp\2\bar

===========================================================================
Show Items That Have Different Contents
===========================================================================

C:\temp\1\quux - 
    C:\temp\2\quux

===========================================================================
Summary: 1 folders, 4 files, 1 source, 1 target, 1 different, 0 with errors
===========================================================================

编辑:如果不明显,则默认视图为sourceOnly + targetOnly + different。我有一个名为“foo”的文件,在两个文件夹中都有相同的内容。

我意识到命令行工具本身不是API,但不幸的是,这是最好的选择。否则,您需要自己爬树 - 版本控制API中没有文件夹差异。所有你得到的是15种令人困惑的方法,可以对各个项目进行操作。

从好的方面来说,如果要检查要比较的所有文件,这意味着您可以在没有任何昂贵的磁盘I / O的情况下进行计算。 (TFS存储每个文件内容的散列)。 Quick Powershell示例:

function Compare-TfsDir([string] $dir1, [string] $dir2, [switch] $includeEqual)
{
    $dir1 = $dir1.ToLower()
    $dir2 = $dir2.ToLower()    
    filter Decorate($dir) 
    { 
        $_ | add-member noteproperty RelativePath $_.ServerItem.ToLower().Replace($dir, "") -passthru |
             add-member noteproperty HashString ($_.HashValue | %{ "{0:X2}" -f $_ } | join-string) -passthru
    }

    $items1 = $tfs.vcs.GetItems($dir1, $tfs.VCS_RecursionType::Full).Items | Decorate $dir1
    $items2 = $tfs.vcs.GetItems($dir2, $tfs.VCS_RecursionType::Full).Items | Decorate $dir2  

    $dirComp = compare -IncludeEqual -Property RelativePath $items1 $items2    
    "---Tree Comparison---"
    $dirComp | ? { $_.SideIndicator -ne "==" } | ft -auto RelativePath, SideIndicator

    $both = $dirComp | ? { $_.SideIndicator -eq "==" } | % { $_.RelativePath } | Linq-ToSet    
    filter InBoth { if ($both.Contains($_.RelativePath)) {$_} }
    $contentComp = compare -inc:$includeEqual -Property HashString, RelativePath `
                           ($items1 | InBoth) ($items2 | InBoth)

    "---Content Comparison---"
    $contentComp | ? { $_.SideIndicator -ne "<=" } | ft -auto RelativePath, SideIndicator
}    

不幸的是,如果您需要支持本地与服务器差异,那么执行此类操作将需要5倍的代码行。