Skip to content

Instantly share code, notes, and snippets.

@kpsychas
Last active May 16, 2017 18:58
Show Gist options
  • Save kpsychas/fbf4e42a0635cef042cf4fea56b0e12f to your computer and use it in GitHub Desktop.
Save kpsychas/fbf4e42a0635cef042cf4fea56b0e12f to your computer and use it in GitHub Desktop.
Python script for plotting LoC statistics
#!/usr/bin/python
"""
Display the per-commit size of the current git branch.
source: http://stackoverflow.com/questions/23907/how-can-i-graph-the-lines-of-code-history-for-git-repo
"""
import subprocess
import re
import sys
import matplotlib.pyplot as plt
import numpy as np
def main(argv):
lines_added = []
lines_removed = []
git = subprocess.Popen(["git", "log", "--shortstat", "--reverse",
"--pretty=oneline"], stdout=subprocess.PIPE)
out, err = git.communicate()
total_files, total_insertions, total_deletions = 0, 0, 0
for line in out.split('\n'):
if not line.strip():
continue
if line[0] != ' ':
# This is a description line
hash, desc = line.split(" ", 1)
else:
# This is a stat line
if "deletions" and "insertions" in line:
data = re.findall(
'(\d+) file[s]? changed, (\d+) insertion[s]?\(\+\), (\d+) deletion[s]?\(-\)',
line)
try:
files, insertions, deletions = (int(x) for x in data[0])
except:
print '!', line, data
elif "insertions" in line:
data = re.findall(
'(\d+) file[s]? changed, (\d+) insertion[s]?\(\+\)',
line)
try:
files, insertions = (int(x) for x in data[0])
except:
print '!', line, data
deletions = 0
elif "deletions" in line:
data = re.findall(
'(\d+) file[s]? changed, (\d+) deletion[s]?\(\+\)',
line)
try:
files, deletions = (int(x) for x in data[0])
except:
print '!', line, data
insertions = 0
total_files += files
lines_added.append(insertions)
lines_removed.append(deletions)
print("{}: {} files, {} lines".format(hash, total_files,
sum(lines_added)-sum(lines_removed)))
lines_added = np.array(lines_added)
lines_removed = np.array(lines_removed)
plt.subplot(1, 2, 1)
plt.title('Absolute statistics')
plt.plot(lines_added, label='added')
plt.plot(lines_removed, label='removed')
plt.plot(lines_added-lines_removed, label='difference')
plt.legend(loc='best', prop={'size': 10})
plt.subplot(1, 2, 2)
plt.title('Cumulative statistics')
plt.plot(lines_added.cumsum(), label='added')
plt.plot(lines_removed.cumsum(), label='removed')
plt.plot(lines_added.cumsum()-lines_removed.cumsum(), label='difference')
plt.legend(loc='best', prop={'size': 10})
plt.savefig('stats.png', dpi=400)
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment