2016-10-19 15:20:54 +00:00
#!/usr/bin/env python3
2016-10-19 15:55:36 +00:00
# Usage: git setmail
2016-10-19 15:20:54 +00:00
#
2016-10-19 15:55:36 +00:00
# Configuration format:
# [
# {
# "match": ".*",
# "email": "mymail@example.com",
# "gpg-key": "...",
# }
# ]
2016-10-19 15:20:54 +00:00
2016-10-19 15:55:36 +00:00
import sys, re, subprocess, os.path, json
2016-10-19 15:20:54 +00:00
2016-10-19 15:55:36 +00:00
CONFIG=os.path.expanduser('~/.config/git-setmail.json')
2016-10-19 15:20:54 +00:00
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
2016-10-19 15:55:36 +00:00
if os.path.isfile(CONFIG):
combinations = json.loads(open(CONFIG).read())
else:
print('Config ({}) found.'.format(CONFIG))
return 1
2016-10-19 15:20:54 +00:00
for combi in combinations:
2016-10-19 15:55:36 +00:00
if re.search(combi['match'], repo):
if combi['email'] == subprocess.check_output(['git', 'config', 'user.email'], universal_newlines=True).strip():
print('Email correctly set to "{}", not modifying.'.format(combi['email']))
2016-10-19 15:20:54 +00:00
else:
2016-10-19 15:55:36 +00:00
print('Found repo "{}", setting email to "{}"...'.format(repo, combi['email']))
subprocess.check_call(['git', 'config', 'user.email', combi['email']])
if 'gpg-key' in combi and combi['gpg-key'] != '':
subprocess.check_call(['git', 'config', 'user.signingkey', combi['gpg-key']])
2016-10-19 15:20:54 +00:00
break
return 0
if __name__ == "__main__":
exit(main())