Mercurial > public > mercurial-scm > hg
comparison mercurial/cmdutil.py @ 22427:bd15932846a4
cmdutil: add json style to log-like commands
Sadly, this can't be done with the normal templater like we do with
XML due to JSON's inter-record comma rules.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 15 Sep 2014 13:11:13 -0500 |
parents | 6f63c47cbb86 |
children | efedda4aed49 |
comparison
equal
deleted
inserted
replaced
22426:f6b533e64ed6 | 22427:bd15932846a4 |
---|---|
11 import util, scmutil, templater, patch, error, templatekw, revlog, copies | 11 import util, scmutil, templater, patch, error, templatekw, revlog, copies |
12 import match as matchmod | 12 import match as matchmod |
13 import context, repair, graphmod, revset, phases, obsolete, pathutil | 13 import context, repair, graphmod, revset, phases, obsolete, pathutil |
14 import changelog | 14 import changelog |
15 import bookmarks | 15 import bookmarks |
16 import encoding | |
16 import lock as lockmod | 17 import lock as lockmod |
17 | 18 |
18 def parsealiases(cmd): | 19 def parsealiases(cmd): |
19 return cmd.lstrip("^").split("|") | 20 return cmd.lstrip("^").split("|") |
20 | 21 |
1011 parents = [] | 1012 parents = [] |
1012 else: | 1013 else: |
1013 parents = [parents[0]] | 1014 parents = [parents[0]] |
1014 return parents | 1015 return parents |
1015 | 1016 |
1017 class jsonchangeset(changeset_printer): | |
1018 '''format changeset information.''' | |
1019 | |
1020 def __init__(self, ui, repo, matchfn, diffopts, buffered): | |
1021 changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered) | |
1022 self.cache = {} | |
1023 self._first = True | |
1024 | |
1025 def close(self): | |
1026 if not self._first: | |
1027 self.ui.write("\n]\n") | |
1028 else: | |
1029 self.ui.write("[]\n") | |
1030 | |
1031 def _show(self, ctx, copies, matchfn, props): | |
1032 '''show a single changeset or file revision''' | |
1033 hexnode = hex(ctx.node()) | |
1034 rev = ctx.rev() | |
1035 j = encoding.jsonescape | |
1036 | |
1037 if self._first: | |
1038 self.ui.write("[\n {") | |
1039 self._first = False | |
1040 else: | |
1041 self.ui.write(",\n {") | |
1042 | |
1043 if self.ui.quiet: | |
1044 self.ui.write('\n "rev": %d' % rev) | |
1045 self.ui.write(',\n "node": "%s"' % hexnode) | |
1046 self.ui.write('\n }') | |
1047 return | |
1048 | |
1049 self.ui.write('\n "rev": %d' % rev) | |
1050 self.ui.write(',\n "node": "%s"' % hexnode) | |
1051 self.ui.write(',\n "branch": "%s"' % j(ctx.branch())) | |
1052 self.ui.write(',\n "phase": "%s"' % ctx.phasestr()) | |
1053 self.ui.write(',\n "user": "%s"' % j(ctx.user())) | |
1054 self.ui.write(',\n "date": [%d, %d]' % ctx.date()) | |
1055 self.ui.write(',\n "desc": "%s"' % j(ctx.description())) | |
1056 | |
1057 self.ui.write(',\n "bookmarks": [%s]' % | |
1058 ", ".join('"%s"' % j(b) for b in ctx.bookmarks())) | |
1059 self.ui.write(',\n "tags": [%s]' % | |
1060 ", ".join('"%s"' % j(t) for t in ctx.tags())) | |
1061 self.ui.write(',\n "parents": [%s]' % | |
1062 ", ".join('"%s"' % c.hex() for c in ctx.parents())) | |
1063 | |
1064 if self.ui.debugflag: | |
1065 self.ui.write(',\n "manifest": "%s"' % hex(ctx.manifestnode())) | |
1066 | |
1067 self.ui.write(',\n "extra": {%s}' % | |
1068 ", ".join('"%s": "%s"' % (j(k), j(v)) | |
1069 for k, v in ctx.extra().items())) | |
1070 | |
1071 files = ctx.status(ctx.p1()) | |
1072 self.ui.write(',\n "modified": [%s]' % | |
1073 ", ".join('"%s"' % j(f) for f in files[0])) | |
1074 self.ui.write(',\n "added": [%s]' % | |
1075 ", ".join('"%s"' % j(f) for f in files[1])) | |
1076 self.ui.write(',\n "removed": [%s]' % | |
1077 ", ".join('"%s"' % j(f) for f in files[2])) | |
1078 | |
1079 elif self.ui.verbose: | |
1080 self.ui.write(',\n "files": [%s]' % | |
1081 ", ".join('"%s"' % j(f) for f in ctx.files())) | |
1082 | |
1083 if copies: | |
1084 self.ui.write(',\n "copies": {%s}' % | |
1085 ", ".join('"%s": %s' % (j(k), j(copies[k])) | |
1086 for k in copies)) | |
1087 | |
1088 matchfn = self.matchfn | |
1089 if matchfn: | |
1090 stat = self.diffopts.get('stat') | |
1091 diff = self.diffopts.get('patch') | |
1092 diffopts = patch.diffopts(self.ui, self.diffopts) | |
1093 node, prev = ctx.node(), ctx.p1().node() | |
1094 if stat: | |
1095 self.ui.pushbuffer() | |
1096 diffordiffstat(self.ui, self.repo, diffopts, prev, node, | |
1097 match=matchfn, stat=True) | |
1098 self.ui.write(',\n "diffstat": "%s"' % j(self.ui.popbuffer())) | |
1099 if diff: | |
1100 self.ui.pushbuffer() | |
1101 diffordiffstat(self.ui, self.repo, diffopts, prev, node, | |
1102 match=matchfn, stat=False) | |
1103 self.ui.write(',\n "diff": "%s"' % j(self.ui.popbuffer())) | |
1104 | |
1105 self.ui.write("\n }") | |
1016 | 1106 |
1017 class changeset_templater(changeset_printer): | 1107 class changeset_templater(changeset_printer): |
1018 '''format changeset information.''' | 1108 '''format changeset information.''' |
1019 | 1109 |
1020 def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered): | 1110 def __init__(self, ui, repo, matchfn, diffopts, tmpl, mapfile, buffered): |
1192 """ | 1282 """ |
1193 # options | 1283 # options |
1194 matchfn = None | 1284 matchfn = None |
1195 if opts.get('patch') or opts.get('stat'): | 1285 if opts.get('patch') or opts.get('stat'): |
1196 matchfn = scmutil.matchall(repo) | 1286 matchfn = scmutil.matchall(repo) |
1287 | |
1288 if opts.get('template') == 'json': | |
1289 return jsonchangeset(ui, repo, matchfn, opts, buffered) | |
1197 | 1290 |
1198 tmpl, mapfile = gettemplate(ui, opts.get('template'), opts.get('style')) | 1291 tmpl, mapfile = gettemplate(ui, opts.get('template'), opts.get('style')) |
1199 | 1292 |
1200 if not tmpl and not mapfile: | 1293 if not tmpl and not mapfile: |
1201 return changeset_printer(ui, repo, matchfn, opts, buffered) | 1294 return changeset_printer(ui, repo, matchfn, opts, buffered) |