From patchwork Wed Feb 22 15:10:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [08, of, 11] hgweb: handle "linerange" request parameter in filediff command From: Denis Laxalde X-Patchwork-Id: 18715 Message-Id: <39e3bd58cb1af11ba2fd.1487776202@sh77.tls.logilab.fr> To: mercurial-devel@mercurial-scm.org Date: Wed, 22 Feb 2017 16:10:02 +0100 # HG changeset patch # User Denis Laxalde # Date 1484922599 -3600 # Fri Jan 20 15:29:59 2017 +0100 # Node ID 39e3bd58cb1af11ba2fd0a1a74bb134711dfe718 # Parent b3f8ef870003ee2ca6b6db70860a065f255902b7 # EXP-Topic linerange-log/hgweb-filelog hgweb: handle "linerange" request parameter in filediff command If a linerange=- query parameter is present on a request to filediff view we use this information to filter the diff to only display the specified range of lines. This is achieved by getting the first "block ancestor" of filectx and using it as the "basectx" in comparison along with the pair of line ranges of both file contexts to generated a filtered diff. Extra tests added in test-hgweb-diffs.t covering changes on hgweb side as well as those in patch/mdiff of previous changesets. Update only gitweb template in this version. diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py --- a/mercurial/hgweb/webcommands.py +++ b/mercurial/hgweb/webcommands.py @@ -728,6 +728,9 @@ def summary(web, req, tmpl): archives=web.archivelist("tip"), labels=web.configlist('web', 'labels')) +def formatlinerange(fromline, toline): + return '%d-%d' % (fromline + 1, toline) + @webcommand('filediff') def filediff(web, req, tmpl): """ @@ -760,7 +763,24 @@ def filediff(web, req, tmpl): if 'style' in req.form: style = req.form['style'][0] - diffs = webutil.diffs(web.repo, tmpl, ctx, basectx, [path], parity, style) + lineranges = {} + linerange = webutil.linerange(req) + if linerange is not None: + assert fctx is not None + lineranges = {fctx: linerange} + ancestors = context.blockancestors(fctx, *linerange, followfirst=True) + try: + next(ancestors) # first iteration returns fctx + p, plinerange = next(ancestors) + except StopIteration: + pass + else: + lineranges[p] = plinerange + basectx = p.changectx() + linerange = formatlinerange(*linerange) + + diffs = webutil.diffs(web.repo, tmpl, ctx, basectx, [path], parity, style, + lineranges) if fctx is not None: rename = webutil.renamelink(fctx) ctx = fctx @@ -772,6 +792,7 @@ def filediff(web, req, tmpl): symrev=webutil.symrevorshortnode(req, ctx), rename=rename, diff=diffs, + linerange=linerange, **webutil.commonentry(web.repo, ctx)) diff = webcommand('diff')(filediff) @@ -970,9 +991,6 @@ def filelog(web, req, tmpl): lrange = webutil.linerange(req) - def formatlinerange(fromline, toline): - return '%d-%d' % (fromline + 1, toline) - lessvars = copy.copy(tmpl.defaults['sessionvars']) lessvars['revcount'] = max(revcount / 2, 1) morevars = copy.copy(tmpl.defaults['sessionvars']) diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py --- a/mercurial/hgweb/webutil.py +++ b/mercurial/hgweb/webutil.py @@ -431,7 +431,7 @@ def listfilediffs(tmpl, files, node, max if len(files) > max: yield tmpl('fileellipses') -def diffs(repo, tmpl, ctx, basectx, files, parity, style): +def diffs(repo, tmpl, ctx, basectx, files, parity, style, lineranges=None): def countgen(): start = 1 @@ -467,7 +467,8 @@ def diffs(repo, tmpl, ctx, basectx, file node2 = ctx.node() block = [] - for chunk in patch.diff(repo, node1, node2, m, opts=diffopts): + for chunk in patch.diff(repo, node1, node2, m, opts=diffopts, + lineranges=lineranges): if chunk.startswith('diff') and block: blockno = next(blockcount) yield tmpl('diffblock', parity=next(parity), blockno=blockno, diff --git a/mercurial/templates/gitweb/filediff.tmpl b/mercurial/templates/gitweb/filediff.tmpl --- a/mercurial/templates/gitweb/filediff.tmpl +++ b/mercurial/templates/gitweb/filediff.tmpl @@ -33,7 +33,9 @@ diff |
-
{file|escape}
+
\ +{file|escape}{if(linerange, ' (line range: {linerange} full diff)')}\ +
{branch%filerevbranch} diff --git a/tests/test-hgweb-diffs.t b/tests/test-hgweb-diffs.t --- a/tests/test-hgweb-diffs.t +++ b/tests/test-hgweb-diffs.t @@ -1094,6 +1094,305 @@ comparison not-modified file + $ cat < c + > 1 + > 2 + > 3 + > 4 + > 5 + > 6 + > EOF + $ hg ci -Amc c + $ cat < c + > 1 + > 2+ + > 3 + > 4 + > 5- + > 6 + > EOF + $ hg ci -m 'update c' c + $ get-with-headers.py localhost:$HGPORT 'diff/tip/c?style=gitweb' + 200 Script output follows + + + + + + + + + + + test: diff c + + + + + + + + + +
c
+ +
+ + + + + + + + + + + +
changeset 746dadddbce95
parent 6 + + 3deb5080745a + +
+ +
+ +
+
+  --- a/c
+  +++ b/c
+  @@ -1,6 +1,6 @@
+   1
+  -2
+  +2+
+   3
+   4
+  -5
+  +5-
+   6
+
+ + + + + + $ get-with-headers.py localhost:$HGPORT 'diff/tip/c?style=gitweb&linerange=1-3' + 200 Script output follows + + + + + + + + + + + test: diff c + + + + + + + + + +
c (line range: 1-3 full diff)
+ + + + + + + + + + + + + +
changeset 746dadddbce95
parent 6 + + 3deb5080745a + +
+ +
+ +
+
+  --- a/c
+  +++ b/c
+  @@ -1,5 +1,5 @@
+   1
+  -2
+  +2+
+   3
+   4
+   5
+
+ + + + + + $ get-with-headers.py localhost:$HGPORT 'diff/tip/c?style=gitweb&linerange=3-6' + 200 Script output follows + + + + + + + + + + + test: diff c + + + + + + + + + +
c (line range: 3-6 full diff)
+ + + + + + + + + + + + + +
changeset 746dadddbce95
parent 6 + + 3deb5080745a + +
+ +
+ +
+
+  --- a/c
+  +++ b/c
+  @@ -2,5 +2,5 @@
+   2
+   3
+   4
+  -5
+  +5-
+   6
+
+ + + + + $ cd .. test import rev as raw-rev