from __future__ import print_function
import git, sys, datetime, numpy, traceback
from matplotlib import pyplot
import seaborn, progressbar

repo = git.Repo(sys.argv[1])
fm = '%Y'
interval = 7 * 24 * 60 * 60
commit2cohort = {}

for commit in repo.iter_commits('master'):
    cohort = datetime.datetime.utcfromtimestamp(commit.committed_date).strftime(fm)
    commit2cohort[commit.hexsha] = cohort

last_date = None
curves = {}
ts = []
for commit in repo.iter_commits('master'):
    if last_date is not None and commit.committed_date > last_date - interval:
        continue
    t = datetime.datetime.utcfromtimestamp(commit.committed_date)
    ts.append(t)
    print(commit.hexsha, t)
    
    last_date = commit.committed_date
    histogram = {}
    entries = [entry for entry in commit.tree.traverse()
               if entry.type == 'blob' and entry.mime_type.startswith('text/')]
    bar = progressbar.ProgressBar(max_value=len(entries))
    for i, entry in enumerate(entries):
        bar.update(i)
        try:
            for old_commit, lines in repo.blame(commit, entry.path):
                cohort = commit2cohort[old_commit.hexsha]
                histogram[cohort] = histogram.get(cohort, 0) + len(lines)
                curves.setdefault(cohort, [])
        except:
            traceback.print_exc()

    for cohort, curve in curves.items():
        curve.append(histogram.get(cohort, 0))

    def rev(l):
        return list(reversed(l))
    cohorts = list(sorted(curves.keys()))
    x = rev(ts)
    y = numpy.array([rev(curves[cohort]) for cohort in cohorts])
    pyplot.clf()
    pyplot.stackplot(x, y, labels=['Code added in %s' % c for c in cohorts])
    pyplot.legend(loc=2)
    pyplot.ylabel('Lines of code')
    pyplot.savefig('cohorts.png')
        

@@ -220,17 +220,29 @@
 rt = %7B%7D%0A
+commits = %5B%5D
 %0A
-
 for comm
@@ -401,16 +401,43 @@
 = cohort
+%0A    commits.append(commit)
 %0A%0Alast_d
@@ -487,32 +487,22 @@
 n re
-po.iter_commits('master'
+versed(commits
 ):%0A 
@@ -559,9 +559,9 @@
 ate 
-%3E
+%3C
  las
@@ -571,9 +571,9 @@
 ate 
--
++
  int
@@ -1430,57 +1430,8 @@
 ))%0A%0A
-    def rev(l):%0A        return list(reversed(l))%0A
     
@@ -1472,24 +1472,8 @@
 )))%0A
-    x = rev(ts)%0A
     
@@ -1493,27 +1493,62 @@
 ay(%5B
-rev(curves%5Bcohort%5D)
+%5B0%5D * (len(ts) - len(curves%5Bcohort%5D)) + curves%5Bcohort%5D
  for
@@ -1606,17 +1606,18 @@
 ackplot(
-x
+ts
 , y, lab

iterate forward instead of backward