[使用Java程序或JGIT克隆PR

问题描述 投票:0回答:1

我想使用Java JGit API克隆特定的请求请求。有人对此有想法吗?或使用Java程序克隆Pull请求的任何替代方法。

下面考虑一下从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的相同功能。有人对此有想法吗?

提前感谢!

java git github bitbucket jgit
1个回答
0
投票

您可以按照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的现成代码段

© www.soinside.com 2019 - 2024. All rights reserved.