How can I get the total number of commits across all our team's projects, in the last 12 months?

For an end-of-year report I want to show how many commits our team has made in total this year, across all our BitBucket projects (approx 140 projects).

Is there a way to get a total number of commits for all the projects together?

PS I know this isn't a measure of team effectiveness, but it would be a fun stat to know. Another one would be the total number of lines that have been added or changed, across all projects, or other things like that.

2 answers

1 accepted

3 votes
Jeff Thomas Atlassian Team Dec 11, 2013

I wanted to do this for my stuff, so I whipped up a quick (and dirty) Python script to get these stats. All of my repositories are under by username, so it was easy to find all the commits. If you let me know how yours are setup, I can probably modify the script. It runs slow, but I'll work on improving it. Let me know if you have any questions, I'll be working on improving it soon.

You'll need to install the requests and python-dateutil modules to run the script.

Sample output

Stats for 2013

Total commits in username/repo1: 21
	Lines added: 1139
	Lines removed: 59

Total commits in username/repo2: 2
	Lines added: 9593
	Lines removed: 0

Total commits in username/repo3: 4
	Lines added: 772
	Lines removed: 75

Total commits: 27
Total lines added: 11504
Total lines removed: 134

Script

import requests, dateutil.parser

baseUrlv2 = "https://bitbucket.org/api/2.0"
baseUrlv1 = "https://bitbucket.org/api/1.0"

username = ""
password = ""
year = 2013

totalCommits = 0
totalAdd = 0
totalRemove = 0
overallAdd = 0
overallRemove = 0
commitCount = 0
commits = []

print ""
print "Stats for {year}".format(year=year)
print ""

r = requests.get("{base}/user/repositories/".format(base=baseUrlv1),
	auth=(username, password))

repos = r.json()

for repo in repos:
	repoSlug = repo['slug']
	r = requests.get("{base}/repositories/{username}/{repo}/commits".format(base=baseUrlv2, username=username, repo=repoSlug),
	auth=(username, password))

	c = r.json()
	commits.extend(c['values'])

	while 'next' in c:
		r = requests.get("{next}".format(next=c['next']), 
			auth=(username, password))
		c = r.json()
		commits.extend(c['values'])

	for commit in commits:
		commitDate = dateutil.parser.parse(commit['date'])
		if commitDate.year == year:
			commitCount += 1

			r = requests.get("{base}/repositories/{username}/{repo}/changesets/{hash}/diffstat/".format(base=baseUrlv1, username=username, repo=repoSlug, hash=commit['hash']),
				auth=(username, password))
			
			try:
				stats = r.json()
			except ValueError:
			    # decoding failed
			    continue

			for stat in stats:
				try:
					totalAdd += stat['diffstat']['added']
					totalRemove += stat['diffstat']['removed']
				except TypeError:
					continue

	print "Total commits in {user}/{repo}: {count}".format(user=username, repo=repoSlug, count=commitCount)		
	print "\tLines added: {add}".format(add=totalAdd)
	print "\tLines removed: {remove}\n".format(remove=totalRemove)
	totalCommits += commitCount	
	overallAdd += totalAdd
	overallRemove += totalRemove
	#reset counters
	commitCount = 0
	totalAdd = 0
	totalRemove = 0
	commits = []

print ""
print "Total commits: {count}".format(count=totalCommits)
print "Total lines added: {count}".format(count=overallAdd)
print "Total lines removed: {count}".format(count=overallRemove)


Wow thanks Jeff, this looks perfect! I'll try it out and report back. But I'm marking this as accepted because it looks like it will do what I need.

Hi Jeff, Thanks for this. I had to add a check after line 32 because repos with no commits were causing an error. if len(c) > 1: Hope that helps someone!

How would this work for a team account?

@Joey Padasian Hi I believe i ran into that problem today, because all of the repos had different owners it would return a 404 on the second request for every repo, to solve it i changed the following in lines 28 and 29:

repoSlug = repo['slug']
r = requests.get("{base}/repositories/{username}/{repo}/commits".format(base=baseUrlv2, username=username, repo=repoSlug),
auth=(username, password))

For this one:

repoOwner = repo['owner']
repoSlug = repo['slug']
url = "{base}/repositories/{owner}/{repo}/commits".format(base=baseUrlv2, username=repoOwner, repo=repoSlug)
r = requests.get(url, auth=(username, password))

The difference is in the parameter username witch is no longer equal to the account owner you specify on top but to the owner of the repo it is currently looping, i hope it helps.

Just out of curiosity, is this API works for bitbucket server or bitbucket cloud

("{base}/user/repositories/".format(base=baseUrlv1)

I am using bitbucket server and that too API version 1.0. I cannot find this API listed .Bitbucket version : Atlassian Bitbucket v5.2.0

If you install https://github.com/visionmedia/git-extrasgit extras on your enviroment machine, you can check the history of all your commits and logs.

git summary

Hi Rey

I notice that 'git summary' still only gives me stats for a single repo when used on the local machine. Is there a way to get it to show a total for all commits across every project in a folder, for example?

Hi Ray, when you say 'environment machine', what do you mean, exactly? The source is hosted BitBucket and we have multiple devs who each work from their individual computers.

It does look like a useful tool though, I'll check it out.

Hi,

I was also looking thsi kind of script for get commit report monthly wise.

I need no of commit report by monthly wise . could You pls suggets,..where and what are need to modify in script

Thank You

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

660 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot