[PATCH] web: provide diffstat to the changeset page

Steven Brown stevengbrown at gmail.com
Sat May 28 01:51:42 CDT 2011


# HG changeset patch
# User Steven Brown <StevenGBrown at gmail.com>
# Date 1306565085 -28800
# Node ID f1c73639ff9b70787389dfdebe0a5de4f80cd105
# Parent  d1a1578c5f7860b17f2f238b2c1e6adb555ffa4f
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.

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -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 = 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()),
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -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


More information about the Mercurial-devel mailing list