5

I want to get all commits from GitHub using Java API. So far I managed to create this simple code:

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());
        }
    }
}

How I can get all commits into the repository from this account?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

4

With the Eclipse GitHub Java API you are using, the class CommitService provides access to repository commits. The method getCommits(repository) can be invoked to retrieve the list of all commits for the given repository.

Sample code to print all commits of a repository:

CommitService commitService = new CommitService(client);
for (RepositoryCommit commit : commitService.getCommits(repository)) {
    System.out.println(commit.getCommit().getMessage());
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Do you know how I can get time of commit? – Peter Penzov Jul 31 '16 at 09:24
  • @PeterPenzov Yes, you can use `commit.getCommit().getCommitter().getDate()` (or `commit.getCommit().getAuthor().getDate()` to retrieve the date at which the commit was made ([see here](http://stackoverflow.com/questions/6755824/what-is-the-difference-between-author-and-committer-in-git) the difference between author and committer). – Tunaki Jul 31 '16 at 09:34
  • One last question. Is there a way to get all commits from today or a range of days? – Peter Penzov Jul 31 '16 at 09:35
  • @PeterPenzov Hmm I don't know of a direct way to do that. [Git API itself provides a way](https://developer.github.com/v3/repos/commits/) with the `since` and `until` parameter. Could be possible to subclass `CommitService` and add a new `getCommits` that supports those parameters ([like done here for the others](https://github.com/eclipse/egit-github/blob/v4.4.0.201606070830-r/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/CommitService.java#L134-L156)). Best to ask a separate question if you want to do that. – Tunaki Jul 31 '16 at 10:11
1

For a given repository, you can use JGit git.log() function (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);
        }
    }
}

Depending on your Eclipse version, you would need the 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>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, do you know what maven dependency I need to import? – Peter Penzov Jul 31 '16 at 09:15
  • @PeterPenzov yes: jgit-core: https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit – VonC Jul 31 '16 at 09:16
  • 1
    how did you get the CookbookHelper here ? – Tanmoy Bhattacharjee Feb 26 '18 at 15:48
  • @TanmoyBhattacharjee That is an example from JGit Cookbook: https://github.com/centic9/jgit-cookbook/blob/33b0eaaa5ce9538aaf808a0d9aa4c389973b2cf5/src/main/java/org/dstadler/jgit/porcelain/WalkAllCommits.java#L35-L48. That project has a CookbookHelper class https://github.com/centic9/jgit-cookbook/blob/33b0eaaa5ce9538aaf808a0d9aa4c389973b2cf5/src/main/java/org/dstadler/jgit/helper/CookbookHelper.java#L26 – VonC Feb 26 '18 at 15:56