##// END OF EJS Templates
template: add CBOR output format...
Yuya Nishihara -
r42163:77ef3498 default
parent child Browse files
Show More
@@ -130,6 +130,7 b' from . import ('
130 util,
130 util,
131 )
131 )
132 from .utils import (
132 from .utils import (
133 cborutil,
133 dateutil,
134 dateutil,
134 stringutil,
135 stringutil,
135 )
136 )
@@ -341,6 +342,18 b' class pickleformatter(baseformatter):'
341 baseformatter.end(self)
342 baseformatter.end(self)
342 self._out.write(pickle.dumps(self._data))
343 self._out.write(pickle.dumps(self._data))
343
344
345 class cborformatter(baseformatter):
346 '''serialize items as an indefinite-length CBOR array'''
347 def __init__(self, ui, out, topic, opts):
348 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
349 self._out = out
350 self._out.write(cborutil.BEGIN_INDEFINITE_ARRAY)
351 def _showitem(self):
352 self._out.write(b''.join(cborutil.streamencode(self._item)))
353 def end(self):
354 baseformatter.end(self)
355 self._out.write(cborutil.BREAK)
356
344 class jsonformatter(baseformatter):
357 class jsonformatter(baseformatter):
345 def __init__(self, ui, out, topic, opts):
358 def __init__(self, ui, out, topic, opts):
346 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
359 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
@@ -617,7 +630,9 b' class templateresources(templater.resour'
617
630
618 def formatter(ui, out, topic, opts):
631 def formatter(ui, out, topic, opts):
619 template = opts.get("template", "")
632 template = opts.get("template", "")
620 if template == "json":
633 if template == "cbor":
634 return cborformatter(ui, out, topic, opts)
635 elif template == "json":
621 return jsonformatter(ui, out, topic, opts)
636 return jsonformatter(ui, out, topic, opts)
622 elif template == "pickle":
637 elif template == "pickle":
623 return pickleformatter(ui, out, topic, opts)
638 return pickleformatter(ui, out, topic, opts)
@@ -142,9 +142,11 b' output containing authors, dates, descri'
142 using templates to make your life easier.
142 using templates to make your life easier.
143
143
144 The ``-T/--template`` argument allows specifying pre-defined styles.
144 The ``-T/--template`` argument allows specifying pre-defined styles.
145 Mercurial ships with the machine-readable styles ``json`` and ``xml``,
145 Mercurial ships with the machine-readable styles ``cbor``, ``json``,
146 which provide JSON and XML output, respectively. These are useful for
146 and ``xml``, which provide CBOR, JSON, and XML output, respectively.
147 producing output that is machine readable as-is.
147 These are useful for producing output that is machine readable as-is.
148
149 (Mercurial 5.0 is required for CBOR style.)
148
150
149 .. important::
151 .. important::
150
152
@@ -542,7 +542,7 b' def changesetdisplayer(ui, repo, opts, d'
542 regular display via changesetprinter() is done.
542 regular display via changesetprinter() is done.
543 """
543 """
544 postargs = (differ, opts, buffered)
544 postargs = (differ, opts, buffered)
545 if opts.get('template') == 'json':
545 if opts.get('template') in {'cbor', 'json'}:
546 fm = ui.formatter('log', opts)
546 fm = ui.formatter('log', opts)
547 return changesetformatter(ui, repo, fm, *postargs)
547 return changesetformatter(ui, repo, fm, *postargs)
548
548
@@ -669,6 +669,70 b' Test xml styles:'
669 </log>
669 </log>
670
670
671
671
672 test CBOR style:
673
674 $ cat <<'EOF' > "$TESTTMP/decodecborarray.py"
675 > from __future__ import absolute_import
676 > from mercurial import pycompat
677 > from mercurial.utils import (
678 > cborutil,
679 > stringutil,
680 > )
681 > data = pycompat.stdin.read()
682 > # our CBOR decoder doesn't support parsing indefinite-length arrays,
683 > # but the log output is indefinite stream by nature.
684 > assert data[:1] == cborutil.BEGIN_INDEFINITE_ARRAY
685 > assert data[-1:] == cborutil.BREAK
686 > items = cborutil.decodeall(data[1:-1])
687 > pycompat.stdout.write(stringutil.pprint(items, indent=1) + b'\n')
688 > EOF
689
690 $ hg log -k nosuch -Tcbor | "$PYTHON" "$TESTTMP/decodecborarray.py"
691 []
692
693 $ hg log -qr0:1 -Tcbor | "$PYTHON" "$TESTTMP/decodecborarray.py"
694 [
695 {
696 'node': '1e4e1b8f71e05681d422154f5421e385fec3454f',
697 'rev': 0
698 },
699 {
700 'node': 'b608e9d1a3f0273ccf70fb85fd6866b3482bf965',
701 'rev': 1
702 }
703 ]
704
705 $ hg log -vpr . -Tcbor --stat | "$PYTHON" "$TESTTMP/decodecborarray.py"
706 [
707 {
708 'bookmarks': [],
709 'branch': 'default',
710 'date': [
711 1577872860,
712 0
713 ],
714 'desc': 'third',
715 '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',
716 'diffstat': ' fourth | 1 +\n second | 1 -\n third | 1 +\n 3 files changed, 2 insertions(+), 1 deletions(-)\n',
717 'files': [
718 'fourth',
719 'second',
720 'third'
721 ],
722 'node': '95c24699272ef57d062b8bccc32c878bf841784a',
723 'parents': [
724 '29114dbae42b9f078cf2714dbe3a86bba8ec7453'
725 ],
726 'phase': 'draft',
727 'rev': 8,
728 'tags': [
729 'tip'
730 ],
731 'user': 'test'
732 }
733 ]
734
735
672 Test JSON style:
736 Test JSON style:
673
737
674 $ hg log -k nosuch -Tjson
738 $ hg log -k nosuch -Tjson
General Comments 0
You need to be logged in to leave comments. Login now