java - 使用java程序或JGIT克隆PR

标签 java git github bitbucket jgit

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

让我们考虑下面是从 GitHub checkout 或克隆 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 处的准备运行代码片段

关于java - 使用java程序或JGIT克隆PR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58487458/

相关文章:

java - 切换到 Java 8 时出现比较器问题

java - Cobertura Eclipse 插件本地站点 - 下载为 Zip

java - 启动 Activity android 时聚焦 ListView 中的第一项

git pull 显示 "fatal: Couldn' t find remote ref refs/heads/xxxx"并挂断

git - 如何将其他用户(windows)添加到gitosis

git 不添加除 .gitignore 之外的项目文件

github - 无法使用 dockerfile 克隆私有(private)存储库

git - 在不禁用权威签名证书的情况下添加自签名 SSL 证书

Java Swing JButton/JLabel : icons are not displayed in their original size

Github - 在 fork 私有(private)仓库后删除协作者