问题
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