Skip to content

Instantly share code, notes, and snippets.

@0penBrain
Last active May 29, 2024 00:50
Show Gist options
  • Save 0penBrain/7be59a48aba778c955d992aa69e524c5 to your computer and use it in GitHub Desktop.
Save 0penBrain/7be59a48aba778c955d992aa69e524c5 to your computer and use it in GitHub Desktop.
The simplest way to get commit count of a GitHub repository through the API
curl -I -k "https://api.github.com/repos/:owner/:repo/commits?per_page=1" | sed -n '/^[Ll]ink:/ s/.*"next".*page=\([0-9]*\).*"last".*/\1/p'
### And that's all !
# I saw many fighting with finding first commit SHA or similar fancy thing.
# Here we just rely on the GH API, asking commits at 1 per page and parsing the last page number in the header of the reply (whose body only holds the last commit !)
# So this is robust and bandwidth efficient. :)
# If one want the commit count of a specific SHA, just use :
curl -I -k "https://api.github.com/repos/:owner/:repo/commits?per_page=1&sha=:sha" | sed -n '/^[Ll]ink:/ s/.*"next".*page=\([0-9]*\).*"last".*/\1/p'
# And for a specific time (date using ISO 8601 format, 'Z' for UTC) :
curl -I -k "https://api.github.com/repos/:owner/:repo/commits?per_page=1&until=yyyy-MM-ddThh:mm:ssZ" | sed -n '/^[Ll]ink:/ s/.*"next".*page=\([0-9]*\).*"last".*/\1/p'
@csm10495
Copy link

csm10495 commented May 1, 2020

👍

@codsane
Copy link

codsane commented Oct 26, 2020

Thanks a ton! 👍

Forked with a quick and dirty Python implementation here: https://gist.github.com/codsane/25f0fd100b565b3fce03d4bbd7e7bf33

@brennanwilkes
Copy link

This is awesome. For anyone wanting to use authenticated calls, replace the curl -I -k "..." with gh api -i /repos/:owner/:repo/commits?per_page=1

@JackySu
Copy link

JackySu commented Nov 15, 2022

if I could give you 100 stars for this beautiful snippet

@DrKabum
Copy link

DrKabum commented Dec 19, 2022

I get a different number than with git rev-list --all --count any idea what I'm doing wrong?

@0penBrain
Copy link
Author

@DrKabum which repo are you targeting?

@DrKabum
Copy link

DrKabum commented Dec 20, 2022

@openbrain thanks for jumping in but I just figured what my mistake was.

Basically, I wanted to get a commit count after a migration of a repo from Bitbucket to Github. The problem was that on GH, the default branch wasn't set to master by default (but a feature branch. This branch is actually the first alphabetically, maybe that's why). So the count from the API was on a different branch than when I ran git rev-list locally on master, hence the different results.

@Vamsi259
Copy link

Vamsi259 commented Mar 4, 2023

curl -I -k "https://api.github.com/repos/:owner/:repo/commits?per_page=1" | sed -n '/^[Ll]ink:/ s/."next".page=([0-9])."last".*/\1/p'

I ran this one as
curl -u "$username:$password" -I -k "https://github.com/repos/$owner/$Repo/commits?per_page=1" | sed -n '/^[Ll]ink:/ s/."next".page=([0-9])."last".*/\1/p'

But i am not getting the commit count

when i ran it as

curl -u "username:password" -I -k "https://github.com/repos/$owner/$Repo/commits?per_page=100000000000000000"

Getting maximum commit count as 100 only not getting the full count for the repos which have more than 100+ commits

@0penBrain
Copy link
Author

@Vamsi259 Why did you remove much '*' characters from the sed regex ? They are important for the command to work.

@Vamsi259
Copy link

Vamsi259 commented Mar 4, 2023

Actually it was typo while pasting the command in comments I tried with the same command

curl -u "$username:$password" -I -k "https://github.com/repos/$owner/$repo/commits?per_page=1" | sed -n '/^[Ll]ink:/ s/."next".page=([0-9])."last".*/\1/p'

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

but it is not giving any result

@Vamsi259
Copy link

Vamsi259 commented Mar 4, 2023

curl -u "$username:$password" -I -k "https://github.com/repos/$owner/$repos/commits?per_page=1" | sed -n '/^[Ll]ink:/ s/."next".page=([0-9])."last".*/\1/p'

@Vamsi259
Copy link

Vamsi259 commented Mar 4, 2023

While pasting the command in comments section few * are getting deleted here

@0penBrain
Copy link
Author

@Vamsi259 you can use backquotes to paste your code
What is the repo against which you're running the command?

@Vamsi259
Copy link

Vamsi259 commented Mar 4, 2023

Its working. Thankyou.. This is really helpful

@0penBrain
Copy link
Author

Its working. Thankyou.. This is really helpful

Great. For sake of curiosity, what was the issue ? 😄

@Vamsi259
Copy link

Vamsi259 commented Mar 4, 2023

I have changed little bit in the curl request from
curl -I -k "https://api.github.com/repos/:owner/:repo/commits?per_page=1" | sed -n '/^[Ll]ink:/ s/.*"next".*page=\([0-9]*\).*"last".*/\1/p'
to
curl -I -k "https://api.github.com/api/v3/repos/:owner/:repo/commits?per_page=1" | sed -n '/^[Ll]ink:/ s/.*"next".*page=\([0-9]*\).*"last".*/\1/p'

then it started working

@0penBrain
Copy link
Author

I have changed little bit in the curl request

OK thx. A bit weird because the Gist uses the standard API. Anyway, good you have it to work.

@0xTiger
Copy link

0xTiger commented Mar 27, 2023

For anyone curious: getting Repository commit counts can be done quite easily with the GraphQL API
https://stackoverflow.com/a/75854793/19264346

@lokeshvasnik
Copy link

I want it in javascript

@0penBrain
Copy link
Author

I want it in javascript

Not a JS expert but something like:

var request = new XMLHttpRequest();
request.open('GET', 'https://api.github.com/repos/:owner/:repo/commits?per_page=1', false);
request.send(null);
request.getResponseHeader('link').match(/"next".*page=([0-9]+).*"last"/)[1];

@ep-cc
Copy link

ep-cc commented Nov 2, 2023

This is awesome. For anyone wanting to use authenticated calls, replace the curl -I -k "..." with gh api -i /repos/:owner/:repo/commits?per_page=1

👍

@Javatrix
Copy link

TYSM for this I spent like half an hour coding a custom Java utility cause I just couldn't get Runtime to work and now I found this. Thank you man. I have to learn more Bash :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment