简体   繁体   中英

How to read all the commits of a public git repository using EGIT?

I am using EGit APIs to read remote repositories. My use case is to read all the commits of any public git repository. Is it possible with EGit?

Following is my code for reading all the repositories :

GitHubClient client = new GitHubClient();
    client.setCredentials("xxx.xx.xx@gmail.com", "********");

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

And to read the commits I use this:

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

This lists only the repositories that belong to me. But let's say I want to read all the commits from a public repo like https://github.com/eclipse/jgit

Is it possible with EGit?

EGit is the Git integration for Eclipse. To access Git repositories programmatically, use JGit. Your code snippets refer to the GitHub API that is also provided by EGit to examine repositories hosted on GitHub.

To get access to the history of an arbitrary Git repository (not just those on GitHub), you'll need to first clone it. For example:

Git.cloneRepository()
  .setURI( "https://github.com/eclipse/jgit.git" )
  .setDirectory( "path/to/local/repo" )
  .call();

A detailed explanation of how to clone repositories with JGit can be found here: http://www.codeaffine.com/2015/11/30/jgit-clone-repository/

Now you can use the LogCommand to list the commits of the local repository. See here for an example: Retrieving Commit Message Log from Git Using JGit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM