Github API:为一个仓库检索所有分支的所有提交 [英] Github API: Retrieve all commits for all branches for a repo

查看:89
本文介绍了Github API:为一个仓库检索所有分支的所有提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据V2文档,您可以使用以下命令列出分支的所有提交:

According to the V2 documentation, you can list all commits for a branch with:

commits/list/:user_id/:repository/:branch

我在V3文档中没有看到相同的功能.

I am not seeing the same functionality in the V3 documentation.

我想使用类似的方式收集所有分支机构:

I would like to collect all branches using something like:

https://api.github.com/repos/:user/:repo/branches

然后遍历它们,并为它们拉取所有提交.另外,如果有一种方法可以直接拉回所有分支的所有提交以进行回购,那将同样有效,即使不是更好.有任何想法吗?

And then iterate through them, pulling all commits for each. Alternatively, if there's a way to pull all commits for all branches for a repo directly, that would work just as well if not better. Any ideas?

更新:我尝试将分支:sha作为参数传递如下:

UPDATE: I tried passing the branch :sha as a param as follows:

params = {:page => 1, :per_page => 100, :sha => b}

问题是当我这样做时,它无法正确分页结果.我觉得我们处理方法不正确.有什么想法吗?

The problem is that when i do this, it doesn't page the results properly. I feel like we're approaching this incorrectly. Any thoughts?

推荐答案

我遇到了完全相同的问题.我确实设法获取了存储库中所有分支的所有提交(由于API的缘故,效率可能不是那样).

I have encountered the exact same problem. I did manage to acquire all the commits for all branches within a repository (probably not that efficient due to the API).

如前所述,首先要收集所有分支:

As you mentioned, first you gather all the branches:

# https://api.github.com/repos/:user/:repo/branches
https://api.github.com/repos/twitter/bootstrap/branches

您缺少的关键是用于获取提交的APIv3使用参考提交(用于在存储库 sha 上列出提交的API调用的参数)来运行.因此,您需要确保在收集分支时还选择了它们的最新sha:

The key that you are missing is that APIv3 for getting commits operates using a reference commit (the parameter for the API call to list commits on a repository sha). So you need to make sure when you collect the branches that you also pick up their latest sha:

[
  {
    "commit": {
      "url": "https://api.github.com/repos/twitter/bootstrap/commits/8b19016c3bec59acb74d95a50efce70af2117382",
      "sha": "8b19016c3bec59acb74d95a50efce70af2117382"
    },
    "name": "gh-pages"
  },
  {
    "commit": {
      "url": "https://api.github.com/repos/twitter/bootstrap/commits/d335adf644b213a5ebc9cee3f37f781ad55194ef",
      "sha": "d335adf644b213a5ebc9cee3f37f781ad55194ef"
    },
    "name": "master"
  }
]

使用最后一次提交的sha

因此,正如我们所看到的,这两个分支具有不同的sha,它们是这些分支上最新的提交sha.您现在可以做的是从每个分支的最新sha中进行迭代:

Working with last commit's sha

So as we see the two branches here have different sha, these are the latest commit sha on those branches. What you can do now is to iterate through each branch from their latest sha:

# With sha parameter of the branch's lastest sha
# https://api.github.com/repos/:user/:repo/commits
https://api.github.com/repos/twitter/bootstrap/commits?per_page=100&sha=d335adf644b213a5ebc9cee3f37f781ad55194ef

因此,上述API调用将列出 twitter/bootstrap master 分支的最后100次提交.使用API​​时,您必须指定下一个提交的sha来获取下一个100个提交.我们可以使用当前示例将最后一次提交的sha(即 7a8d6b19767a92b1c4ea45d88d4eedc2b29bf1fa )用作下一个API调用的输入:

So the above API call will list the last 100 commits of the master branch of twitter/bootstrap. Working with the API you have to specify the next commit's sha to get the next 100 commits. We can use the last commit's sha (which is 7a8d6b19767a92b1c4ea45d88d4eedc2b29bf1fa using the current example) as input for the next API call:

# Next API call for commits (use the last commit's sha)
# https://api.github.com/repos/:user/:repo/commits
https://api.github.com/repos/twitter/bootstrap/commits?per_page=100&sha=7a8d6b19767a92b1c4ea45d88d4eedc2b29bf1fa

重复此过程,直到最后一次提交的sha与API的调用sha参数相同为止.

This process is repeated until the last commit's sha is the same as the API's call sha parameter.

就是一个分支.现在,您将相同的方法应用于其他分支(来自最新sha的工作).

That is it for one branch. Now you apply the same approach for the other branch (work from the latest sha).

这种方法存在很大的问题...由于分支共享一些相同的提交,因此,当您移至另一个分支时,您将一遍又一遍地看到相同的提交.

There is a large issue with this approach... Since branches share some identical commits you will see the same commits over-and-over again as you move to another branch.

我可以想象有一种更有效的方法可以完成此任务,但这对我有用.

I can image that there is a much more efficient way to accomplish this, yet this worked for me.

这篇关于Github API:为一个仓库检索所有分支的所有提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆