-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-repo-add-email-notifications
executable file
·84 lines (71 loc) · 2.91 KB
/
git-repo-add-email-notifications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# Configures email notification in git repositories.
# Requires:
# 1. Path to git_multimail.py from 'contrib/hooks/multimail' in the git source code
# (https://github.com/mhagger/git-multimail)
# 2. '/usr/sbin/sendmail' is configured properly to send emails
# e.g.
# Enable email notifications for '*.git/'
# $ repo-add-email-notifications -a -e 'xxx@my-email.com'
#
# Enable email notifications for one repo
# $ repo-add-email-notifications -e 'xxx@my-email.com MY-REPO.git
import sys
import os
import glob
import subprocess
import optparse
import ConfigParser
def program_name():
return os.path.basename(sys.argv[0])
SAMPLE_CONFIG = '''
# NOTE: these are only used as the _DEFAULTS_ for git-repo-add-email-notifications
# See $REPO.git/config for the actual configured email addresses
[emails]
i@earth.org
# scottt.tw@gmail.com
'''
def main(args):
p = optparse.OptionParser(usage='%prog [OPTIONS] REPO [REPO]...', option_list=[
optparse.Option('-a', '--all', action='store_true', default=False,
help='install email hook in all bare repos (*.git) in current directory'),
optparse.Option('-e', '--emails', default=None,
help='initial list of comma separated email addresses'),
optparse.Option('-c', '--config', default='emails.ini',
help='read email addresses from INI file'),
optparse.Option('--git-multimail-path',
default=os.path.expanduser(
'~/git-multimail/git-multimail/git_multimail.py'),
help=('path to git_multiimail.py from '
'"congrib/hooks/multimail" in git source')),
])
(options, args) = p.parse_args(args)
if options.emails is not None:
emails = [ x for x in options.emails.split(',') ]
else:
try:
config_file = open(options.config)
except IOError:
sys.stderr.write('%s: write a "emails.ini" file like below or pass "--emails"\n'
% (program_name(),))
sys.stderr.write(SAMPLE_CONFIG)
sys.exit(2)
with config_file:
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(config_file)
emails = config.options('emails')
if options.all:
args = glob.glob('*.git')
if not args:
p.print_help()
sys.exit(2)
for i in args:
if not os.path.isdir(i):
sys.stderr.write('"%s" is not a repo, ignoring\n' % (i,))
continue
subprocess.check_call(['ln', '-sf', options.git_multimail_path,
os.path.join(i, 'hooks', 'post-receive')])
subprocess.check_call(['git', 'config', 'multimailhook.mailinglist', ', '.join(emails)],
cwd=i)
if __name__ == '__main__':
main(sys.argv[1:])