6

当我运行以下示例代码时:

public static void main(String[] args) throws IOException, GitAPIException {
    Git git = new Git(new FileRepository(PATH_TO_REPO);
    BlameCommand blameCommand = git.blame();
    blameCommand.setStartCommit(git.getRepository().resolve("HEAD"));
    blameCommand.setFilePath(PATH_TO_FILE);
    BlameResult blameResult = blameCommand.call();

    Set<Integer> iterations = new HashSet<>();
    Set<Integer> gitSourceLines = new HashSet<>();

    RawText rawText = blameResult.getResultContents();
    for (int i = 0; i < rawText.size(); i++) {
        iterations.add(i);
        gitSourceLines.add(blameResult.getSourceLine(i));
        System.out.println("i = " + i + 
                ", getSourceLine(i) = " + blameResult.getSourceLine(i));
    }
    System.out.println("iterations size: " + iterations.size());
    System.out.println("sourceLines size: " + gitSourceLines.size());
}

该文件的结果如下:

i = 0, getSourceLine(i) = 0
i = 1, getSourceLine(i) = 1
i = 2, getSourceLine(i) = 3
i = 3, getSourceLine(i) = 4
i = 4, getSourceLine(i) = 4
i = 5, getSourceLine(i) = 7
i = 6, getSourceLine(i) = 8
i = 7, getSourceLine(i) = 9
i = 8, getSourceLine(i) = 13
i = 9, getSourceLine(i) = 14
i = 10, getSourceLine(i) = 15
i = 11, getSourceLine(i) = 16
i = 12, getSourceLine(i) = 17
i = 13, getSourceLine(i) = 18
i = 14, getSourceLine(i) = 19
i = 15, getSourceLine(i) = 16
...
...
...
iterations size: 49
sourceLines size: 36

可以注意到,在责备结果中跳过了一些代码行。不仅方法getSourceLine(i)会跳过它们,还有其他方法,例如getSourceCommit(i)getSourceAuthor(i)。我在不同的文件和不同的存储库上尝试了相同的代码,结果总是不可预测的(如上所述)。

我的代码中是否存在故障,或者我得到的结果是否有解释?

PS:我的目标是计算每个修订版有多少行。因此,如果此失败是 JGit 中的错误,我将不胜感激在 java 中执行 git blame 的任何替代解决方案。

4

0 回答 0