Patchwork [3,of,3,V2] annotate: pack line content into annotateline object (API)

login
register
mail settings
Submitter Yuya Nishihara
Date March 18, 2018, 3:45 a.m.
Message ID <395f0824cb28c050ab65.1521344734@mimosa>
Download mbox | patch
Permalink /patch/29591/
State Accepted
Headers show

Comments

Yuya Nishihara - March 18, 2018, 3:45 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521343699 -32400
#      Sun Mar 18 12:28:19 2018 +0900
# Node ID 395f0824cb28c050ab650011c95a514c750f6400
# Parent  edba0c7df0a076dc383824f39c3ec16a0849b189
annotate: pack line content into annotateline object (API)

Just for code readability. We can do that since the annotateline type is
no longer used while computing the history.
Augie Fackler - March 22, 2018, 1:58 a.m.
> On Mar 17, 2018, at 23:45, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1521343699 -32400
> #      Sun Mar 18 12:28:19 2018 +0900
> # Node ID 395f0824cb28c050ab650011c95a514c750f6400
> # Parent  edba0c7df0a076dc383824f39c3ec16a0849b189
> annotate: pack line content into annotateline object (API)

queued these, thanks!

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -396,7 +396,7 @@  def annotate(ui, repo, *pats, **opts):
         pieces = []
 
         for f, sep in funcmap:
-            l = [f(n) for n, dummy in lines]
+            l = [f(n) for n in lines]
             if fm.isplain():
                 sizes = [encoding.colwidth(x) for x in l]
                 ml = max(sizes)
@@ -405,7 +405,7 @@  def annotate(ui, repo, *pats, **opts):
                 formats.append(['%s' for x in l])
             pieces.append(l)
 
-        for f, p, (n, l) in zip(zip(*formats), zip(*pieces), lines):
+        for f, p, n in zip(zip(*formats), zip(*pieces), lines):
             fm.startitem()
             fm.context(fctx=n.fctx)
             fm.write(fields, "".join(f), *p)
@@ -413,9 +413,9 @@  def annotate(ui, repo, *pats, **opts):
                 fmt = "* %s"
             else:
                 fmt = ": %s"
-            fm.write('line', fmt, l)
-
-        if not lines[-1][1].endswith('\n'):
+            fm.write('line', fmt, n.text)
+
+        if not lines[-1].text.endswith('\n'):
             fm.plain('\n')
         fm.end()
 
diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -968,11 +968,12 @@  class basefilectx(object):
         return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
 
     def annotate(self, follow=False, skiprevs=None, diffopts=None):
-        """Returns a list of tuples of (attr, line) for each line in the file
-
-        - attr.fctx is the filectx of the node where that line was last changed
-        - attr.lineno is the line number at the first appearance in the managed
+        """Returns a list of annotateline objects for each line in the file
+
+        - line.fctx is the filectx of the node where that line was last changed
+        - line.lineno is the line number at the first appearance in the managed
           file
+        - line.text is the data on that line (including newline character)
         """
         getlog = util.lrucachefunc(lambda x: self._repo.file(x))
 
diff --git a/mercurial/dagop.py b/mercurial/dagop.py
--- a/mercurial/dagop.py
+++ b/mercurial/dagop.py
@@ -368,6 +368,7 @@  class annotateline(object):
     lineno = attr.ib()
     # Whether this annotation was the result of a skip-annotate.
     skip = attr.ib(default=False)
+    text = attr.ib(default=None)
 
 @attr.s(slots=True, frozen=True)
 class _annotatedfile(object):
@@ -514,9 +515,8 @@  def annotate(base, parents, skiprevs=Non
             del pcache[f]
 
     a = hist[base]
-    return [(annotateline(fctx, lineno, skip), line)
-            for fctx, lineno, skip, line
-            in zip(a.fctxs, a.linenos, a.skips, mdiff.splitnewlines(a.text))]
+    return [annotateline(*r) for r in zip(a.fctxs, a.linenos, a.skips,
+                                          mdiff.splitnewlines(a.text))]
 
 def toposort(revs, parentsfunc, firstbranch=()):
     """Yield revisions from heads to roots one (topo) branch at a time.
diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -946,13 +946,14 @@  def annotate(web):
         if fctx.isbinary():
             mt = (mimetypes.guess_type(fctx.path())[0]
                   or 'application/octet-stream')
-            lines = [((fctx.filectx(fctx.filerev()), 1), '(binary:%s)' % mt)]
+            lines = [dagop.annotateline(fctx=fctx.filectx(fctx.filerev()),
+                                        lineno=1, text='(binary:%s)' % mt)]
         else:
             lines = webutil.annotate(web.req, fctx, web.repo.ui)
 
         previousrev = None
         blockparitygen = paritygen(1)
-        for lineno, (aline, l) in enumerate(lines):
+        for lineno, aline in enumerate(lines):
             f = aline.fctx
             rev = f.rev()
             if rev != previousrev:
@@ -972,7 +973,7 @@  def annotate(web):
                    "blockhead": blockhead,
                    "blockparity": blockparity,
                    "targetline": aline.lineno,
-                   "line": l,
+                   "line": aline.text,
                    "lineno": lineno + 1,
                    "lineid": "l%d" % (lineno + 1),
                    "linenumber": "% 6d" % (lineno + 1),