# HG changeset patch # User Yuya Nishihara # Date 2017-06-02 15:25:24 # Node ID 7a209737f01ce0d153244fd36bb417edfd8a3039 # Parent d3d262ff42c0e003fe110892f8ababab8ee1ed6a 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. 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 ..