diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -8,7 +8,7 @@
 import os, mimetypes, re, cgi, copy
 import webutil
 from mercurial import error, encoding, archival, templater, templatefilters
-from mercurial.node import short, hex
+from mercurial.node import short, hex, nullid
 from mercurial.util import binary
 from common import paritygen, staticfile, get_contact, ErrorResponse
 from common import HTTP_OK, HTTP_FORBIDDEN, HTTP_NOT_FOUND
@@ -597,7 +597,39 @@ def comparison(web, req, tmpl):
     else:
         context = parsecontext(web.config('web', 'comparisoncontext', '5'))
 
-    comparison = webutil.compare(tmpl, ctx, path, context)
+    def filelines(f):
+        if binary(f.data()):
+            mt = mimetypes.guess_type(f.path())[0]
+            if not mt:
+                mt = 'application/octet-stream'
+            return [_('(binary file %s, hash: %s)') % (mt, hex(f.filenode()))]
+        return f.data().splitlines()
+
+    if path in ctx:
+        fctx = ctx[path]
+        rightrev = fctx.filerev()
+        rightnode = fctx.filenode()
+        rightlines = filelines(fctx)
+        parents = fctx.parents()
+        if not parents:
+            leftrev = -1
+            leftnode = nullid
+            leftlines = ()
+        else:
+            pfctx = parents[0]
+            leftrev = pfctx.filerev()
+            leftnode = pfctx.filenode()
+            leftlines = filelines(pfctx)
+    else:
+        rightrev = -1
+        rightnode = nullid
+        rightlines = ()
+        fctx = ctx.parents()[0][path]
+        leftrev = fctx.filerev()
+        leftnode = fctx.filenode()
+        leftlines = filelines(fctx)
+
+    comparison = webutil.compare(tmpl, context, leftlines, rightlines)
     return tmpl('filecomparison',
                 file=path,
                 node=hex(ctx.node()),
@@ -609,6 +641,10 @@ def comparison(web, req, tmpl):
                 branch=webutil.nodebranchnodefault(ctx),
                 parent=webutil.parents(ctx),
                 child=webutil.children(ctx),
+                leftrev=leftrev,
+                leftnode=hex(leftnode),
+                rightrev=rightrev,
+                rightnode=hex(rightnode),
                 comparison=comparison)
 
 def annotate(web, req, tmpl):
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -6,7 +6,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-import os, mimetypes, copy
+import os, copy
 from mercurial import match, patch, scmutil, error, ui, util
 from mercurial.i18n import _
 from mercurial.node import hex, nullid
@@ -227,17 +227,9 @@ def diffs(repo, tmpl, ctx, files, parity
     yield tmpl('diffblock', parity=parity.next(), blockno=blockno,
                lines=prettyprintlines(''.join(block), blockno))
 
-def compare(tmpl, ctx, path, context):
+def compare(tmpl, context, leftlines, rightlines):
     '''Generator function that provides side-by-side comparison data.'''
 
-    def filelines(f):
-        if util.binary(f.data()):
-            mt = mimetypes.guess_type(f.path())[0]
-            if not mt:
-                mt = 'application/octet-stream'
-            return [_('(binary file %s, hash: %s)') % (mt, hex(f.filenode()))]
-        return f.data().splitlines()
-
     def compline(type, leftlineno, leftline, rightlineno, rightline):
         lineid = leftlineno and ("l%s" % leftlineno) or ''
         lineid += rightlineno and ("r%s" % rightlineno) or ''
@@ -275,43 +267,12 @@ def compare(tmpl, ctx, path, context):
                                    rightlineno=i + 1,
                                    rightline=rightlines[i])
 
-    if path in ctx:
-        fctx = ctx[path]
-        rightrev = fctx.filerev()
-        rightnode = fctx.filenode()
-        rightlines = filelines(fctx)
-        parents = fctx.parents()
-        if not parents:
-            leftrev = -1
-            leftnode = nullid
-            leftlines = ()
-        else:
-            pfctx = parents[0]
-            leftrev = pfctx.filerev()
-            leftnode = pfctx.filenode()
-            leftlines = filelines(pfctx)
-    else:
-        rightrev = -1
-        rightnode = nullid
-        rightlines = ()
-        fctx = ctx.parents()[0][path]
-        leftrev = fctx.filerev()
-        leftnode = fctx.filenode()
-        leftlines = filelines(fctx)
-
     s = difflib.SequenceMatcher(None, leftlines, rightlines)
     if context < 0:
-        blocks = [tmpl('comparisonblock', lines=getblock(s.get_opcodes()))]
+        yield tmpl('comparisonblock', lines=getblock(s.get_opcodes()))
     else:
-        blocks = (tmpl('comparisonblock', lines=getblock(oc))
-                     for oc in s.get_grouped_opcodes(n=context))
-
-    yield tmpl('comparison',
-               leftrev=leftrev,
-               leftnode=hex(leftnode),
-               rightrev=rightrev,
-               rightnode=hex(rightnode),
-               blocks=blocks)
+        for oc in s.get_grouped_opcodes(n=context):
+            yield tmpl('comparisonblock', lines=getblock(oc))
 
 def diffstatgen(ctx):
     '''Generator function that provides the diffstat data.'''
diff --git a/mercurial/templates/coal/map b/mercurial/templates/coal/map
--- a/mercurial/templates/coal/map
+++ b/mercurial/templates/coal/map
@@ -84,16 +84,6 @@ difflineminus = '<a href="#{lineid}" id=
 difflineat = '<a href="#{lineid}" id="{lineid}">{linenumber}</a> <span class="atline">{line|escape}</span>'
 diffline = '<a href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}'
 
-comparison = '
-  <table class="bigtable">
-    <thead class="header">
-      <tr>
-        <th>{leftrev}:{leftnode|short}</th>
-        <th>{rightrev}:{rightnode|short}</th>
-      </tr>
-    </thead>
-    {blocks}
-  </table>'
 comparisonblock ='
   <tbody class="block">
   {lines}
diff --git a/mercurial/templates/gitweb/filecomparison.tmpl b/mercurial/templates/gitweb/filecomparison.tmpl
--- a/mercurial/templates/gitweb/filecomparison.tmpl
+++ b/mercurial/templates/gitweb/filecomparison.tmpl
@@ -55,7 +55,15 @@ comparison |
 </div>
 
 <div class="comparison">
-{comparison}
+  <table style="border-collapse:collapse;">
+    <thead class="header">
+      <tr>
+        <th>{leftrev}:{leftnode|short}</th>
+        <th>{rightrev}:{rightnode|short}</th>
+      </tr>
+    </thead>
+    {comparison}
+  </table>
 </div>
 
 </div>
diff --git a/mercurial/templates/gitweb/map b/mercurial/templates/gitweb/map
--- a/mercurial/templates/gitweb/map
+++ b/mercurial/templates/gitweb/map
@@ -103,16 +103,6 @@ difflineminus = '<span style="color:#cc0
 difflineat = '<span style="color:#990099;"><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</span>'
 diffline = '<span><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</span>'
 
-comparison = '
-  <table style="border-collapse:collapse;">
-    <thead class="header">
-      <tr>
-        <th>{leftrev}:{leftnode|short}</th>
-        <th>{rightrev}:{rightnode|short}</th>
-      </tr>
-    </thead>
-    {blocks}
-  </table>'
 comparisonblock ='
   <tbody class="block">
   {lines}
diff --git a/mercurial/templates/monoblue/filecomparison.tmpl b/mercurial/templates/monoblue/filecomparison.tmpl
--- a/mercurial/templates/monoblue/filecomparison.tmpl
+++ b/mercurial/templates/monoblue/filecomparison.tmpl
@@ -58,7 +58,15 @@
     </div>
 
     <div class="comparison">
-    {comparison}
+      <table class="bigtable">
+        <thead class="header">
+          <tr>
+            <th>{leftrev}:{leftnode|short}</th>
+            <th>{rightrev}:{rightnode|short}</th>
+          </tr>
+        </thead>
+        {comparison}
+      </table>
     </div>
 
 {footer}
diff --git a/mercurial/templates/monoblue/map b/mercurial/templates/monoblue/map
--- a/mercurial/templates/monoblue/map
+++ b/mercurial/templates/monoblue/map
@@ -98,16 +98,6 @@ difflineminus = '<span style="color:#cc0
 difflineat = '<span style="color:#990099;"><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</span>'
 diffline = '<span><a class="linenr" href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}</span>'
 
-comparison = '
-  <table class="bigtable">
-    <thead class="header">
-      <tr>
-        <th>{leftrev}:{leftnode|short}</th>
-        <th>{rightrev}:{rightnode|short}</th>
-      </tr>
-    </thead>
-    {blocks}
-  </table>'
 comparisonblock ='
   <tbody class="block">
   {lines}
diff --git a/mercurial/templates/paper/filecomparison.tmpl b/mercurial/templates/paper/filecomparison.tmpl
--- a/mercurial/templates/paper/filecomparison.tmpl
+++ b/mercurial/templates/paper/filecomparison.tmpl
@@ -76,7 +76,15 @@ files, or words in the commit message</d
   <span class="legendinfo replace">replaced</span>
 </div>
 
-{comparison}
+<table class="bigtable">
+  <thead class="header">
+    <tr>
+      <th>{leftrev}:{leftnode|short}</th>
+      <th>{rightrev}:{rightnode|short}</th>
+    </tr>
+  </thead>
+  {comparison}
+</table>
 
 </div>
 </div>
diff --git a/mercurial/templates/paper/map b/mercurial/templates/paper/map
--- a/mercurial/templates/paper/map
+++ b/mercurial/templates/paper/map
@@ -83,16 +83,6 @@ difflineminus = '<a href="#{lineid}" id=
 difflineat = '<a href="#{lineid}" id="{lineid}">{linenumber}</a> <span class="atline">{line|escape}</span>'
 diffline = '<a href="#{lineid}" id="{lineid}">{linenumber}</a> {line|escape}'
 
-comparison = '
-  <table class="bigtable">
-    <thead class="header">
-      <tr>
-        <th>{leftrev}:{leftnode|short}</th>
-        <th>{rightrev}:{rightnode|short}</th>
-      </tr>
-    </thead>
-    {blocks}
-  </table>'
 comparisonblock ='
   <tbody class="block">
   {lines}
diff --git a/tests/test-hgweb-diffs.t b/tests/test-hgweb-diffs.t
--- a/tests/test-hgweb-diffs.t
+++ b/tests/test-hgweb-diffs.t
@@ -642,15 +642,14 @@ comparison new file
     <span class="legendinfo replace">replaced</span>
   </div>
   
-  
   <table class="bigtable">
-  <thead class="header">
-  <tr>
-  <th>-1:000000000000</th>
-  <th>0:b789fdd96dc2</th>
-  </tr>
-  </thead>
-  
+    <thead class="header">
+      <tr>
+        <th>-1:000000000000</th>
+        <th>0:b789fdd96dc2</th>
+      </tr>
+    </thead>
+    
   <tbody class="block">
   
   <tr>
@@ -765,15 +764,14 @@ comparison existing file
     <span class="legendinfo replace">replaced</span>
   </div>
   
-  
   <table class="bigtable">
-  <thead class="header">
-  <tr>
-  <th>0:b789fdd96dc2</th>
-  <th>1:a80d06849b33</th>
-  </tr>
-  </thead>
-  
+    <thead class="header">
+      <tr>
+        <th>0:b789fdd96dc2</th>
+        <th>1:a80d06849b33</th>
+      </tr>
+    </thead>
+    
   <tbody class="block">
   
   <tr>
@@ -890,15 +888,14 @@ comparison removed file
     <span class="legendinfo replace">replaced</span>
   </div>
   
-  
   <table class="bigtable">
-  <thead class="header">
-  <tr>
-  <th>1:a80d06849b33</th>
-  <th>-1:000000000000</th>
-  </tr>
-  </thead>
-  
+    <thead class="header">
+      <tr>
+        <th>1:a80d06849b33</th>
+        <th>-1:000000000000</th>
+      </tr>
+    </thead>
+    
   <tbody class="block">
   
   <tr>