繁体   English   中英

使用 java 程序或 JGIT 克隆 PR

[英]Clone PR using java program or JGIT

我想使用 java JGit API 克隆一个特定的拉取请求。 有人对此有想法吗? 或使用 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 的相同功能。 有人对此有想法吗?

提前致谢!

您可以按照https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally中的说明执行此操作

https://github.com/github/testrepo/pull/6/commits拉 PR #6 的基本步骤是

    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() */);
        }
    }

请参阅https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/CheckoutGitHubPullRequest.java的可运行代码段

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM