Created
December 6, 2016 15:37
-
-
Save mounirmesselmeni/fe3b68c77bcf7451deea5bee82a032cd to your computer and use it in GitHub Desktop.
A python pre-commit hooks for git to test againts configured repository email, useful when you want to use different emails for work and personal projects
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 python | |
import sys | |
import subprocess | |
REMOTE_WORK_PREFIX = 'awesomecompany' | |
EMAIL_WORK_PREFIX = 'awesomecompany.com' | |
def git(args, split_lines=False, strip=True): | |
args = ['git'] + args | |
git = subprocess.Popen(args, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
git_output = git.stdout.read() | |
git_stderr = git.stderr.read() | |
if strip: | |
git_output = git_output.strip() | |
if split_lines: | |
git_output = git_output.split('\n') | |
return (git_output, git_stderr) | |
def use_work_email_for_work_repo(): | |
remote = git(['remote', 'show', 'origin'])[0] | |
email = git(['config', 'user.email'])[0] | |
return REMOTE_WORK_PREFIX not in remote or EMAIL_WORK_PREFIX in email | |
if __name__ == '__main__': | |
print('running pre-commit hook') | |
if not use_work_email_for_work_repo(): | |
print('Wrong email for working repo!') | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment