Sunday, April 10, 2016

Programmatically do “Git blame -w” in C#

Leave a Comment

I need to programmatically get the last author of a specific line in the Git history with C#. I tried using libgit2sharp :

var repo = new LibGit2Sharp.Repository(gitRepositoryPath); string relativePath = MakeRelativeSimple(filename); var blameHunks = repo.Blame(relativePath); // next : find the hunk which overlap the desired line number 

But this is the equivalent of the command

git blame <file>

And in fact I need

git blame -w <file> (to ignore whitespace when comparing)

Libgit2sharp do not set the -w switch and don't provide any parameter/option to set it. What are my options ? Do you know any other library compatible with the -w switch of the blame command ?

2 Answers

Answers 1

Maybe using NGIT library will help. That is direct (automatic) port of java JGIT library. Install via nuget package, then:

    static void Main() {         var git = Git.Init().SetDirectory("C:\\MyGitRepo").Call();                     string relativePath = "MyFolder/MyFile.cs";                     var blameHunks = git.Blame().SetFilePath(relativePath).SetTextComparator(RawTextComparator.WS_IGNORE_ALL).Call();         blameHunks.ComputeAll();         var firstLineCommit = blameHunks.GetSourceCommit(0);         // next : find the hunk which overlap the desired line number         Console.ReadKey();     } 

Note SetTextComparator(RawTextComparator.WS_IGNORE_ALL) part.

Answers 2

When I hit similar advanced scenarios where the git lib isn't cutting it, I just shell out using start process to the real git command line. It's not sexy, but it's mighty effective.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment