rtyler / slide-git-scripts

A collection of the Git scripts that we use here at Slide, Inc.

This URL has Read+Write access

slide-git-scripts / post-commit.py
100644 102 lines (78 sloc) 2.983 kb
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''
This post-commit hook is intended to be used to send "GITCOMMIT" emails to
the specified target address.
 
The mails sent to the specified address look something like this
 
From: tyler@slide.com
To: commits@slide.com
Subject: GITCOMMIT [$MACHINE/$BRANCH/ce0520c] Minor change
 
Author: R. Tyler Ballance <tyler@slide.com>
Commit: asdb123b123bfd123
 
Changes committed:
M file
 
Minor change
 
diff --git a/file b/file
index bc1f44a..4468922 100644
--- a/file
+++ b/file
@@ -992,28 +992,5 @@
 
$DIFF
The post-commit hook also supports sending "attention" mails with the
syntax of "attn tyler,jason". This will send a "[Review Request] mail
formatted similar to the following:
 
From: tyler@slide.com
CC: tyler@slide.com, jason@slide.com
Subject: [Review Request] Minor change
 
(commit body here)
--
file | 6 ++++--
1 files changed, 4 insertions(+), 2 deletions(-)
 
diff --git a/file b/file
index bc1f44a..4468922 100644
--- a/file
+++ b/file
@@ -992,28 +992,5 @@
 
$DIFF
'''
 
import getpass
import os
import socket
import smtplib
import sys
 
from optparse import OptionParser
 
SMTP_SERVER = 'smtp.your.com'
MAIL_SUFFIX = '@your.com'
 
def find_attn(commit):
rc = re.findall(r'(?:^|\s)attn[:\s]+([\w \t,-]+[\w,])', commit)
if not rc:
return []
if len(rc) > 1:
return rc
return rc[0].split(',')
 
def mail_commit(address):
user = os.getenv('PG_USER') or getpass.getuser()
machine = socket.gethostname()
branch = os.popen('git symbolic-ref HEAD').read().rstrip()
branch = branch.replace('refs/heads/', '')
changes = os.popen('git diff HEAD^...HEAD --name-status --no-color').read().rstrip()
message = os.popen('git show HEAD --pretty=format:"From: %(user)s@slide.com\r\nTo: %(to)s\r\nSubject: GITCOMMIT [%(hostname)s/%(branch)s/%%h] %%s\r\n\r\nAuthor: %%aN <%%ae>\r\nCommit: %%H\r\n\r\nChanges committed:\r\n\t%(changes)s\r\n\r\n%%s\r\n\r\n%%b\r\n\r\n" --no-color' % {
'user' : user, 'to' : address, 'branch' : branch, 'hostname' : machine, 'changes' : '\n\t'.join(changes.split('\n'))}).read().rstrip()
print 'Sending a commit mail to %s' % (address)
 
s = smtplib.SMTP(SMTP_SERVER)
addresses = [address]
s.sendmail('%s@%s' % (user, MAIL_SUFFIX), addresses, message)
 
addresses = find_attn(message)
if addresses:
addresses.append(user)
addresses = ['%s@%s' % (a.strip(), MAIL_SUFFIX) for a in addresses]
print 'Sending a review mail to: %s' % ','.join(addresses)
message = os.popen('git format-patch --stdout HEAD^ --subject-prefix="Review Request" --cc=%s' % ','.join(addresses)).read().rstrip()
s.sendmail('%s@%s' % (user, MAIL_SUFFIX) , addresses, message)
s.quit()
 
if __name__ == '__main__':
op = OptionParser()
op.add_option('-m', '--mail', dest='address', help='Email address to mail commit messages to')
opts, args = op.parse_args()
 
if not opts.address:
print '*** You need to specify a mailing address! ***'
exit
 
mail_commit(opts.address)