代码之家  ›  专栏  ›  技术社区  ›  roh

如何比较跨TFS项目的源代码

  •  1
  • roh  · 技术社区  · 17 年前

    我们有许多旧项目最近被添加到了TFS版本控制中。现在,我们面临的问题是,我们的每个项目都包含已相互独立地修改和维护的公共代码的副本。

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

    由于解决方案的大小和性质,目前不可选择重组存储库或对解决方案进行重大重构。

    我还对现有的任何API感兴趣,这些API可能会自动执行这个过程。

    谢谢!

    1 回复  |  直到 17 年前
        1
  •  3
  •   Richard Berg    17 年前

    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/无提示

    ===========================================================================
    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存储每个文件内容的哈希)。快速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倍的代码行。