[PATCH 1 of 2 v2] web: provide diff summary to the changeset page

Steven Brown stevengbrown at gmail.com
Sat Jun 11 08:54:16 CDT 2011


# HG changeset patch
# User Steven Brown <StevenGBrown at gmail.com>
# Date 1307797903 -28800
# Node ID 002e66bb87d13f1a0af3b7081c7ab8f6397a7a27
# Parent  81fc9678b018c928e2c5333b9055a66616db0531
web: provide diff summary to the changeset page

This is the same message displayed at the end of the "diff --stat" command.
For example, "9 files changed, 1651 insertions(+), 2 deletions(-)".

The webutil.diffstatgen function allows the diffstat data to be lazily
calculated only once and then re-used.

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -271,7 +271,8 @@
     diffs = webutil.diffs(web.repo, tmpl, ctx, None, parity, style)
 
     parity = paritygen(web.stripecount)
-    diffstat = webutil.diffstat(tmpl, ctx, parity)
+    diffstatgen = webutil.diffstatgen(ctx)
+    diffstat = webutil.diffstat(tmpl, ctx, diffstatgen, parity)
 
     return tmpl('changeset',
                 diff=diffs,
@@ -286,6 +287,7 @@
                 desc=ctx.description(),
                 date=ctx.date(),
                 files=files,
+                diffsummary=lambda **x: webutil.diffsummary(diffstatgen),
                 diffstat=diffstat,
                 archives=web.archivelist(ctx.hex()),
                 tags=webutil.nodetagsdict(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
@@ -8,6 +8,7 @@
 
 import os, copy
 from mercurial import match, patch, scmutil, error, ui, util
+from mercurial.i18n import _
 from mercurial.node import hex, nullid
 
 def up(p):
@@ -211,11 +212,25 @@
     yield tmpl('diffblock', parity=parity.next(),
                lines=prettyprintlines(''.join(block)))
 
-def diffstat(tmpl, ctx, parity):
-    '''Return a diffstat template for each file in the diff.'''
+def diffstatgen(ctx):
+    '''Generator function that provides the diffstat data.'''
 
     stats = patch.diffstatdata(util.iterlines(ctx.diff()))
     maxname, maxtotal, addtotal, removetotal, binary = patch.diffstatsum(stats)
+    while True:
+        yield stats, maxname, maxtotal, addtotal, removetotal, binary
+
+def diffsummary(statgen):
+    '''Return a short summary of the diff.'''
+
+    stats, maxname, maxtotal, addtotal, removetotal, binary = statgen.next()
+    return _(' %d files changed, %d insertions(+), %d deletions(-)\n') % (
+             len(stats), addtotal, removetotal)
+
+def diffstat(tmpl, ctx, statgen, parity):
+    '''Return a diffstat template for each file in the diff.'''
+
+    stats, maxname, maxtotal, addtotal, removetotal, binary = statgen.next()
     files = ctx.files()
 
     def pct(i):


More information about the Mercurial-devel mailing list