[PATCH] hgweb: add block numbers to diff regions and related links

Paul Boddie paul at boddie.org.uk
Thu Mar 22 19:33:09 CDT 2012


# HG changeset patch
# User Paul Boddie <paul at boddie.org.uk>
# Date 1332462691 -3600
# Node ID 9261ec526f8d3f4654cffe5bb29ac4cb559d7179
# Parent  12e3f93b1cbc7c930e941f0adfe632c8ad70b73d
hgweb: add block numbers to diff regions and related links

The changeset view may show several diff regions, one per file, and this patch
numbers each of them so that links produced by the filenodelink fragment can
reference each diff region produced by the diffblock fragment through the use
of the blockno variable made available to both of them. This permits
navigation to diff regions on the changeset page from the file list, and
where the :target pseudo-class is supported in browsers, permits selective
presentation of diffs, showing one at a time instead of potentially many in
what would otherwise be a very long page that is difficult to navigate.

diff -r 12e3f93b1cbc -r 9261ec526f8d mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py	Thu Mar 15 16:57:19 2012 +0100
+++ b/mercurial/hgweb/webcommands.py	Fri Mar 23 01:31:31 2012 +0100
@@ -262,10 +262,10 @@
 
     files = []
     parity = paritygen(web.stripecount)
-    for f in ctx.files():
+    for blockno, f in enumerate(ctx.files()):
         template = f in ctx and 'filenodelink' or 'filenolink'
         files.append(tmpl(template,
-                          node=ctx.hex(), file=f,
+                          node=ctx.hex(), file=f, blockno=blockno+1,
                           parity=parity.next()))
 
     style = web.config('web', 'style', 'paper')
diff -r 12e3f93b1cbc -r 9261ec526f8d mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py	Thu Mar 15 16:57:19 2012 +0100
+++ b/mercurial/hgweb/webutil.py	Fri Mar 23 01:31:31 2012 +0100
@@ -173,8 +173,7 @@
             start += 1
 
     blockcount = countgen()
-    def prettyprintlines(diff):
-        blockno = blockcount.next()
+    def prettyprintlines(diff, blockno):
         for lineno, l in enumerate(diff.splitlines(True)):
             lineno = "%d.%d" % (blockno, lineno + 1)
             if l.startswith('+'):
@@ -203,14 +202,16 @@
     block = []
     for chunk in patch.diff(repo, node1, node2, m, opts=diffopts):
         if chunk.startswith('diff') and block:
-            yield tmpl('diffblock', parity=parity.next(),
-                       lines=prettyprintlines(''.join(block)))
+            blockno = blockcount.next()
+            yield tmpl('diffblock', parity=parity.next(), blockno=blockno,
+                       lines=prettyprintlines(''.join(block), blockno))
             block = []
         if chunk.startswith('diff') and style != 'raw':
             chunk = ''.join(chunk.splitlines(True)[1:])
         block.append(chunk)
-    yield tmpl('diffblock', parity=parity.next(),
-               lines=prettyprintlines(''.join(block)))
+    blockno = blockcount.next()
+    yield tmpl('diffblock', parity=parity.next(), blockno=blockno,
+               lines=prettyprintlines(''.join(block), blockno))
 
 def diffstatgen(ctx):
     '''Generator function that provides the diffstat data.'''
diff -r 12e3f93b1cbc -r 9261ec526f8d tests/test-hgweb-diffs.t
--- a/tests/test-hgweb-diffs.t	Thu Mar 15 16:57:19 2012 +0100
+++ b/tests/test-hgweb-diffs.t	Fri Mar 23 01:31:31 2012 +0100
@@ -552,6 +552,35 @@
   $ cd test1
   $ hg import -q --exact http://localhost:$HGPORT/rev/1
 
+raw revision with diff block numbers
+
+  $ "$TESTDIR/killdaemons.py"
+  $ cat <<EOF > .hg/hgrc
+  > [web]
+  > templates = rawdiff
+  > EOF
+  $ mkdir rawdiff
+  $ cat <<EOF > rawdiff/map
+  > mimetype = 'text/plain; charset={encoding}'
+  > changeset = '{diff}'
+  > difflineplus = '{line}'
+  > difflineminus = '{line}'
+  > difflineat = '{line}'
+  > diffline = '{line}'
+  > filenodelink = ''
+  > filenolink = ''
+  > fileline = '{line}'
+  > diffblock = 'Block: {blockno}\n{lines}\n'
+  > EOF
+  $ hg serve -n test -p $HGPORT -d --pid-file=hg.pid -A access.log -E errors.log
+  $ cat hg.pid >> $DAEMON_PIDS
+  $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/raw-rev/0'
+  $ "$TESTDIR/killdaemons.py"
+  $ rm .hg/hgrc rawdiff/map
+  $ rmdir rawdiff
+  $ hg serve -n test -p $HGPORT -d --pid-file=hg.pid -A access.log -E errors.log
+  $ cat hg.pid >> $DAEMON_PIDS
+
 errors
 
   $ cat ../test/errors.log


More information about the Mercurial-devel mailing list