changeset 43102:829088e87032

log: populate keywords if specified in custom -Tjson(...) or -Tcbor(...) To make things simple, early return for ui.quiet is disabled if the formatter is templated and provides some datahint().
author Yuya Nishihara <yuya@tcha.org>
date Sun, 06 Oct 2019 14:58:41 -0400
parents 1d12ae5096d1
children c95b2f40db7c
files mercurial/logcmdutil.py tests/test-template-map.t
diffstat 2 files changed, 74 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/logcmdutil.py	Sat Oct 05 23:30:09 2019 -0400
+++ b/mercurial/logcmdutil.py	Sun Oct 06 14:58:41 2019 -0400
@@ -440,7 +440,8 @@
         fm.context(ctx=ctx)
         fm.data(rev=scmutil.intrev(ctx), node=fm.hexfunc(scmutil.binnode(ctx)))
 
-        if self.ui.quiet:
+        datahint = fm.datahint()
+        if self.ui.quiet and not datahint:
             return
 
         fm.data(
@@ -456,12 +457,17 @@
             ),
         )
 
-        if self.ui.debugflag:
-            fm.data(
-                manifest=fm.hexfunc(ctx.manifestnode() or wdirid),
-                extra=fm.formatdict(ctx.extra()),
-            )
+        if self.ui.debugflag or b'manifest' in datahint:
+            fm.data(manifest=fm.hexfunc(ctx.manifestnode() or wdirid))
+        if self.ui.debugflag or b'extra' in datahint:
+            fm.data(extra=fm.formatdict(ctx.extra()))
 
+        if (
+            self.ui.debugflag
+            or b'modified' in datahint
+            or b'added' in datahint
+            or b'removed' in datahint
+        ):
             files = ctx.p1().status(ctx)
             fm.data(
                 modified=fm.formatlist(files[0], name=b'file'),
@@ -469,18 +475,19 @@
                 removed=fm.formatlist(files[2], name=b'file'),
             )
 
-        elif self.ui.verbose:
+        verbose = not self.ui.debugflag and self.ui.verbose
+        if verbose or b'files' in datahint:
             fm.data(files=fm.formatlist(ctx.files(), name=b'file'))
-            if copies:
-                fm.data(
-                    copies=fm.formatdict(copies, key=b'name', value=b'source')
-                )
+        if verbose and copies or b'copies' in datahint:
+            fm.data(
+                copies=fm.formatdict(copies or {}, key=b'name', value=b'source')
+            )
 
-        if self._includestat:
+        if self._includestat or b'diffstat' in datahint:
             self.ui.pushbuffer()
             self._differ.showdiff(self.ui, ctx, self._diffopts, stat=True)
             fm.data(diffstat=self.ui.popbuffer())
-        if self._includediff:
+        if self._includediff or b'diff' in datahint:
             self.ui.pushbuffer()
             self._differ.showdiff(self.ui, ctx, self._diffopts, stat=False)
             fm.data(diff=self.ui.popbuffer())
--- a/tests/test-template-map.t	Sat Oct 05 23:30:09 2019 -0400
+++ b/tests/test-template-map.t	Sun Oct 06 14:58:41 2019 -0400
@@ -1119,6 +1119,60 @@
    {"parents": ["0000000000000000000000000000000000000000"], "rev": 7}
   ]
 
+  $ hg log -qr. -T'json(rev, parents)'
+  [
+   {"parents": ["29114dbae42b9f078cf2714dbe3a86bba8ec7453"], "rev": 8}
+  ]
+
+  $ hg log -r. -T'json(diff)'
+  [
+   {"diff": "diff -r 29114dbae42b -r 95c24699272e fourth\n--- /dev/null\tThu Jan 01 00:00:00 1970 +0000\n+++ b/fourth\tWed Jan 01 10:01:00 2020 +0000\n@@ -0,0 +1,1 @@\n+second\ndiff -r 29114dbae42b -r 95c24699272e second\n--- a/second\tMon Jan 12 13:46:40 1970 +0000\n+++ /dev/null\tThu Jan 01 00:00:00 1970 +0000\n@@ -1,1 +0,0 @@\n-second\ndiff -r 29114dbae42b -r 95c24699272e third\n--- /dev/null\tThu Jan 01 00:00:00 1970 +0000\n+++ b/third\tWed Jan 01 10:01:00 2020 +0000\n@@ -0,0 +1,1 @@\n+third\n"}
+  ]
+
+  $ hg log -r. -T'json(diffstat)'
+  [
+   {"diffstat": " fourth |  1 +\n second |  1 -\n third  |  1 +\n 3 files changed, 2 insertions(+), 1 deletions(-)\n"}
+  ]
+
+  $ hg log -r. -T'json(manifest)'
+  [
+   {"manifest": "94961b75a2da554b4df6fb599e5bfc7d48de0c64"}
+  ]
+
+  $ hg log -r. -T'json(extra)'
+  [
+   {"extra": {"branch": "default"}}
+  ]
+
+  $ hg log -r3 -T'json(modified)'
+  [
+   {"modified": ["c"]}
+  ]
+
+  $ hg log -r. -T'json(added)'
+  [
+   {"added": ["fourth", "third"]}
+  ]
+
+  $ hg log -r. -T'json(removed)'
+  [
+   {"removed": ["second"]}
+  ]
+
+  $ hg log -r. -T'json(files)'
+  [
+   {"files": ["fourth", "second", "third"]}
+  ]
+
+ --copies is the exception. copies dict is built only when --copies switch
+ is on:
+
+  $ hg log -r'.^:' -T'json(copies)' --copies
+  [
+   {"copies": {}},
+   {"copies": {"fourth": "second"}}
+  ]
+
   $ hg log -r. -T'json()'
   [
    {}