【问题标题】:JGit BlameCommand keeps returning nullJGit BlameCommand 不断返回 null
【发布时间】:2020-09-06 22:36:05
【问题描述】:

对于我当前的项目,我需要找出哪些用户最后修改了给定文件中的一系列行。为此,我一直在使用 JGit,并在 Kotlin 中编写代码。到目前为止一切顺利,我意识到当我尝试使用 JGit blame 命令时,它会一直返回 null,即使它没有抛出异常。这是我目前拥有的原型函数,它将在感兴趣的范围内逐行打印最后一个提交者的电子邮件(不要介意 LocationInfo 类,它是一个自定义数据类,其中包含我要检查的文件路径和范围):

fun getContributors(location: LocationInfo, git: Git, commitID: ObjectId)  {
    val blamer = BlameCommand(git.repository)
    blamer.setFilePath(location.path.substringAfter(git.repository.directory.parent)) 
    blamer.setStartCommit(commitID)
    val blameResult = blamer.call() // blameResult is always null!
    println("Blaming: ${location.path.substringAfter(git.repository.directory.parent + "/")}")
    for (line in location.range.start..location.range.end) {
        println(blameResult.getSourceCommitter(line).emailAddress)
    }
}

当我在终端导航到存储库时,签出旧提交(与我的代码中的提交 ID 相同)并在相同的文件(与我的代码中的路径相同)上运行 git blame 命令,它确实做到了它的意思并向我提供我需要的信息。这个问题的原因可能是什么?我很确定我也为函数提供了合法的参数。我对 BlameCommand 的处理是否不正确?

【问题讨论】:

  • 我不知道 JGit,但是查看您的代码,我想到了一个小可能性:当您设置文件路径时,substringAfter() 是否会导致前导分隔符,如果是这样,你也试过删除它吗?
  • @gidds 是的,两种选择都试过了,可惜没有最终产品。尝试使用与我在第 6 行的 println 语句中相同的路径

标签: git kotlin jgit blame


【解决方案1】:

您可以将您对该命令的处理与org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BlameCommandTest.java 进行比较,例如here

BlameCommand command = new BlameCommand(db);
command.setFilePath("file.txt");
BlameResult lines = command.call();

因此,请确保 location.path.substringAfter(git.repository.directory.parent) 实际上引用的是文件,而不是文件夹。

【讨论】:

    猜你喜欢
    • 2012-05-11
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2021-06-08
    相关资源
    最近更新 更多