Patchwork [1,of,4] hgweb: expose raw line numbers to templates

login
register
mail settings
Submitter Gregory Szorc
Date April 11, 2015, 2:40 a.m.
Message ID <e1ce2479222ac3727bcc.1428720040@gps-mbp.local>
Download mbox | patch
Permalink /patch/8603/
State Accepted
Commit 4a7ca84241e94caecbaa36cc6e5aacc72b8fdc9a
Headers show

Comments

Gregory Szorc - April 11, 2015, 2:40 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1428719652 14400
#      Fri Apr 10 22:34:12 2015 -0400
# Node ID e1ce2479222ac3727bcc5f565bfe414ccac9bf59
# Parent  52ff737c63d2b2cb41185549aa9c35bc47317032
hgweb: expose raw line numbers to templates

Surpringly, the templates didn't receive an unmodified version of the
line numbers. Expose it to make implementing the JSON templates easier.

In theory, we could post-process an existing template variable. But
extra string manipulation seems quite wasteful, especially on items that
could occur hundreds or even thousands of times in output.

Patch

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -926,8 +926,9 @@  def annotate(web, req, tmpl):
                    "extra": f.extra(),
                    "file": f.path(),
                    "targetline": targetline,
                    "line": l,
+                   "lineno": lineno + 1,
                    "lineid": "l%d" % (lineno + 1),
                    "linenumber": "% 6d" % (lineno + 1),
                    "revdate": f.date()}
 
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -351,9 +351,9 @@  def diffs(repo, tmpl, ctx, basectx, file
 
     blockcount = countgen()
     def prettyprintlines(diff, blockno):
         for lineno, l in enumerate(diff.splitlines(True)):
-            lineno = "%d.%d" % (blockno, lineno + 1)
+            difflineno = "%d.%d" % (blockno, lineno + 1)
             if l.startswith('+'):
                 ltype = "difflineplus"
             elif l.startswith('-'):
                 ltype = "difflineminus"
@@ -362,10 +362,11 @@  def diffs(repo, tmpl, ctx, basectx, file
             else:
                 ltype = "diffline"
             yield tmpl(ltype,
                        line=l,
-                       lineid="l%s" % lineno,
-                       linenumber="% 8s" % lineno)
+                       lineno=lineno + 1,
+                       lineid="l%s" % difflineno,
+                       linenumber="% 8s" % difflineno)
 
     if files:
         m = match.exact(repo.root, repo.getcwd(), files)
     else:
@@ -404,10 +405,12 @@  def compare(tmpl, context, leftlines, ri
         lineid += rightlineno and ("r%s" % rightlineno) or ''
         return tmpl('comparisonline',
                     type=type,
                     lineid=lineid,
+                    leftlineno=leftlineno,
                     leftlinenumber="% 6s" % (leftlineno or ''),
                     leftline=leftline or '',
+                    rightlineno=rightlineno,
                     rightlinenumber="% 6s" % (rightlineno or ''),
                     rightline=rightline or '')
 
     def getblock(opcodes):