7

我需要使用 C# 以编程方式获取 Git 历史中特定行的最后一位作者。我尝试使用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

但这相当于命令

git blame <file>

事实上我需要

git blame -w <file>(比较时忽略空格)

Libgit2sharp不设置-w开关,也不提供任何参数/选项来设置它。我有什么选择?您知道与命令-w开关兼容的任何其他库吗?blame

4

3 回答 3

4

当我遇到 git lib 没有切割它的类似高级场景时,我只是使用 start 进程将其输出到真正的 git 命令行。它不性感,但非常有效。

于 2016-04-06T06:08:09.083 回答
3

也许使用 NGIT 库会有所帮助。那是java JGIT库的直接(自动)端口。通过nuget包安装,然后:

    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();
    }

注意 SetTextComparator(RawTextComparator.WS_IGNORE_ALL) 部分。

于 2016-04-04T09:11:34.387 回答
1

不幸的是,libgit2sharp 在提取责任方面太慢了,并且在实际场景中使用此功能是不切实际的。所以,我认为最好的方法是使用 Powershell 脚本来使用底层的超高速原生 git。然后将结果重定向到您的应用程序。

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']}}
于 2018-01-07T05:32:59.573 回答