Patchwork [2,of,2] annotate: restructure formatter output to be nested list (BC)

login
register
mail settings
Submitter Yuya Nishihara
Date June 2, 2017, 4:02 p.m.
Message ID <b6eb65cbc42b8c3b3a6e.1496419347@mimosa>
Download mbox | patch
Permalink /patch/21138/
State Accepted
Headers show

Comments

Yuya Nishihara - June 2, 2017, 4:02 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1496417124 -32400
#      Sat Jun 03 00:25:24 2017 +0900
# Node ID b6eb65cbc42b8c3b3a6e4a21827732fc8b56f1cf
# Parent  52237356336d4141f9f1a31995276703cbb436b7
annotate: restructure formatter output to be nested list (BC)

Annotate data should be in [(file, [line...])...] form, but there was no
API to represent such data structure when I ported it to formatter. Now
we have fm.nested() and the -T option is still experimental, so we can fix
the data format.
Augie Fackler - June 3, 2017, 5 a.m.
On Sat, Jun 03, 2017 at 01:02:27AM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1496417124 -32400
> #      Sat Jun 03 00:25:24 2017 +0900
> # Node ID b6eb65cbc42b8c3b3a6e4a21827732fc8b56f1cf
> # Parent  52237356336d4141f9f1a31995276703cbb436b7
> annotate: restructure formatter output to be nested list (BC)

queued, thanks

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -377,15 +377,18 @@  def annotate(ui, repo, *pats, **opts):
 
     for abs in ctx.walk(m):
         fctx = ctx[abs]
+        rootfm.startitem()
+        rootfm.data(abspath=abs, path=m.rel(abs))
         if not opts.get('text') and fctx.isbinary():
             rootfm.plain(_("%s: binary file\n")
                          % ((pats and m.rel(abs)) or abs))
             continue
 
-        fm = rootfm
+        fm = rootfm.nested('lines')
         lines = fctx.annotate(follow=follow, linenumber=linenumber,
                               skiprevs=skiprevs, diffopts=diffopts)
         if not lines:
+            fm.end()
             continue
         formats = []
         pieces = []
@@ -407,6 +410,7 @@  def annotate(ui, repo, *pats, **opts):
 
         if not lines[-1][1].endswith('\n'):
             fm.plain('\n')
+        fm.end()
 
     rootfm.end()
 
diff --git a/tests/test-annotate.t b/tests/test-annotate.t
--- a/tests/test-annotate.t
+++ b/tests/test-annotate.t
@@ -56,21 +56,18 @@  annotate (JSON)
   $ hg annotate -Tjson a
   [
    {
-    "line": "a\n",
-    "rev": 0
+    "abspath": "a",
+    "lines": [{"line": "a\n", "rev": 0}],
+    "path": "a"
    }
   ]
 
   $ hg annotate -Tjson -cdfnul a
   [
    {
-    "date": [1.0, 0],
-    "file": "a",
-    "line": "a\n",
-    "line_number": 1,
-    "node": "8435f90966e442695d2ded29fdade2bac5ad8065",
-    "rev": 0,
-    "user": "nobody"
+    "abspath": "a",
+    "lines": [{"date": [1.0, 0], "file": "a", "line": "a\n", "line_number": 1, "node": "8435f90966e442695d2ded29fdade2bac5ad8065", "rev": 0, "user": "nobody"}],
+    "path": "a"
    }
   ]
 
@@ -88,6 +85,37 @@  annotate (JSON)
   > EOF
   $ hg ci -mb2 -d '2 0'
 
+annotate multiple files (JSON)
+
+  $ hg annotate -Tjson a b
+  [
+   {
+    "abspath": "a",
+    "lines": [{"line": "a\n", "rev": 0}, {"line": "a\n", "rev": 1}, {"line": "a\n", "rev": 1}],
+    "path": "a"
+   },
+   {
+    "abspath": "b",
+    "lines": [{"line": "a\n", "rev": 0}, {"line": "a\n", "rev": 1}, {"line": "a\n", "rev": 1}, {"line": "b4\n", "rev": 3}, {"line": "b5\n", "rev": 3}, {"line": "b6\n", "rev": 3}],
+    "path": "b"
+   }
+  ]
+
+annotate multiple files (template)
+
+  $ hg annotate -T'== {abspath} ==\n{lines % "{rev}: {line}"}' a b
+  == a ==
+  0: a
+  1: a
+  1: a
+  == b ==
+  0: a
+  1: a
+  1: a
+  3: b4
+  3: b5
+  3: b6
+
 annotate -n b
 
   $ hg annotate -n b
@@ -500,14 +528,9 @@  annotate modified file
   $ hg annotate -ncr "wdir()" -Tjson foo
   [
    {
-    "line": "foo\n",
-    "node": "472b18db256d1e8282064eab4bfdaf48cbfe83cd",
-    "rev": 11
-   },
-   {
-    "line": "foofoo\n",
-    "node": null,
-    "rev": null
+    "abspath": "foo",
+    "lines": [{"line": "foo\n", "node": "472b18db256d1e8282064eab4bfdaf48cbfe83cd", "rev": 11}, {"line": "foofoo\n", "node": null, "rev": null}],
+    "path": "foo"
    }
   ]
 
@@ -742,6 +765,28 @@  check error cases
   hg: parse error: descend argument must be a boolean
   [255]
 
+Test empty annotate output
+
+  $ printf '\0' > binary
+  $ touch empty
+  $ hg ci -qAm 'add binary and empty files'
+
+  $ hg annotate binary empty
+  binary: binary file
+
+  $ hg annotate -Tjson binary empty
+  [
+   {
+    "abspath": "binary",
+    "path": "binary"
+   },
+   {
+    "abspath": "empty",
+    "lines": [],
+    "path": "empty"
+   }
+  ]
+
 Test annotate with whitespace options
 
   $ cd ..