Patchwork template: add a log --style=json template.

login
register
mail settings
Submitter John Carter
Date Sept. 10, 2013, 9:54 p.m.
Message ID <CAFD1m3EC72G0iVNpx-WZi+ucar5UZcdOhp6v3=P0HWgWOJ1mZQ@mail.gmail.com>
Download mbox | patch
Permalink /patch/2419/
State Changes Requested, archived
Headers show

Comments

John Carter - Sept. 10, 2013, 9:54 p.m.
# HG changeset patch
# User John Carter <john.carter@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
Matt Mackall - Sept. 14, 2013, 8:37 p.m.
On Wed, 2013-09-11 at 09:54 +1200, John Carter wrote:
> # HG changeset patch
> # User John Carter <john.carter@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.

While I like the idea of adding JSON output, I've already got plans to
do it a different way:

http://mercurial.selenic.com/wiki/GenericTemplatingPlan

Patch

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]
+
+
+
+
+