Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

git - Abort merge using JGit

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...