web: provide diffstat to the changeset page
authorSteven Brown <StevenGBrown@gmail.com>
Sat, 28 May 2011 14:44:45 +0800
changeset 14490 1d3e2349304a
parent 14489 3a27faf9a999
child 14491 a086b91ce7fb
web: provide diffstat to the changeset page This includes all affected files, so it can be used for an extended view of the files or as a replacement for the filenodelink and filenolink templates.
mercurial/hgweb/webcommands.py
mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webcommands.py	Tue May 31 16:24:47 2011 -0500
+++ b/mercurial/hgweb/webcommands.py	Sat May 28 14:44:45 2011 +0800
@@ -263,12 +263,16 @@
                           node=ctx.hex(), file=f,
                           parity=parity.next()))
 
-    parity = paritygen(web.stripecount)
     style = web.config('web', 'style', 'paper')
     if 'style' in req.form:
         style = req.form['style'][0]
 
+    parity = paritygen(web.stripecount)
     diffs = webutil.diffs(web.repo, tmpl, ctx, None, parity, style)
+
+    parity = paritygen(web.stripecount)
+    diffstat = lambda: webutil.diffstat(tmpl, ctx, parity)
+
     return tmpl('changeset',
                 diff=diffs,
                 rev=ctx.rev(),
@@ -282,6 +286,7 @@
                 desc=ctx.description(),
                 date=ctx.date(),
                 files=files,
+                diffstat=diffstat,
                 archives=web.archivelist(ctx.hex()),
                 tags=webutil.nodetagsdict(web.repo, ctx.node()),
                 bookmarks=webutil.nodebookmarksdict(web.repo, ctx.node()),
--- a/mercurial/hgweb/webutil.py	Tue May 31 16:24:47 2011 -0500
+++ b/mercurial/hgweb/webutil.py	Sat May 28 14:44:45 2011 +0800
@@ -7,7 +7,7 @@
 # GNU General Public License version 2 or any later version.
 
 import os, copy
-from mercurial import match, patch, scmutil, error, ui
+from mercurial import match, patch, scmutil, error, ui, util
 from mercurial.node import hex, nullid
 
 def up(p):
@@ -211,6 +211,26 @@
     yield tmpl('diffblock', parity=parity.next(),
                lines=prettyprintlines(''.join(block)))
 
+def diffstat(tmpl, ctx, parity):
+    '''Return a diffstat template for each file in the cset.'''
+
+    stats = patch.diffstatdata(util.iterlines(ctx.diff()))
+    maxname, maxtotal, addtotal, removetotal, binary = patch.diffstatsum(stats)
+
+    statsdict = {}
+    if maxtotal > 0:
+        for filename, adds, removes, isbinary in stats:
+            total = adds + removes
+            addpct = (float(adds) / maxtotal) * 100
+            removepct = (float(removes) / maxtotal) * 100
+            statsdict[filename] = (total, addpct, removepct)
+
+    for f in ctx.files():
+        template = f in ctx and 'diffstatlink' or 'diffstatnolink'
+        total, addpct, removepct = statsdict.get(f, ('', 0, 0))
+        yield tmpl(template, node=ctx.hex(), file=f, total=total,
+            addpct=addpct, removepct=removepct, parity=parity.next())
+
 class sessionvars(object):
     def __init__(self, vars, start='?'):
         self.start = start