要合并,可以使用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的尝试
小智 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
| 归档时间: |
|
| 查看次数: |
6879 次 |
| 最近记录: |