git - 如何使用 GitHub V3 API 获取 repo 的提交计数?

标签 git github github-api

我正在尝试使用 API 计算许多大型 github repos 的提交,因此我想避免获取整个提交列表(以这种方式为例:api.github.com/repos/jasonrudolph/keyboard/commits ) 并计算它们。

如果我有第一个(初始)提交的哈希,我可以 use this technique to compare the first commit to the latest它很高兴地以这种方式报告中间的 total_commits(所以我需要添加一个)。不幸的是,我看不到如何使用 API 优雅地获得第一次提交。

基本 repo URL 确实给了我 created_at(这个 url 是一个例子:api.github.com/repos/jasonrudolph/keyboard),所以我可以通过将提交限制在创建日期之前来减少提交集(这个 url 是一个例子:api.github.com/repos/jasonrudolph/keyboard/commits?until=2013-03-30T16:01:43Z)并使用最早的一个(总是排在最后?)或者可能是一个空父项(不确定 fork 项目是否有初始父项提交)。

有没有更好的方法来获取存储库的第一个提交哈希值?

更好的是,对于一个简单的统计数据来说,这整件事似乎很复杂,我想知道我是否遗漏了什么。使用 API 获取 repo 提交计数有什么更好的想法吗?

编辑:这个somewhat similar question正在尝试按某些文件进行过滤(“并在其中过滤到特定文件。”),因此有不同的答案。

最佳答案

可以考虑使用GraphQL API v4使用 aliases 同时对多个存储库执行提交计数.以下将获取 3 个不同存储库的所有分支的提交计数(每个存储库最多 100 个分支):

{
  gson: repository(owner: "google", name: "gson") {
    ...RepoFragment
  }
  martian: repository(owner: "google", name: "martian") {
    ...RepoFragment
  }
  keyboard: repository(owner: "jasonrudolph", name: "keyboard") {
    ...RepoFragment
  }
}

fragment RepoFragment on Repository {
  name
  refs(first: 100, refPrefix: "refs/heads/") {
    edges {
      node {
        name
        target {
          ... on Commit {
            id
            history(first: 0) {
              totalCount
            }
          }
        }
      }
    }
  }
}

Try it in the explorer

RepoFragment 是一个 fragment这有助于避免每个 repo 的重复查询字段

如果你只需要默认分支上的提交计数,那就更直接了:

{
  gson: repository(owner: "google", name: "gson") {
    ...RepoFragment
  }
  martian: repository(owner: "google", name: "martian") {
    ...RepoFragment
  }
  keyboard: repository(owner: "jasonrudolph", name: "keyboard") {
    ...RepoFragment
  }
}

fragment RepoFragment on Repository {
  name
  defaultBranchRef {
    name
    target {
      ... on Commit {
        id
        history(first: 0) {
          totalCount
        }
      }
    }
  }
}

Try it in the explorer

关于git - 如何使用 GitHub V3 API 获取 repo 的提交计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27931139/

相关文章:

git - 在 git 中过滤远程分支

git - 我在 GitHub 上 merge 了一个 pull 请求,并且必须更改该提交的作者。如何?

git - 通过命令行在github上自动打开一个pull request

oauth - GitHub API3 删除访问 token

git post-receive 无法正常工作

Git mv 失败并出现 fatal error : source directory is empty

ruby - 使用 Octokit.rb 输出 repos URL

api - 是否可以在 GitHub 中定义问题编号?

git - stash 我当前正在处理的内容,并恢复到上次提交

windows - github在windows上生成ssh key