forked from bluef/gith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-receive
49 lines (36 loc) · 1.29 KB
/
post-receive
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
#!/usr/bin/env python
import os
import sys
import re
import smtplib
from email.mime.text import MIMEText
from subprocess import Popen
from subprocess import PIPE
'''
send the mail to the admin after someone has pushed to the remote server
'''
receiver = 'you@host' #your email address
sender = 'someone@somehost' #need to be a valid one
def sendMail(user, ref, content):
msg = MIMEText('%(name)s just pushed to %(ref)s \n\nList of files been modified:\n %(content)s' % {'name':user, 'ref':ref, 'content':content})
msg['Subject'] = '[PUSH ALERT] %(name)s just pushed to %(ref)s' % {'name':user, 'ref':ref}
msg['From'] = sender
msg['To'] = receiver
server = smtplib.SMTP('localhost')
server.sendmail(sender, [receiver], msg.as_string())
server.quit()
def getDiff(oldRev, newRev):
diff = Popen("git diff --stat " + oldRev + ".." + newRev, stdout=PIPE, shell=True).communicate()[0]
return diff
user = os.getenv("GIT_USER")
line = sys.stdin.readline() #read push information with stdin
result = re.compile(r'^(.+?)\s(.+?)\s(.+)\n$').search(line)
if result is None : #unable to resolve the parameters
print 'Critical error'
sys.exit(1)
else :
oldRev = result.group(1).strip()
newRev = result.group(2).strip()
ref = result.group(3).strip()
msg = getDiff(oldRev, newRev)
sendMail(user, ref, msg)