How to use Team Foundation's library to calcul

2020-07-03 06:36发布

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));

标签: c# .net tfs diff
2条回答
Luminary・发光体
2楼-- · 2020-07-03 07:12

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;
        }
查看更多
冷血范
3楼-- · 2020-07-03 07:15

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

查看更多
登录 后发表回答