Skip to content

Instantly share code, notes, and snippets.

@dmerejkowsky
Last active February 29, 2016 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmerejkowsky/ccb91c7bf4b2c495dcde to your computer and use it in GitHub Desktop.
Save dmerejkowsky/ccb91c7bf4b2c495dcde to your computer and use it in GitHub Desktop.
Get git history statistics
import sys
import subprocess
import packaging.version
import tabulate
out = subprocess.check_output(["git", "tag"]).decode("UTF-8")
current_branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"])
if not current_branch:
sys.exit("Not on a branch")
current_branch = current_branch.decode("UTF-8").strip()
tags = out.splitlines()
tags.sort(key=packaging.version.Version)
tags.append(current_branch)
lines = list()
for before, after in zip(tags, tags[1:]):
stat = subprocess.check_output(["git", "diff", "--shortstat", "%s..%s" % (before, after)])
stat = stat.decode("UTF-8")
files, _, _, insertions, _, deletions, _ = stat.split()
lines.append([after, files, insertions, deletions])
out = tabulate.tabulate(lines, headers=["version", "files", "insertions", "deletions"])
print(out)
# Example of output:
"""
version files insertions deletions
---------- ------- ------------ -----------
v1.12.1 610 22399 7840
v1.14 177 3889 1865
v1.14.1 4 14 5
...
devel 1019 4923 2291
"""
@dmerejkowsky
Copy link
Author

Requires packaging and tabulate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment