[PATCH 1 of 2 stable] hgweb: fixes traceback for invalid files by removing top-level template

wujek srujek wujek.srujek at gmail.com
Mon Jul 30 18:33:46 CDT 2012


# HG changeset patch
# User wujek srujek <wujek.srujek at googlemail.com>
# Date 1343688140 -7200
# Branch stable
# Node ID a9add455b8e6b2a15e0533895cb3f6c3b4529426
# Parent  a09cc6aeed4a6d54a3846b6653165fd3b328d1ef
hgweb: fixes traceback for invalid files by removing top-level template

The top-level 'comparison' template was not really needed, and it also caused a
traceback to be shown for inexistent files (as reported by Ross Lagerwall).
Getting rid of it makes the overall templating structure simpler and causes
invalid files to be handled nicely.

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
@@ -595,7 +595,39 @@
     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()),
@@ -607,6 +639,10 @@
                 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
@@ -221,17 +221,9 @@
     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 ''
@@ -269,43 +261,12 @@
                                    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 @@
 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 @@
 </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 @@
 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 @@
 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 @@
   <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 @@
 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 @@
     <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 @@
     <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 @@
     <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>


More information about the Mercurial-devel mailing list