<p>Basically I want to get the number of lines-of-code in the repository after each commit.</p>

<p>The only (really crappy) ways I have found is to use <code>git filter-branch</code> to run <code>wc -l *</code>, and a script that runs <code>git reset --hard</code> on each commit, then runs <code>wc -l</code></p>

<p>To make it a bit clearer, when the tool is run, it would output the lines of code of the very first commit, then the second and so on. This is what I want the tool to output (as an example):</p>

<pre class="lang-none prettyprint-override"><code>me@something:~/$ gitsloc --branch master
10
48
153
450
1734
1542
</code></pre>

<p>I've played around with the ruby 'git' library, but the closest I found was using the <code>.lines()</code> method on a diff, which seems like it should give the added lines (but does not: it returns 0 when you delete lines for example)</p>

<pre class="lang-rb prettyprint-override"><code>require 'rubygems'
require 'git'

total = 0
g = Git.open(working_dir = '/Users/dbr/Desktop/code_projects/tvdb_api')    

last = nil
g.log.each do |cur|
  diff = g.diff(last, cur)
  total = total + diff.lines
  puts total
  last = cur
end
</code></pre>

[{'answer_id': 24190, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 3, 'selected': False, 'text': "<p>The first thing that jumps to mind is the possibility of your git history having a nonlinear history. You might have difficulty determining a sensible sequence of commits.</p>\n\n<p>Having said that, it seems like you could keep a log of commit ids and the corresponding lines of code in that commit. In a post-commit hook, starting from the HEAD revision, work backwards (branching to multiple parents if necessary) until all paths reach a commit that you've already seen before. That should give you the total lines of code for each commit id.</p>\n\n<p>Does that help any? I have a feeling that I've misunderstood something about your question.</p>\n"}, {'answer_id': 35664, 'author': 'fserb', 'author_id': 3702, 'author_profile': 'https://Stackoverflow.com/users/3702', 'pm_score': 5, 'selected': False, 'text': '<p>You may get both added and removed lines with git log, like:</p>\n\n<pre><code>git log --shortstat --reverse --pretty=oneline\n</code></pre>\n\n<p>From this, you can write a similar script to the one you did using this info. In python:</p>\n\n<pre><code>#!/usr/bin/python\n\n"""\nDisplay the per-commit size of the current git branch.\n"""\n\nimport subprocess\nimport re\nimport sys\n\ndef main(argv):\n  git = subprocess.Popen(["git", "log", "--shortstat", "--reverse",\n                        "--pretty=oneline"], stdout=subprocess.PIPE)\n  out, err = git.communicate()\n  total_files, total_insertions, total_deletions = 0, 0, 0\n  for line in out.split(\'\\n\'):\n    if not line: continue\n    if line[0] != \' \': \n      # This is a description line\n      hash, desc = line.split(" ", 1)\n    else:\n      # This is a stat line\n      data = re.findall(\n        \' (\\d+) files changed, (\\d+) insertions\\(\\+\\), (\\d+) deletions\\(-\\)\', \n        line)\n      files, insertions, deletions = ( int(x) for x in data[0] )\n      total_files += files\n      total_insertions += insertions\n      total_deletions += deletions\n      print "%s: %d files, %d lines" % (hash, total_files,\n                                        total_insertions - total_deletions)\n\n\nif __name__ == \'__main__\':\n  sys.exit(main(sys.argv))\n</code></pre>\n'}, {'answer_id': 2854506, 'author': 'cboettig', 'author_id': 258662, 'author_profile': 'https://Stackoverflow.com/users/258662', 'pm_score': 6, 'selected': True, 'text': '<p>You might also consider <a href="http://gitstats.sourceforge.net/" rel="noreferrer">gitstats</a>, which generates this graph as an html file.  </p>\n'}, {'answer_id': 3180919, 'author': 'ma11hew28', 'author_id': 242933, 'author_profile': 'https://Stackoverflow.com/users/242933', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://github.com/ITikhonov/git-loc" rel="noreferrer">http://github.com/ITikhonov/git-loc</a> worked right out of the box for me.</p>\n'}]
['https://Stackoverflow.com/questions/23907', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/745/']