diff mercurial/hgweb/webcommands.py @ 17302:5c64ce6168da stable

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.
author wujek srujek <wujek.srujek@googlemail.com>
date Tue, 31 Jul 2012 14:14:15 +0200
parents f2d6b4f8e78c
children 06217d3cf8d9
line wrap: on
line diff
--- a/mercurial/hgweb/webcommands.py	Mon Jul 30 22:33:45 2012 -0500
+++ b/mercurial/hgweb/webcommands.py	Tue Jul 31 14:14:15 2012 +0200
@@ -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 @@
     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 @@
                 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):