#!/usr/bin/env python3 # Usage: git setmail myemail@example.com:"github.com:myprefix" myothermail@example.com:".*" # # Configuration format: One combination per line `:` import sys, re, subprocess, os.path CONFIG=os.path.expanduser('~/.config/git-setmail.conf') def main(): try: repo = [x.split()[1] for x in subprocess.check_output(['git', 'remote', '-v'], universal_newlines=True).split('\n') if re.match('origin.*\(push\)', x)][0] except: print('Could not find origin, not setting email.') return 1 combinations = sys.argv[1:] if len(combinations) == 0: if os.path.isfile(CONFIG): combinations = open(CONFIG).read().split('\n') else: print('Neither CLI arguments nor config ({}) found.'.format(CONFIG)) return 1 for combi in combinations: email, match = combi.split(':', 1) if re.search(match, repo): if email == subprocess.check_output(['git', 'config', 'user.email'], universal_newlines=True).strip(): print('Email correctly set to "{}", not modifying.'.format(email)) else: print('Found repo "{}", setting email to "{}"...'.format(repo, email)) subprocess.check_call(['git', 'config', 'user.email', email]) break return 0 if __name__ == "__main__": exit(main())