[PATCH] template: add a log --style=json template.

John Carter john.carter at taitradio.com
Tue Sep 10 16:54:44 CDT 2013


# HG changeset patch
# User John Carter <john.carter at taitradio.com>
# Date 1378848541 -43200
# Node ID 38e756e6fb00f80f2762c3962a07a846e07176ca
# Parent  79e5de2bfa8cd18e81e71fbba1a62eee15b5e354
template: add map-cmdline.json to output logs in json format

Provides a very simple / low impedance format for exporting log data
to other scripting langauges. It is nicer than map-cmdline.xml as it
has a natural mapping to the primitive data types of most scripting
languages.

It would be better to provide additional filters or facilities at the
python level. (eg. start,no,last and end _changesets, and extras
keyword using a formal hash)

However, I thought as a first patch it would be less intrustive just
to use public facilities.

Sample output in diff for test/test-log.t

diff --git a/mercurial/templates/map-cmdline.json
b/mercurial/templates/map-cmdline.json
new file mode 100644
--- /dev/null
+++ b/mercurial/templates/map-cmdline.json
@@ -0,0 +1,41 @@
+header = '[\n'
+# Alas, changesets do not follow the
+# start_changesets, no_changesets, changeset, last_changeset, end_changesets
+# pattern...
+# And JSON obsessively INSISTS you have something after the final comma!
+footer = 'null]\n'
+
+# The extras field stinks, would need to rewrite showextras python method!
+changeset = '   \{\n      "revision": "{rev}",\n      "node":
"{node}",\n      "branches": "{branches}",\n      "bookmarks":
"{bookmarks}",\n      "tags": "{tags}",\n      "parents": {parents},\n
     "email": "{author|email|jsonescape}",\n      "author":
"{author|person|jsonescape}",\n      "date": "{date|rfc3339date}",\n
   "desc": "{desc|jsonescape}",\n      "file_adds": {file_adds},\n
 "file_dels": {file_dels},\n      "file_mods": {file_mods},\n
"file_copies": {file_copies},\n      "extras": {extras|json}\n   },\n'
+
+no_parents = '[]'
+start_parents = '[\n'
+parent        = '         \{"rev": {rev}, "node": "{node}"},\n'
+last_parent   = '         \{"rev": {rev}, "node": "{node}"}\n'
+end_parents   = '      ]'
+
+no_file_adds = '[]'
+start_file_adds = '[\n'
+file_add        = '         "{file_add|jsonescape}",\n'
+last_file_add   = '         "{file_add|jsonescape}"\n'
+end_file_adds   = '      ]'
+
+
+no_file_dels = '[]'
+start_file_dels = '[\n'
+file_del        = '         "{file_del|jsonescape}",\n'
+last_file_del   = '         "{file_del|jsonescape}"\n'
+end_file_dels   = '      ]'
+
+no_file_mods = '[]'
+start_file_mods = '[\n'
+file_mod        = '         "{file_mod|jsonescape}",\n'
+last_file_mod   = '         "{file_mod|jsonescape}"\n'
+end_file_mods   = '      ]'
+
+no_file_copies    = '[]'
+start_file_copies = '[\n'
+file_copy         = '         \{\n            "source":
"{source|jsonescape}",\n            "name": "{name|jsonescape}"\n
   },\n'
+last_file_copy    = '         \{\n            "source":
"{source|jsonescape}",\n            "name": "{name|jsonescape}"\n
   }\n'
+end_file_copies   = '      ]'
+
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -459,7 +459,7 @@

   $ hg log --style notexist
   abort: style 'notexist' not found
-  (available styles: bisect, changelog, compact, default, phases, xml)
+  (available styles: bisect, changelog, compact, default, json, phases, xml)
   [255]

 Error if style missing key:
diff --git a/tests/test-log.t b/tests/test-log.t
--- a/tests/test-log.t
+++ b/tests/test-log.t
@@ -88,7 +88,7 @@

   $ hg log -f -l1 --style something
   abort: style 'something' not found
-  (available styles: bisect, changelog, compact, default, phases, xml)
+  (available styles: bisect, changelog, compact, default, json, phases, xml)
   [255]

 -f, phases style
@@ -1367,3 +1367,38 @@


   $ cd ..
+
+Check --style=json
+
+  $ hg init checkjsonstyle
+  $ cd checkjsonstyle
+  $ echo 1 > a.c
+  $ hg add a.c
+  $ hg commit -m message a.c
+  $ hg log --style=json
+  [
+     {
+        "revision": "0",
+        "node": "c1fc6ccda7a8fd4f09adf2dc7533fe259446ff7c",
+        "branches": "",
+        "bookmarks": "",
+        "tags": "tip",
+        "parents": [],
+        "email": "test",
+        "author": "test",
+        "date": "1970-01-01T00:00:00+00:00",
+        "desc": "message",
+        "file_adds": [
+           "a.c"
+        ],
+        "file_dels": [],
+        "file_mods": [],
+        "file_copies": [],
+        "extras": [["branch=default"]]
+     },
+  null]
+
+
+
+
+

-- 

------------------------------
This email, including any attachments, is only for the intended recipient. 
It is subject to copyright, is confidential and may be the subject of legal 
or other privilege, none of which is waived or lost by reason of this 
transmission.
If you are not an intended recipient, you may not use, disseminate, 
distribute or reproduce such email, any attachments, or any part thereof. 
If you have received a message in error, please notify the sender 
immediately and erase all copies of the message and any attachments.
Unfortunately, we cannot warrant that the email has not been altered or 
corrupted during transmission nor can we guarantee that any email or any 
attachments are free from computer viruses or other conditions which may 
damage or interfere with recipient data, hardware or software. The 
recipient relies upon its own procedures and assumes all risk of use and of 
opening any attachments.
------------------------------


More information about the Mercurial-devel mailing list