使用JGit中止合并 [英] Abort merge using JGit

查看:125
本文介绍了使用JGit中止合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这非常简单:如何模拟命令 JGit中的git merge --abort?我需要在真正的合并之前预览"冲突

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

推荐答案

在JGit中,git merge --abort没有直接等效项.此代码段可以作为起点:

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();

它清空合并状态文件,然后将索引和工作目录重置为当前HEAD提交的内容.

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
}

这篇关于使用JGit中止合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆