使用java程序或JGIT克隆PR

使用java程序或JGIT克隆PR,java,git,github,bitbucket,jgit,Java,Git,Github,Bitbucket,Jgit,我想使用JavaJGITAPI克隆一个特定的请求。有人对此有想法吗?或者使用java程序克隆拉取请求的任何替代方法 让我们考虑下面的代码来检查或克隆Github中的PR, 1: git clone https://github.com/deepak-kumbhar/spring-boot-logging-example.git 2. cd PROJECT_NAME 3. git fetch origin pull/1/head:pr-1 (Where 1 is number or PR) 4.

我想使用JavaJGITAPI克隆一个特定的请求。有人对此有想法吗?或者使用java程序克隆拉取请求的任何替代方法

让我们考虑下面的代码来检查或克隆Github中的PR,

1: git clone https://github.com/deepak-kumbhar/spring-boot-logging-example.git
2. cd PROJECT_NAME
3. git fetch origin pull/1/head:pr-1 (Where 1 is number or PR)
4. git checkout pr-1 (To activate the PR)
我希望使用JGit实现相同的功能。有人对此有想法吗


提前谢谢

您可以按照中的说明执行此操作

从中提取PR#6的基本步骤如下

System.out.println(“从“+REMOTE_URL+”克隆到“+localPath”);
try(Git result=Git.cloneRepository()
.setURI(远程URL)
.setDirectory(本地路径)
.setProgressMonitor(新的SimpleProgressMonitor())
.call()){
//注意:call()返回一个已打开的存储库,需要关闭该存储库以避免文件句柄泄漏!
System.out.println(“拥有存储库:+result.getRepository().getDirectory());
FetchResult FetchResult=result.fetch()
.setRemote(远程URL)
.SETREF规格(“+refs/pull/6/头:pr_6”)
.call();
System.out.println(“获取PR时的结果:+fetchResult.getMessages());
Ref checkoutRef=result.checkout()
.setName(“pr_6”)
.call();
System.out.println(“签出的PR,现在正在打印日志,它应该包括顶部PR的两个提交”);
Iterable logs=result.log()
.call();
for(RevCommit rev:logs){
System.out.println(“提交:“+rev/*+”,名称:“+rev.getName()+”,id:“+rev.getId().getName()*/”;
}
}
请参阅上的“准备运行”代码段

    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository()
            .setURI(REMOTE_URL)
            .setDirectory(localPath)
            .setProgressMonitor(new SimpleProgressMonitor())
            .call()) {
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        System.out.println("Having repository: " + result.getRepository().getDirectory());

        FetchResult fetchResult = result.fetch()
                .setRemote(REMOTE_URL)
                .setRefSpecs("+refs/pull/6/head:pr_6")
                .call();

        System.out.println("Result when fetching the PR: " + fetchResult.getMessages());

        Ref checkoutRef = result.checkout()
                .setName("pr_6")
                .call();

        System.out.println("Checked out PR, now printing log, it should include two commits from the PR on top");

        Iterable<RevCommit> logs = result.log()
                .call();
        for (RevCommit rev : logs) {
            System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
        }
    }