How to use Team Foundation's library to calculate unified diff?

我是研究僧i 提交于 2019-12-09 14:31:23

问题


I want to calculate a unified diff comparing two documents. (The diff is to go in an email, and Wikipedia says unified diff is the best plain text diff format.)

Team Foundation has a command line interface do that

> tf diff /format:unified alice.txt bob.txt
- Alice started to her feet,
+ Bob started to her feet,

(Example files at https://gist.github.com/hickford/5656513)

Brilliant, but I'd rather use a library than start an external process, for the usual reasons.

Searching MSDN, I found Team Foundation has a .NET library Microsoft.TeamFoundation.VersionControl. However, the documentation didn't give any examples of calculating a diff.

How do I calculate a unified diff with the Team Foundation library?


Edit: I tried the method Difference.DiffItems but it didn't work—the file diff.txt was left empty.

var before = @"c:\alice.txt";
var after = @"c:\bob.txt";

var path = @"c:\diff.txt";
using (var w = new StreamWriter(path))
{
    var options = new DiffOptions();
    options.OutputType = DiffOutputType.Unified;
    options.StreamWriter = w;

    Difference.DiffFiles(
    before, FileType.Detect(before, null),
    after, FileType.Detect(after, null),
    options );
}

Console.WriteLine(File.ReadAllText(path));

回答1:


Please try DiffSegment

        var diff = Difference.DiffFiles(
        before, FileType.Detect(before, null),
        after, FileType.Detect(after, null),
        options);

        while (diff != null){                
            //DO What you like with the diff(s)
            diff = diff.Next;
        }



回答2:


Guess it's not possible :( You can only do this from the command line with tf.exe.



来源:https://stackoverflow.com/questions/16612156/how-to-use-team-foundations-library-to-calculate-unified-diff

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