# HG changeset patch # User mpm@selenic.com # Date 1115860270 28800 # Node ID 115106376f4544f474f41896021231b2d333c6a1 # Parent ad2ea1185f04094dd487d2a65f43aba5d2c9bfc2 hgweb.py from Jake Edge remove hgweb.py from URLs so that it will work as index.cgi diff --git a/hgweb.py b/hgweb.py new file mode 100644 --- /dev/null +++ b/hgweb.py @@ -0,0 +1,182 @@ +#!/usr/bin/env python +# +# hgweb.py - 0.1 - 9 May 2005 - (c) 2005 Jake Edge +# - web interface to a mercurial repository +# +# This software may be used and distributed according to the terms +# of the GNU General Public License, incorporated herein by reference. + +# useful for debugging +import cgitb +cgitb.enable() + +import os, cgi, time, re, difflib +from mercurial import hg, mdiff + +repo_path = "." # change as needed + +def nl2br(text): + return re.sub('\n', '
', text) + +def obfuscate(text): + l = [] + for c in text: + l.append('&#%d;' % ord(c)) + return ''.join(l) + +def httphdr(): + print 'Content-type: text/html\n\n' + +def htmldoctype(): + print '' + +def htmlhead(title): + print '' + print '' + print '%s' % (title, ) + print '' + +def ent_change(repo, nodeid): + changes = repo.changelog.read(nodeid) + hn = hg.hex(nodeid) + i = repo.changelog.rev(nodeid) + (h1, h2) = [ hg.hex(x) for x in repo.changelog.parents(nodeid) ] + datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0]))) + mf = repo.manifest.read(changes[0]) + print '' + print '\t' + \ + '' % (obfuscate(changes[1]), ) + print '\t\t' + \ + '' % \ + (hn, nl2br(changes[4]), ) + print '\t' % (datestr, ) + print '\t\t' +# print '\t' % (hn, hn, ) + print '
author:%sdescription:' + \ + '%s
date:%s UTCfiles:' + for f in changes[3]: + print '\t\t%s' % \ + (hg.hex(mf[f]), f, f, ), + print '  ' + print '\t
revision:%d:%s

' + +def ent_diff(a, b, fn): + a = a.splitlines(1) + b = b.splitlines(1) + l = difflib.unified_diff(a, b, fn, fn) + print '
'
+    for line in l:
+        line = cgi.escape(line[:-1])
+        if line.startswith('+'):
+            print '%s' % (line, )
+        elif line.startswith('-'):
+            print '%s' % (line, )
+        elif line.startswith('@'):
+            print '%s' % (line, )
+        else:
+            print line
+    print '
' + +def ent_checkin(repo, nodeid): + changes = repo.changelog.read(nodeid) + hn = hg.hex(nodeid) + i = repo.changelog.rev(nodeid) + parents = repo.changelog.parents(nodeid) + (h1, h2) = [ hg.hex(x) for x in parents ] + (i1, i2) = [ repo.changelog.rev(x) for x in parents ] + datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0]))) + mf = repo.manifest.read(changes[0]) + print '' + print '\t' % (hn, hn, ) + print '\t' + print '\t' % \ + (hg.hex(changes[0]), hg.hex(changes[0]), ) + print '\t' + \ + '' % (obfuscate(changes[1]), ) + print '\t\t' + \ + '' % \ + (hn, nl2br(changes[4]), ) + print '\t' % (datestr, ) + print '\t\t' + print '
revision:%d:' % (i, ), + print '%s
parent(s):%d:' % (i1, ) + print '%s' % (h1, h1, ), + if i2 != -1: + print '  %d:%s' % \ + (i2, h2, h2, ), + else: + print '  %d:%s' % (i2, h2, ), + print '
manifest:%d:' % \ + (repo.manifest.rev(changes[0]), ), + print '%s
author:%sdescription:' + \ + '%s
date:%s UTCfiles:' + for f in changes[3]: + print '\t\t%s' % \ + (hg.hex(mf[f]), f, f, ), + print '  ' + print '\t

' + + (c, a, d) = repo.diffrevs(parents[0], nodeid) + change = repo.changelog.read(parents[0]) + mf2 = repo.manifest.read(change[0]) + for f in c: + ent_diff(repo.file(f).read(mf2[f]), repo.file(f).read(mf[f]), f) + for f in a: + ent_diff('', repo.file(f).read(mf[f]), f) + for f in d: + ent_diff(repo.file(f).read(mf2[f]), '', f) + +def ent_file(repo, nodeid, fn): + print '
%s (%s)
' % (fn, hg.hex(nodeid), ) + print '
'
+    print cgi.escape(repo.file(fn).read(nodeid))
+    print '
' + +httphdr() +htmldoctype() +htmlhead('Mercurial Web') + +print '' + + +args = cgi.parse() + +ui = hg.ui() +repo = hg.repository(ui, repo_path) + +if not args.has_key('cmd'): + print '' + for i in xrange(repo.changelog.count()-1, -1, -1): + n = repo.changelog.node(i) + print '' + + print '
' + ent_change(repo, n) + print '
' +elif args['cmd'][0] == 'chkin': + if not args.has_key('nd'): + print '
No Node!
' + else: + ent_checkin(repo, hg.bin(args['nd'][0])) +elif args['cmd'][0] == 'file': + if not args.has_key('nd'): + print '
No Node!
' + elif not args.has_key('fn'): + print '
No Filename!
' + else: + ent_file(repo, hg.bin(args['nd'][0]), args['fn'][0]) + +else: + print '
unknown command: ', args['cmd'][0], '
' + +print '' +print ''