6

I want to get how many commits has done until a certain branch created from the Github API.

For example in git cli I'm doing: git log --no-merges --oneline ${branchHash} | wc -l and I can see the number.

From the Github API there's a limit of 100 so if I have more than 100 commits I can't get them all.

Is there any solution for that case?

yershalom
  • 786
  • 1
  • 8
  • 19

2 Answers2

1

I wrote a little thing to solve this:

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

It is based on using the compare URL of the GitHub Commit API, and using the total_commits field:

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

commit_count = commit_req.json()['total_commits'] + 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
yershalom
  • 786
  • 1
  • 8
  • 19
  • 2
    +1. Links-only answers are discouraged (https://meta.stackexchange.com/q/8231/6309), so I took the libery to add some references, links and details: please edit it some more if you think I missed something. – VonC Dec 17 '17 at 13:36
0

In order to avoid doing multiple queries, you could use a GraphQL one, similar to this one or this one: it will fetch all the commits for a given branch, allowing you to count them.

{
  repository(name: "sickvim", owner: "jonathansick") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          id
          history(first: 5) {
            pageInfo {
              hasNextPage
            }
            edges {
              node {
                oid
              }...
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks mate, But I tried something like this and got the next answer: ```{ "data": null, "errors": [ { "message": "Requesting 101 records on the connection exceeds the `first` limit of 100 records." } ] }``` – yershalom Dec 10 '17 at 12:24
  • Hi, Used this: ```{ repository(name: "sickvim", owner: "jonathansick") { ref(qualifiedName: "master") { target { ... on Commit { id history(first: 101) { pageInfo { hasNextPage } edges { node { messageHeadline oid message author { name email date } } } } } } } } } ``` – yershalom Dec 10 '17 at 13:15