How to get conflicting lines with JGit

雨燕双飞 提交于 2019-12-11 06:28:06

问题


I'm developing the application, in which JGit is used.

After a pull, I have conflicting files. I can get it from

List<String> list = git.status().call().getConflicting();

The list contains files in conflict.

I know, that I can get conflicting files from

Map<String, int[][]> conflicts = git.pull().call().getMergeResult().getConflicts();

but it doesn't work if I restart my application. After the restart, I will have an empty map because I'm not able to redo the pull when the repository is in merging state.

How can I get conflicting lines by the name of file via JGit API?


回答1:


You could try to use a ResolveMerger to re-run the merge like so:

ThreeWayMerger merger = StrategyResolve.newMerger( repository, true );
merger.merge( headCommit, fetchedCommit );

Note that the MergeCommand that is called during pull may use a different merge strategy. See MergeCommand ~ line 337 for details. However, make sure to create an in-core merger (the second argument must be true).

With merger.getMergeResults() you should be able to get the conflicting lines.

The whole approach, however, may fail because your work directory is already dirty with conflict markers (<<<<<<<<). Depending on your overall goal, I suggest reconsidering your approach to pull.

If you fetch changes from the upstream repository (without merging immediately) you can dry-run the merge as outlined above as often as necessary. The FetchResult returned by FetchCommand::call() contains information about the commit(s) that were fetched.



来源:https://stackoverflow.com/questions/38792964/how-to-get-conflicting-lines-with-jgit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!