public
Created

A python script to count commits in all of your own repositories on github (not cheking branches)

  • Download Gist
count-github-commits.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#! /usr/bin/env python2
import json
import requests
 
 
def count_user_commits(user):
r = requests.get('https://api.github.com/users/%s/repos' % user)
repos = json.loads(r.content)
 
for repo in repos:
if repo['fork'] is True:
# skip it
continue
n = count_repo_commits(repo['url'] + '/commits')
repo['num_commits'] = n
yield repo
 
 
def count_repo_commits(commits_url, _acc=0):
r = requests.get(commits_url)
commits = json.loads(r.content)
n = len(commits)
if n == 0:
return _acc
link = r.headers.get('link')
if link is None:
return _acc + n
next_url = find_next(r.headers['link'])
if next_url is None:
return _acc + n
# try to be tail recursive, even when it doesn't matter in CPython
return count_repo_commits(next_url, _acc + n)
 
 
# given a link header from github, find the link for the next url which they use for pagination
def find_next(link):
for l in link.split(','):
a, b = l.split(';')
if b.strip() == 'rel="next"':
return a.strip()[1:-1]
 
 
if __name__ == '__main__':
import sys
try:
user = sys.argv[1]
except IndexError:
print "Usage: %s <username>" % sys.argv[0]
sys.exit(1)
total = 0
for repo in count_user_commits(user):
print "Repo `%(name)s` has %(num_commits)d commits, size %(size)d." % repo
total += repo['num_commits']
print "Total commits: %d" % total

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.