comparison mercurial/hgweb.py @ 132:210eeb6f5197

making hgweb class
author jake@edge2.net
date Sat, 21 May 2005 11:46:16 -0700
parents c9d51742471c
children fb84d3e71042
comparison
equal deleted inserted replaced
131:c9d51742471c 132:210eeb6f5197
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # hgweb.py - 0.1 - 9 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> 3 # hgweb.py - 0.2 - 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 # - web interface to a mercurial repository 4 # - web interface to a mercurial repository
5 # 5 #
6 # This software may be used and distributed according to the terms 6 # This software may be used and distributed according to the terms
7 # of the GNU General Public License, incorporated herein by reference. 7 # of the GNU General Public License, incorporated herein by reference.
8 8
10 import cgitb 10 import cgitb
11 cgitb.enable() 11 cgitb.enable()
12 12
13 import os, cgi, time, re, difflib, sys, zlib 13 import os, cgi, time, re, difflib, sys, zlib
14 from mercurial import hg, mdiff 14 from mercurial import hg, mdiff
15
16 repo_path = "." # change as needed
17 15
18 def nl2br(text): 16 def nl2br(text):
19 return re.sub('\n', '<br />', text) 17 return re.sub('\n', '<br />', text)
20 18
21 def obfuscate(text): 19 def obfuscate(text):
288 print '&nbsp;&nbsp;%d:<a href="?cmd=file;nd=%s;fn=%s">%s</a>' % \ 286 print '&nbsp;&nbsp;%d:<a href="?cmd=file;nd=%s;fn=%s">%s</a>' % \
289 (p2i, p2s, self.fn, p2s ), 287 (p2i, p2s, self.fn, p2s ),
290 print '</td></tr>' 288 print '</td></tr>'
291 print '</table><br />' 289 print '</table><br />'
292 290
293 args = cgi.parse() 291 class hgweb:
294 292 repo_path = "."
295 ui = hg.ui() 293 numchanges = 50
296 repo = hg.repository(ui, repo_path) 294
297 295 def __init__(self):
298 if not args.has_key('cmd') or args['cmd'][0] == 'changes': 296 pass
299 page = change_list(repo, 'Mercurial') 297
300 hi = args.get('hi', ( repo.changelog.count(), )) 298 def run(self):
301 page.content(hi = int(hi[0])) 299
302 page.endpage() 300 args = cgi.parse()
303 301
304 elif args['cmd'][0] == 'chkin': 302 ui = hg.ui()
305 if not args.has_key('nd'): 303 repo = hg.repository(ui, self.repo_path)
306 page = errpage() 304
307 print '<div class="errmsg">No Node!</div>' 305 if not args.has_key('cmd') or args['cmd'][0] == 'changes':
308 else: 306 page = change_list(repo, 'Mercurial')
309 page = checkin(repo, args['nd'][0]) 307 hi = args.get('hi', ( repo.changelog.count(), ))
310 page.content() 308 page.content(hi = int(hi[0]))
311 page.endpage() 309 page.endpage()
312 310
313 elif args['cmd'][0] == 'file': 311 elif args['cmd'][0] == 'chkin':
314 if not (args.has_key('nd') and args.has_key('fn')) and \ 312 if not args.has_key('nd'):
315 not (args.has_key('cs') and args.has_key('fn')): 313 page = errpage()
316 page = errpage() 314 print '<div class="errmsg">No Node!</div>'
317 print '<div class="errmsg">Invalid Args!</div>' 315 else:
318 else: 316 page = checkin(repo, args['nd'][0])
319 if args.has_key('nd'): 317 page.content()
320 page = filepage(repo, args['fn'][0], node=args['nd'][0]) 318 page.endpage()
319
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()
332
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()
341
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))
358
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(" "))
373
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())
379
321 else: 380 else:
322 page = filepage(repo, args['fn'][0], cs=args['cs'][0]) 381 page = errpage()
323 page.content() 382 print '<div class="errmsg">unknown command: %s</div>' % \
324 page.endpage() 383 cgi.escape(args['cmd'][0])
325 384 page.endpage()
326 elif args['cmd'][0] == 'mf': 385
327 if not args.has_key('nd'): 386 if __name__ == "__main__":
328 page = errpage() 387 hgweb().run()
329 print '<div class="errmsg">No Node!</div>'
330 else:
331 page = mfpage(repo, args['nd'][0])
332 page.content()
333 page.endpage()
334
335 elif args['cmd'][0] == 'hist':
336 if not args.has_key('fn'):
337 page = errpage()
338 print '<div class="errmsg">No Filename!</div>'
339 else:
340 page = histpage(repo, args['fn'][0])
341 page.content()
342 page.endpage()
343
344 elif args['cmd'][0] == 'branches':
345 httphdr("text/plain")
346 nodes = []
347 if args.has_key('nodes'):
348 nodes = map(hg.bin, args['nodes'][0].split(" "))
349 for b in repo.branches(nodes):
350 print " ".join(map(hg.hex, b))
351
352 elif args['cmd'][0] == 'between':
353 httphdr("text/plain")
354 nodes = []
355 if args.has_key('pairs'):
356 pairs = [ map(hg.bin, p.split("-"))
357 for p in args['pairs'][0].split(" ") ]
358 for b in repo.between(pairs):
359 print " ".join(map(hg.hex, b))
360
361 elif args['cmd'][0] == 'changegroup':
362 httphdr("application/hg-changegroup")
363 nodes = []
364 if args.has_key('roots'):
365 nodes = map(hg.bin, args['roots'][0].split(" "))
366
367 z = zlib.compressobj()
368 for chunk in repo.changegroup(nodes):
369 sys.stdout.write(z.compress(chunk))
370
371 sys.stdout.write(z.flush())
372
373 else:
374 page = errpage()
375 print '<div class="errmsg">unknown command: %s</div>' % \
376 cgi.escape(args['cmd'][0])
377 page.endpage()