Skip to content

Instantly share code, notes, and snippets.

@adamduren
Last active August 29, 2015 14:03
Show Gist options
  • Save adamduren/c6dc5684da3162875aa0 to your computer and use it in GitHub Desktop.
Save adamduren/c6dc5684da3162875aa0 to your computer and use it in GitHub Desktop.
Give you a github style diffstats, with the ability to exclude paths for more relevancy.
from __future__ import print_function
import sys
# Gives you a github style diffstats.
# With the ability to exclude paths for more relevancy.
#
# Generate a diffstat file with:
# git diff --numstat master...HEAD > stats.diff
#
# Run this scipt
# python better_diff_stats.py stats.diff
EXCLUDE_PATHS = [
'project/static/libs', # example path
]
def calc_stats():
diff_filename = sys.argv[1]
diff_file = open(diff_filename, 'r')
total_adds = 0
total_dels = 0
num_files = 0
for line in diff_file:
num_adds, num_dels, filename = line.split('\t')
if (num_adds == '-'
or num_dels == '-'
or any(map(filename.startswith, EXCLUDE_PATHS))):
continue
num_adds = int(num_adds)
num_dels = int(num_dels) * -1
num_files += 1
total_adds += int(num_adds)
total_dels += int(num_dels)
print('{:+}\t{:+}\t{}'.format(num_adds, num_dels, filename), end='')
print('')
print('Showing {} changed files with {:,} additions and {:,} deletions.'.format(num_files, total_adds, total_dels))
print('Resulting in a net difference of {:+,} lines.'.format(total_adds + total_dels))
if __name__ == '__main__':
calc_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment