Patchwork [2,of,6] hgweb: provide symrev (symbolic revision) property to the templates

login
register
mail settings
Submitter Anton Shestakov
Date June 16, 2015, 4:27 p.m.
Message ID <d6c8ce47bc7a805c50d7.1434472024@neuro>
Download mbox | patch
Permalink /patch/9671/
State Accepted
Commit 85fb416f2fa7b1db5c63370441f1a49c43c57928
Headers show

Comments

Anton Shestakov - June 16, 2015, 4:27 p.m.
# HG changeset patch
# User Anton Shestakov <av6@dwimlabs.net>
# Date 1434391645 -28800
#      Tue Jun 16 02:07:25 2015 +0800
# Node ID d6c8ce47bc7a805c50d7d623cb03f67215aed5f7
# Parent  5aac89ac77042396c390cb1c5088e5989d1f5a9d
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.

Patch

diff --git a/hgext/highlight/__init__.py b/hgext/highlight/__init__.py
--- a/hgext/highlight/__init__.py
+++ b/hgext/highlight/__init__.py
@@ -30,7 +30,7 @@  from mercurial import extensions, encodi
 # 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 @@  def filerevision_highlight(orig, web, tm
     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))
diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -100,7 +100,7 @@  def rawfile(web, req, tmpl):
     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 @@  def _filerevision(web, tmpl, fctx):
                 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 @@  def file(web, req, tmpl):
     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 @@  def _search(web, req, tmpl):
     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 @@  def changelog(web, req, tmpl, shortlog=F
     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 @@  def changelog(web, req, tmpl, shortlog=F
         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 @@  def manifest(web, req, tmpl):
 
     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 @@  def manifest(web, req, tmpl):
 
     return tmpl("manifest",
                 rev=ctx.rev(),
+                symrev=symrev,
                 node=hex(node),
                 path=abspath,
                 up=webutil.up(abspath),
@@ -755,6 +764,7 @@  def summary(web, req, tmpl):
                 branches=branches,
                 shortlog=changelist,
                 node=tip.hex(),
+                symrev='tip',
                 archives=web.archivelist("tip"))
 
 @webcommand('filediff')
@@ -803,6 +813,7 @@  def filediff(web, req, tmpl):
                 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 @@  def comparison(web, req, tmpl):
                 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 @@  def annotate(web, req, tmpl):
                 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 @@  def filelog(web, req, tmpl):
     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 @@  def graph(web, req, tmpl):
     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 @@  def graph(web, req, tmpl):
     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,
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -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 @@  def changelistentry(web, ctx, tmpl):
         "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 @@  def changesetentry(web, req, tmpl, ctx):
         diff=diff,
         rev=ctx.rev(),
         node=ctx.hex(),
+        symrev=symrevorshortnode(req, ctx),
         parent=tuple(parents(ctx)),
         child=children(ctx),
         basenode=basectx.hex(),