简体   繁体   English

如何在提交之前将JGit归咎于文件?

[英]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) Foo.txt (合并)

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 它适用于git blame Foo.txt

Problem: I tried to do the following, but the blame is null inside the loop. 问题:我尝试执行以下操作,但null内的错误为null

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. 至少对于添加到该文件的工作目录版本中的行,将返回一个名为Not Committed Yet的作者。

However, your code should be prepared cope with getSourceAuthor() returning null . 但是,应准备好代码,使之与返回null getSourceAuthor()适应。 The JavaDoc states that the return value may be null . JavaDoc声明返回值可以为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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