我想知道在从Github API创建某个分支之前已经完成了多少次提交。
例如在我正在做的git cli:git log --no-merges --oneline ${branchHash} | wc -l
,我可以看到这个数字。
从Github API有100个限制,所以如果我有超过100个提交,我无法全部获得。
那个案子有什么解决方案吗?
我写了一个小东西来解决这个问题:
Gist "Easy way to calculate commits count from the GitHub API”。
它基于使用compare
URL的GitHub Commit API,并使用total_commits
字段:
compare_url = '{}/repos/{}/{}/compare/{}...{}'.format(base_url, owner, repo, first_commit, sha)
commit_count = commit_req.json()['total_commits'] + 1
为了避免进行多次查询,您可以使用GraphQL one,类似于this one或this one:它将获取给定分支的所有提交,允许您对它们进行计数。
{
repository(name: "sickvim", owner: "jonathansick") {
ref(qualifiedName: "master") {
target {
... on Commit {
id
history(first: 5) {
pageInfo {
hasNextPage
}
edges {
node {
oid
}...