简体   繁体   中英

How to JGit blame a file before commit?

Context: My merge ran into conflicts and I have a file like this example:

Foo.txt (merged)

1
<<<<<< HEAD
2-master
======
2-side
>>>>>> df803849788fde47965b3dc8f07f07d48320ea9c
3

Question: In order to get the developers who actually changed the conflicting lines, how to blame result file (above) prior to the commit? It works for git blame Foo.txt

Problem: I tried to do the following, but the blame is null inside the loop.

MergeResult m = runMerge(aScenario);
BlameCommand blamer = new BlameCommand(git.getRepository());
BufferedReader br =  new BufferedReader(new FileReader(new File(mergedfilepath)));
BlameResult blame = blamer.setFilePath(mergedfilepath).call();
for (int i = 0; (line= br.readLine())!=null ; i++) {
    // the blame at this point is null.
    PersonIdent person = blame.getSourceAuthor(i);
    System.out.println(person.getName() + ": "+ line);
}

I think the source of the traversal should be the result contents.

Starting from there you can loop over the changed regions and ask for the author. For example:

BlameResult blameResult = git.blame().setFilePath( ... ).call();
int size = blameResult.getResultContents().size();
for( int i = 0; i < size; i++ ) {
  System.out.println( blameResult.getSourceAuthor( i ) );
}

At least for lines added to the work directory version of the file, an author named Not Committed Yet is returned.

However, your code should be prepared cope with getSourceAuthor() returning null . The JavaDoc states that the return value may be null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM