# HG changeset patch # User Dirkjan Ochtman # Date 2008-06-18 05:06:41 # Node ID 0dba955c263683bf17edad53543b34cc1ad08366 # Parent 127e8c3466d1969b495fd58eb114191d666a72df add graph page to hgweb 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 @@
    diff --git a/templates/coal/error.tmpl b/templates/coal/error.tmpl --- a/templates/coal/error.tmpl +++ b/templates/coal/error.tmpl @@ -11,6 +11,7 @@ diff --git a/templates/coal/fileannotate.tmpl b/templates/coal/fileannotate.tmpl --- a/templates/coal/fileannotate.tmpl +++ b/templates/coal/fileannotate.tmpl @@ -11,6 +11,7 @@ diff --git a/templates/coal/filediff.tmpl b/templates/coal/filediff.tmpl --- a/templates/coal/filediff.tmpl +++ b/templates/coal/filediff.tmpl @@ -11,6 +11,7 @@
      diff --git a/templates/coal/filelog.tmpl b/templates/coal/filelog.tmpl --- a/templates/coal/filelog.tmpl +++ b/templates/coal/filelog.tmpl @@ -16,6 +16,7 @@
        diff --git a/templates/coal/filerevision.tmpl b/templates/coal/filerevision.tmpl --- a/templates/coal/filerevision.tmpl +++ b/templates/coal/filerevision.tmpl @@ -11,6 +11,7 @@
          diff --git a/templates/coal/graph.tmpl b/templates/coal/graph.tmpl new file mode 100644 --- /dev/null +++ b/templates/coal/graph.tmpl @@ -0,0 +1,114 @@ +{header} +{repo|escape}: revision graph + + + + + + +
          + + +
          +

          {repo|escape}

          +

          graph

          + + + + + +
          The revision graph only works with JavaScript-enabled browsers.
          + +
          +
            + +
              +
              + + + + + + +
              +
              + +{footer} diff --git a/templates/coal/manifest.tmpl b/templates/coal/manifest.tmpl --- a/templates/coal/manifest.tmpl +++ b/templates/coal/manifest.tmpl @@ -11,6 +11,7 @@
                diff --git a/templates/coal/map b/templates/coal/map --- a/templates/coal/map +++ b/templates/coal/map @@ -8,6 +8,7 @@ search = search.tmpl changelog = shortlog.tmpl shortlog = shortlog.tmpl shortlogentry = shortlogentry.tmpl +graph = graph.tmpl naventry = '{label|escape} ' navshortentry = '{label|escape} ' diff --git a/templates/coal/search.tmpl b/templates/coal/search.tmpl --- a/templates/coal/search.tmpl +++ b/templates/coal/search.tmpl @@ -11,6 +11,7 @@ diff --git a/templates/coal/shortlog.tmpl b/templates/coal/shortlog.tmpl --- a/templates/coal/shortlog.tmpl +++ b/templates/coal/shortlog.tmpl @@ -15,6 +15,7 @@
                  diff --git a/templates/coal/tags.tmpl b/templates/coal/tags.tmpl --- a/templates/coal/tags.tmpl +++ b/templates/coal/tags.tmpl @@ -15,6 +15,7 @@ diff --git a/templates/static/excanvas.js b/templates/static/excanvas.js new file mode 100644 --- /dev/null +++ b/templates/static/excanvas.js @@ -0,0 +1,19 @@ +if(!window.CanvasRenderingContext2D){(function(){var I=Math,i=I.round,L=I.sin,M=I.cos,m=10,A=m/2,Q={init:function(a){var b=a||document;if(/MSIE/.test(navigator.userAgent)&&!window.opera){var c=this;b.attachEvent("onreadystatechange",function(){c.r(b)})}},r:function(a){if(a.readyState=="complete"){if(!a.namespaces["s"]){a.namespaces.add("g_vml_","urn:schemas-microsoft-com:vml")}var b=a.createStyleSheet();b.cssText="canvas{display:inline-block;overflow:hidden;text-align:left;width:300px;height:150px}g_vml_\\:*{behavior:url(#default#VML)}"; +var c=a.getElementsByTagName("canvas");for(var d=0;d"){var d="/"+a.tagName,e;while((e=a.nextSibling)&&e.tagName!=d){e.removeNode()}if(e){e.removeNode()}}a.parentNode.replaceChild(c,a);return c},initElement:function(a){a=this.q(a);a.getContext=function(){if(this.l){return this.l}return this.l=new K(this)};a.attachEvent("onpropertychange",V);a.attachEvent("onresize", +W);var b=a.attributes;if(b.width&&b.width.specified){a.style.width=b.width.nodeValue+"px"}else{a.width=a.clientWidth}if(b.height&&b.height.specified){a.style.height=b.height.nodeValue+"px"}else{a.height=a.clientHeight}return a}};function V(a){var b=a.srcElement;switch(a.propertyName){case "width":b.style.width=b.attributes.width.nodeValue+"px";b.getContext().clearRect();break;case "height":b.style.height=b.attributes.height.nodeValue+"px";b.getContext().clearRect();break}}function W(a){var b=a.srcElement; +if(b.firstChild){b.firstChild.style.width=b.clientWidth+"px";b.firstChild.style.height=b.clientHeight+"px"}}Q.init();var R=[];for(var E=0;E<16;E++){for(var F=0;F<16;F++){R[E*16+F]=E.toString(16)+F.toString(16)}}function J(){return[[1,0,0],[0,1,0],[0,0,1]]}function G(a,b){var c=J();for(var d=0;d<3;d++){for(var e=0;e<3;e++){var g=0;for(var h=0;h<3;h++){g+=a[d][h]*b[h][e]}c[d][e]=g}}return c}function N(a,b){b.fillStyle=a.fillStyle;b.lineCap=a.lineCap;b.lineJoin=a.lineJoin;b.lineWidth=a.lineWidth;b.miterLimit= +a.miterLimit;b.shadowBlur=a.shadowBlur;b.shadowColor=a.shadowColor;b.shadowOffsetX=a.shadowOffsetX;b.shadowOffsetY=a.shadowOffsetY;b.strokeStyle=a.strokeStyle;b.d=a.d;b.e=a.e}function O(a){var b,c=1;a=String(a);if(a.substring(0,3)=="rgb"){var d=a.indexOf("(",3),e=a.indexOf(")",d+1),g=a.substring(d+1,e).split(",");b="#";for(var h=0;h<3;h++){b+=R[Number(g[h])]}if(g.length==4&&a.substr(3,1)=="a"){c=g[3]}}else{b=a}return[b,c]}function S(a){switch(a){case "butt":return"flat";case "round":return"round"; +case "square":default:return"square"}}function K(a){this.a=J();this.m=[];this.k=[];this.c=[];this.strokeStyle="#000";this.fillStyle="#000";this.lineWidth=1;this.lineJoin="miter";this.lineCap="butt";this.miterLimit=m*1;this.globalAlpha=1;this.canvas=a;var b=a.ownerDocument.createElement("div");b.style.width=a.clientWidth+"px";b.style.height=a.clientHeight+"px";b.style.overflow="hidden";b.style.position="absolute";a.appendChild(b);this.j=b;this.d=1;this.e=1}var j=K.prototype;j.clearRect=function(){this.j.innerHTML= +"";this.c=[]};j.beginPath=function(){this.c=[]};j.moveTo=function(a,b){this.c.push({type:"moveTo",x:a,y:b});this.f=a;this.g=b};j.lineTo=function(a,b){this.c.push({type:"lineTo",x:a,y:b});this.f=a;this.g=b};j.bezierCurveTo=function(a,b,c,d,e,g){this.c.push({type:"bezierCurveTo",cp1x:a,cp1y:b,cp2x:c,cp2y:d,x:e,y:g});this.f=e;this.g=g};j.quadraticCurveTo=function(a,b,c,d){var e=this.f+0.6666666666666666*(a-this.f),g=this.g+0.6666666666666666*(b-this.g),h=e+(c-this.f)/3,l=g+(d-this.g)/3;this.bezierCurveTo(e, +g,h,l,c,d)};j.arc=function(a,b,c,d,e,g){c*=m;var h=g?"at":"wa",l=a+M(d)*c-A,n=b+L(d)*c-A,o=a+M(e)*c-A,f=b+L(e)*c-A;if(l==o&&!g){l+=0.125}this.c.push({type:h,x:a,y:b,radius:c,xStart:l,yStart:n,xEnd:o,yEnd:f})};j.rect=function(a,b,c,d){this.moveTo(a,b);this.lineTo(a+c,b);this.lineTo(a+c,b+d);this.lineTo(a,b+d);this.closePath()};j.strokeRect=function(a,b,c,d){this.beginPath();this.moveTo(a,b);this.lineTo(a+c,b);this.lineTo(a+c,b+d);this.lineTo(a,b+d);this.closePath();this.stroke()};j.fillRect=function(a, +b,c,d){this.beginPath();this.moveTo(a,b);this.lineTo(a+c,b);this.lineTo(a+c,b+d);this.lineTo(a,b+d);this.closePath();this.fill()};j.createLinearGradient=function(a,b,c,d){var e=new H("gradient");return e};j.createRadialGradient=function(a,b,c,d,e,g){var h=new H("gradientradial");h.n=c;h.o=g;h.i.x=a;h.i.y=b;return h};j.drawImage=function(a,b){var c,d,e,g,h,l,n,o,f=a.runtimeStyle.width,k=a.runtimeStyle.height;a.runtimeStyle.width="auto";a.runtimeStyle.height="auto";var q=a.width,r=a.height;a.runtimeStyle.width= +f;a.runtimeStyle.height=k;if(arguments.length==3){c=arguments[1];d=arguments[2];h=(l=0);n=(e=q);o=(g=r)}else if(arguments.length==5){c=arguments[1];d=arguments[2];e=arguments[3];g=arguments[4];h=(l=0);n=q;o=r}else if(arguments.length==9){h=arguments[1];l=arguments[2];n=arguments[3];o=arguments[4];c=arguments[5];d=arguments[6];e=arguments[7];g=arguments[8]}else{throw"Invalid number of arguments";}var s=this.b(c,d),t=[],v=10,w=10;t.push(" ','","");this.j.insertAdjacentHTML("BeforeEnd",t.join(""))};j.stroke=function(a){var b=[],c=O(a?this.fillStyle:this.strokeStyle),d=c[0],e=c[1]*this.globalAlpha,g=10,h=10;b.push("n.x){n.x=k.x}if(l.y== +null||k.yn.y){n.y=k.y}}}b.push(' ">');if(typeof this.fillStyle=="object"){var v={x:"50%",y:"50%"},w=n.x-l.x,x=n.y-l.y,p=w>x?w:x;v.x=i(this.fillStyle.i.x/w*100+50)+"%";v.y=i(this.fillStyle.i.y/x*100+50)+"%";var y=[];if(this.fillStyle.p=="gradientradial"){var z=this.fillStyle.n/p*100,B=this.fillStyle.o/p*100-z}else{var z=0,B=100}var C={offset:null,color:null},D={offset:null,color:null};this.fillStyle.h.sort(function(T,U){return T.offset-U.offset});for(var o=0;oC.offset||C.offset==null){C.offset=u.offset;C.color=u.color}if(u.offset')}else if(a){b.push('')}else{b.push("')}b.push("");this.j.insertAdjacentHTML("beforeEnd",b.join(""));this.c=[]};j.fill=function(){this.stroke(true)};j.closePath=function(){this.c.push({type:"close"})};j.b=function(a,b){return{x:m*(a*this.a[0][0]+b*this.a[1][0]+this.a[2][0])-A,y:m*(a*this.a[0][1]+b*this.a[1][1]+this.a[2][1])-A}};j.save=function(){var a={};N(this,a); +this.k.push(a);this.m.push(this.a);this.a=G(J(),this.a)};j.restore=function(){N(this.k.pop(),this);this.a=this.m.pop()};j.translate=function(a,b){var c=[[1,0,0],[0,1,0],[a,b,1]];this.a=G(c,this.a)};j.rotate=function(a){var b=M(a),c=L(a),d=[[b,c,0],[-c,b,0],[0,0,1]];this.a=G(d,this.a)};j.scale=function(a,b){this.d*=a;this.e*=b;var c=[[a,0,0],[0,b,0],[0,0,1]];this.a=G(c,this.a)};j.clip=function(){};j.arcTo=function(){};j.createPattern=function(){return new P};function H(a){this.p=a;this.n=0;this.o= +0;this.h=[];this.i={x:0,y:0}}H.prototype.addColorStop=function(a,b){b=O(b);this.h.push({offset:1-a,color:b})};function P(){}G_vmlCanvasManager=Q;CanvasRenderingContext2D=K;CanvasGradient=H;CanvasPattern=P})()}; diff --git a/templates/static/graph.js b/templates/static/graph.js new file mode 100644 --- /dev/null +++ b/templates/static/graph.js @@ -0,0 +1,127 @@ +// branch_renderer.js - Rendering of branch DAGs on the client side +// +// Copyright 2008 Dirkjan Ochtman +// Copyright 2006 Alexander Schremmer +// +// derived from code written by Scott James Remnant +// Copyright 2005 Canonical Ltd. +// +// This software may be used and distributed according to the terms +// of the GNU General Public License, incorporated herein by reference. + +var colors = [ + [ 1.0, 0.0, 0.0 ], + [ 1.0, 1.0, 0.0 ], + [ 0.0, 1.0, 0.0 ], + [ 0.0, 1.0, 1.0 ], + [ 0.0, 0.0, 1.0 ], + [ 1.0, 0.0, 1.0 ] +]; + +function Graph() { + + this.canvas = document.getElementById('graph'); + if (navigator.userAgent.indexOf('MSIE') >= 0) this.canvas = window.G_vmlCanvasManager.initElement(this.canvas); + this.ctx = this.canvas.getContext('2d'); + this.ctx.strokeStyle = 'rgb(0, 0, 0)'; + this.ctx.fillStyle = 'rgb(0, 0, 0)'; + this.cur = [0, 0]; + this.line_width = 3; + this.bg = [0, 4]; + this.cell = [2, 0]; + this.columns = 0; + this.revlink = ''; + + this.scale = function(height) { + this.bg_height = height; + this.box_size = Math.floor(this.bg_height / 1.2); + this.cell_height = this.box_size; + } + + function colorPart(num) { + num *= 255 + num = num < 0 ? 0 : num; + num = num > 255 ? 255 : num; + var digits = Math.round(num).toString(16); + if (num < 16) { + return '0' + digits; + } else { + return digits; + } + } + + this.setColor = function(color, bg, fg) { + + // Set the colour. + // + // Picks a distinct colour based on an internal wheel; the bg + // parameter provides the value that should be assigned to the 'zero' + // colours and the fg parameter provides the multiplier that should be + // applied to the foreground colours. + + color %= colors.length; + var red = (colors[color][0] * fg) || bg; + var green = (colors[color][1] * fg) || bg; + var blue = (colors[color][2] * fg) || bg; + red = Math.round(red * 255); + green = Math.round(green * 255); + blue = Math.round(blue * 255); + var s = 'rgb(' + red + ', ' + green + ', ' + blue + ')'; + this.ctx.strokeStyle = s; + this.ctx.fillStyle = s; + return s; + + } + + this.render = function(data) { + + for (var i in data) { + + var parity = i % 2; + this.cell[1] += this.bg_height; + this.bg[1] += this.bg_height; + + var cur = data[i]; + var node = cur[1]; + var edges = cur[2]; + var fold = false; + + for (var j in edges) { + + line = edges[j]; + start = line[0]; + end = line[1]; + color = line[2]; + + if (end > this.columns) { + this.columns += 1; + } else if (start == this.columns && start > end) { + var fold = true; + } + + x0 = this.cell[0] + this.box_size * start + this.box_size / 2; + y0 = this.bg[1] - this.bg_height / 2; + x1 = this.cell[0] + this.box_size * end + this.box_size / 2; + y1 = this.bg[1] + this.bg_height / 2; + + this.edge(x0, y0, x1, y1, color); + + } + + // Draw the revision node in the right column + + column = node[0] + color = node[1] + + radius = this.box_size / 8; + x = this.cell[0] + this.box_size * column + this.box_size / 2; + y = this.bg[1] - this.bg_height / 2; + this.vertex(x, y, color, parity, cur); + + if (fold) this.columns -= 1; + + } + + } + +} diff --git a/templates/static/style-coal.css b/templates/static/style-coal.css --- a/templates/static/style-coal.css +++ b/templates/static/style-coal.css @@ -154,3 +154,44 @@ div.description { margin: 1em 0 1em 0; padding: .3em; } + +div#wrapper { + position: relative; + border-top: 1px solid black; + border-bottom: 1px solid black; + margin: 0; + padding: 0; +} + +canvas { + position: absolute; + z-index: 5; + top: -0.7em; + margin: 0; +} + +ul#graphnodes { + position: absolute; + z-index: 10; + top: -1.0em; + list-style: none inside none; + padding: 0; +} + +ul#nodebgs { + list-style: none inside none; + padding: 0; + margin: 0; + top: -0.7em; +} + +ul#graphnodes li, ul#nodebgs li { + height: 39px; +} + +ul#graphnodes li .info { + display: block; + font-size: 70%; + position: relative; + top: -3px; +}