2 # Copyright (c) 2010 Eric S. Raymond <esr@thyrsus.com>
3 # Distributed under BSD terms.
5 # This script contains porcelain and porcelain byproducts.
6 # It's Python because the Python standard libraries avoid portability/security
7 # issues raised by callouts in the ancestral Perl and sh scripts. It should
8 # be compatible back to Python 2.1.5
10 # usage: ciabot.py [-V] [-n] [-p projectname] [refname [commits...]]
12 # This script is meant to be run either in a post-commit hook or in an
13 # update hook. Try it with -n to see the notification mail dumped to
14 # stdout and verify that it looks sane. With -V it dumps its version
17 # In post-commit, run it without arguments. It will query for
18 # current HEAD and the latest commit ID to get the information it
21 # In update, call it with a refname followed by a list of commits:
22 # You want to reverse the order git rev-list emits because it lists
23 # from most recent to oldest.
25 # /path/to/ciabot.py ${refname} $(git rev-list ${oldhead}..${newhead} | tac)
27 # Configuration variables affecting this script:
29 # ciabot.project = name of the project
30 # ciabot.repo = name of the project repo for gitweb/cgit purposes
31 # ciabot.xmlrpc = if true (default), ship notifications via XML-RPC
32 # ciabot.revformat = format in which the revision is shown
34 # ciabot.project defaults to the directory name of the repository toplevel.
35 # ciabot.repo defaults to ciabot.project lowercased.
37 # This means that in the normal case you need not do any configuration at all,
38 # but setting the project name will speed it up slightly.
40 # The revformat variable may have the following values
41 # raw -> full hex ID of commit
42 # short -> first 12 chars of hex ID
43 # describe = -> describe relative to last tag, falling back to short
44 # The default is 'describe'.
46 # Note: the CIA project now says only XML-RPC is reliable, so
51 if sys.hexversion < 0x02000000:
52 # The limiter is the xml.sax module
53 sys.stderr.write("ciabot.py: requires Python 2.0.0 or later.\n")
56 import os, commands, socket, urllib
57 from xml.sax.saxutils import escape
59 # Changeset URL prefix for your repo: when the commit ID is appended
60 # to this, it should point at a CGI that will display the commit
61 # through gitweb or something similar. The defaults will probably
62 # work if you have a typical gitweb/cgit setup.
64 #urlprefix="http://%(host)s/cgi-bin/gitweb.cgi?p=%(repo)s;a=commit;h="
65 urlprefix="http://%(host)s/cgi-bin/cgit.cgi/%(repo)s/commit/?id="
67 # The service used to turn your gitwebbish URL into a tinyurl so it
68 # will take up less space on the IRC notification line.
69 tinyifier = "http://tinyurl.com/api-create.php?url="
71 # The template used to generate the XML messages to CIA. You can make
72 # visible changes to the IRC-bot notification lines by hacking this.
73 # The default will produce a notfication line that looks like this:
75 # ${project}: ${author} ${repo}:${branch} * ${rev} ${files}: ${logmsg} ${url}
77 # By omitting $files you can collapse the files part to a single slash.
81 <name>CIA Python client for Git</name>
82 <version>%(version)s</version>
83 <url>%(generator)s</url>
86 <project>%(project)s</project>
87 <branch>%(repo)s:%(branch)s</branch>
89 <timestamp>%(ts)s</timestamp>
92 <author>%(author)s</author>
93 <revision>%(rev)s</revision>
97 <log>%(logmsg)s %(url)s</log>
105 # No user-serviceable parts below this line:
108 # Where to ship e-mail notifications.
109 toaddr = "cia@cia.vc"
111 # Identify the generator script.
112 # Should only change when the script itself gets a new home and maintainer.
113 generator = "http://www.catb.org/~esr/ciabot.py"
117 return commands.getstatusoutput(command)[1]
119 def report(refname, merged, xmlrpc=True):
120 "Generate a commit notification to be reported to CIA"
122 # Try to tinyfy a reference to a web view for this commit.
124 url = open(urllib.urlretrieve(tinyifier + urlprefix + merged)[0]).read()
126 url = urlprefix + merged
128 branch = os.path.basename(refname)
130 # Compute a description for the revision
131 if revformat == 'raw':
133 elif revformat == 'short':
135 else: # revformat == 'describe'
136 rev = do("git describe %s 2>/dev/null" % merged)
140 # Extract the meta-information for the commit
141 files=do("git diff-tree -r --name-only '"+ merged +"' | sed -e '1d' -e 's-.*-<file>&</file>-'")
142 metainfo = do("git log -1 '--pretty=format:%an <%ae>%n%at%n%s' " + merged)
143 (author, ts, logmsg) = metainfo.split("\n")
144 logmsg = escape(logmsg)
146 # This discards the part of the author's address after @.
147 # Might be be nice to ship the full email address, if not
148 # for spammers' address harvesters - getting this wrong
149 # would make the freenode #commits channel into harvester heaven.
150 author = escape(author.replace("<", "").split("@")[0].split()[-1])
152 # This ignores the timezone. Not clear what to do with it...
153 ts = ts.strip().split()[0]
156 context.update(globals())
160 Message-ID: <%(merged)s.%(author)s@%(project)s>
163 Content-type: text/xml
166 %(out)s''' % locals()
173 if __name__ == "__main__":
176 # Get all config variables
177 revformat = do("git config --get ciabot.revformat")
178 project = do("git config --get ciabot.project")
179 repo = do("git config --get ciabot.repo")
180 xmlrpc = do("git config --get ciabot.xmlrpc")
181 xmlrpc = not (xmlrpc and xmlrpc == "false")
183 host = socket.getfqdn()
184 fromaddr = "CIABOT-NOREPLY@" + host
187 (options, arguments) = getopt.getopt(sys.argv[1:], "np:xV")
188 except getopt.GetoptError, msg:
189 print "ciabot.py: " + str(msg)
193 for (switch, val) in options:
201 print "ciabot.py: version", version
204 # The project variable defaults to the name of the repository toplevel.
208 if os.path.exists(os.path.join(here, ".git")):
209 project = os.path.basename(here)
212 sys.stderr.write("ciabot.py: no .git below root!\n")
214 here = os.path.dirname(here)
217 repo = project.lower()
219 urlprefix = urlprefix % globals()
221 # The script wants a reference to head followed by the list of
222 # commit ID to report about.
223 if len(arguments) == 0:
224 refname = do("git symbolic-ref HEAD 2>/dev/null")
225 merges = [do("git rev-parse HEAD")]
227 refname = arguments[0]
228 merges = arguments[1:]
233 server = xmlrpclib.Server('http://cia.vc/RPC2');
236 server = smtplib.SMTP('localhost')
238 for merged in merges:
239 message = report(refname, merged, xmlrpc)
244 # RPC server is flaky, this can fail due to timeout.
245 server.hub.deliver(message)
246 except socket.error, e:
247 sys.stderr.write("%s\n" % e)
249 server.sendmail(fromaddr, [toaddr], message)