【问题标题】:Get all commits from GitHub using Java API使用 Java API 从 GitHub 获取所有提交
【发布时间】:2016-07-31 08:55:17
【问题描述】:

我想使用 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());
        }
    }
}

我如何才能从这个帐户将所有提交提交到存储库?

【问题讨论】:

    标签: java github github-api


    【解决方案1】:

    使用您正在使用的Eclipse GitHub Java API,类CommitService 提供对存储库提交的访问。可以调用方法getCommits(repository) 来检索给定存储库的所有提交列表。

    打印存储库所有提交的示例代码:

    CommitService commitService = new CommitService(client);
    for (RepositoryCommit commit : commitService.getCommits(repository)) {
        System.out.println(commit.getCommit().getMessage());
    }
    

    【讨论】:

    • 你知道我如何获得提交时间吗?
    • @PeterPenzov 是的,您可以使用commit.getCommit().getCommitter().getDate()(或commit.getCommit().getAuthor().getDate() 检索提交的日期(see here 作者和提交者之间的区别)。
    • 最后一个问题。有没有办法从今天或几天内获取所有提交?
    • @PeterPenzov 嗯,我不知道有什么直接的方法可以做到这一点。 Git API itself provides a waysinceuntil 参数。可以继承CommitService 并添加一个支持这些参数的新getCommits (like done here for the others)。如果你想这样做,最好问一个单独的问题。
    【解决方案2】:

    对于给定的存储库,您可以使用 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 dependency:

    <!-- 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>
    

    【讨论】:

    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 2015-01-31
    • 2023-03-03
    • 1970-01-01
    • 2018-01-05
    • 2020-12-31
    • 2017-03-28
    • 2012-12-23
    相关资源
    最近更新 更多