hgweb: move the diffs() generator into webutil
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Mon, 03 Nov 2008 20:41:48 +0100
changeset 7310 bd522d09d5e3
parent 7309 e74a9173c2d7
child 7311 de9c87fe1620
hgweb: move the diffs() generator into webutil
mercurial/hgweb/common.py
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/webcommands.py
mercurial/hgweb/webutil.py
tests/test-hgweb-commands.out
tests/test-hgweb-diffs.out
tests/test-hgweb-removed.out
--- a/mercurial/hgweb/common.py	Mon Nov 03 20:05:03 2008 +0100
+++ b/mercurial/hgweb/common.py	Mon Nov 03 20:41:48 2008 +0100
@@ -112,12 +112,6 @@
             parity = 1 - parity
             count = 0
 
-def countgen(start=0, step=1):
-    """count forever -- useful for line numbers"""
-    while True:
-        yield start
-        start += step
-
 def get_contact(config):
     """Return repo contact information or empty string.
 
--- a/mercurial/hgweb/hgweb_mod.py	Mon Nov 03 20:05:03 2008 +0100
+++ b/mercurial/hgweb/hgweb_mod.py	Mon Nov 03 20:41:48 2008 +0100
@@ -9,9 +9,9 @@
 import os, mimetypes
 from mercurial.node import hex, nullid
 from mercurial.repo import RepoError
-from mercurial import ui, hg, util, patch, hook, match
+from mercurial import ui, hg, util, hook
 from mercurial import revlog, templater, templatefilters
-from common import get_mtime, style_map, paritygen, countgen, ErrorResponse
+from common import get_mtime, style_map, ErrorResponse
 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
 from common import HTTP_UNAUTHORIZED, HTTP_METHOD_NOT_ALLOWED
 from request import wsgirequest
@@ -276,48 +276,6 @@
         if len(files) > self.maxfiles:
             yield tmpl("fileellipses")
 
-    def diff(self, tmpl, node1, node2, files):
-
-        blockcount = countgen()
-        def prettyprintlines(diff):
-            blockno = blockcount.next()
-            for lineno, l in enumerate(diff.splitlines(True)):
-                if blockno == 0:
-                    lineno = lineno + 1
-                else:
-                    lineno = "%d.%d" % (blockno, lineno + 1)
-                if l.startswith('+'):
-                    ltype = "difflineplus"
-                elif l.startswith('-'):
-                    ltype = "difflineminus"
-                elif l.startswith('@'):
-                    ltype = "difflineat"
-                else:
-                    ltype = "diffline"
-                yield tmpl(ltype,
-                           line=l,
-                           lineid="l%s" % lineno,
-                           linenumber="% 8s" % lineno)
-
-        if files:
-            m = match.exact(self.repo.root, self.repo.getcwd(), files)
-        else:
-            m = match.always(self.repo.root, self.repo.getcwd())
-
-        block = []
-        parity = paritygen(self.stripecount)
-        diffopts = patch.diffopts(self.repo.ui, untrusted=True)
-        for chunk in patch.diff(self.repo, node1, node2, m, opts=diffopts):
-            if chunk.startswith('diff') and block:
-                yield tmpl('diffblock', parity=parity.next(),
-                           lines=prettyprintlines(''.join(block)))
-                block = []
-            if chunk.startswith('diff'):
-                chunk = ''.join(chunk.splitlines(True)[1:])
-            block.append(chunk)
-        yield tmpl('diffblock', parity=parity.next(),
-                   lines=prettyprintlines(''.join(block)))
-
     archive_specs = {
         'bz2': ('application/x-tar', 'tbz2', '.tar.bz2', None),
         'gz': ('application/x-tar', 'tgz', '.tar.gz', None),
--- a/mercurial/hgweb/webcommands.py	Mon Nov 03 20:05:03 2008 +0100
+++ b/mercurial/hgweb/webcommands.py	Mon Nov 03 20:41:48 2008 +0100
@@ -224,24 +224,23 @@
 
 def changeset(web, req, tmpl):
     ctx = webutil.changectx(web.repo, req)
-    n = ctx.node()
-    showtags = webutil.showtag(web.repo, tmpl, 'changesettag', n)
+    showtags = webutil.showtag(web.repo, tmpl, 'changesettag', ctx.node())
     parents = ctx.parents()
-    p1 = parents[0].node()
 
     files = []
     parity = paritygen(web.stripecount)
     for f in ctx.files():
         template = f in ctx and 'filenodelink' or 'filenolink'
         files.append(tmpl(template,
-                          node=hex(n), file=f,
+                          node=ctx.hex(), file=f,
                           parity=parity.next()))
 
-    diffs = web.diff(tmpl, p1, n, None)
+    parity = paritygen(web.stripecount)
+    diffs = webutil.diffs(web.repo, tmpl, ctx, None, parity)
     return tmpl('changeset',
                 diff=diffs,
                 rev=ctx.rev(),
-                node=hex(n),
+                node=ctx.hex(),
                 parent=webutil.siblings(parents),
                 child=webutil.siblings(ctx.children()),
                 changesettag=showtags,
@@ -249,8 +248,8 @@
                 desc=ctx.description(),
                 date=ctx.date(),
                 files=files,
-                archives=web.archivelist(hex(n)),
-                tags=webutil.nodetagsdict(web.repo, n),
+                archives=web.archivelist(ctx.hex()),
+                tags=webutil.nodetagsdict(web.repo, ctx.node()),
                 branch=webutil.nodebranchnodefault(ctx),
                 inbranch=webutil.nodeinbranch(web.repo, ctx),
                 branches=webutil.nodebranchdict(web.repo, ctx))
@@ -446,9 +445,9 @@
         n = ctx.node()
         # path already defined in except clause
         parents = ctx.parents()
-        p1 = parents and parents[0].node() or nullid
 
-    diffs = web.diff(tmpl, p1, n, [path])
+    parity = paritygen(web.stripecount)
+    diffs = webutil.diffs(web.repo, tmpl, fctx or ctx, [path], parity)
     rename = fctx and webutil.renamelink(fctx) or []
     ctx = fctx and fctx or ctx
     return tmpl("filediff",
--- a/mercurial/hgweb/webutil.py	Mon Nov 03 20:05:03 2008 +0100
+++ b/mercurial/hgweb/webutil.py	Mon Nov 03 20:41:48 2008 +0100
@@ -7,6 +7,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 import os
+from mercurial import match, patch
 from mercurial.node import hex, nullid
 from mercurial.repo import RepoError
 from mercurial import util
@@ -141,3 +142,51 @@
         fctx = repo.filectx(path, fileid=changeid)
 
     return fctx
+
+def diffs(repo, tmpl, ctx, files, parity):
+
+    def countgen():
+        start = 1
+        while True:
+            yield start
+            start += 1
+
+    blockcount = countgen()
+    def prettyprintlines(diff):
+        blockno = blockcount.next()
+        for lineno, l in enumerate(diff.splitlines(True)):
+            lineno = "%d.%d" % (blockno, lineno + 1)
+            if l.startswith('+'):
+                ltype = "difflineplus"
+            elif l.startswith('-'):
+                ltype = "difflineminus"
+            elif l.startswith('@'):
+                ltype = "difflineat"
+            else:
+                ltype = "diffline"
+            yield tmpl(ltype,
+                       line=l,
+                       lineid="l%s" % lineno,
+                       linenumber="% 8s" % lineno)
+
+    if files:
+        m = match.exact(repo.root, repo.getcwd(), files)
+    else:
+        m = match.always(repo.root, repo.getcwd())
+
+    diffopts = patch.diffopts(repo.ui, untrusted=True)
+    parents = ctx.parents()
+    node1 = parents and parents[0].node() or nullid
+    node2 = ctx.node()
+
+    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)))
+            block = []
+        if chunk.startswith('diff'):
+            chunk = ''.join(chunk.splitlines(True)[1:])
+        block.append(chunk)
+    yield tmpl('diffblock', parity=parity.next(),
+               lines=prettyprintlines(''.join(block)))
Binary file tests/test-hgweb-commands.out has changed
--- a/tests/test-hgweb-diffs.out	Mon Nov 03 20:05:03 2008 +0100
+++ b/tests/test-hgweb-diffs.out	Mon Nov 03 20:41:48 2008 +0100
@@ -56,14 +56,14 @@
 </table>
 
 <div id="changesetDiff">
-<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1" id="l1">       1</a>--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="plusline"><a class="lineno" href="#l2" id="l2">       2</a>+++ b/a	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="atline"><a class="lineno" href="#l3" id="l3">       3</a>@@ -0,0 +1,1 @@
-</span><span class="plusline"><a class="lineno" href="#l4" id="l4">       4</a>+a
-</span></pre><pre class="parity1"><span class="minusline"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="plusline"><a class="lineno" href="#l1.2" id="l1.2">     1.2</a>+++ b/b	Thu Jan 01 00:00:00 1970 +0000
+<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="plusline"><a class="lineno" href="#l1.2" id="l1.2">     1.2</a>+++ b/a	Thu Jan 01 00:00:00 1970 +0000
 </span><span class="atline"><a class="lineno" href="#l1.3" id="l1.3">     1.3</a>@@ -0,0 +1,1 @@
-</span><span class="plusline"><a class="lineno" href="#l1.4" id="l1.4">     1.4</a>+b
+</span><span class="plusline"><a class="lineno" href="#l1.4" id="l1.4">     1.4</a>+a
+</span></pre><pre class="parity1"><span class="minusline"><a class="lineno" href="#l2.1" id="l2.1">     2.1</a>--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="plusline"><a class="lineno" href="#l2.2" id="l2.2">     2.2</a>+++ b/b	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="atline"><a class="lineno" href="#l2.3" id="l2.3">     2.3</a>@@ -0,0 +1,1 @@
+</span><span class="plusline"><a class="lineno" href="#l2.4" id="l2.4">     2.4</a>+b
 </span></pre>
 </div>
 
@@ -116,10 +116,10 @@
 </table>
 
 <div id="fileDiff">
-<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1" id="l1">       1</a>--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="plusline"><a class="lineno" href="#l2" id="l2">       2</a>+++ b/a	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="atline"><a class="lineno" href="#l3" id="l3">       3</a>@@ -0,0 +1,1 @@
-</span><span class="plusline"><a class="lineno" href="#l4" id="l4">       4</a>+a
+<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="plusline"><a class="lineno" href="#l1.2" id="l1.2">     1.2</a>+++ b/a	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="atline"><a class="lineno" href="#l1.3" id="l1.3">     1.3</a>@@ -0,0 +1,1 @@
+</span><span class="plusline"><a class="lineno" href="#l1.4" id="l1.4">     1.4</a>+a
 </span></pre>
 </div>
 
@@ -188,16 +188,16 @@
 </table>
 
 <div id="changesetDiff">
-<pre class="parity0"><a class="lineno" href="#l1" id="l1">       1</a>new file mode 100644
-<span class="minusline"><a class="lineno" href="#l2" id="l2">       2</a>--- /dev/null
-</span><span class="plusline"><a class="lineno" href="#l3" id="l3">       3</a>+++ b/a
-</span><span class="atline"><a class="lineno" href="#l4" id="l4">       4</a>@@ -0,0 +1,1 @@
-</span><span class="plusline"><a class="lineno" href="#l5" id="l5">       5</a>+a
-</span></pre><pre class="parity1"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>new file mode 100644
+<pre class="parity0"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>new file mode 100644
 <span class="minusline"><a class="lineno" href="#l1.2" id="l1.2">     1.2</a>--- /dev/null
-</span><span class="plusline"><a class="lineno" href="#l1.3" id="l1.3">     1.3</a>+++ b/b
+</span><span class="plusline"><a class="lineno" href="#l1.3" id="l1.3">     1.3</a>+++ b/a
 </span><span class="atline"><a class="lineno" href="#l1.4" id="l1.4">     1.4</a>@@ -0,0 +1,1 @@
-</span><span class="plusline"><a class="lineno" href="#l1.5" id="l1.5">     1.5</a>+b
+</span><span class="plusline"><a class="lineno" href="#l1.5" id="l1.5">     1.5</a>+a
+</span></pre><pre class="parity1"><a class="lineno" href="#l2.1" id="l2.1">     2.1</a>new file mode 100644
+<span class="minusline"><a class="lineno" href="#l2.2" id="l2.2">     2.2</a>--- /dev/null
+</span><span class="plusline"><a class="lineno" href="#l2.3" id="l2.3">     2.3</a>+++ b/b
+</span><span class="atline"><a class="lineno" href="#l2.4" id="l2.4">     2.4</a>@@ -0,0 +1,1 @@
+</span><span class="plusline"><a class="lineno" href="#l2.5" id="l2.5">     2.5</a>+b
 </span></pre>
 </div>
 
@@ -250,11 +250,11 @@
 </table>
 
 <div id="fileDiff">
-<pre class="parity0"><a class="lineno" href="#l1" id="l1">       1</a>new file mode 100755
-<span class="minusline"><a class="lineno" href="#l2" id="l2">       2</a>--- /dev/null
-</span><span class="plusline"><a class="lineno" href="#l3" id="l3">       3</a>+++ b/a
-</span><span class="atline"><a class="lineno" href="#l4" id="l4">       4</a>@@ -0,0 +1,1 @@
-</span><span class="plusline"><a class="lineno" href="#l5" id="l5">       5</a>+a
+<pre class="parity0"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>new file mode 100755
+<span class="minusline"><a class="lineno" href="#l1.2" id="l1.2">     1.2</a>--- /dev/null
+</span><span class="plusline"><a class="lineno" href="#l1.3" id="l1.3">     1.3</a>+++ b/a
+</span><span class="atline"><a class="lineno" href="#l1.4" id="l1.4">     1.4</a>@@ -0,0 +1,1 @@
+</span><span class="plusline"><a class="lineno" href="#l1.5" id="l1.5">     1.5</a>+a
 </span></pre>
 </div>
 
--- a/tests/test-hgweb-removed.out	Mon Nov 03 20:05:03 2008 +0100
+++ b/tests/test-hgweb-removed.out	Mon Nov 03 20:41:48 2008 +0100
@@ -54,10 +54,10 @@
 </table>
 
 <div id="changesetDiff">
-<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1" id="l1">       1</a>--- a/a	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="plusline"><a class="lineno" href="#l2" id="l2">       2</a>+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="atline"><a class="lineno" href="#l3" id="l3">       3</a>@@ -1,1 +0,0 @@
-</span><span class="minusline"><a class="lineno" href="#l4" id="l4">       4</a>-a
+<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>--- a/a	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="plusline"><a class="lineno" href="#l1.2" id="l1.2">     1.2</a>+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="atline"><a class="lineno" href="#l1.3" id="l1.3">     1.3</a>@@ -1,1 +0,0 @@
+</span><span class="minusline"><a class="lineno" href="#l1.4" id="l1.4">     1.4</a>-a
 </span></pre>
 </div>
 
@@ -110,10 +110,10 @@
 </table>
 
 <div id="fileDiff">
-<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1" id="l1">       1</a>--- a/a	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="plusline"><a class="lineno" href="#l2" id="l2">       2</a>+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
-</span><span class="atline"><a class="lineno" href="#l3" id="l3">       3</a>@@ -1,1 +0,0 @@
-</span><span class="minusline"><a class="lineno" href="#l4" id="l4">       4</a>-a
+<pre class="parity0"><span class="minusline"><a class="lineno" href="#l1.1" id="l1.1">     1.1</a>--- a/a	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="plusline"><a class="lineno" href="#l1.2" id="l1.2">     1.2</a>+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
+</span><span class="atline"><a class="lineno" href="#l1.3" id="l1.3">     1.3</a>@@ -1,1 +0,0 @@
+</span><span class="minusline"><a class="lineno" href="#l1.4" id="l1.4">     1.4</a>-a
 </span></pre>
 </div>