Wrong result when using JGit-guilt

When I run the following code example:

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 as follows:

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

You may notice that some lines of code are skipped with the result of the fault. Not only the getSourceLine (i) method skips them, but other methods such as getSourceCommit (i) and getSourceAuthor (i). I tried the same code for different files and different repositories, and the results were always unpredictable (as indicated above).

Is there a failure in my code or is there an explanation of the results that I get?

PS: My goal is to calculate how many lines belong to each revision. Therefore, if this glitch is a bug in JGit, I would appreciate any alternative solutions for doing git guilt in java.

+4

Source: https://habr.com/ru/post/1654258/


All Articles