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.

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):

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).

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 repositoryrepo_owner="notfoss"# Name of the repositoryrepo_name="archlinux-openrc-services"# URL for the API requestrepo_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 repositoryfirst_commit="041f4e99d95c70188f5d564f8af2c203b6a072ca"# Hash of the latest commit is fetched through the GitHub APIlatest_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 formatgithub_data=$(curl -s "$repo_api_url"/compare/${first_commit}...${latest_commit})# Get the value of the total_commits keynum_commits=$(echo"$github_data"| jshon -e total_commits)# Total number of commits = (value of the total_commits key) + 1echo$(( num_commits +1))