Skip to content

Instantly share code, notes, and snippets.

@noga-dev
Forked from codsane/commitCount.py
Last active June 29, 2021 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noga-dev/9a1290149b96074abfbf1ecc6ad90589 to your computer and use it in GitHub Desktop.
Save noga-dev/9a1290149b96074abfbf1ecc6ad90589 to your computer and use it in GitHub Desktop.
Simplest way to get commit count of a GitHub repo using the API
import 'dart:html';
void main() async {
print(await getCommits(repo: 'river_pod', owner: 'rrousselGit'));
}
Future<String> getCommits({required String repo, required String owner}) async => RegExp(r'(\d+)')
.allMatches((await HttpRequest.request(
'https://api.github.com/repos/$owner/$repo/commits?per_page=1'))
.responseHeaders['link']!)
.last
.group(0)!;
import requests
import re
def commitCount(u, r):
return re.search('\d+$', requests.get('https://api.github.com/repos/{}/{}/commits?per_page=1'.format(u, r)).links['last']['url']).group()
def latestCommitInfo(u, r):
""" Get info about the latest commit of a GitHub repo """
response = requests.get('https://api.github.com/repos/{}/{}/commits?per_page=1'.format(u, r))
commit = response.json()[0]; commit['number'] = re.search('\d+$', response.links['last']['url']).group()
return commit
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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment