0


I need to get authors of methods list.
I've tried to do it by comparing methods name with patches content by using LibGit2Sharp, but it's too long, there are to many commits.
I wanted to do it quickly, somehow like code lens.
Now I'm trying to get it by own VS plugin, may be there are some easyer ways.
Thanks.

FeniXVRN
  • 1
  • 1

2 Answers2

0

Take a look at git blame filename output. It seems LibGit2Sharp supports this. You can run it for specific method if you know the line numbers where method starts and ends e.g. by running git blame -L 1,10 filename you will see who was the last changing lines 1-10 in a file.

Alexey Andrushkevich
  • 5,992
  • 2
  • 34
  • 49
  • Blame doesn't work perfect. Sometimes it can't get initial commit. – FeniXVRN Jun 05 '15 at 16:01
  • Unfortunately I don't have VS 2013 Ultimate installed so can't experiment with codelens functionality to see how exactly it finds the author of a method. The only assumption I have is that it runs `git blame` for specific lines of file, then look for a most popular editor of those lines. – Alexey Andrushkevich Jun 05 '15 at 16:22
  • Thanks for idea, I've found that git log --follow is more correct :) – FeniXVRN Jun 08 '15 at 07:54
0

There is a code sample of searching by blame:

var blames = repo.Blame(parameters.FilePath.Replace(_repository, ""));
var commitDate = DateTime.Now;
foreach (var blame in blames)
{
    if (blame.InitialCommit.Sha != (blame.InitialCommit.Parents.FirstOrDefault() == null ? blame.InitialCommit.Sha : blame.InitialCommit.Parents.First().Sha) &&
        (!blame.InitialCommit.Message.ToLower().Contains("merge")))
    {
        Tree commitTree1 = repo.Lookup<LibGit2Sharp.Commit>(blame.InitialCommit.Sha).Tree;
        Tree commitTree2 = repo.Lookup<LibGit2Sharp.Commit>(blame.InitialCommit.Parents.FirstOrDefault() == null ? blame.InitialCommit.Sha : blame.InitialCommit.Parents.First().Sha).Tree;
        string content = repo.Diff.Compare<Patch>(commitTree1, commitTree2).Content;
        if (content.Contains(parameters.TestName))
        {
            if (commitDate.CompareTo(blame.InitialSignature.When.Date) > 0)
            {
                commitDate = blame.InitialSignature.When.Date;
                author = string.Format("Email: {0}; Name: {1}", blame.InitialCommit.Author.Email, blame.InitialCommit.Author.Name);
            }
        }
    }
}
FeniXVRN
  • 1
  • 1