diff --git a/mercurial/graphmod.py b/mercurial/graphmod.py new file mode 100644 --- /dev/null +++ b/mercurial/graphmod.py @@ -0,0 +1,74 @@ +# Revision graph generator for Mercurial +# +# Copyright 2008 Dirkjan Ochtman +# Copyright 2007 Joel Rosdahl +# +# This software may be used and distributed according to the terms of +# the GNU General Public License, incorporated herein by reference. + +from node import nullrev, short +import ui, hg, util, templatefilters + +def graph(repo, start_rev, stop_rev): + """incremental revision grapher + + This generator function walks through the revision history from + revision start_rev to revision stop_rev (which must be less than + or equal to start_rev) and for each revision emits tuples with the + following elements: + + - Current node + - Column and color for the current node + - Edges; a list of (col, next_col, color) indicating the edges between + the current node and its parents. + - First line of the changeset description + - The changeset author + - The changeset date/time + """ + + assert start_rev >= stop_rev + curr_rev = start_rev + revs = [] + cl = repo.changelog + colors = {} + new_color = 1 + + while curr_rev >= stop_rev: + node = cl.node(curr_rev) + + # Compute revs and next_revs + if curr_rev not in revs: + revs.append(curr_rev) # new head + colors[curr_rev] = new_color + new_color += 1 + + idx = revs.index(curr_rev) + color = colors.pop(curr_rev) + next = revs[:] + + # Add parents to next_revs + parents = [x for x in cl.parentrevs(curr_rev) if x != nullrev] + addparents = [p for p in parents if p not in next] + next[idx:idx + 1] = addparents + + # Set colors for the parents + for i, p in enumerate(addparents): + if not i: + colors[p] = color + else: + colors[p] = new_color + new_color += 1 + + # Add edges to the graph + edges = [] + for col, r in enumerate(revs): + if r in next: + edges.append((col, next.index(r), colors[r])) + elif r == curr_rev: + for p in parents: + edges.append((col, next.index(p), colors[p])) + + # Yield and move on + yield (repo.changectx(curr_rev), (idx, color), edges) + revs = next + curr_rev -= 1 diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py --- a/mercurial/hgweb/webcommands.py +++ b/mercurial/hgweb/webcommands.py @@ -5,14 +5,15 @@ # This software may be used and distributed according to the terms # of the GNU General Public License, incorporated herein by reference. -import os, mimetypes, re +import os, mimetypes, re, cgi import webutil -from mercurial import revlog, archival +from mercurial import revlog, archival, templatefilters from mercurial.node import short, hex, nullid -from mercurial.util import binary +from mercurial.util import binary, datestr from mercurial.repo import RepoError from common import paritygen, staticfile, get_contact, ErrorResponse from common import HTTP_OK, HTTP_NOT_FOUND +from mercurial import graphmod # __all__ is populated with the allowed commands. Be sure to add to it if # you're adding a new command, or the new command won't work. @@ -20,7 +21,7 @@ from common import HTTP_OK, HTTP_NOT_FOU __all__ = [ 'log', 'rawfile', 'file', 'changelog', 'shortlog', 'changeset', 'rev', 'manifest', 'tags', 'summary', 'filediff', 'diff', 'annotate', 'filelog', - 'archive', 'static', + 'archive', 'static', 'graph', ] def log(web, req, tmpl): @@ -574,3 +575,37 @@ def static(web, req, tmpl): os.path.join(web.templatepath, "static"), untrusted=False) return [staticfile(static, fname, req)] + +def graph(web, req, tmpl): + rev = webutil.changectx(web.repo, req).rev() + revcount = int(req.form.get('revcount', [25])[0]) + bg_height = 39 + + max_rev = web.repo.changelog.count() - 1 + revnode = web.repo.changelog.node(rev) + revnode_hex = hex(revnode) + uprev = min(max_rev, rev + revcount) + downrev = max(0, rev - revcount) + lessrev = max(0, rev - revcount / 2) + + maxchanges = web.maxshortchanges or web.maxchanges + count = web.repo.changelog.count() + changenav = webutil.revnavgen(rev, maxchanges, count, web.repo.changectx) + + tree = list(graphmod.graph(web.repo, rev, rev - revcount)) + canvasheight = (len(tree) + 1) * bg_height - 27; + + data = [] + for i, (ctx, vtx, edges) in enumerate(tree): + node = short(ctx.node()) + age = templatefilters.age(ctx.date()) + desc = templatefilters.firstline(ctx.description()) + desc = cgi.escape(desc) + user = cgi.escape(templatefilters.person(ctx.user())) + data.append((node, vtx, edges, desc, user, age, ctx.tags())) + + return tmpl('graph', rev=rev, revcount=revcount, uprev=uprev, + lessrev=lessrev, revcountmore=revcount and 2 * revcount or 1, + revcountless=revcount / 2, downrev=downrev, + canvasheight=canvasheight, bg_height=bg_height, + jsdata=data, node=revnode_hex, changenav=changenav) diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py --- a/mercurial/templatefilters.py +++ b/mercurial/templatefilters.py @@ -122,6 +122,36 @@ def xmlescape(text): .replace("'", ''')) # ' invalid in HTML return re.sub('[\x00-\x08\x0B\x0C\x0E-\x1F]', ' ', text) +_escapes = [ + ('\\', '\\\\'), ('"', '\\"'), ('\t', '\\t'), ('\n', '\\n'), + ('\r', '\\r'), ('\f', '\\f'), ('\b', '\\b'), +] + +def json(obj): + if obj is None or obj is False or obj is True: + return {None: 'null', False: 'false', True: 'true'}[obj] + elif isinstance(obj, int) or isinstance(obj, float): + return str(obj) + elif isinstance(obj, str): + for k, v in _escapes: + obj = obj.replace(k, v) + return '"%s"' % obj + elif isinstance(obj, unicode): + return json(obj.encode('utf-8')) + elif hasattr(obj, 'keys'): + out = [] + for k, v in obj.iteritems(): + s = '%s: %s' % (json(k), json(v)) + out.append(s) + return '{' + ', '.join(out) + '}' + elif hasattr(obj, '__iter__'): + out = [] + for i in obj: + out.append(json(i)) + return '[' + ', '.join(out) + ']' + else: + raise TypeError('cannot encode type %s' % obj.__class__.__name__) + filters = { "addbreaks": nl2br, "basename": os.path.basename, @@ -150,5 +180,5 @@ filters = { "user": lambda x: util.shortuser(x), "stringescape": lambda x: x.encode('string_escape'), "xmlescape": xmlescape, - } - + "json": json, +} diff --git a/templates/coal/changeset.tmpl b/templates/coal/changeset.tmpl --- a/templates/coal/changeset.tmpl +++ b/templates/coal/changeset.tmpl @@ -10,6 +10,7 @@