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 |
|
61
|
27 |
def httphdr(type = "text/html"):
|
|
28 |
print 'Content-type: %s\n' % type
|
57
|
29 |
|
|
30 |
def htmldoctype():
|
|
31 |
print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">'
|
|
32 |
|
|
33 |
def htmlhead(title):
|
|
34 |
print '<HTML>'
|
|
35 |
print '<!-- created by hgweb 0.1 - jake@edge2.net -->'
|
|
36 |
print '<HEAD><TITLE>%s</TITLE></HEAD>' % (title, )
|
|
37 |
print '<style type="text/css">'
|
|
38 |
print 'body { font-family: sans-serif; font-size: 12px; }'
|
|
39 |
print 'table { font-size: 12px; }'
|
|
40 |
print '.errmsg { font-size: 200%; color: red; }'
|
|
41 |
print '.filename { font-size: 150%; color: purple; }'
|
|
42 |
print '.plusline { color: green; }'
|
|
43 |
print '.minusline { color: red; }'
|
|
44 |
print '.atline { color: purple; }'
|
|
45 |
print '</style>'
|
|
46 |
|
61
|
47 |
def startpage(title):
|
|
48 |
httphdr()
|
|
49 |
htmldoctype()
|
|
50 |
htmlhead(title)
|
|
51 |
print '<BODY>'
|
|
52 |
|
|
53 |
def endpage():
|
|
54 |
print '</BODY>'
|
|
55 |
print '</HTML>'
|
|
56 |
|
|
57 |
|
|
58 |
|
57
|
59 |
def ent_change(repo, nodeid):
|
|
60 |
changes = repo.changelog.read(nodeid)
|
|
61 |
hn = hg.hex(nodeid)
|
|
62 |
i = repo.changelog.rev(nodeid)
|
|
63 |
(h1, h2) = [ hg.hex(x) for x in repo.changelog.parents(nodeid) ]
|
|
64 |
datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0])))
|
|
65 |
print '<table width="100%" border="1">'
|
|
66 |
print '\t<tr><td valign="top" width="10%%">author:</td>' + \
|
|
67 |
'<td valign="top" width="20%%">%s</td>' % (obfuscate(changes[1]), )
|
|
68 |
print '\t\t<td valign="top" width="10%%">description:</td>' + \
|
|
69 |
'<td width="60%%">' + \
|
|
70 |
'<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
71 |
(hn, nl2br(changes[4]), )
|
|
72 |
print '\t<tr><td>date:</td><td>%s UTC</td>' % (datestr, )
|
|
73 |
print '\t\t<td valign="top">files:</td><td valign="top">'
|
|
74 |
for f in changes[3]:
|
61
|
75 |
print '\t\t%s ' % f
|
57
|
76 |
print '\t</td></tr>'
|
|
77 |
# print '\t<tr><td>revision:</td><td colspan="3">%d:<a ' % (i, ) + \
|
|
78 |
# 'href="?cmd=rev;nd=%s">%s</a></td></tr>' % (hn, hn, )
|
|
79 |
print '</table><br />'
|
|
80 |
|
|
81 |
def ent_diff(a, b, fn):
|
|
82 |
a = a.splitlines(1)
|
|
83 |
b = b.splitlines(1)
|
|
84 |
l = difflib.unified_diff(a, b, fn, fn)
|
|
85 |
print '<pre>'
|
|
86 |
for line in l:
|
|
87 |
line = cgi.escape(line[:-1])
|
|
88 |
if line.startswith('+'):
|
|
89 |
print '<span class="plusline">%s</span>' % (line, )
|
|
90 |
elif line.startswith('-'):
|
|
91 |
print '<span class="minusline">%s</span>' % (line, )
|
|
92 |
elif line.startswith('@'):
|
|
93 |
print '<span class="atline">%s</span>' % (line, )
|
|
94 |
else:
|
|
95 |
print line
|
|
96 |
print '</pre>'
|
|
97 |
|
|
98 |
def ent_checkin(repo, nodeid):
|
61
|
99 |
startpage("Mercurial Web")
|
|
100 |
|
57
|
101 |
changes = repo.changelog.read(nodeid)
|
|
102 |
hn = hg.hex(nodeid)
|
|
103 |
i = repo.changelog.rev(nodeid)
|
|
104 |
parents = repo.changelog.parents(nodeid)
|
|
105 |
(h1, h2) = [ hg.hex(x) for x in parents ]
|
|
106 |
(i1, i2) = [ repo.changelog.rev(x) for x in parents ]
|
|
107 |
datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0])))
|
|
108 |
mf = repo.manifest.read(changes[0])
|
|
109 |
print '<table width="100%" border="1">'
|
|
110 |
print '\t<tr><td>revision:</td><td colspan="3">%d:' % (i, ),
|
|
111 |
print '<a href="?cmd=rev;nd=%s">%s</a></td></tr>' % (hn, hn, )
|
|
112 |
print '\t<tr><td>parent(s):</td><td colspan="3">%d:' % (i1, )
|
|
113 |
print '<a href="?cmd=rev;nd=%s">%s</a>' % (h1, h1, ),
|
|
114 |
if i2 != -1:
|
|
115 |
print ' %d:<a href="?cmd=rev;nd=%s">%s</a>' % \
|
|
116 |
(i2, h2, h2, ),
|
|
117 |
else:
|
|
118 |
print ' %d:%s' % (i2, h2, ),
|
|
119 |
print '</td></tr>'
|
|
120 |
print '\t<tr><td>manifest:</td><td colspan="3">%d:' % \
|
|
121 |
(repo.manifest.rev(changes[0]), ),
|
|
122 |
print '<a href="?cmd=mf;nd=%s">%s</a></td></tr>' % \
|
|
123 |
(hg.hex(changes[0]), hg.hex(changes[0]), )
|
|
124 |
print '\t<tr><td valign="top" width="10%%">author:</td>' + \
|
|
125 |
'<td valign="top" width="20%%">%s</td>' % (obfuscate(changes[1]), )
|
|
126 |
print '\t\t<td valign="top" width="10%%">description:</td>' + \
|
|
127 |
'<td width="60%%">' + \
|
|
128 |
'<a href="?cmd=chkin;nd=%s">%s</a></td></tr>' % \
|
|
129 |
(hn, nl2br(changes[4]), )
|
|
130 |
print '\t<tr><td>date:</td><td>%s UTC</td>' % (datestr, )
|
|
131 |
print '\t\t<td valign="top">files:</td><td valign="top">'
|
|
132 |
for f in changes[3]:
|
|
133 |
print '\t\t<a href="?cmd=file;nd=%s&fn=%s">%s</a>' % \
|
|
134 |
(hg.hex(mf[f]), f, f, ),
|
|
135 |
print ' '
|
|
136 |
print '\t</td></tr>'
|
|
137 |
print '</table><br />'
|
|
138 |
|
|
139 |
(c, a, d) = repo.diffrevs(parents[0], nodeid)
|
|
140 |
change = repo.changelog.read(parents[0])
|
|
141 |
mf2 = repo.manifest.read(change[0])
|
|
142 |
for f in c:
|
|
143 |
ent_diff(repo.file(f).read(mf2[f]), repo.file(f).read(mf[f]), f)
|
|
144 |
for f in a:
|
|
145 |
ent_diff('', repo.file(f).read(mf[f]), f)
|
|
146 |
for f in d:
|
|
147 |
ent_diff(repo.file(f).read(mf2[f]), '', f)
|
|
148 |
|
61
|
149 |
endpage()
|
|
150 |
|
|
151 |
|
57
|
152 |
def ent_file(repo, nodeid, fn):
|
|
153 |
print '<div class="filename">%s (%s)</div>' % (fn, hg.hex(nodeid), )
|
|
154 |
print '<pre>'
|
|
155 |
print cgi.escape(repo.file(fn).read(nodeid))
|
|
156 |
print '</pre>'
|
|
157 |
|
61
|
158 |
def change_page():
|
|
159 |
startpage("Mercurial Web")
|
|
160 |
print '<table width="100%" align="center">'
|
|
161 |
for i in xrange(0, repo.changelog.count()):
|
|
162 |
n = repo.changelog.node(i)
|
|
163 |
print '<tr><td>'
|
|
164 |
ent_change(repo, n)
|
|
165 |
print '</td></th>'
|
57
|
166 |
|
61
|
167 |
print '</table>'
|
|
168 |
endpage()
|
57
|
169 |
|
|
170 |
args = cgi.parse()
|
|
171 |
|
|
172 |
ui = hg.ui()
|
|
173 |
repo = hg.repository(ui, repo_path)
|
|
174 |
|
|
175 |
if not args.has_key('cmd'):
|
61
|
176 |
change_page()
|
|
177 |
|
57
|
178 |
elif args['cmd'][0] == 'chkin':
|
|
179 |
if not args.has_key('nd'):
|
|
180 |
print '<div class="errmsg">No Node!</div>'
|
|
181 |
else:
|
|
182 |
ent_checkin(repo, hg.bin(args['nd'][0]))
|
61
|
183 |
|
57
|
184 |
elif args['cmd'][0] == 'file':
|
61
|
185 |
startpage("Mercurial Web")
|
|
186 |
|
57
|
187 |
if not args.has_key('nd'):
|
|
188 |
print '<div class="errmsg">No Node!</div>'
|
|
189 |
elif not args.has_key('fn'):
|
|
190 |
print '<div class="errmsg">No Filename!</div>'
|
|
191 |
else:
|
|
192 |
ent_file(repo, hg.bin(args['nd'][0]), args['fn'][0])
|
61
|
193 |
endpage()
|
|
194 |
|
|
195 |
elif args['cmd'][0] == 'branches':
|
|
196 |
httphdr("text/plain")
|
|
197 |
nodes = []
|
|
198 |
if args.has_key('nodes'):
|
|
199 |
nodes = map(hg.bin, args['nodes'][0].split(" "))
|
|
200 |
for b in repo.branches(nodes):
|
|
201 |
print " ".join(map(hg.hex, b))
|
|
202 |
|
|
203 |
elif args['cmd'][0] == 'between':
|
|
204 |
httphdr("text/plain")
|
|
205 |
nodes = []
|
|
206 |
if args.has_key('pairs'):
|
|
207 |
pairs = [ map(hg.bin, p.split("-"))
|
|
208 |
for p in args['pairs'][0].split(" ") ]
|
|
209 |
for b in repo.between(pairs):
|
|
210 |
print " ".join(map(hg.hex, b))
|
|
211 |
|
|
212 |
elif args['cmd'][0] == 'changegroup':
|
|
213 |
httphdr("application/hg-changegroup")
|
|
214 |
nodes = []
|
|
215 |
if args.has_key('roots'):
|
|
216 |
nodes = map(hg.bin, args['roots'][0].split(" "))
|
|
217 |
|
|
218 |
z = zlib.compressobj()
|
|
219 |
for chunk in repo.changegroup(nodes):
|
|
220 |
sys.stdout.write(z.compress(chunk))
|
|
221 |
|
|
222 |
sys.stdout.write(z.flush())
|
57
|
223 |
|
|
224 |
else:
|
61
|
225 |
startpage("Mercurial Web Error")
|
57
|
226 |
print '<div class="errmsg">unknown command: ', args['cmd'][0], '</div>'
|
61
|
227 |
endpage()
|