如何在JGit中合并? [英] How to merge in JGit?

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

问题描述



假设我想将 master 与<$ c $合并c> foo 分支,我该怎么做? 要合并,可以使用 MergeCommand (在org.eclipse.jgit.api包中),位于 CheckoutCommand 之后。为你提供一个例子,因为Jgit确实没有例子:

  Git git = ... //你通过CloneCommand,InitCommand 
//或通过文件系统

CheckoutCommand coCmd = git.checkout();
//命令是api模块的一部分,其中包括类似git的调用
coCmd.setName(master);
coCmd.setCreateBranch(false); //可能不需要,只是为了确保
coCmd.call(); //切换到主分支

MergeCommand mgCmd = git.merge();
mgCmd.include(foo); //foo被认为是对分支的引用
MergeResult res = mgCmd.call(); //实际上做合并

if(res.getMergeStatus()。equals(MergeResult.MergeStatus.CONFLICTING)){
System.out.println(res.getConflicts()。toString( ));
//告知用户他必须处理冲突
}

I没有尝试代码,所以它可能不完美,但它只是提供一个开始。我没有包括进口。使用JGit进行开发意味着需要基于 javadoc


进行大量尝试

How do I merge in JGit?

Let's say I want to merge master with foo branch, how do I do this?

解决方案

To merge, you can use the MergeCommand (in package org.eclipse.jgit.api), after a CheckoutCommand. To provide you with an example, because indeed Jgit lacks examples:

Git git = ... // you get it through a CloneCommand, InitCommand 
              // or through the file system

CheckoutCommand coCmd = git.checkout(); 
// Commands are part of the api module, which include git-like calls
coCmd.setName("master");
coCmd.setCreateBranch(false); // probably not needed, just to make sure
coCmd.call(); // switch to "master" branch

MergeCommand mgCmd = git.merge();
mgCmd.include("foo"); // "foo" is considered as a Ref to a branch
MergeResult res = mgCmd.call(); // actually do the merge

if (res.getMergeStatus().equals(MergeResult.MergeStatus.CONFLICTING)){
   System.out.println(res.getConflicts().toString());
   // inform the user he has to handle the conflicts
}

I did not try the code so it might not be perfect, but it's just to provide a start. And I didn't include the imports. Developing with JGit implies a lot of tries based on the javadoc

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

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