如何从Github API获取提交数量直到某个分支

问题描述 投票:5回答:2

我想知道在从Github API创建某个分支之前已经完成了多少次提交。

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

从Github API有100个限制,所以如果我有超过100个提交,我无法全部获得。

那个案子有什么解决方案吗?

git github github-api
2个回答
1
投票

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

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

它基于使用compare URLGitHub Commit API,并使用total_commits字段:

compare_url = '{}/repos/{}/{}/compare/{}...{}'.format(base_url, owner, repo, first_commit, sha)

commit_count = commit_req.json()['total_commits'] + 1

0
投票

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

{
  repository(name: "sickvim", owner: "jonathansick") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          id
          history(first: 5) {
            pageInfo {
              hasNextPage
            }
            edges {
              node {
                oid
              }...
© www.soinside.com 2019 - 2024. All rights reserved.