How can you easily format mercury log "hg log" as JSON parsing?

I need an example from the command line about how easy it is to use hg log , with hg filters and safely output it to JSON (with an array containing files).

A multi-platform platform (windows / unix) will be great.

+5
source share
2 answers

Edit: this answer here works for any version of hg. for newer versions of hg (3.1+), see another answer which is more efficient and simple.

Here is an example of a solid oneliner.

It uses mercurials hg log and outputs its output to python oneliner. Hg log template configured to output valid python literals. python oneliner converts it to JSON.

 hg log --date ">2014-10-01" --no-merges --keyword " mantis@1953 " --keyword " mantis@1955 " --template "[\"{node|short}\",\"{author|user|urlescape}\",\"{date|rfc3339date}\",\"{desc|urlescape}\", [{files % '\"{file}\",'}]]\n" --user marinus --user develop | python -c "import sys,ast,json,urllib; x=[[z[0], urllib.unquote(z[1]).decode('utf8'), z[2], urllib.unquote(z[3]).decode('utf8'), z[4]] for z in [ast.literal_eval(y) for y in sys.stdin.readlines()]]; print(json.dumps(x,indent=2))" 

The above example is for unix , but you can just replace \" with "" if you need Windows compatibility. That you want unformatted JSON, set" indent "to None .

The python code is compatible with 2/3 (any modern version) and does not use any external modules.

See below for more information on the hg commands used:

 hg help log hg help dates hg help templates 

The python code uses the nested list comprehensions , google for more information.

+4
source

Mercurial has built-in JSON support. You can get the log output in JSON format simply by using:

 hg log -Tjson 

Filters can be used as usual, and you can add the -v (verbose) option to get the files.

Note that this is a relatively new feature (see the wiki for more details), which is probably why this is not clearly documented so far.

You can also get xml output using:

 hg log -Txml 
+11
source

Source: https://habr.com/ru/post/1207927/


All Articles