我想使用 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/