diff options
author | Mike Frysinger <vapier@google.com> | 2025-04-30 13:29:20 -0400 |
---|---|---|
committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2025-04-30 12:25:15 -0700 |
commit | 1acbc14c34f264e0158436dc3d0265d500848462 (patch) | |
tree | 789df4c5324fe0b485ef502f6aac8e9e20314943 /subcmds/manifest.py | |
parent | c448ba9cc7c68b91a122e293402dcc96f511b655 (diff) | |
download | git-repo-1acbc14c34f264e0158436dc3d0265d500848462.tar.gz |
manifest: generalize --json as --format=<format>
This will make it easier to add more formats without exploding the
common --xxx space and checking a large set of boolean flags.
Also fill out the test coverage while we're here.
Bug: b/412725063
Change-Id: I754013dc6cb3445f8a0979cefec599d55dafdcff
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/471941
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Mike Frysinger <vapier@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'subcmds/manifest.py')
-rw-r--r-- | subcmds/manifest.py | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/subcmds/manifest.py b/subcmds/manifest.py index bb6dc930..9786580a 100644 --- a/subcmds/manifest.py +++ b/subcmds/manifest.py | |||
@@ -12,7 +12,9 @@ | |||
12 | # See the License for the specific language governing permissions and | 12 | # See the License for the specific language governing permissions and |
13 | # limitations under the License. | 13 | # limitations under the License. |
14 | 14 | ||
15 | import enum | ||
15 | import json | 16 | import json |
17 | import optparse | ||
16 | import os | 18 | import os |
17 | import sys | 19 | import sys |
18 | 20 | ||
@@ -23,6 +25,16 @@ from repo_logging import RepoLogger | |||
23 | logger = RepoLogger(__file__) | 25 | logger = RepoLogger(__file__) |
24 | 26 | ||
25 | 27 | ||
28 | class OutputFormat(enum.Enum): | ||
29 | """Type for the requested output format.""" | ||
30 | |||
31 | # Canonicalized manifest in XML format. | ||
32 | XML = enum.auto() | ||
33 | |||
34 | # Canonicalized manifest in JSON format. | ||
35 | JSON = enum.auto() | ||
36 | |||
37 | |||
26 | class Manifest(PagedCommand): | 38 | class Manifest(PagedCommand): |
27 | COMMON = False | 39 | COMMON = False |
28 | helpSummary = "Manifest inspection utility" | 40 | helpSummary = "Manifest inspection utility" |
@@ -42,6 +54,10 @@ revisions set to the current commit hash. These are known as | |||
42 | In this case, the 'upstream' attribute is set to the ref we were on | 54 | In this case, the 'upstream' attribute is set to the ref we were on |
43 | when the manifest was generated. The 'dest-branch' attribute is set | 55 | when the manifest was generated. The 'dest-branch' attribute is set |
44 | to indicate the remote ref to push changes to via 'repo upload'. | 56 | to indicate the remote ref to push changes to via 'repo upload'. |
57 | |||
58 | Multiple output formats are supported via --format. The default output | ||
59 | is XML, and formats are generally "condensed". Use --pretty for more | ||
60 | human-readable variations. | ||
45 | """ | 61 | """ |
46 | 62 | ||
47 | @property | 63 | @property |
@@ -86,11 +102,21 @@ to indicate the remote ref to push changes to via 'repo upload'. | |||
86 | "(only of use if the branch names for a sha1 manifest are " | 102 | "(only of use if the branch names for a sha1 manifest are " |
87 | "sensitive)", | 103 | "sensitive)", |
88 | ) | 104 | ) |
105 | # Replaced with --format=json. Kept for backwards compatibility. | ||
106 | # Can delete in Jun 2026 or later. | ||
89 | p.add_option( | 107 | p.add_option( |
90 | "--json", | 108 | "--json", |
91 | default=False, | 109 | action="store_const", |
92 | action="store_true", | 110 | dest="format", |
93 | help="output manifest in JSON format (experimental)", | 111 | const=OutputFormat.JSON.name.lower(), |
112 | help=optparse.SUPPRESS_HELP, | ||
113 | ) | ||
114 | formats = tuple(x.lower() for x in OutputFormat.__members__.keys()) | ||
115 | p.add_option( | ||
116 | "--format", | ||
117 | default=OutputFormat.XML.name.lower(), | ||
118 | choices=formats, | ||
119 | help=f"output format: {', '.join(formats)} (default: %default)", | ||
94 | ) | 120 | ) |
95 | p.add_option( | 121 | p.add_option( |
96 | "--pretty", | 122 | "--pretty", |
@@ -121,6 +147,8 @@ to indicate the remote ref to push changes to via 'repo upload'. | |||
121 | if opt.manifest_name: | 147 | if opt.manifest_name: |
122 | self.manifest.Override(opt.manifest_name, False) | 148 | self.manifest.Override(opt.manifest_name, False) |
123 | 149 | ||
150 | output_format = OutputFormat[opt.format.upper()] | ||
151 | |||
124 | for manifest in self.ManifestList(opt): | 152 | for manifest in self.ManifestList(opt): |
125 | output_file = opt.output_file | 153 | output_file = opt.output_file |
126 | if output_file == "-": | 154 | if output_file == "-": |
@@ -135,8 +163,7 @@ to indicate the remote ref to push changes to via 'repo upload'. | |||
135 | 163 | ||
136 | manifest.SetUseLocalManifests(not opt.ignore_local_manifests) | 164 | manifest.SetUseLocalManifests(not opt.ignore_local_manifests) |
137 | 165 | ||
138 | if opt.json: | 166 | if output_format == OutputFormat.JSON: |
139 | logger.warning("warning: --json is experimental!") | ||
140 | doc = manifest.ToDict( | 167 | doc = manifest.ToDict( |
141 | peg_rev=opt.peg_rev, | 168 | peg_rev=opt.peg_rev, |
142 | peg_rev_upstream=opt.peg_rev_upstream, | 169 | peg_rev_upstream=opt.peg_rev_upstream, |
@@ -152,7 +179,7 @@ to indicate the remote ref to push changes to via 'repo upload'. | |||
152 | "separators": (",", ": ") if opt.pretty else (",", ":"), | 179 | "separators": (",", ": ") if opt.pretty else (",", ":"), |
153 | "sort_keys": True, | 180 | "sort_keys": True, |
154 | } | 181 | } |
155 | fd.write(json.dumps(doc, **json_settings)) | 182 | fd.write(json.dumps(doc, **json_settings) + "\n") |
156 | else: | 183 | else: |
157 | manifest.Save( | 184 | manifest.Save( |
158 | fd, | 185 | fd, |