问题
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