Get total number of commits for a repository using the GitHub API

I was trying to get the total number of commits for a repository using the GitHub API. After scouring over the documentation, I was unable to find a way to do that. Searching the web revealed nothing, as well.

So, I came up with the following *hackish* solution.

Note: I'll be using the following repository for illustration:

https://github.com/notfoss/archlinux-openrc-services

Obtaining hashes for the commits

We need the hashes of the first commit and the latest commit to get the total number of commits.

Hash of the first commit

Obtaining the hash of the first (initial) commit is tricky, as I couldn't find a way to do that through the API.

So, to do that you'll either have to visit the web interface of that repository on GitHub or clone that repository and find it through git log. The good thing is that under normal circumstances, the hash is unlikely to change, so you can save it and be done with it.

In our case, the hash of the first commit is:

041f4e99d95c70188f5d564f8af2c203b6a072ca

Note: If you need to get the hash of the first commit regularly, then this post is of no use to you. As you can simply clone the repository everytime, and find the number of commits locally.

Hash of the latest commit

Next, we need the hash of the latest commit on the required branch. You can get that using the following syntax (explanation):

GET /repos/:owner/:repo/git/refs/:ref

In our case (using the master branch):

curl https://api.github.com/repos/notfoss/archlinux-openrc-services/git/refs/heads/master

gives:

{
  "ref": "refs/heads/master",
  "url": "https://api.github.com/repos/notfoss/archlinux-openrc-services/git/refs/heads/master",
  "object": {
    "sha": "115316c62322091fd35f5e446faef83a30b000c2",
    "type": "commit",
    "url": "https://api.github.com/repos/notfoss/archlinux-openrc-services/git/commits/115316c62322091fd35f5e446faef83a30b000c2"
  }
}

As you can see, the hash (sha key) of the latest commit is:

115316c62322091fd35f5e446faef83a30b000c2

Comparing the two commits

Through the GitHub documentation, I found that comparison of two commits produces the following keys among other output: "ahead_by" and "total_commits" (I observed that the value of both these keys is always the same).

For comparing two commits, the syntax is (reference):

GET /repos/:owner/:repo/compare/:base...:head

So, in our case, we can do:

curl https://api.github.com/repos/notfoss/archlinux-openrc-services/compare/041f4e99d95c70188f5d564f8af2c203b6a072ca...115316c62322091fd35f5e446faef83a30b000c2

Here's the relevant portion of the output produced by the above command:

"status": "ahead",
"ahead_by": 59,
"behind_by": 0,
"total_commits": 59,

On inspecting the value of the "total_commits" key, I noticed that it is one less than the total number of commits for a repository.

So, to get the total number of commits in a repository, use:

(value of "total_commits") + 1

Which equals 60 in our case. And as of now, that is indeed the total number of commits for my repository!

Here's a Bash script which incorporates the above steps:

#!/usr/bin/env bash

# Get the number of commits for a repository using the GitHub API
# Requires curl and jshon: http://kmkeen.com/jshon/

# Owner of the repository
repo_owner="notfoss"

# Name of the repository
repo_name="archlinux-openrc-services"

# URL for the API request
repo_api_url="https://api.github.com/repos/${repo_owner}/${repo_name}"

# Hashes of the first and the latest commits
# Replace this with the hash of the first commit for your repository
first_commit="041f4e99d95c70188f5d564f8af2c203b6a072ca"

# Hash of the latest commit is fetched through the GitHub API
latest_commit=$(curl -s "$repo_api_url"/git/refs/heads/master | jshon -e object | jshon -e sha -u)

# Data returned by the GitHub API in JSON format
github_data=$(curl -s "$repo_api_url"/compare/${first_commit}...${latest_commit})

# Get the value of the total_commits key
num_commits=$(echo "$github_data" | jshon -e total_commits)

# Total number of commits = (value of the total_commits key) + 1
echo $(( num_commits + 1 ))

Comments

Comments powered by Disqus