Created
March 13, 2020 21:28
-
-
Save godric-cz/f91e59b16f9256607af83fce25a7fa87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import datetime as dt | |
import shlex | |
import subprocess | |
def get_files(commit): | |
hashes = [] | |
cmd = ('git', 'ls-tree', '-r', commit) | |
proc = subprocess.run(cmd, capture_output=True, text=True) | |
for line in proc.stdout.strip().split('\n'): | |
stats, path = line.split('\t') | |
_, _, object_hash = stats.split(' ') | |
hashes.append(object_hash) | |
return hashes | |
def get_size(object_hash): | |
cmd = ('git', 'cat-file', '-s', object_hash) | |
proc = subprocess.run(cmd, capture_output=True, text=True) | |
return int(proc.stdout.strip()) | |
def get_charcount(object_hash): | |
# TODO unused, but more precise results (ignores indent etc) | |
cmd_pattern = "git cat-file blob {} | tr '\\n' ' ' | sed -r 's/[ \\-]+/ /g' | wc -m" | |
cmd = cmd_pattern.format(shlex.quote(object_hash)) | |
proc = subprocess.run(cmd, capture_output=True, text=True, shell=True) | |
return int(proc.stdout.strip()) | |
cmd = ('git', 'log', '--pretty=%H\t%at', '--reverse') | |
proc = subprocess.run(cmd, capture_output=True, text=True) | |
lines = proc.stdout.strip().split('\n') | |
for commit_data in lines: | |
commit, timestamp_str = commit_data.strip().split('\t') | |
author_datetime = dt.datetime.fromtimestamp(int(timestamp_str)) | |
files = get_files(commit) | |
total_size = sum([get_size(file) for file in files]) | |
print(author_datetime, total_size, sep='\t') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment