Skip to content

Instantly share code, notes, and snippets.

@tatterdemalion
Created December 11, 2015 21:19
Show Gist options
  • Save tatterdemalion/e459e43061da8df68017 to your computer and use it in GitHub Desktop.
Save tatterdemalion/e459e43061da8df68017 to your computer and use it in GitHub Desktop.
git post-checkout hook to check django migrations
#!/usr/bin/env python
import sys
import subprocess
from collections import defaultdict
fn, previous_head, current_head, is_branch = sys.argv
if not is_branch == '1':
sys.exit(0)
diff = defaultdict(list)
output = subprocess.check_output(
'git --no-pager diff --name-status %s %s ' % (
previous_head, current_head), shell=True)
for i in output.split('\n'):
if not i:
continue
op, fname = i.split('\t')
diff[op].append(fname)
to_revert = []
for i in diff.get('D', []):
if 'migrations' in i:
to_revert.append(i)
to_run = []
for i in diff.get('A', []):
if 'migrations' in i:
to_run.append(i)
if to_revert:
print('\nATTENTION!!!\n'
'You have deleted migrations in this branch.\n'
'You may need to return to the previous one and revert those:')
for i in to_revert:
print('\t * %s' % i)
print('\nPlease ignore this warning if you\'ve already done it')
if to_run:
print('You have new migrations in this branch:')
for i in to_run:
print(i)
print('You can see the migrations by running:')
print('python manage.py migrate --list')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment