[英]Get all commits from GitHub using Java API
我想使用Java API從GitHub獲取所有提交。 到目前為止,我設法創建了以下簡單代碼:
import java.io.IOException;
import java.util.List;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class GithubImplTest
{
public void testSomeMethod() throws IOException
{
GitHubClient client = new GitHubClient();
client.setCredentials("sonratestw@gmail.com", "sono");
RepositoryService service = new RepositoryService(client);
List<Repository> repositories = service.getRepositories();
for (int i = 0; i < repositories.size(); i++)
{
Repository get = repositories.get(i);
System.out.println("Repository Name: " + get.getName());
}
}
}
如何從該帳戶將所有提交提交到存儲庫中?
使用您正在使用的Eclipse GitHub Java API ,類CommitService
提供對存儲庫提交的訪問。 可以調用方法getCommits(repository)
來檢索給定存儲庫的所有提交的列表。
用於打印存儲庫的所有提交的示例代碼:
CommitService commitService = new CommitService(client);
for (RepositoryCommit commit : commitService.getCommits(repository)) {
System.out.println(commit.getCommit().getMessage());
}
對於給定的存儲庫,可以使用JGit git.log()
函數( LogCommand
):
public static void main(String[] args) throws IOException, InvalidRefNameException, GitAPIException {
try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
try (Git git = new Git(repository)) {
Iterable<RevCommit> commits = git.log().all().call();
int count = 0;
for (RevCommit commit : commits) {
System.out.println("LogCommit: " + commit);
count++;
}
System.out.println(count);
}
}
}
根據您的Eclipse版本,您將需要jgit-core
maven依賴項 :
<!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.4.1.201607150455-r</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.