diff options
| author | Mike Frysinger <vapier@google.com> | 2020-09-02 04:31:10 -0400 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2020-11-17 01:38:00 +0000 |
| commit | 23411d3f9c3df36b68080cf457ca093f8f1c1f21 (patch) | |
| tree | 86a541b8e6d94a9aa16e6e1f3a5883c32af17a23 /subcmds/manifest.py | |
| parent | 160748f828f0fdaf09d3be86c73840d9e28eccdb (diff) | |
| download | git-repo-23411d3f9c3df36b68080cf457ca093f8f1c1f21.tar.gz | |
manifest: add a --json output option
Sometimes parsing JSON is easier than parsing XML, especially when
the XML format is limited (which ours is). Add a --json option to
the manifest command to quickly emit that form.
Bug: https://crbug.com/gerrit/11743
Change-Id: Ia2bb254a78ae2b70a851638b4545fcafe8c1a76b
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/280436
Reviewed-by: Michael Mortensen <mmortensen@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'subcmds/manifest.py')
| -rw-r--r-- | subcmds/manifest.py | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/subcmds/manifest.py b/subcmds/manifest.py index f0a0d069..0052d7aa 100644 --- a/subcmds/manifest.py +++ b/subcmds/manifest.py | |||
| @@ -15,6 +15,8 @@ | |||
| 15 | # limitations under the License. | 15 | # limitations under the License. |
| 16 | 16 | ||
| 17 | from __future__ import print_function | 17 | from __future__ import print_function |
| 18 | |||
| 19 | import json | ||
| 18 | import os | 20 | import os |
| 19 | import sys | 21 | import sys |
| 20 | 22 | ||
| @@ -68,6 +70,10 @@ to indicate the remote ref to push changes to via 'repo upload'. | |||
| 68 | help='If in -r mode, do not write the dest-branch field. ' | 70 | help='If in -r mode, do not write the dest-branch field. ' |
| 69 | 'Only of use if the branch names for a sha1 manifest are ' | 71 | 'Only of use if the branch names for a sha1 manifest are ' |
| 70 | 'sensitive.') | 72 | 'sensitive.') |
| 73 | p.add_option('--json', default=False, action='store_true', | ||
| 74 | help='Output manifest in JSON format (experimental).') | ||
| 75 | p.add_option('--pretty', default=False, action='store_true', | ||
| 76 | help='Format output for humans to read.') | ||
| 71 | p.add_option('-o', '--output-file', | 77 | p.add_option('-o', '--output-file', |
| 72 | dest='output_file', | 78 | dest='output_file', |
| 73 | default='-', | 79 | default='-', |
| @@ -83,10 +89,26 @@ to indicate the remote ref to push changes to via 'repo upload'. | |||
| 83 | fd = sys.stdout | 89 | fd = sys.stdout |
| 84 | else: | 90 | else: |
| 85 | fd = open(opt.output_file, 'w') | 91 | fd = open(opt.output_file, 'w') |
| 86 | self.manifest.Save(fd, | 92 | if opt.json: |
| 87 | peg_rev=opt.peg_rev, | 93 | print('warning: --json is experimental!', file=sys.stderr) |
| 88 | peg_rev_upstream=opt.peg_rev_upstream, | 94 | doc = self.manifest.ToDict(peg_rev=opt.peg_rev, |
| 89 | peg_rev_dest_branch=opt.peg_rev_dest_branch) | 95 | peg_rev_upstream=opt.peg_rev_upstream, |
| 96 | peg_rev_dest_branch=opt.peg_rev_dest_branch) | ||
| 97 | |||
| 98 | json_settings = { | ||
| 99 | # JSON style guide says Uunicode characters are fully allowed. | ||
| 100 | 'ensure_ascii': False, | ||
| 101 | # We use 2 space indent to match JSON style guide. | ||
| 102 | 'indent': 2 if opt.pretty else None, | ||
| 103 | 'separators': (',', ': ') if opt.pretty else (',', ':'), | ||
| 104 | 'sort_keys': True, | ||
| 105 | } | ||
| 106 | fd.write(json.dumps(doc, **json_settings)) | ||
| 107 | else: | ||
| 108 | self.manifest.Save(fd, | ||
| 109 | peg_rev=opt.peg_rev, | ||
| 110 | peg_rev_upstream=opt.peg_rev_upstream, | ||
| 111 | peg_rev_dest_branch=opt.peg_rev_dest_branch) | ||
| 90 | fd.close() | 112 | fd.close() |
| 91 | if opt.output_file != '-': | 113 | if opt.output_file != '-': |
| 92 | print('Saved manifest to %s' % opt.output_file, file=sys.stderr) | 114 | print('Saved manifest to %s' % opt.output_file, file=sys.stderr) |
