I'm trying to implement a "blame" mechanism for my team's project. To do this, I've created a program which, through power tools, gets the specific changeset for each line of code in a file:
private readonly Regex changeSetRegex = new Regex(@"^(?<changeset>\d+)(?<codeLine>.*)", RegexOptions.Compiled | RegexOptions.Multiline);
public Changeset GetAnnotations(string filepath, int lineNumber)
{
var versionControlServer = CreateVersionControlServer();
var line = changeSetRegex.Matches(ExecutePowerTools(filepath))[lineNumber];
return versionControlServer.GetChangeset(int.Parse(line.Groups["changeset"].Value), false, false);
}
private static VersionControlServer CreateVersionControlServer()
{
var projectCollection = new TfsTeamProjectCollection(new Uri(myTfsUri));
var versionControlServer = projectCollection.GetService<VersionControlServer>();
return versionControlServer;
}
private static string ExecutePowerTools(string filepath)
{
string TfptLocation = @"..\..\Dependencies\Microsoft Team Foundation Server 2013 Power Tools\TFPT.EXE";
string bla = string.Format(@"annotate /noprompt {0}", filepath);
var startInfo = new ProcessStartInfo();
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.FileName = TfptLocation;
startInfo.Arguments = bla;
using (var process = Process.Start(startInfo))
{
StringBuilder sb = new StringBuilder();
while (!process.HasExited)
{
String s = process.StandardOutput.ReadToEnd();
sb.Append(s);
}
return sb.ToString();
}
}
Now, something very strange happens - In some files, when I view the annotations using the GUI I get different changesets for the same line and, accordingly, different blames, from the ones I get using the above code. I can reproduce this difference when I use the "tfpt annotate" command (which is essentially what the code does).
It might be worth mentioning that the changes in this case are minor - changing from spaces to tabs. Perhaps there is a way for VS to recognize this and not display it in the GUI, but this is purely speculative