0

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

Using GUI we get CS316105 for all lines

And using the command line we get CS396066 for lines 6-11

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

Jimmy Lee Jones
  • 785
  • 4
  • 18

1 Answers1

1

I couldn't reproduce your issue. Please make sure you are checking the same version when you use GUI and command line. Try to use command looks like:

tfpt annotate myFile.cs;C123 /noprompt
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Thanks for your response. I'm not quite sure I understand what you meant, but here's what I've tried: `VersionSpec versionSpec = VersionSpec.Latest;` `string versionString = versionSpec.DisplayString; // Returns "T"` in command line: `C:\Work\Trunk>tfpt annotate C:\Work\Trunk\MyFile.py;T` The results are the same – Jimmy Lee Jones Feb 17 '16 at 09:20
  • In addition, I've also tried with the explicit version number from the GUI, e.g. `C:\Work\Trunk>tfpt annotate C:\Work\Trunk\MyFile.py;C429543` – Jimmy Lee Jones Feb 17 '16 at 09:26
  • 1
    If you right click changeset C429543 in History of VS, and select Annotate, would you get the same result as the GUI from command? – Cece Dong - MSFT Feb 18 '16 at 05:40
  • No, I get the exact same results as seen in the attached images: The top image was taken after right click + annotate and the bottom after inputting `C:\Work\Trunk>tfpt annotate C:\Work\Trunk\MyFile.py;C429543` – Jimmy Lee Jones Feb 18 '16 at 05:57
  • 1
    Weird, still can't reproduce your issue. You were right click+annotate C429543 in VS as well? In VS, if you select line 12, just for an example, you would see blue color highlight the annotation for this line in the left pane. While in GUI of command line, please also check line 12 to see whether the changeset is the same as the blue color highlight in VS. It doesn't make sense that the same line has different annotation in a file with same version. – Cece Dong - MSFT Feb 18 '16 at 08:20
  • I'm positive, this is as strange to me as it is to you. Maybe it has to do with the fact that this is a Python file and the changes made were replacing spaces with tabs? You can see, in the bottom image, that the "problematic" annotated lines are marked with a yellowish color (VS added this, not me), this is probably more than coincidence – Jimmy Lee Jones Feb 18 '16 at 08:33
  • 1
    I could reproduce your scenario in one situation: when I right click Annotate for a file which has a pending change in Source Control Explorer, the lunched UI shows "myFile.cs;C123", but actually this annotation is not for C123, but for the modified file which is not checked in TFS yet. When I run command "tfpt annotate myFile.cs;C123", I get real annotation for C123 in TFS. In order to get same result as command "tfpt annotate myFile.cs;C123", I need to go to History, find changeset C123, then right click "Annotate", this annotation will be the same as command line shows. – Cece Dong - MSFT Feb 23 '16 at 10:02
  • Thanks a lot for the help, though I still can't get it to work propely – Jimmy Lee Jones Feb 24 '16 at 10:57