1

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

1 Answers1

2

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.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Sorry, but I can't see the actual difference to my actual solution apart from the fact that I created a variable to hold the the line content. If `...` means the file path this is what my `mergedfilepath` holds. I checked the path of the file and it is correct. I used your piece of code and got the same `null` value. – Alcemir Santos Apr 23 '16 at 11:48
  • At which line is `null` returned? – Rüdiger Herrmann Apr 23 '16 at 12:15
  • I meant, for which line of the file to merge did `getSourceAuthor()` return `null`? – Rüdiger Herrmann Apr 25 '16 at 07:22
  • I can't say that once the variable `blameResult` is `null`, right? – Alcemir Santos Apr 25 '16 at 12:08
  • I am afraid, I don't understand the above comment. Could you please tell, based on the snippet in your question, for which value of `i` does `getSourceAuthor( i )` return `null`? – Rüdiger Herrmann Apr 25 '16 at 15:35