clear_ref_cache(): inline function
[git/jrn.git] / contrib / ciabot / ciabot.py
blob9775dffb5d49521a7078643104762cd136edffcc
1 #!/usr/bin/env python
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. If there's nothing unusual about your hosting setup,
14 # you can specify the project name with a -p option and avoid having
15 # to modify this script. Try it with -n to see the notification mail
16 # dumped to stdout and verify that it looks sane. With -V it dumps its
17 # version and exits.
19 # In post-commit, run it without arguments (other than possibly a -p
20 # option). It will query for current HEAD and the latest commit ID to
21 # get the information it needs.
23 # In update, call it with a refname followed by a list of commits:
24 # You want to reverse the order git rev-list emits becxause it lists
25 # from most recent to oldest.
27 # /path/to/ciabot.py ${refname} $(git rev-list ${oldhead}..${newhead} | tac)
29 # Note: this script uses mail, not XML-RPC, in order to avoid stalling
30 # until timeout when the CIA XML-RPC server is down.
34 # The project as known to CIA. You will either want to change this
35 # or invoke the script with a -p option to set it.
37 project=None
40 # You may not need to change these:
42 import os, sys, commands, socket, urllib
44 # Name of the repository.
45 # You can hardwire this to make the script faster.
46 repo = os.path.basename(os.getcwd())
48 # Fully-qualified domain name of this host.
49 # You can hardwire this to make the script faster.
50 host = socket.getfqdn()
52 # Changeset URL prefix for your repo: when the commit ID is appended
53 # to this, it should point at a CGI that will display the commit
54 # through gitweb or something similar. The defaults will probably
55 # work if you have a typical gitweb/cgit setup.
57 #urlprefix="http://%(host)s/cgi-bin/gitweb.cgi?p=%(repo)s;a=commit;h="
58 urlprefix="http://%(host)s/cgi-bin/cgit.cgi/%(repo)s/commit/?id="
60 # The service used to turn your gitwebbish URL into a tinyurl so it
61 # will take up less space on the IRC notification line.
62 tinyifier = "http://tinyurl.com/api-create.php?url="
64 # The template used to generate the XML messages to CIA. You can make
65 # visible changes to the IRC-bot notification lines by hacking this.
66 # The default will produce a notfication line that looks like this:
68 # ${project}: ${author} ${repo}:${branch} * ${rev} ${files}: ${logmsg} ${url}
70 # By omitting $files you can collapse the files part to a single slash.
71 xml = '''\
72 <message>
73 <generator>
74 <name>CIA Python client for Git</name>
75 <version>%(gitver)s</version>
76 <url>%(generator)s</url>
77 </generator>
78 <source>
79 <project>%(project)s</project>
80 <branch>%(repo)s:%(branch)s</branch>
81 </source>
82 <timestamp>%(ts)s</timestamp>
83 <body>
84 <commit>
85 <author>%(author)s</author>
86 <revision>%(rev)s</revision>
87 <files>
88 %(files)s
89 </files>
90 <log>%(logmsg)s %(url)s</log>
91 <url>%(url)s</url>
92 </commit>
93 </body>
94 </message>
95 '''
98 # No user-serviceable parts below this line:
101 # Addresses for the e-mail. The from address is a dummy, since CIA
102 # will never reply to this mail.
103 fromaddr = "CIABOT-NOREPLY@" + host
104 toaddr = "cia@cia.navi.cx"
106 # Identify the generator script.
107 # Should only change when the script itself gets a new home and maintainer.
108 generator="http://www.catb.org/~esr/ciabot.py"
110 def do(command):
111 return commands.getstatusoutput(command)[1]
113 def report(refname, merged):
114 "Generate a commit notification to be reported to CIA"
116 # Try to tinyfy a reference to a web view for this commit.
117 try:
118 url = open(urllib.urlretrieve(tinyifier + urlprefix + merged)[0]).read()
119 except:
120 url = urlprefix + merged
122 branch = os.path.basename(refname)
124 # Compute a shortnane for the revision
125 rev = do("git describe '"+ merged +"' 2>/dev/null") or merged[:12]
127 # Extract the neta-information for the commit
128 rawcommit = do("git cat-file commit " + merged)
129 files=do("git diff-tree -r --name-only '"+ merged +"' | sed -e '1d' -e 's-.*-<file>&</file>-'")
130 inheader = True
131 headers = {}
132 logmsg = ""
133 for line in rawcommit.split("\n"):
134 if inheader:
135 if line:
136 fields = line.split()
137 headers[fields[0]] = " ".join(fields[1:])
138 else:
139 inheader = False
140 else:
141 logmsg = line
142 break
143 (author, ts) = headers["author"].split(">")
145 # This discards the part of the authors addrsss after @.
146 # Might be bnicece to ship the full email address, if not
147 # for spammers' address harvesters - getting this wrong
148 # would make the freenode #commits channel into harvester heaven.
149 author = author.replace("<", "").split("@")[0].split()[-1]
151 # This ignores the timezone. Not clear what to do with it...
152 ts = ts.strip().split()[0]
154 context = locals()
155 context.update(globals())
157 out = xml % context
159 message = '''\
160 Message-ID: <%(merged)s.%(author)s@%(project)s>
161 From: %(fromaddr)s
162 To: %(toaddr)s
163 Content-type: text/xml
164 Subject: DeliverXML
166 %(out)s''' % locals()
168 return message
170 if __name__ == "__main__":
171 import getopt
173 try:
174 (options, arguments) = getopt.getopt(sys.argv[1:], "np:V")
175 except getopt.GetoptError, msg:
176 print "ciabot.py: " + str(msg)
177 raise SystemExit, 1
179 mailit = True
180 for (switch, val) in options:
181 if switch == '-p':
182 project = val
183 elif switch == '-n':
184 mailit = False
185 elif switch == '-V':
186 print "ciabot.py: version 3.2"
187 sys.exit(0)
189 # Cough and die if user has not specified a project
190 if not project:
191 sys.stderr.write("ciabot.py: no project specified, bailing out.\n")
192 sys.exit(1)
194 # We'll need the git version number.
195 gitver = do("git --version").split()[0]
197 urlprefix = urlprefix % globals()
199 # The script wants a reference to head followed by the list of
200 # commit ID to report about.
201 if len(arguments) == 0:
202 refname = do("git symbolic-ref HEAD 2>/dev/null")
203 merges = [do("git rev-parse HEAD")]
204 else:
205 refname = arguments[0]
206 merges = arguments[1:]
208 if mailit:
209 import smtplib
210 server = smtplib.SMTP('localhost')
212 for merged in merges:
213 message = report(refname, merged)
214 if mailit:
215 server.sendmail(fromaddr, [toaddr], message)
216 else:
217 print message
219 if mailit:
220 server.quit()
222 #End