通过API在GitHub存储库中查找最早的提交

问题描述:

确定何时在GitHub存储库中进行首次提交的最有效方法是什么?存储库具有created_at属性,但是对于包含导入历史记录的存储库,最早的提交可能会明显更旧.

What is the most efficient way to determine when the initial commit in a GitHub repository was made? Repositories have a created_at property, but for repositories that contain imported history the oldest commit may be significantly older.

使用命令行时,类似这样的方法将起作用:

When using the command line something like this would work:

git rev-list --max-parents=0 HEAD

但是,我在GitHub API中看不到等效项.

However I don't see an equivalent in the GitHub API.

如果数据已经缓存(在GitHub上)并且取决于您的精度要求,那么只需两个请求即可完成.

This can be done in as few as two requests, if data is already cached (on GitHub's side) and depending on your precision requirements.

首先通过创建GET/repos/:owner/:repo/commits并将until参数设置为创建时间(如VonC的答案所建议)并限制返回的数量,首先检查创建时间之前是否确实存在提交到1次提交(通过per_page参数).

First check to see if there are in fact commits before the creation time by doing a GET for /repos/:owner/:repo/commits with the until parameter set to the creation time (as suggested by VonC's answer) and limiting the number returned to 1 commit (via the per_page parameter).

如果在创建时间之前有提交,则贡献者统计终结点(/repos/:owner/:repo/stats/contributors)可以被调用.响应的每个贡献者都有一个weeks列表,最早的w值与最早的提交在同一星期.

If there are commits before the creation time, then the contributors statistics endpoint (/repos/:owner/:repo/stats/contributors) can be invoked. The response has a weeks list per contributor, and the oldest w value there is the same week as the oldest commit.

如果需要精确的时间戳,则可以再次使用提交列表终结点,并将untilsince设置为最早的一周值之后的7天.

If you need a precise timestamp, you can then use the commits listing endpoint again with until and since set to the 7 days after the oldest week value.

请注意,统计信息端点可能会返回202,指示统计信息不可用,在这种情况下,需要在几秒钟后重试.

Note that the statistics endpoint may return a 202 indicating that statistics are not available, in which case a retry in a few seconds is required.