Programmatically do "Git blame -w" in C #

I need to programmatically get the last author of a specific line in a Git story using 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 a team

git blame <file>

And actually I need

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

Libgit2sharp does not set the -w and does not provide a parameter / parameter to set it. What are my options? Do you know any other library compatible with the -w of the blame command?

+6
source share
3 answers

When I come across similar advanced scripts where git lib doesn't cut it, I just use the start process in the real git command line. It is not sexy, but it is very effective.

+3
source

Perhaps using the NGIT library will help. This is the direct (automatic) port of the java java 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).

+2
source

Unfortunately, libgit2sharp is too slow to extract fault, and using this function is impractical in real-world scenarios. Therefore, it is best to use a Powershell script to use basic superfast native git. And then redirect the result to your application.

 git blame -l -e -c {commit-sha} -- "{file-path}" | where { $_ -match '(?<sha>\w{40})\s+\(<(?<email>[\w\.\-] +@ [\w\-]+\.\w{2,3})>\s+(?<datetime>\d\d\d\d-\d\d-\d\d\s\d\d\:\d\d:\d\d\s-\d\d\d\d)\s+(?<lineNumber>\d+)\)\w*' } | foreach { new-object PSObject –prop @{ Email = $matches['email'];lineNumber = $matches['lineNumber'];dateTime = $matches['dateTime'];Sha = $matches['sha']}} 
0
source

All Articles