如何获得从 Github API 到某个分支的提交数

How can I get number of commits from Github API until certain branch

我想知道在从 Github API.

创建某个分支之前完成了多少次提交

例如在 git cli 中我正在做:git log --no-merges --oneline ${branchHash} | wc -l 我可以看到数字。

从 Github API 开始,有 100 个的限制,所以如果我有超过 100 个提交,我将无法全部完成。

这种情况有什么解决办法吗?

为了避免进行多次查询,您可以使用 GraphQL one, similar to this one or this one:它将获取给定分支的所有提交,允许您对它们进行计数。

{
  repository(name: "sickvim", owner: "jonathansick") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          id
          history(first: 5) {
            pageInfo {
              hasNextPage
            }
            edges {
              node {
                oid
              }...

我写了一些东西来解决这个问题:

Gist "Easy way to calculate commits count from the GitHub API”。

它基于使用 compare URL of the 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