Interrupting a Merge Using JGit

It's pretty simple: how to simulate a command git merge --abortin JGit? I need to “view” conflicts before real merging

+4
source share
1 answer

JGit has no direct equivalent for git merge --abort. This piece of code can serve as a starting point:

// clear the merge state
repository.writeMergeCommitMsg( null );
repository.writeMergeHeads( null );

// reset the index and work directory to HEAD
Git.wrap( repository ).reset().setMode( ResetType.HARD ).call();

It cleans up the merge status files, and then flushes the index and working directory into the contents of the current HEAD commit.

To check if two commits can be merged, you can also use the built-in merge. This will avoid checking for a conflicting commit in the working directory until reset later.

RevCommit parentCommit = mergeCommit.getParent( 0 );
revWalk.parseHeaders( parentCommit );

ResolveMerger merger = ( ResolveMerger )MergeStrategy.RESOLVE.newMerger( repository, true );
merger.setWorkingTreeIterator( new FileTreeIterator( repository ) );
merger.setBase( parentCommit.getTree() );
if( !merger.merge( headCommit, mergeCommit ) ) {
  if( merger.failed() {
    throw new IllegalStateException( "Should not happen with in-core mergers" );
  }
  // inspect merger.getMergeResults() for further details
}
+2
source

Source: https://habr.com/ru/post/1584103/


All Articles