如何在JGit中合并?

Tow*_*wer 11 java git jgit

如何合并JGit?

假设我想masterfoo分支合并,我该怎么做?

Vin*_*nce 7

要合并,可以使用MergeCommand(在包org.eclipse.jgit.api中)CheckoutCommand.为您提供一个示例,因为Jgit确实缺少示例:

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
}
Run Code Online (Sandbox Code Playgroud)

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

  • 我正在使用的类 org.eclipse.jgit.api.MergeCommand 没有对公共方法 include(String) 进行 decalre。有 3 个名为“include”的公共方法,但没有一个只接受一个字符串。 (2认同)

小智 5

传递 ObjectId 对我来说很方便,例如

void merge(String uri,File file){
    try(Git git = Git.cloneRepository()
                     .setURI(uri)
                     .setBranch("master")
                     .setDirectory(file)
                     .call()){
        ObjectId objectId = git.getRepository().resolve("origin/develop");
        MergeResult mergeResult = git.merge().include(objectId).call();
        log.debug(mergeResult.getMergeStatus());
    } catch (GitAPIException | IOException e) {
        log.error("error",e);
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以在这里找到更多示例:https ://github.com/centic9/jgit-cookbook