57
|
1 #!/usr/bin/env python
|
|
2 #
|
|
3 # hgweb.py - 0.1 - 9 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
|
|
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
|
61
|
13 import os, cgi, time, re, difflib, sys, zlib
|
57
|
14 from mercurial import hg, mdiff
|
|
15
|
|
16 repo_path = "." # change as needed
|
|
17
|
|
18 def nl2br(text):
|
|
19 return re.sub('\n', '<br />', text)
|
|
20
|
|
21 def obfuscate(text):
|
|
22 l = []
|
|
23 for c in text:
|
|
24 l.append('&#%d;' % ord(c))
|
|
25 return ''.join(l)
|
|
26
|
100
|
27 def httphdr(type):
|
61
|
28 print 'Content-type: %s\n' % type
|
57
|
29
|
100
|
30 class page:
|
|
31 def __init__(self, type="text/html", title="Mercurial Web",
|
|
32 charset="ISO-8859-1"):
|
|
33 print 'Content-type: %s; charset=%s\n' % (type, charset)
|
|
34 print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
|
|
35 print '<HTML>'
|
|
36 print '<!-- created by hgweb 0.1 - jake@edge2.net -->'
|
|
37 print '<HEAD><TITLE>%s</TITLE>' % title
|
|
38 print '<style type="text/css">'
|
|
39 print 'body { font-family: sans-serif; font-size: 12px; }'
|
|
40 print 'table { font-size: 12px; }'
|
|
41 print '.errmsg { font-size: 200%; color: red; }'
|
|
42 print '.filename { 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>'
|
57
|
49
|
100
|
50 def endpage(self):
|
|
51 print '</BODY>'
|
|
52 print '</HTML>'
|
61
|
53
|
100
|
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>'
|
57
|
70
|
100
|
71 class errpage(page):
|
|
72 def __init__(self):
|
|
73 page.__init__(self, title="Mercurial Web Error Page")
|
|
74
|
|
75 class change_list(page):
|
|
76 def __init__(self, repo, reponame):
|
|
77 page.__init__(self)
|
|
78 self.repo = repo
|
|
79 print '<h3>Changes For: %s</h3>' % reponame
|
57
|
80
|
100
|
81 def content(self, start='tip', end='0', boundtype='rev'):
|
|
82 print '<table summary="" width="100%" align="center">'
|
|
83 cl = []
|
|
84 for i in xrange(self.repo.changelog.count()):
|
|
85 n = self.repo.changelog.node(i)
|
|
86 cl.append((n, self.repo.changelog.read(n)))
|
|
87 cl.reverse()
|
|
88 for n, ch in cl:
|
|
89 print '<tr><td>'
|
|
90 self.change_table(n, ch)
|
|
91 print '</td></tr>'
|
|
92
|
|
93 print '</table>'
|
61
|
94
|
100
|
95 def change_table(self, nodeid, changes):
|
|
96 hn = hg.hex(nodeid)
|
|
97 i = self.repo.changelog.rev(nodeid)
|
|
98 (h1, h2) = [ hg.hex(x) for x in self.repo.changelog.parents(nodeid) ]
|
|
99 datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0])))
|
|
100 print '<table summary="" width="100%" border="1">'
|
|
101 print '\t<tr><td valign="top" width="10%">author:</td>' + \
|
|
102 '<td valign="top" width="20%%">%s</td>' % \
|
|
103 (obfuscate(changes[1]), )
|
|
104 print '\t\t<td valign="top" width="10%">description:</td>' + \
|
|
105 '<td width="60%">' + \
|
|
106 '<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
107 (hn, nl2br(cgi.escape(changes[4])), )
|
|
108 print '\t<tr><td>date:</td><td>%s UTC</td>' % (datestr, )
|
|
109 print '\t\t<td valign="top">files:</td><td valign="top">'
|
|
110 for f in changes[3]:
|
|
111 print '\t\t<a href="?cmd=file;cs=%s;fn=%s">%s</a> ' % \
|
|
112 (hn, f, cgi.escape(f), )
|
|
113 print '\t</td></tr>'
|
|
114 print '\t<tr><td>revision:</td><td colspan="3">%d:<a ' % (i, ) + \
|
|
115 'href="?cmd=chkin;nd=%s">%s</a></td></tr>' % (hn, hn, )
|
|
116 print '</table><br />'
|
|
117
|
|
118 class checkin(page):
|
|
119 def __init__(self, repo, nodestr):
|
|
120 page.__init__(self)
|
|
121 self.repo = repo
|
|
122 self.node = hg.bin(nodestr)
|
|
123 self.nodestr = nodestr
|
|
124 print '<h3>Checkin: %s</h3>' % nodestr
|
57
|
125
|
100
|
126 def content(self):
|
|
127 changes = self.repo.changelog.read(self.node)
|
|
128 i = self.repo.changelog.rev(self.node)
|
|
129 parents = self.repo.changelog.parents(self.node)
|
|
130 (h1, h2) = [ hg.hex(x) for x in parents ]
|
|
131 (i1, i2) = [ self.repo.changelog.rev(x) for x in parents ]
|
|
132 datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0])))
|
|
133 mf = self.repo.manifest.read(changes[0])
|
|
134 print '<table summary="" width="100%" border="1">'
|
|
135 print '\t<tr><td>revision:</td><td colspan="3">%d:' % (i, ),
|
|
136 print '<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
137 (self.nodestr, self.nodestr, )
|
|
138 print '\t<tr><td>parent(s):</td><td colspan="3">%d:' % (i1, )
|
|
139 print '<a href="?cmd=chkin;nd=%s">%s</a>' % (h1, h1, ),
|
|
140 if i2 != -1:
|
|
141 print ' %d:<a href="?cmd=chkin;nd=%s">%s</a>' % \
|
|
142 (i2, h2, h2, ),
|
|
143 else:
|
|
144 print ' %d:%s' % (i2, h2, ),
|
|
145 print '</td></tr>'
|
|
146 print '\t<tr><td>manifest:</td><td colspan="3">%d:' % \
|
|
147 (self.repo.manifest.rev(changes[0]), ),
|
|
148 print '<a href="?cmd=mf;nd=%s">%s</a></td></tr>' % \
|
|
149 (hg.hex(changes[0]), hg.hex(changes[0]), )
|
|
150 print '\t<tr><td valign="top" width="10%">author:</td>' + \
|
|
151 '<td valign="top" width="20%%">%s</td>' % \
|
|
152 (obfuscate(changes[1]), )
|
|
153 print '\t\t<td valign="top" width="10%">description:</td>' + \
|
|
154 '<td width="60%">' + \
|
|
155 '<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
156 (self.nodestr, nl2br(cgi.escape(changes[4])), )
|
|
157 print '\t<tr><td>date:</td><td>%s UTC</td>' % (datestr, )
|
|
158 print '\t\t<td valign="top">files:</td><td valign="top">'
|
|
159 for f in changes[3]:
|
|
160 print '\t\t<a href="?cmd=file;nd=%s&fn=%s">%s</a>' % \
|
|
161 (hg.hex(mf[f]), f, cgi.escape(f), ),
|
|
162 print ' '
|
|
163 print '\t</td></tr>'
|
|
164 print '</table><br />'
|
61
|
165
|
100
|
166 (c, a, d) = self.repo.diffrevs(parents[0], self.node)
|
|
167 change = self.repo.changelog.read(parents[0])
|
|
168 mf2 = self.repo.manifest.read(change[0])
|
|
169 for f in c:
|
|
170 self.show_diff(self.repo.file(f).read(mf2[f]), \
|
|
171 self.repo.file(f).read(mf[f]), f)
|
|
172 for f in a:
|
|
173 self.show_diff('', self.repo.file(f).read(mf[f]), f)
|
|
174 for f in d:
|
|
175 self.show_diff(self.repo.file(f).read(mf2[f]), '', f)
|
57
|
176
|
100
|
177 class filepage(page):
|
|
178 def __init__(self, repo, fn, node=None, cs=None):
|
|
179 page.__init__(self)
|
|
180 self.repo = repo
|
|
181 self.fn = fn
|
|
182 if cs:
|
|
183 chng = self.repo.changelog.read(hg.bin(cs))
|
|
184 mf = self.repo.manifest.read(chng[0])
|
|
185 self.node = mf[self.fn]
|
|
186 self.nodestr = hg.hex(self.node)
|
|
187 else:
|
|
188 self.nodestr = node
|
|
189 self.node = hg.bin(node)
|
|
190 print '<div class="filename">%s (%s)</div>' % \
|
|
191 (cgi.escape(self.fn), self.nodestr, )
|
|
192 print '<a href="?cmd=hist;fn=%s">history</a><br />' % self.fn
|
57
|
193
|
100
|
194 def content(self):
|
|
195 print '<pre>'
|
|
196 print cgi.escape(self.repo.file(self.fn).read(self.node))
|
|
197 print '</pre>'
|
57
|
198
|
|
199 args = cgi.parse()
|
|
200
|
|
201 ui = hg.ui()
|
|
202 repo = hg.repository(ui, repo_path)
|
|
203
|
|
204 if not args.has_key('cmd'):
|
100
|
205 page = change_list(repo, 'Mercurial')
|
|
206 page.content()
|
|
207 page.endpage()
|
61
|
208
|
57
|
209 elif args['cmd'][0] == 'chkin':
|
|
210 if not args.has_key('nd'):
|
100
|
211 page = errpage()
|
57
|
212 print '<div class="errmsg">No Node!</div>'
|
|
213 else:
|
100
|
214 page = checkin(repo, args['nd'][0])
|
|
215 page.content()
|
|
216 page.endpage()
|
61
|
217
|
57
|
218 elif args['cmd'][0] == 'file':
|
100
|
219 if not (args.has_key('nd') and args.has_key('fn')) and \
|
|
220 not (args.has_key('cs') and args.has_key('fn')):
|
|
221 page = errpage()
|
|
222 print '<div class="errmsg">Invalid Args!</div>'
|
57
|
223 else:
|
100
|
224 if args.has_key('nd'):
|
|
225 page = filepage(repo, args['fn'][0], node=args['nd'][0])
|
|
226 else:
|
|
227 page = filepage(repo, args['fn'][0], cs=args['cs'][0])
|
|
228 page.content()
|
|
229 page.endpage()
|
61
|
230
|
|
231 elif args['cmd'][0] == 'branches':
|
|
232 httphdr("text/plain")
|
|
233 nodes = []
|
|
234 if args.has_key('nodes'):
|
|
235 nodes = map(hg.bin, args['nodes'][0].split(" "))
|
|
236 for b in repo.branches(nodes):
|
|
237 print " ".join(map(hg.hex, b))
|
|
238
|
|
239 elif args['cmd'][0] == 'between':
|
|
240 httphdr("text/plain")
|
|
241 nodes = []
|
|
242 if args.has_key('pairs'):
|
|
243 pairs = [ map(hg.bin, p.split("-"))
|
|
244 for p in args['pairs'][0].split(" ") ]
|
|
245 for b in repo.between(pairs):
|
|
246 print " ".join(map(hg.hex, b))
|
|
247
|
|
248 elif args['cmd'][0] == 'changegroup':
|
|
249 httphdr("application/hg-changegroup")
|
|
250 nodes = []
|
|
251 if args.has_key('roots'):
|
|
252 nodes = map(hg.bin, args['roots'][0].split(" "))
|
|
253
|
|
254 z = zlib.compressobj()
|
|
255 for chunk in repo.changegroup(nodes):
|
|
256 sys.stdout.write(z.compress(chunk))
|
|
257
|
|
258 sys.stdout.write(z.flush())
|
57
|
259
|
|
260 else:
|
100
|
261 page = errpage()
|
|
262 print '<div class="errmsg">unknown command: %s</div>' % \
|
|
263 cgi.escape(args['cmd'][0])
|
|
264 page.endpage()
|