5

Is there a more efficient way of getting the count of additions/deletions related to a commit than looping through every single commit and calling the:

GET /repos/:owner/:repo/commits/:sha

(https://developer.github.com/v3/repos/commits/)

Just to get the:

"stats": {
   "additions": 104,
   "deletions": 4,
   "total": 108
},

Data?

Unfortunately the commits endpoint:

GET /repos/:owner/:repo/commits

Contains a lot of data about each commit but not this detail which means a huge number of additional API calls to get it.

2 Answers 2

4

Whenever you need multiple GitHub API query, check if GraphQL (introduced by GitHub last Sept. 2016) could allow you to get all those commits in one query.

You can see examples here and apply to GitHub GraphQL early access, but that sees to be the only way to get:

  • all stats from all commits (and only the stats)
  • in one query
Sign up to request clarification or add additional context in comments.

2 Comments

VonC thanks - I will take a look, but it looks like it is in preview and we really need something which is in production I think. I am still assuming then that this is not possible with the current GitHub API.
@chrisb Yes, it does not seem possible at the moment with the current GitHub API indeed.
2

It is now possible to get commit stats (additions, deletions & changedFiles count) using the GraphQL API :

To get commit stats for the 100 first commit on the default branch :

{
  repository(owner: "google", name: "gson") {
    defaultBranchRef {
      name
      target {
        ... on Commit {
          id
          history(first: 100) {
            nodes {
              oid
              message
              additions
              deletions
              changedFiles
            }
          }
        }
      }
    }
  }
}

Try it in the explorer

To get commit stats for the first 10 branches, for the 100 first commits of each one of these branches:

{
  repository(owner: "google", name: "gson") {
    refs(first: 10, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              id
              history(first: 100) {
                nodes {
                  oid
                  message
                  additions
                  deletions
                  changedFiles
                }
              }
            }
          }
        }
      }
    }
  }
}

Try it in the explorer

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.