author | mpm@selenic.com |
Mon, 23 May 2005 16:00:02 -0800 | |
changeset 138 | c77a679e9cfa |
parent 137 | b45b1b00fc9e |
child 142 | 529bf610092e |
permissions | -rw-r--r-- |
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 |
|
138 | 14 |
from mercurial.hg import * |
15 |
||
16 |
def age(t): |
|
17 |
def plural(t, c): |
|
18 |
if c == 1: return t |
|
19 |
return t + "s" |
|
20 |
def fmt(t, c): |
|
21 |
return "%d %s" % (c, plural(t, c)) |
|
22 |
||
23 |
now = time.time() |
|
24 |
delta = max(1, int(now - t)) |
|
25 |
||
26 |
scales = [["second", 1], |
|
27 |
["minute", 60], |
|
28 |
["hour", 3600], |
|
29 |
["day", 3600 * 24], |
|
30 |
["week", 3600 * 24 * 7], |
|
31 |
["month", 3600 * 24 * 30], |
|
32 |
["year", 3600 * 24 * 365]] |
|
33 |
||
34 |
scales.reverse() |
|
35 |
||
36 |
for t, s in scales: |
|
37 |
n = delta / s |
|
38 |
if n >= 1: return fmt(t, n) |
|
131 | 39 |
|
40 |
def nl2br(text): |
|
138 | 41 |
return text.replace('\n', '<br/>') |
131 | 42 |
|
43 |
def obfuscate(text): |
|
138 | 44 |
return ''.join([ '&#%d' % ord(c) for c in text ]) |
45 |
||
46 |
def up(p): |
|
47 |
if p[0] != "/": p = "/" + p |
|
48 |
if p[-1] == "/": p = p[:-1] |
|
49 |
up = os.path.dirname(p) |
|
50 |
if up == "/": |
|
51 |
return "/" |
|
52 |
return up + "/" |
|
131 | 53 |
|
54 |
def httphdr(type): |
|
55 |
print 'Content-type: %s\n' % type |
|
56 |
||
135 | 57 |
def write(*things): |
58 |
for thing in things: |
|
59 |
if hasattr(thing, "__iter__"): |
|
60 |
for part in thing: |
|
61 |
write(part) |
|
62 |
else: |
|
63 |
sys.stdout.write(str(thing)) |
|
64 |
||
138 | 65 |
def template(tmpl, **map): |
66 |
while tmpl: |
|
67 |
m = re.search(r"#([a-zA-Z0-9]+)#", tmpl) |
|
68 |
if m: |
|
69 |
yield tmpl[:m.start(0)] |
|
70 |
v = map.get(m.group(1), "") |
|
71 |
yield callable(v) and v() or v |
|
72 |
tmpl = tmpl[m.end(0):] |
|
73 |
else: |
|
74 |
yield tmpl |
|
75 |
return |
|
76 |
||
77 |
class templater: |
|
78 |
def __init__(self, mapfile): |
|
79 |
self.cache = {} |
|
80 |
self.map = {} |
|
81 |
self.base = os.path.dirname(mapfile) |
|
82 |
||
83 |
for l in file(mapfile): |
|
84 |
m = re.match(r'(\S+)\s*=\s*"(.*)"$', l) |
|
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
85 |
if m: |
138 | 86 |
self.cache[m.group(1)] = m.group(2) |
87 |
else: |
|
88 |
m = re.match(r'(\S+)\s*=\s*(\S+)', l) |
|
89 |
if m: |
|
90 |
self.map[m.group(1)] = os.path.join(self.base, m.group(2)) |
|
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
91 |
else: |
138 | 92 |
raise "unknown map entry '%s'" % l |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
93 |
|
138 | 94 |
def __call__(self, t, **map): |
95 |
try: |
|
96 |
tmpl = self.cache[t] |
|
97 |
except KeyError: |
|
98 |
tmpl = self.cache[t] = file(self.map[t]).read() |
|
99 |
return template(tmpl, **map) |
|
100 |
||
101 |
class hgweb: |
|
102 |
maxchanges = 20 |
|
103 |
maxfiles = 10 |
|
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
104 |
|
138 | 105 |
def __init__(self, path, name, templatemap): |
106 |
self.reponame = name |
|
107 |
self.repo = repository(ui(), path) |
|
108 |
self.t = templater(templatemap) |
|
131 | 109 |
|
138 | 110 |
def date(self, cs): |
111 |
return time.asctime(time.gmtime(float(cs[2].split(' ')[0]))) |
|
112 |
||
113 |
def listfiles(self, files, mf): |
|
114 |
for f in files[:self.maxfiles]: |
|
115 |
yield self.t("filenodelink", node = hex(mf[f]), file = f) |
|
116 |
if len(files) > self.maxfiles: |
|
117 |
yield self.t("fileellipses") |
|
118 |
||
119 |
def listfilediffs(self, files, changeset): |
|
120 |
for f in files[:self.maxfiles]: |
|
121 |
yield self.t("filedifflink", node = hex(changeset), file = f) |
|
122 |
if len(files) > self.maxfiles: |
|
123 |
yield self.t("fileellipses") |
|
124 |
||
125 |
def diff(self, node1, node2, files): |
|
126 |
def filterfiles(list, files): |
|
127 |
l = [ x for x in list if x in files ] |
|
128 |
||
129 |
for f in files: |
|
130 |
if f[-1] != os.sep: f += os.sep |
|
131 |
l += [ x for x in list if x.startswith(f) ] |
|
132 |
return l |
|
131 | 133 |
|
138 | 134 |
def prettyprint(diff): |
135 |
for l in diff.splitlines(1): |
|
136 |
line = cgi.escape(l) |
|
137 |
if line.startswith('+'): |
|
138 |
yield self.t("difflineplus", line = line) |
|
139 |
elif line.startswith('-'): |
|
140 |
yield self.t("difflineminus", line = line) |
|
141 |
elif line.startswith('@'): |
|
142 |
yield self.t("difflineat", line = line) |
|
143 |
else: |
|
144 |
yield self.t("diffline", line = line) |
|
131 | 145 |
|
138 | 146 |
r = self.repo |
147 |
cl = r.changelog |
|
148 |
mf = r.manifest |
|
149 |
change1 = cl.read(node1) |
|
150 |
change2 = cl.read(node2) |
|
151 |
mmap1 = mf.read(change1[0]) |
|
152 |
mmap2 = mf.read(change2[0]) |
|
153 |
date1 = self.date(change1) |
|
154 |
date2 = self.date(change2) |
|
131 | 155 |
|
138 | 156 |
c, a, d = r.diffrevs(node1, node2) |
157 |
c, a, d = map(lambda x: filterfiles(x, files), (c, a, d)) |
|
131 | 158 |
|
138 | 159 |
for f in c: |
160 |
to = r.file(f).read(mmap1[f]) |
|
161 |
tn = r.file(f).read(mmap2[f]) |
|
162 |
yield prettyprint(mdiff.unidiff(to, date1, tn, date2, f)) |
|
163 |
for f in a: |
|
164 |
to = "" |
|
165 |
tn = r.file(f).read(mmap2[f]) |
|
166 |
yield prettyprint(mdiff.unidiff(to, date1, tn, date2, f)) |
|
167 |
for f in d: |
|
168 |
to = r.file(f).read(mmap1[f]) |
|
169 |
tn = "" |
|
170 |
yield prettyprint(mdiff.unidiff(to, date1, tn, date2, f)) |
|
131 | 171 |
|
138 | 172 |
def changelog(self, pos=None): |
173 |
def changenav(): |
|
174 |
def seq(factor = 1): |
|
175 |
yield 1 * factor |
|
176 |
yield 2 * factor |
|
177 |
yield 5 * factor |
|
178 |
for f in seq(factor * 10): |
|
179 |
yield f |
|
180 |
||
181 |
linear = range(0, count - 2, self.maxchanges)[0:8] |
|
182 |
||
183 |
for i in linear: |
|
184 |
yield self.t("naventry", rev = max(i, 1)) |
|
131 | 185 |
|
138 | 186 |
for s in seq(): |
187 |
if s > count - 2: break |
|
188 |
if s > linear[-1]: |
|
189 |
yield self.t("naventry", rev = s) |
|
190 |
||
191 |
yield self.t("naventry", rev = count - 1) |
|
131 | 192 |
|
138 | 193 |
def changelist(): |
194 |
cl = self.repo.changelog |
|
195 |
l = [] # build a list in forward order for efficiency |
|
196 |
for i in range(start, end + 1): |
|
197 |
n = cl.node(i) |
|
198 |
changes = cl.read(n) |
|
199 |
hn = hex(n) |
|
200 |
p1, p2 = cl.parents(n) |
|
201 |
t = float(changes[2].split(' ')[0]) |
|
131 | 202 |
|
138 | 203 |
l.insert(0, self.t( |
204 |
'changelogentry', |
|
205 |
author = obfuscate(changes[1]), |
|
206 |
shortdesc = cgi.escape(changes[4].splitlines()[0]), |
|
207 |
age = age(t), |
|
208 |
p1 = hex(p1), p2 = hex(p2), |
|
209 |
p1rev = cl.rev(p1), p2rev = cl.rev(p2), |
|
210 |
manifest = hex(changes[0]), |
|
211 |
desc = nl2br(cgi.escape(changes[4])), |
|
212 |
date = time.asctime(time.gmtime(t)), |
|
213 |
files = self.listfilediffs(changes[3], n), |
|
214 |
rev = i, |
|
215 |
node = hn)) |
|
216 |
||
217 |
yield l |
|
131 | 218 |
|
138 | 219 |
count = self.repo.changelog.count() |
220 |
pos = pos or count - 1 |
|
221 |
end = min(pos, count - 1) |
|
222 |
start = max(0, pos - self.maxchanges) |
|
223 |
end = min(count - 1, start + self.maxchanges) |
|
224 |
||
225 |
yield self.t('changelog', repo = self.reponame, changenav = changenav, |
|
226 |
rev = pos, changesets = count, changelist = changelist) |
|
131 | 227 |
|
138 | 228 |
def changeset(self, nodeid): |
229 |
n = bin(nodeid) |
|
230 |
cl = self.repo.changelog |
|
231 |
changes = cl.read(n) |
|
232 |
p1, p2 = cl.parents(n) |
|
233 |
p1rev, p2rev = cl.rev(p1), cl.rev(p2) |
|
234 |
t = float(changes[2].split(' ')[0]) |
|
235 |
||
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
236 |
files = [] |
138 | 237 |
mf = self.repo.manifest.read(changes[0]) |
131 | 238 |
for f in changes[3]: |
138 | 239 |
files.append(self.t("filenodelink", |
240 |
filenode = hex(mf[f]), file = f)) |
|
241 |
||
242 |
def diff(): |
|
243 |
yield self.diff(p1, n, changes[3]) |
|
131 | 244 |
|
138 | 245 |
yield self.t('changeset', |
246 |
diff = diff, |
|
247 |
rev = cl.rev(n), |
|
248 |
node = nodeid, |
|
249 |
shortdesc = cgi.escape(changes[4].splitlines()[0]), |
|
250 |
p1 = hex(p1), p2 = hex(p2), |
|
251 |
p1rev = cl.rev(p1), p2rev = cl.rev(p2), |
|
252 |
manifest = hex(changes[0]), |
|
253 |
author = obfuscate(changes[1]), |
|
254 |
desc = nl2br(cgi.escape(changes[4])), |
|
255 |
date = time.asctime(time.gmtime(t)), |
|
256 |
files = files) |
|
131 | 257 |
|
138 | 258 |
def filelog(self, f, filenode): |
259 |
cl = self.repo.changelog |
|
260 |
fl = self.repo.file(f) |
|
261 |
count = fl.count() |
|
262 |
||
263 |
def entries(): |
|
264 |
l = [] |
|
265 |
for i in range(count): |
|
266 |
||
267 |
n = fl.node(i) |
|
268 |
lr = fl.linkrev(n) |
|
269 |
cn = cl.node(lr) |
|
270 |
cs = cl.read(cl.node(lr)) |
|
271 |
p1, p2 = fl.parents(n) |
|
272 |
t = float(cs[2].split(' ')[0]) |
|
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
273 |
|
138 | 274 |
l.insert(0, self.t("filelogentry", |
275 |
filenode = hex(n), |
|
276 |
filerev = i, |
|
277 |
file = f, |
|
278 |
node = hex(cn), |
|
279 |
author = obfuscate(cs[1]), |
|
280 |
age = age(t), |
|
281 |
date = time.asctime(time.gmtime(t)), |
|
282 |
shortdesc = cgi.escape(cs[4].splitlines()[0]), |
|
283 |
p1 = hex(p1), p2 = hex(p2), |
|
284 |
p1rev = fl.rev(p1), p2rev = fl.rev(p2))) |
|
285 |
||
286 |
yield l |
|
287 |
||
288 |
yield self.t("filelog", |
|
289 |
file = f, |
|
290 |
filenode = filenode, |
|
291 |
entries = entries) |
|
131 | 292 |
|
138 | 293 |
def filerevision(self, f, node): |
294 |
fl = self.repo.file(f) |
|
295 |
n = bin(node) |
|
296 |
text = cgi.escape(fl.read(n)) |
|
297 |
changerev = fl.linkrev(n) |
|
298 |
cl = self.repo.changelog |
|
299 |
cn = cl.node(changerev) |
|
300 |
cs = cl.read(cn) |
|
301 |
p1, p2 = fl.parents(n) |
|
302 |
t = float(cs[2].split(' ')[0]) |
|
303 |
mfn = cs[0] |
|
304 |
||
305 |
yield self.t("filerevision", file = f, |
|
306 |
filenode = node, |
|
307 |
path = up(f), |
|
308 |
text = text, |
|
309 |
rev = changerev, |
|
310 |
node = hex(cn), |
|
311 |
manifest = hex(mfn), |
|
312 |
author = obfuscate(cs[1]), |
|
313 |
age = age(t), |
|
314 |
date = time.asctime(time.gmtime(t)), |
|
315 |
shortdesc = cgi.escape(cs[4].splitlines()[0]), |
|
316 |
p1 = hex(p1), p2 = hex(p2), |
|
317 |
p1rev = fl.rev(p1), p2rev = fl.rev(p2)) |
|
318 |
||
131 | 319 |
|
138 | 320 |
def fileannotate(self, f, node): |
321 |
bcache = {} |
|
322 |
ncache = {} |
|
323 |
fl = self.repo.file(f) |
|
324 |
n = bin(node) |
|
325 |
changerev = fl.linkrev(n) |
|
326 |
||
327 |
cl = self.repo.changelog |
|
328 |
cn = cl.node(changerev) |
|
329 |
cs = cl.read(cn) |
|
330 |
p1, p2 = fl.parents(n) |
|
331 |
t = float(cs[2].split(' ')[0]) |
|
332 |
mfn = cs[0] |
|
131 | 333 |
|
138 | 334 |
def annotate(): |
335 |
for r, l in fl.annotate(n): |
|
336 |
try: |
|
337 |
cnode = ncache[r] |
|
338 |
except KeyError: |
|
339 |
cnode = ncache[r] = self.repo.changelog.node(r) |
|
340 |
||
341 |
try: |
|
342 |
name = bcache[r] |
|
343 |
except KeyError: |
|
344 |
cl = self.repo.changelog.read(cnode) |
|
345 |
name = cl[1] |
|
346 |
f = name.find('@') |
|
347 |
if f >= 0: |
|
348 |
name = name[:f] |
|
349 |
bcache[r] = name |
|
131 | 350 |
|
138 | 351 |
yield self.t("annotateline", |
352 |
node = hex(cnode), |
|
353 |
rev = r, |
|
354 |
author = name, |
|
355 |
file = f, |
|
356 |
line = cgi.escape(l)) |
|
357 |
||
358 |
yield self.t("fileannotate", |
|
359 |
file = f, |
|
360 |
filenode = node, |
|
361 |
annotate = annotate, |
|
362 |
path = up(f), |
|
363 |
rev = changerev, |
|
364 |
node = hex(cn), |
|
365 |
manifest = hex(mfn), |
|
366 |
author = obfuscate(cs[1]), |
|
367 |
age = age(t), |
|
368 |
date = time.asctime(time.gmtime(t)), |
|
369 |
shortdesc = cgi.escape(cs[4].splitlines()[0]), |
|
370 |
p1 = hex(p1), p2 = hex(p2), |
|
371 |
p1rev = fl.rev(p1), p2rev = fl.rev(p2)) |
|
136 | 372 |
|
138 | 373 |
def manifest(self, mnode, path): |
374 |
mf = self.repo.manifest.read(bin(mnode)) |
|
375 |
rev = self.repo.manifest.rev(bin(mnode)) |
|
376 |
node = self.repo.changelog.node(rev) |
|
377 |
||
378 |
dirs = {} |
|
379 |
files = {} |
|
380 |
short = {} |
|
136 | 381 |
|
138 | 382 |
p = path[1:] |
383 |
l = len(p) |
|
131 | 384 |
|
138 | 385 |
for f,n in mf.items(): |
386 |
if f[:l] != p: |
|
387 |
continue |
|
388 |
remain = f[l:] |
|
389 |
if "/" in remain: |
|
390 |
short = remain[:remain.find("/") + 1] # bleah |
|
391 |
dirs[short] = 1 |
|
392 |
else: |
|
393 |
short = os.path.basename(remain) |
|
394 |
files[short] = (f, n) |
|
131 | 395 |
|
138 | 396 |
def dirlist(): |
397 |
dl = dirs.keys() |
|
398 |
dl.sort() |
|
399 |
||
400 |
for d in dl: |
|
401 |
yield self.t("manifestdirentry", |
|
402 |
path = os.path.join(path, d), |
|
403 |
manifest = mnode, basename = d[:-1]) |
|
131 | 404 |
|
138 | 405 |
def filelist(): |
406 |
fl = files.keys() |
|
407 |
fl.sort() |
|
408 |
for f in fl: |
|
409 |
full, fnode = files[f] |
|
410 |
yield self.t("manifestfileentry", |
|
411 |
file = full, manifest = mnode, filenode = hex(fnode), |
|
412 |
basename = f) |
|
413 |
||
414 |
yield self.t("manifest", |
|
415 |
manifest = mnode, |
|
416 |
rev = rev, |
|
417 |
node = hex(node), |
|
418 |
path = path, |
|
419 |
up = up(path), |
|
420 |
dirs = dirlist, |
|
421 |
files = filelist) |
|
131 | 422 |
|
138 | 423 |
def filediff(self, file, changeset): |
424 |
n = bin(changeset) |
|
425 |
cl = self.repo.changelog |
|
426 |
p1 = cl.parents(n)[0] |
|
427 |
cs = cl.read(n) |
|
428 |
mf = self.repo.manifest.read(cs[0]) |
|
429 |
||
430 |
def diff(): |
|
431 |
yield self.diff(p1, n, file) |
|
131 | 432 |
|
138 | 433 |
yield self.t("filediff", |
434 |
file = file, |
|
435 |
filenode = hex(mf[file]), |
|
436 |
node = changeset, |
|
437 |
rev = self.repo.changelog.rev(n), |
|
438 |
p1 = hex(p1), |
|
439 |
p1rev = self.repo.changelog.rev(p1), |
|
440 |
diff = diff) |
|
441 |
||
442 |
# header and footer, css |
|
443 |
# add tags to things |
|
444 |
# show parents |
|
445 |
# diff between rev and parent in changeset and file |
|
446 |
# manifest links |
|
447 |
# browse at top |
|
448 |
# tags -> list of changesets corresponding to tags |
|
449 |
# find tag, changeset, file |
|
131 | 450 |
|
132 | 451 |
def run(self): |
452 |
args = cgi.parse() |
|
453 |
||
138 | 454 |
if not args.has_key('cmd') or args['cmd'][0] == 'changelog': |
455 |
hi = self.repo.changelog.count() |
|
456 |
if args.has_key('pos'): |
|
457 |
hi = int(args['pos'][0]) |
|
131 | 458 |
|
138 | 459 |
write(self.changelog(hi)) |
132 | 460 |
|
138 | 461 |
elif args['cmd'][0] == 'changeset': |
462 |
write(self.changeset(args['node'][0])) |
|
463 |
||
464 |
elif args['cmd'][0] == 'manifest': |
|
465 |
write(self.manifest(args['manifest'][0], args['path'][0])) |
|
466 |
||
467 |
elif args['cmd'][0] == 'filediff': |
|
468 |
write(self.filediff(args['file'][0], args['node'][0])) |
|
131 | 469 |
|
132 | 470 |
elif args['cmd'][0] == 'file': |
138 | 471 |
write(self.filerevision(args['file'][0], args['filenode'][0])) |
131 | 472 |
|
138 | 473 |
elif args['cmd'][0] == 'annotate': |
474 |
write(self.fileannotate(args['file'][0], args['filenode'][0])) |
|
131 | 475 |
|
138 | 476 |
elif args['cmd'][0] == 'filelog': |
477 |
write(self.filelog(args['file'][0], args['filenode'][0])) |
|
136 | 478 |
|
132 | 479 |
elif args['cmd'][0] == 'branches': |
480 |
httphdr("text/plain") |
|
481 |
nodes = [] |
|
482 |
if args.has_key('nodes'): |
|
138 | 483 |
nodes = map(bin, args['nodes'][0].split(" ")) |
484 |
for b in self.repo.branches(nodes): |
|
485 |
sys.stdout.write(" ".join(map(hex, b)) + "\n") |
|
131 | 486 |
|
132 | 487 |
elif args['cmd'][0] == 'between': |
488 |
httphdr("text/plain") |
|
489 |
nodes = [] |
|
490 |
if args.has_key('pairs'): |
|
138 | 491 |
pairs = [ map(bin, p.split("-")) |
132 | 492 |
for p in args['pairs'][0].split(" ") ] |
138 | 493 |
for b in self.repo.between(pairs): |
494 |
sys.stdout.write(" ".join(map(hex, b)) + "\n") |
|
132 | 495 |
|
496 |
elif args['cmd'][0] == 'changegroup': |
|
497 |
httphdr("application/hg-changegroup") |
|
498 |
nodes = [] |
|
499 |
if args.has_key('roots'): |
|
138 | 500 |
nodes = map(bin, args['roots'][0].split(" ")) |
131 | 501 |
|
132 | 502 |
z = zlib.compressobj() |
138 | 503 |
for chunk in self.repo.changegroup(nodes): |
132 | 504 |
sys.stdout.write(z.compress(chunk)) |
505 |
||
506 |
sys.stdout.write(z.flush()) |
|
131 | 507 |
|
132 | 508 |
else: |
138 | 509 |
write(self.t("error")) |
131 | 510 |
|
132 | 511 |
if __name__ == "__main__": |
512 |
hgweb().run() |