【问题标题】:github api: How to efficiently find the number of commits for a repository?github api:如何有效地查找存储库的提交次数?
【发布时间】:2013-04-01 22:07:37
【问题描述】:

我想查找对特定 github 项目以及在其中对特定文件的提交次数。我检查了github api docs,但只找到了一个用于实际返回所有提交的 API。这将是非常低效的,因为我必须通过所有提交进行多次 api 调用以进行分页。

谁有更好的主意?

【问题讨论】:

  • 查看我的更新答案。
  • @dorw 您好,请问您是如何集成访问令牌的?

标签: github github-api


【解决方案1】:

2013 年 5 月更新:参见“File CRUD and repository statistics now available in the API

你现在可以Get the last year of commit activity data

GET /repos/:owner/:repo/stats/commit_activity

返回按周分组的最后一年的提交活动。 days 数组是每天的一组提交,从星期日开始。

不是完全你在找什么,但更接近。


原始答案(2010 年 4 月)

不,当前 API 不支持使用“log --all”列出所有分支的所有提交。

唯一的选择出现在“Github API: Retrieve all commits for all branches for a repo”中,并列出所有提交的所有页面,一个又一个分支

这似乎比另一种替代方法实际上克隆 Github repo 和apply git commands on that local clone
(主要是git shortlog


注意:您还可以签出由Arcsector 创建的python script

【讨论】:

  • @dorw 如果您有关于 API 的具体建议或功能请求,您应该发送电子邮件至 GitHub 支持并让他们知道:github.com/contact
  • 你知道一年多能拿到吗?
  • @Pachonk 否:我想 github 支持会。
【解决方案2】:

使用GraphQL API v4,您可以使用totalCount 为每个分支获取每个分支的总提交计数:

{
  repository(owner: "google", name: "gson") {
    name
    refs(first: 100, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              id
              history(first: 0) {
                totalCount
              }
            }
          }
        }
      }
    }
  }
}

Test it in the explorer

【讨论】:

    【解决方案3】:

    纯 JS 实现

    const base_url = 'https://api.github.com';
    
        function httpGet(theUrl, return_headers) {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open("GET", theUrl, false); // false for synchronous request
            xmlHttp.send(null);
            if (return_headers) {
                return xmlHttp
            }
            return xmlHttp.responseText;
        }
    
        function get_all_commits_count(owner, repo, sha) {
            let first_commit = get_first_commit(owner, repo);
            let compare_url = base_url + '/repos/' + owner + '/' + repo + '/compare/' + first_commit + '...' + sha;
            let commit_req = httpGet(compare_url);
            let commit_count = JSON.parse(commit_req)['total_commits'] + 1;
            console.log('Commit Count: ', commit_count);
            return commit_count
        }
    
        function get_first_commit(owner, repo) {
            let url = base_url + '/repos/' + owner + '/' + repo + '/commits';
            let req = httpGet(url, true);
            let first_commit_hash = '';
            if (req.getResponseHeader('Link')) {
                let page_url = req.getResponseHeader('Link').split(',')[1].split(';')[0].split('<')[1].split('>')[0];
                let req_last_commit = httpGet(page_url);
                let first_commit = JSON.parse(req_last_commit);
                first_commit_hash = first_commit[first_commit.length - 1]['sha']
            } else {
                let first_commit = JSON.parse(req.responseText);
                first_commit_hash = first_commit[first_commit.length - 1]['sha'];
            }
            return first_commit_hash;
        }
    
        let owner = 'getredash';
        let repo = 'redash';
        let sha = 'master';
        get_all_commits_count(owner, repo, sha);
    

    学分 - https://gist.github.com/yershalom/a7c08f9441d1aadb13777bce4c7cdc3b

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2015-07-04
      • 2020-11-20
      • 2018-09-01
      • 1970-01-01
      • 2019-08-20
      • 2015-08-03
      相关资源
      最近更新 更多