changeset 25602:85fb416f2fa7

hgweb: provide symrev (symbolic revision) property to the templates One of the features of hgweb is that current position in repo history is remembered between separate requests. That is, links from /rev/<node_hash> lead to /file/<node_hash> or /log/<node_hash>, so it's easy to dig deep into the history. However, such links could only use node hashes and local revision numbers, so while staying at one exact revision is easy, staying on top of the changes is not, because hashes presumably can't change (local revision numbers can, but probably not in a way you'd find useful for navigating). So while you could use 'tip' or 'default' in a url, links on that page would be permanent. This is not always desired (think /rev/tip or /graph/stable or /log/@) and is sometimes just confusing (i.e. /log/<not the tip hash>, when recent history is not displayed). And if user changed url deliberately to say default instead of <some node hash>, the page ignores that fact and uses node hash in its links, which means that navigation is, in a way, broken. This new property, symrev, is used for storing current revision the way it was specified, so then templates can use it in links and thus "not dereference" the symbolic revision. It is an additional way to produce links, so not every link needs to drop {node|short} in favor of {symrev}, many will still use node hash (log and filelog entries, annotate lines, etc). Some pages (e.g. summary, tags) always use the tip changeset for their context, in such cases symrev is set to 'tip'. This is needed in case the pages want to provide archive links. highlight extension needs to be updated, since _filerevision now takes an additional positional argument (signature "web, req, tmpl" is used by most of webcommands.py functions). More references to symbolic revisions and related gripes: issue2296, issue2826, issue3594, issue3634.
author Anton Shestakov <av6@dwimlabs.net>
date Tue, 16 Jun 2015 02:07:25 +0800
parents 3ec8351fa6ed
children 1cac419ab831
files hgext/highlight/__init__.py mercurial/hgweb/webcommands.py mercurial/hgweb/webutil.py
diffstat 3 files changed, 38 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/highlight/__init__.py	Tue Jun 16 23:06:57 2015 -0400
+++ b/hgext/highlight/__init__.py	Tue Jun 16 02:07:25 2015 +0800
@@ -30,7 +30,7 @@
 # leave the attribute unspecified.
 testedwith = 'internal'
 
-def filerevision_highlight(orig, web, tmpl, fctx):
+def filerevision_highlight(orig, web, req, tmpl, fctx):
     mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
     # only pygmentize for mimetype containing 'html' so we both match
     # 'text/html' and possibly 'application/xhtml+xml' in the future
@@ -42,7 +42,7 @@
     if 'html' in mt:
         style = web.config('web', 'pygments_style', 'colorful')
         highlight.pygmentize('fileline', fctx, style, tmpl)
-    return orig(web, tmpl, fctx)
+    return orig(web, req, tmpl, fctx)
 
 def annotate_highlight(orig, web, req, tmpl):
     mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
--- a/mercurial/hgweb/webcommands.py	Tue Jun 16 23:06:57 2015 -0400
+++ b/mercurial/hgweb/webcommands.py	Tue Jun 16 02:07:25 2015 +0800
@@ -100,7 +100,7 @@
     req.respond(HTTP_OK, mt, path, body=text)
     return []
 
-def _filerevision(web, tmpl, fctx):
+def _filerevision(web, req, tmpl, fctx):
     f = fctx.path()
     text = fctx.data()
     parity = paritygen(web.stripecount)
@@ -121,6 +121,7 @@
                 path=webutil.up(f),
                 text=lines(),
                 rev=fctx.rev(),
+                symrev=webutil.symrevorshortnode(req, fctx),
                 node=fctx.hex(),
                 author=fctx.user(),
                 date=fctx.date(),
@@ -158,7 +159,7 @@
     if not path:
         return manifest(web, req, tmpl)
     try:
-        return _filerevision(web, tmpl, webutil.filectx(web.repo, req))
+        return _filerevision(web, req, tmpl, webutil.filectx(web.repo, req))
     except error.LookupError, inst:
         try:
             return manifest(web, req, tmpl)
@@ -316,7 +317,7 @@
     tip = web.repo['tip']
     parity = paritygen(web.stripecount)
 
-    return tmpl('search', query=query, node=tip.hex(),
+    return tmpl('search', query=query, node=tip.hex(), symrev='tip',
                 entries=changelist, archives=web.archivelist("tip"),
                 morevars=morevars, lessvars=lessvars,
                 modedesc=searchfunc[1],
@@ -351,10 +352,12 @@
     query = ''
     if 'node' in req.form:
         ctx = webutil.changectx(web.repo, req)
+        symrev = webutil.symrevorshortnode(req, ctx)
     elif 'rev' in req.form:
         return _search(web, req, tmpl)
     else:
         ctx = web.repo['tip']
+        symrev = 'tip'
 
     def changelist():
         revs = []
@@ -403,7 +406,7 @@
         nextentry = []
 
     return tmpl(shortlog and 'shortlog' or 'changelog', changenav=changenav,
-                node=ctx.hex(), rev=pos, changesets=count,
+                node=ctx.hex(), rev=pos, symrev=symrev, changesets=count,
                 entries=entries,
                 latestentry=latestentry, nextentry=nextentry,
                 archives=web.archivelist("tip"), revcount=revcount,
@@ -470,7 +473,12 @@
 
     The ``manifest`` template will be rendered for this handler.
     """
-    ctx = webutil.changectx(web.repo, req)
+    if 'node' in req.form:
+        ctx = webutil.changectx(web.repo, req)
+        symrev = webutil.symrevorshortnode(req, ctx)
+    else:
+        ctx = web.repo['tip']
+        symrev = 'tip'
     path = webutil.cleanpath(web.repo, req.form.get('file', [''])[0])
     mf = ctx.manifest()
     node = ctx.node()
@@ -539,6 +547,7 @@
 
     return tmpl("manifest",
                 rev=ctx.rev(),
+                symrev=symrev,
                 node=hex(node),
                 path=abspath,
                 up=webutil.up(abspath),
@@ -755,6 +764,7 @@
                 branches=branches,
                 shortlog=changelist,
                 node=tip.hex(),
+                symrev='tip',
                 archives=web.archivelist("tip"))
 
 @webcommand('filediff')
@@ -803,6 +813,7 @@
                 file=path,
                 node=hex(n),
                 rev=ctx.rev(),
+                symrev=webutil.symrevorshortnode(req, ctx),
                 date=ctx.date(),
                 desc=ctx.description(),
                 extra=ctx.extra(),
@@ -877,6 +888,7 @@
                 file=path,
                 node=hex(ctx.node()),
                 rev=ctx.rev(),
+                symrev=webutil.symrevorshortnode(req, ctx),
                 date=ctx.date(),
                 desc=ctx.description(),
                 extra=ctx.extra(),
@@ -944,6 +956,7 @@
                 annotate=annotate,
                 path=webutil.up(f),
                 rev=fctx.rev(),
+                symrev=webutil.symrevorshortnode(req, fctx),
                 node=fctx.hex(),
                 author=fctx.user(),
                 date=fctx.date(),
@@ -1043,6 +1056,7 @@
     revnav = webutil.filerevnav(web.repo, fctx.path())
     nav = revnav.gen(end - 1, revcount, count)
     return tmpl("filelog", file=f, node=fctx.hex(), nav=nav,
+                symrev=webutil.symrevorshortnode(req, fctx),
                 entries=entries,
                 latestentry=latestentry,
                 revcount=revcount, morevars=morevars, lessvars=lessvars)
@@ -1149,7 +1163,12 @@
     This handler will render the ``graph`` template.
     """
 
-    ctx = webutil.changectx(web.repo, req)
+    if 'node' in req.form:
+        ctx = webutil.changectx(web.repo, req)
+        symrev = webutil.symrevorshortnode(req, ctx)
+    else:
+        ctx = web.repo['tip']
+        symrev = 'tip'
     rev = ctx.rev()
 
     bg_height = 39
@@ -1252,7 +1271,8 @@
     rows = len(tree)
     canvasheight = (rows + 1) * bg_height - 27
 
-    return tmpl('graph', rev=rev, revcount=revcount, uprev=uprev,
+    return tmpl('graph', rev=rev, symrev=symrev, revcount=revcount,
+                uprev=uprev,
                 lessvars=lessvars, morevars=morevars, downrev=downrev,
                 cols=cols, rows=rows,
                 canvaswidth=(cols + 1) * bg_height,
--- a/mercurial/hgweb/webutil.py	Tue Jun 16 23:06:57 2015 -0400
+++ b/mercurial/hgweb/webutil.py	Tue Jun 16 02:07:25 2015 +0800
@@ -6,10 +6,10 @@
 # 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, copy
+import os, copy, urllib
 from mercurial import match, patch, error, ui, util, pathutil, context
 from mercurial.i18n import _
-from mercurial.node import hex, nullid
+from mercurial.node import hex, nullid, short
 from common import ErrorResponse, paritygen
 from common import HTTP_NOT_FOUND
 import difflib
@@ -279,6 +279,12 @@
         "branches": nodebranchdict(repo, ctx)
     }
 
+def symrevorshortnode(req, ctx):
+    if 'node' in req.form:
+        return urllib.quote(req.form['node'][0])
+    else:
+        return short(ctx.node())
+
 def changesetentry(web, req, tmpl, ctx):
     '''Obtain a dictionary to be used to render the "changeset" template.'''
 
@@ -314,6 +320,7 @@
         diff=diff,
         rev=ctx.rev(),
         node=ctx.hex(),
+        symrev=symrevorshortnode(req, ctx),
         parent=tuple(parents(ctx)),
         child=children(ctx),
         basenode=basectx.hex(),