Abort merge using JGit

情到浓时终转凉″ 提交于 2019-11-28 04:47:44

问题


This is pretty straightforward: How do I simulate the command git merge --abort in JGit? I need to "preview" the conflicts prior to the real merge


回答1:


There is no direct equivalent for git merge --abort in JGit. This code snippet may 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 empties the merge state files and then resets index and work directory to the contents of the current HEAD commit.

In order to test if two commits can be merged, you could also use an in-core merger. This would avoid checking out the conflicting commit into the work directory just to reset them later on.

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
}


来源:https://stackoverflow.com/questions/29807016/abort-merge-using-jgit

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