简体   繁体   English

如何使用github Java API(org.eclipse.egit.github。*)搜索给定的提交哈希

[英]How to use github java API (org.eclipse.egit.github.*) to search for a given commit hash

It is possible to receive details regarding a given commit from calling github Search API found in here with providing the relevant commit hash, Now I need to get the same response by using github java API (org.eclipse.egit.github.*) which can be found in here . 它可以接收有关细节给定的调用commit github Search API在发现这里有提供相关的提交哈希值,现在我需要使用相同的响应github java API (org.eclipse.egit.github.*)这可以在这里找到。 According to their documentation of the version 2.1.5 found in here , there is no method in CommitService class to get commit information by providing only the commit hash . 根据他们在此处找到的版本2.1.5的文档, CommitService class没有方法通过仅提供提交哈希来获取提交信息。 Is there a workaround to reach them? 有没有解决方法可以解决这些问题? Thanks in advance 提前致谢

You can use the CommitService.getCommit(IRepositoryIdProvider, String) method, just feed in one more argument, the repository where the commit will be searched. 您可以使用CommitService.getCommit(IRepositoryIdProvider, String)方法,只需再输入一个参数即可搜索提交的存储库。 For example, 例如,

GitHubClient client = new GitHubClient(server).setCredentials(login, token);
RepositoryService repoService = new RepositoryService(client);

// If you know which repository to search (you know the owner and repo name)
Repository repository = repoService.getRepository(owner, repoName);

CommitService commitService = new CommitService(client)
Commit commit1 = commitService.getCommit(repository, sha).getCommit();
System.out.println("Author: " + commit1.getAuthor().getName());
System.out.println("Message: " + commit1.getMessage());
System.out.println("URL: " + commit1.getUrl());

Or you may just loop through each repository returned from the RepositoryService.getRepositories() method, if you don't know which repository to search. 或者,如果您不知道要搜索哪个存储库,则可以遍历从RepositoryService.getRepositories()方法返回的每个存储库。 For example, 例如,

List<Repository> repositories = repoService.getRepositories();
Commit commit2 = null;
for (Repository repo : repositories) {
    try {
        commit2 = commitService.getCommit(repo, sha).getCommit();
        System.out.println("Repo: " + repo.getName());
        System.out.println("Author: " + commit2.getAuthor().getName());
        System.out.println("Message: " + commit2.getMessage());
        System.out.println("URL: " + commit2.getUrl());
        break;
    } catch (RequestException re) {
        if (re.getMessage().endsWith("Not Found (404)")) {
            continue;
        } else {
            throw re;
        }
    }
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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