Wrong result when using JGit blame
Asked Answered
I

0

6

When I run the following sample code:

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

the result for this file is the following:

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

One can notice, that some lines of code are skipped at the blame result. Not only the method getSourceLine(i) skips them, but also other methods like getSourceCommit(i) and getSourceAuthor(i). I tried the same code on different files and on different repositories and the results were always unpredictable (as above).

Is there a failure in my code or is there an explanation for the results I'm getting?

P.S.: My aim is to to count how many lines belong to each revision. So if this failure is a bug in JGit, I'd appreciate any alternative solutions to execute git blame in java.

Incorporated answered 11/9, 2016 at 10:47 Comment(1)
As a side note: you shouldn't be using FileRepository directly. See here for more on how to access Git repositories with JGit: codeaffine.com/2014/09/22/access-git-repository-with-jgitUnsatisfactory

© 2022 - 2024 — McMap. All rights reserved.