Created
April 5, 2018 10:38
-
-
Save EoinTravers/641e4a67acdc7e70ff4be27c85cd8db1 to your computer and use it in GitHub Desktop.
See git commits over the last N days, across all your local repositories. A tool for disorganised people, who are trying to do better.
This file contains hidden or 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 python2 | |
""" | |
Run this script in the folder that contains all of your Git repositories | |
to see an ordered summary of your commits from the past N days. | |
Output is tab-delimited, so suggested usage (to see last month's worth of work) is: | |
cd ~/git_repo_folder/ | |
python git-diary.py 30 | column -ts $'\t' | |
""" | |
import os | |
import subprocess | |
from glob import glob | |
import datetime | |
import time | |
import sys | |
import pandas as pd | |
def pretty_date(t): | |
return datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S') | |
heads = glob('*/.git/logs/HEAD') # Find git repos | |
dirs = [p.split('/')[0] for p in heads] | |
# How many days to look in the past? | |
# Either passed as an argument, or prompted. | |
if len(sys.argv) == 2: | |
since = int(sys.argv[1]) | |
else: | |
since = input('How many days to go back? ') | |
now_unix = time.time() | |
seconds_per_day = 60*60*24 | |
since_unix = now_unix - since*seconds_per_day | |
cmd = ['git', 'log', | |
'--pretty=format:"%ad - %s"', | |
'--since=%i' % since_unix, '--date=iso'] | |
# Cycle through repos and get recent commits | |
all_logs = [] | |
for d in dirs: | |
os.chdir(d) | |
logs = subprocess.check_output(cmd).replace('\"', '').split('\n') | |
if logs == [['']] or logs == ['']: | |
pass | |
else: | |
try: | |
logs = [log.split(' - ') for log in logs] | |
log_df = pd.DataFrame() | |
log_df['time'] = [pd.to_datetime(log[0]) for log in logs] | |
log_df['msg'] = [log[1] for log in logs] | |
log_df['dir'] = d | |
all_logs.append(log_df) | |
except Exception as e: | |
print 'Error processing %s' % d | |
print e | |
print logs | |
os.chdir('..') | |
log = pd.concat(all_logs).sort_values(by='time') | |
log = log[['dir', 'time', 'msg']] | |
# Print output | |
for i, l in log.iterrows(): | |
print '\t'.join([str(v) for v in l]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment