Create a gist now

Instantly share code, notes, and snippets.

A python script to count commits in all of your own repositories on github (not cheking branches)
#! /usr/bin/env python2
import json
import requests
def count_user_commits(user):
r = requests.get('' % user)
repos = json.loads(r.content)
for repo in repos:
if repo['fork'] is True:
# skip it
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
user = sys.argv[1]
except IndexError:
print "Usage: %s <username>" % sys.argv[0]
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment