2017-12-15 11:12:49 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-12-14 10:18:00 +00:00
|
|
|
|
2016-10-19 15:55:36 +00:00
|
|
|
# Usage: git setmail
|
2016-10-19 15:20:54 +00:00
|
|
|
#
|
2017-12-15 11:12:49 +00:00
|
|
|
# Configuration format:
|
2016-10-19 15:55:36 +00:00
|
|
|
# [
|
|
|
|
# {
|
|
|
|
# "match": ".*",
|
|
|
|
# "email": "mymail@example.com",
|
2023-12-16 17:48:15 +00:00
|
|
|
# "signingkey": "...",
|
2016-10-19 15:55:36 +00:00
|
|
|
# }
|
|
|
|
# ]
|
2016-10-19 15:20:54 +00:00
|
|
|
|
2017-12-15 11:12:49 +00:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import os.path
|
|
|
|
import json
|
2016-10-19 15:20:54 +00:00
|
|
|
|
2017-12-15 11:12:49 +00:00
|
|
|
CONFIG = os.path.expanduser('~/.config/git-committerconfig.json')
|
2016-10-19 15:20:54 +00:00
|
|
|
|
|
|
|
|
2017-12-15 11:12:49 +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:
|
2023-12-16 17:48:15 +00:00
|
|
|
print('Could not find origin, not setting local git configuration.')
|
2017-12-15 11:12:49 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if os.path.isfile(CONFIG):
|
|
|
|
combinations = json.loads(open(CONFIG).read())
|
|
|
|
else:
|
|
|
|
print('Config ({}) found.'.format(CONFIG))
|
|
|
|
return 1
|
|
|
|
|
|
|
|
for combi in combinations:
|
|
|
|
if re.search(combi['match'], repo):
|
2023-12-16 17:48:15 +00:00
|
|
|
set_local_config('user.email', combi['email'])
|
|
|
|
|
|
|
|
if 'signingkey' in combi and combi['signingkey'] != '':
|
|
|
|
set_local_config('user.signingkey', combi['signingkey'])
|
|
|
|
set_local_config('gpg.format', 'ssh' if re.search(
|
|
|
|
r'^(?:ssh|ecdsa)', combi['signingkey']) else 'openpgp')
|
|
|
|
|
2017-12-15 11:12:49 +00:00
|
|
|
if 'commit-opts' in combi:
|
2023-12-16 17:48:15 +00:00
|
|
|
set_local_config('commit.cliopts', combi['commit-opts'])
|
|
|
|
|
2017-12-15 11:12:49 +00:00
|
|
|
break
|
2016-10-19 15:20:54 +00:00
|
|
|
|
2017-12-15 11:12:49 +00:00
|
|
|
return 0
|
2016-10-19 15:20:54 +00:00
|
|
|
|
|
|
|
|
2023-12-16 17:48:15 +00:00
|
|
|
def set_local_config(param, value):
|
|
|
|
subprocess.check_call(['git', 'config', '--local', param, value])
|
|
|
|
|
|
|
|
|
2016-10-19 15:20:54 +00:00
|
|
|
if __name__ == "__main__":
|
2017-12-15 11:12:49 +00:00
|
|
|
exit(main())
|