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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How would this work for a team account?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@joe_midi 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Same here. Did you use this with bitbucket server?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I just extended @Jeff Thomas script for API v2.0 that works on 2021.
You can use it from: https://github.com/bc67da8d/bitbucket-commit-counter
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Does this actually work for you?
I get a 401 status code when I try run this. Seems it has to do with the Basic Authentication :\
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I just extended your script, so that it worked for me (per workspace):
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jeff,
I am looking to generate a report in JIRA for the commits made in the last 12 months which contains fileName, commit Id, JIRA ID, repository Name, Lines changed. Please let me know if you have updated script
Thanks,
Jay
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The python script does not work for Bitbucket server/ datacenter edition
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Gaurav,
For Bitbucket Server/Data Center you may use Awesome Graphs for Bitbucket app.
I.e., the Activity graph shows total number of commits per project or repository during the selected period of time up to 12 months.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for this Anastasia. Do we have any opensource version of it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Gaurav, we don't have an opensource version, but you can try it for free and even after the end of the trial period some of the basic features remain available for you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anastasia, Thanks. That's wonderful to know. Can I know what all basic features will remain available in Bitbucket post trial ends?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.