131
|
1 #!/usr/bin/env python
|
|
2 #
|
132
|
3 # hgweb.py - 0.2 - 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
|
131
|
4 # - web interface to a mercurial repository
|
|
5 #
|
|
6 # This software may be used and distributed according to the terms
|
|
7 # of the GNU General Public License, incorporated herein by reference.
|
|
8
|
|
9 # useful for debugging
|
|
10 import cgitb
|
|
11 cgitb.enable()
|
|
12
|
|
13 import os, cgi, time, re, difflib, sys, zlib
|
|
14 from mercurial import hg, mdiff
|
|
15
|
|
16 def nl2br(text):
|
|
17 return re.sub('\n', '<br />', text)
|
|
18
|
|
19 def obfuscate(text):
|
|
20 l = []
|
|
21 for c in text:
|
|
22 l.append('&#%d;' % ord(c))
|
|
23 return ''.join(l)
|
|
24
|
|
25 def httphdr(type):
|
|
26 print 'Content-type: %s\n' % type
|
|
27
|
|
28 class page:
|
|
29 def __init__(self, type="text/html", title="Mercurial Web",
|
|
30 charset="ISO-8859-1"):
|
|
31 print 'Content-type: %s; charset=%s\n' % (type, charset)
|
|
32 print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
|
|
33 print '<HTML>'
|
|
34 print '<!-- created by hgweb 0.1 - jake@edge2.net -->'
|
|
35 print '<HEAD><TITLE>%s</TITLE>' % title
|
|
36 print '<style type="text/css">'
|
|
37 print 'body { font-family: sans-serif; font-size: 12px; }'
|
|
38 print 'table { font-size: 12px; }'
|
|
39 print '.errmsg { font-size: 200%; color: red; }'
|
|
40 print '.filename { font-size: 150%; color: purple; }'
|
|
41 print '.manifest { font-size: 150%; color: purple; }'
|
|
42 print '.filehist { font-size: 150%; color: purple; }'
|
|
43 print '.plusline { color: green; }'
|
|
44 print '.minusline { color: red; }'
|
|
45 print '.atline { color: purple; }'
|
|
46 print '</style>'
|
|
47 print '</HEAD>'
|
|
48 print '<BODY>'
|
|
49
|
|
50 def endpage(self):
|
|
51 print '</BODY>'
|
|
52 print '</HTML>'
|
|
53
|
|
54 def show_diff(self, a, b, fn):
|
|
55 a = a.splitlines(1)
|
|
56 b = b.splitlines(1)
|
|
57 l = difflib.unified_diff(a, b, fn, fn)
|
|
58 print '<pre>'
|
|
59 for line in l:
|
|
60 line = cgi.escape(line[:-1])
|
|
61 if line.startswith('+'):
|
|
62 print '<span class="plusline">%s</span>' % (line, )
|
|
63 elif line.startswith('-'):
|
|
64 print '<span class="minusline">%s</span>' % (line, )
|
|
65 elif line.startswith('@'):
|
|
66 print '<span class="atline">%s</span>' % (line, )
|
|
67 else:
|
|
68 print line
|
|
69 print '</pre>'
|
|
70
|
|
71 class errpage(page):
|
|
72 def __init__(self):
|
|
73 page.__init__(self, title="Mercurial Web Error Page")
|
|
74
|
|
75 class change_list(page):
|
|
76
|
|
77 numchanges = 50 # number of changes to show
|
|
78
|
|
79 def __init__(self, repo, reponame):
|
|
80 page.__init__(self)
|
|
81 self.repo = repo
|
|
82 print '<h3>Changes For: %s</h3>' % reponame
|
|
83
|
|
84 def content(self, hi=None):
|
|
85 cl = []
|
|
86 count = self.repo.changelog.count()
|
|
87 if not hi:
|
|
88 hi = count
|
|
89 elif hi < self.numchanges:
|
|
90 hi = self.numchanges
|
|
91
|
|
92 start = 0
|
|
93 if hi - self.numchanges >= 0:
|
|
94 start = hi - self.numchanges
|
|
95
|
|
96 nav = "Displaying Revisions: %d-%d" % (start, hi-1)
|
|
97 if start != 0:
|
|
98 nav = ('<a href="?cmd=changes;hi=%d">Previous %d</a> ' \
|
|
99 % (start, self.numchanges)) + nav
|
|
100 if hi != count:
|
|
101 if hi + self.numchanges <= count:
|
|
102 nav += ' <a href="?cmd=changes;hi=%d">Next %d</a>' \
|
|
103 % (hi + self.numchanges, self.numchanges)
|
|
104 else:
|
|
105 nav += ' <a href="?cmd=changes">Next %d</a>' % \
|
|
106 self.numchanges
|
|
107
|
|
108 print '<center>%s</center>' % nav
|
|
109
|
|
110 for i in xrange(start, hi):
|
|
111 n = self.repo.changelog.node(i)
|
|
112 cl.append((n, self.repo.changelog.read(n)))
|
|
113 cl.reverse()
|
|
114
|
|
115 print '<table summary="" width="100%" align="center">'
|
|
116 for n, ch in cl:
|
|
117 print '<tr><td>'
|
|
118 self.change_table(n, ch)
|
|
119 print '</td></tr>'
|
|
120 print '</table>'
|
|
121
|
|
122 print '<center>%s</center>' % nav
|
|
123
|
|
124 def change_table(self, nodeid, changes):
|
|
125 hn = hg.hex(nodeid)
|
|
126 i = self.repo.changelog.rev(nodeid)
|
|
127 (h1, h2) = [ hg.hex(x) for x in self.repo.changelog.parents(nodeid) ]
|
|
128 datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0])))
|
|
129 print '<table summary="" width="100%" border="1">'
|
|
130 print '\t<tr><td valign="top" width="10%">author:</td>' + \
|
|
131 '<td valign="top" width="20%%">%s</td>' % \
|
|
132 (obfuscate(changes[1]), )
|
|
133 print '\t\t<td valign="top" width="10%">description:</td>' + \
|
|
134 '<td width="60%">' + \
|
|
135 '<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
136 (hn, nl2br(cgi.escape(changes[4])), )
|
|
137 print '\t<tr><td>date:</td><td>%s UTC</td>' % (datestr, )
|
|
138 print '\t\t<td valign="top">files:</td><td valign="top">'
|
|
139 for f in changes[3]:
|
|
140 print '\t\t<a href="?cmd=file;cs=%s;fn=%s">%s</a> ' % \
|
|
141 (hn, f, cgi.escape(f), )
|
|
142 print '\t</td></tr>'
|
|
143 print '\t<tr><td>revision:</td><td colspan="3">%d:<a ' % (i, ) + \
|
|
144 'href="?cmd=chkin;nd=%s">%s</a></td></tr>' % (hn, hn, )
|
|
145 print '</table><br />'
|
|
146
|
|
147 class checkin(page):
|
|
148 def __init__(self, repo, nodestr):
|
|
149 page.__init__(self)
|
|
150 self.repo = repo
|
|
151 self.node = hg.bin(nodestr)
|
|
152 self.nodestr = nodestr
|
|
153 print '<h3>Checkin: %s</h3>' % nodestr
|
|
154
|
|
155 def content(self):
|
|
156 changes = self.repo.changelog.read(self.node)
|
|
157 i = self.repo.changelog.rev(self.node)
|
|
158 parents = self.repo.changelog.parents(self.node)
|
|
159 (h1, h2) = [ hg.hex(x) for x in parents ]
|
|
160 (i1, i2) = [ self.repo.changelog.rev(x) for x in parents ]
|
|
161 datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0])))
|
|
162 mf = self.repo.manifest.read(changes[0])
|
|
163 print '<table summary="" width="100%" border="1">'
|
|
164 print '\t<tr><td>revision:</td><td colspan="3">%d:' % (i, ),
|
|
165 print '<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
166 (self.nodestr, self.nodestr, )
|
|
167 print '\t<tr><td>parent(s):</td><td colspan="3">%d:' % (i1, )
|
|
168 print '<a href="?cmd=chkin;nd=%s">%s</a>' % (h1, h1, ),
|
|
169 if i2 != -1:
|
|
170 print ' %d:<a href="?cmd=chkin;nd=%s">%s</a>' % \
|
|
171 (i2, h2, h2, ),
|
|
172 else:
|
|
173 print ' %d:%s' % (i2, h2, ),
|
|
174 print '</td></tr>'
|
|
175 print '\t<tr><td>manifest:</td><td colspan="3">%d:' % \
|
|
176 (self.repo.manifest.rev(changes[0]), ),
|
|
177 print '<a href="?cmd=mf;nd=%s">%s</a></td></tr>' % \
|
|
178 (hg.hex(changes[0]), hg.hex(changes[0]), )
|
|
179 print '\t<tr><td valign="top" width="10%">author:</td>' + \
|
|
180 '<td valign="top" width="20%%">%s</td>' % \
|
|
181 (obfuscate(changes[1]), )
|
|
182 print '\t\t<td valign="top" width="10%">description:</td>' + \
|
|
183 '<td width="60%">' + \
|
|
184 '<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
185 (self.nodestr, nl2br(cgi.escape(changes[4])), )
|
|
186 print '\t<tr><td>date:</td><td>%s UTC</td>' % (datestr, )
|
|
187 print '\t\t<td valign="top">files:</td><td valign="top">'
|
|
188 for f in changes[3]:
|
|
189 print '\t\t<a href="?cmd=file;nd=%s&fn=%s">%s</a>' % \
|
|
190 (hg.hex(mf[f]), f, cgi.escape(f), ),
|
|
191 print ' '
|
|
192 print '\t</td></tr>'
|
|
193 print '</table><br />'
|
|
194
|
|
195 (c, a, d) = self.repo.diffrevs(parents[0], self.node)
|
|
196 change = self.repo.changelog.read(parents[0])
|
|
197 mf2 = self.repo.manifest.read(change[0])
|
|
198 for f in c:
|
|
199 self.show_diff(self.repo.file(f).read(mf2[f]), \
|
|
200 self.repo.file(f).read(mf[f]), f)
|
|
201 for f in a:
|
|
202 self.show_diff('', self.repo.file(f).read(mf[f]), f)
|
|
203 for f in d:
|
|
204 self.show_diff(self.repo.file(f).read(mf2[f]), '', f)
|
|
205
|
|
206 class filepage(page):
|
|
207 def __init__(self, repo, fn, node=None, cs=None):
|
|
208 page.__init__(self)
|
|
209 self.repo = repo
|
|
210 self.fn = fn
|
|
211 if cs:
|
|
212 chng = self.repo.changelog.read(hg.bin(cs))
|
|
213 mf = self.repo.manifest.read(chng[0])
|
|
214 self.node = mf[self.fn]
|
|
215 self.nodestr = hg.hex(self.node)
|
|
216 else:
|
|
217 self.nodestr = node
|
|
218 self.node = hg.bin(node)
|
|
219 print '<div class="filename">%s (%s)</div>' % \
|
|
220 (cgi.escape(self.fn), self.nodestr, )
|
|
221 print '<a href="?cmd=hist;fn=%s">history</a><br />' % self.fn
|
|
222
|
|
223 def content(self):
|
|
224 print '<pre>'
|
|
225 print cgi.escape(self.repo.file(self.fn).read(self.node))
|
|
226 print '</pre>'
|
|
227
|
|
228 class mfpage(page):
|
|
229 def __init__(self, repo, node):
|
|
230 page.__init__(self)
|
|
231 self.repo = repo
|
|
232 self.nodestr = node
|
|
233 self.node = hg.bin(node)
|
|
234
|
|
235 def content(self):
|
|
236 mf = self.repo.manifest.read(self.node)
|
|
237 fns = mf.keys()
|
|
238 fns.sort()
|
|
239 print '<div class="manifest">Manifest (%s)</div>' % self.nodestr
|
|
240 for f in fns:
|
|
241 print '<a href="?cmd=file;fn=%s;nd=%s">%s</a><br />' % \
|
|
242 (f, hg.hex(mf[f]), f)
|
|
243
|
|
244 class histpage(page):
|
|
245 def __init__(self, repo, fn):
|
|
246 page.__init__(self)
|
|
247 self.repo = repo
|
|
248 self.fn = fn
|
|
249
|
|
250 def content(self):
|
|
251 print '<div class="filehist">File History: %s</div>' % self.fn
|
|
252 r = self.repo.file(self.fn)
|
|
253 print '<br />'
|
|
254 print '<table summary="" width="100%" align="center">'
|
|
255 for i in xrange(r.count()-1, -1, -1):
|
|
256 n = r.node(i)
|
|
257 (p1, p2) = r.parents(n)
|
|
258 (h, h1, h2) = map(hg.hex, (n, p1, p2))
|
|
259 (i1, i2) = map(r.rev, (p1, p2))
|
|
260 ci = r.linkrev(n)
|
|
261 cn = self.repo.changelog.node(ci)
|
|
262 cs = hg.hex(cn)
|
|
263 changes = self.repo.changelog.read(cn)
|
|
264 print '<tr><td>'
|
|
265 self.hist_ent(i, h, i1, h1, i2, h2, ci, cs, changes)
|
|
266 print '</tr></td>'
|
|
267 print '</table>'
|
|
268
|
|
269 def hist_ent(self, revi, revs, p1i, p1s, p2i, p2s, ci, cs, changes):
|
|
270 datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0])))
|
|
271 print '<table summary="" width="100%" border="1">'
|
|
272 print '\t<tr><td valign="top" width="10%">author:</td>' + \
|
|
273 '<td valign="top" width="20%%">%s</td>' % \
|
|
274 (obfuscate(changes[1]), )
|
|
275 print '\t\t<td valign="top" width="10%">description:</td>' + \
|
|
276 '<td width="60%">' + \
|
|
277 '<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
278 (cs, nl2br(cgi.escape(changes[4])), )
|
|
279 print '\t<tr><td>date:</td><td>%s UTC</td>' % (datestr, )
|
|
280 print '\t\t<td>revision:</td><td>%d:<a ' % (revi, ) + \
|
|
281 'href="?cmd=file;cs=%s;fn=%s">%s</a></td></tr>' % \
|
|
282 (cs, self.fn, revs )
|
|
283 print '\t<tr><td>parent(s):</td><td colspan="3">%d:' % (p1i, )
|
|
284 print '<a href="?cmd=file;nd=%s;fn=%s">%s</a>' % (p1s, self.fn, p1s, ),
|
|
285 if p2i != -1:
|
|
286 print ' %d:<a href="?cmd=file;nd=%s;fn=%s">%s</a>' % \
|
|
287 (p2i, p2s, self.fn, p2s ),
|
|
288 print '</td></tr>'
|
|
289 print '</table><br />'
|
|
290
|
132
|
291 class hgweb:
|
|
292 repo_path = "."
|
|
293 numchanges = 50
|
131
|
294
|
132
|
295 def __init__(self):
|
|
296 pass
|
131
|
297
|
132
|
298 def run(self):
|
|
299
|
|
300 args = cgi.parse()
|
|
301
|
|
302 ui = hg.ui()
|
|
303 repo = hg.repository(ui, self.repo_path)
|
131
|
304
|
132
|
305 if not args.has_key('cmd') or args['cmd'][0] == 'changes':
|
|
306 page = change_list(repo, 'Mercurial')
|
|
307 hi = args.get('hi', ( repo.changelog.count(), ))
|
|
308 page.content(hi = int(hi[0]))
|
|
309 page.endpage()
|
|
310
|
|
311 elif args['cmd'][0] == 'chkin':
|
|
312 if not args.has_key('nd'):
|
|
313 page = errpage()
|
|
314 print '<div class="errmsg">No Node!</div>'
|
|
315 else:
|
|
316 page = checkin(repo, args['nd'][0])
|
|
317 page.content()
|
|
318 page.endpage()
|
131
|
319
|
132
|
320 elif args['cmd'][0] == 'file':
|
|
321 if not (args.has_key('nd') and args.has_key('fn')) and \
|
|
322 not (args.has_key('cs') and args.has_key('fn')):
|
|
323 page = errpage()
|
|
324 print '<div class="errmsg">Invalid Args!</div>'
|
|
325 else:
|
|
326 if args.has_key('nd'):
|
|
327 page = filepage(repo, args['fn'][0], node=args['nd'][0])
|
|
328 else:
|
|
329 page = filepage(repo, args['fn'][0], cs=args['cs'][0])
|
|
330 page.content()
|
|
331 page.endpage()
|
131
|
332
|
132
|
333 elif args['cmd'][0] == 'mf':
|
|
334 if not args.has_key('nd'):
|
|
335 page = errpage()
|
|
336 print '<div class="errmsg">No Node!</div>'
|
|
337 else:
|
|
338 page = mfpage(repo, args['nd'][0])
|
|
339 page.content()
|
|
340 page.endpage()
|
131
|
341
|
132
|
342 elif args['cmd'][0] == 'hist':
|
|
343 if not args.has_key('fn'):
|
|
344 page = errpage()
|
|
345 print '<div class="errmsg">No Filename!</div>'
|
|
346 else:
|
|
347 page = histpage(repo, args['fn'][0])
|
|
348 page.content()
|
|
349 page.endpage()
|
|
350
|
|
351 elif args['cmd'][0] == 'branches':
|
|
352 httphdr("text/plain")
|
|
353 nodes = []
|
|
354 if args.has_key('nodes'):
|
|
355 nodes = map(hg.bin, args['nodes'][0].split(" "))
|
|
356 for b in repo.branches(nodes):
|
|
357 print " ".join(map(hg.hex, b))
|
131
|
358
|
132
|
359 elif args['cmd'][0] == 'between':
|
|
360 httphdr("text/plain")
|
|
361 nodes = []
|
|
362 if args.has_key('pairs'):
|
|
363 pairs = [ map(hg.bin, p.split("-"))
|
|
364 for p in args['pairs'][0].split(" ") ]
|
|
365 for b in repo.between(pairs):
|
|
366 print " ".join(map(hg.hex, b))
|
|
367
|
|
368 elif args['cmd'][0] == 'changegroup':
|
|
369 httphdr("application/hg-changegroup")
|
|
370 nodes = []
|
|
371 if args.has_key('roots'):
|
|
372 nodes = map(hg.bin, args['roots'][0].split(" "))
|
131
|
373
|
132
|
374 z = zlib.compressobj()
|
|
375 for chunk in repo.changegroup(nodes):
|
|
376 sys.stdout.write(z.compress(chunk))
|
|
377
|
|
378 sys.stdout.write(z.flush())
|
131
|
379
|
132
|
380 else:
|
|
381 page = errpage()
|
|
382 print '<div class="errmsg">unknown command: %s</div>' % \
|
|
383 cgi.escape(args['cmd'][0])
|
|
384 page.endpage()
|
131
|
385
|
132
|
386 if __name__ == "__main__":
|
|
387 hgweb().run()
|