summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2025-04-30 13:29:20 -0400
committerLUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com>2025-04-30 12:25:15 -0700
commit1acbc14c34f264e0158436dc3d0265d500848462 (patch)
tree789df4c5324fe0b485ef502f6aac8e9e20314943
parentc448ba9cc7c68b91a122e293402dcc96f511b655 (diff)
downloadgit-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>
-rw-r--r--man/repo-manifest.18
-rw-r--r--subcmds/manifest.py39
-rw-r--r--tests/test_subcmds_manifest.py156
3 files changed, 195 insertions, 8 deletions
diff --git a/man/repo-manifest.1 b/man/repo-manifest.1
index f2f7290d..74e09145 100644
--- a/man/repo-manifest.1
+++ b/man/repo-manifest.1
@@ -30,8 +30,8 @@ if in \fB\-r\fR mode, do not write the dest\-branch field
30(only of use if the branch names for a sha1 manifest 30(only of use if the branch names for a sha1 manifest
31are sensitive) 31are sensitive)
32.TP 32.TP
33\fB\-\-json\fR 33\fB\-\-format\fR=\fI\,FORMAT\/\fR
34output manifest in JSON format (experimental) 34output format: xml, json (default: xml)
35.TP 35.TP
36\fB\-\-pretty\fR 36\fB\-\-pretty\fR
37format output for humans to read 37format output for humans to read
@@ -78,6 +78,10 @@ set to the ref we were on when the manifest was generated. The 'dest\-branch'
78attribute is set to indicate the remote ref to push changes to via 'repo 78attribute is set to indicate the remote ref to push changes to via 'repo
79upload'. 79upload'.
80.PP 80.PP
81Multiple output formats are supported via \fB\-\-format\fR. The default output is XML,
82and formats are generally "condensed". Use \fB\-\-pretty\fR for more human\-readable
83variations.
84.PP
81repo Manifest Format 85repo Manifest Format
82.PP 86.PP
83A repo manifest describes the structure of a repo client; that is the 87A repo manifest describes the structure of a repo client; that is the
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
15import enum
15import json 16import json
17import optparse
16import os 18import os
17import sys 19import sys
18 20
@@ -23,6 +25,16 @@ from repo_logging import RepoLogger
23logger = RepoLogger(__file__) 25logger = RepoLogger(__file__)
24 26
25 27
28class 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
26class Manifest(PagedCommand): 38class 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
42In this case, the 'upstream' attribute is set to the ref we were on 54In this case, the 'upstream' attribute is set to the ref we were on
43when the manifest was generated. The 'dest-branch' attribute is set 55when the manifest was generated. The 'dest-branch' attribute is set
44to indicate the remote ref to push changes to via 'repo upload'. 56to indicate the remote ref to push changes to via 'repo upload'.
57
58Multiple output formats are supported via --format. The default output
59is XML, and formats are generally "condensed". Use --pretty for more
60human-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,
diff --git a/tests/test_subcmds_manifest.py b/tests/test_subcmds_manifest.py
new file mode 100644
index 00000000..9b1ffb30
--- /dev/null
+++ b/tests/test_subcmds_manifest.py
@@ -0,0 +1,156 @@
1# Copyright (C) 2025 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unittests for the subcmds/manifest.py module."""
16
17import json
18from pathlib import Path
19from unittest import mock
20
21import manifest_xml
22from subcmds import manifest
23
24
25_EXAMPLE_MANIFEST = """\
26<?xml version="1.0" encoding="UTF-8"?>
27<manifest>
28 <remote name="test-remote" fetch="http://localhost" />
29 <default remote="test-remote" revision="refs/heads/main" />
30 <project name="repohooks" path="src/repohooks"/>
31 <repo-hooks in-project="repohooks" enabled-list="a, b"/>
32</manifest>
33"""
34
35
36def _get_cmd(repodir: Path) -> manifest.Manifest:
37 """Instantiate a manifest command object to test."""
38 manifests_git = repodir / "manifests.git"
39 manifests_git.mkdir()
40 (manifests_git / "config").write_text(
41 """
42[remote "origin"]
43\turl = http://localhost/manifest
44"""
45 )
46 client = manifest_xml.RepoClient(repodir=str(repodir))
47 git_event_log = mock.MagicMock(ErrorEvent=mock.Mock(return_value=None))
48 return manifest.Manifest(
49 repodir=client.repodir,
50 client=client,
51 manifest=client.manifest,
52 outer_client=client,
53 outer_manifest=client.manifest,
54 git_event_log=git_event_log,
55 )
56
57
58def test_output_format_xml_file(tmp_path):
59 """Test writing XML to a file."""
60 path = tmp_path / "manifest.xml"
61 path.write_text(_EXAMPLE_MANIFEST)
62 outpath = tmp_path / "output.xml"
63 cmd = _get_cmd(tmp_path)
64 opt, args = cmd.OptionParser.parse_args(["--output-file", str(outpath)])
65 cmd.Execute(opt, args)
66 # Normalize the output a bit as we don't exactly care.
67 normalize = lambda data: "\n".join(
68 x.strip() for x in data.splitlines() if x.strip()
69 )
70 assert (
71 normalize(outpath.read_text())
72 == """<?xml version="1.0" encoding="UTF-8"?>
73<manifest>
74<remote name="test-remote" fetch="http://localhost"/>
75<default remote="test-remote" revision="refs/heads/main"/>
76<project name="repohooks" path="src/repohooks"/>
77<repo-hooks in-project="repohooks" enabled-list="a b"/>
78</manifest>"""
79 )
80
81
82def test_output_format_xml_stdout(tmp_path, capsys):
83 """Test writing XML to stdout."""
84 path = tmp_path / "manifest.xml"
85 path.write_text(_EXAMPLE_MANIFEST)
86 cmd = _get_cmd(tmp_path)
87 opt, args = cmd.OptionParser.parse_args(["--format", "xml"])
88 cmd.Execute(opt, args)
89 # Normalize the output a bit as we don't exactly care.
90 normalize = lambda data: "\n".join(
91 x.strip() for x in data.splitlines() if x.strip()
92 )
93 stdout = capsys.readouterr().out
94 assert (
95 normalize(stdout)
96 == """<?xml version="1.0" encoding="UTF-8"?>
97<manifest>
98<remote name="test-remote" fetch="http://localhost"/>
99<default remote="test-remote" revision="refs/heads/main"/>
100<project name="repohooks" path="src/repohooks"/>
101<repo-hooks in-project="repohooks" enabled-list="a b"/>
102</manifest>"""
103 )
104
105
106def test_output_format_json(tmp_path, capsys):
107 """Test writing JSON."""
108 path = tmp_path / "manifest.xml"
109 path.write_text(_EXAMPLE_MANIFEST)
110 cmd = _get_cmd(tmp_path)
111 opt, args = cmd.OptionParser.parse_args(["--format", "json"])
112 cmd.Execute(opt, args)
113 obj = json.loads(capsys.readouterr().out)
114 assert obj == {
115 "default": {"remote": "test-remote", "revision": "refs/heads/main"},
116 "project": [{"name": "repohooks", "path": "src/repohooks"}],
117 "remote": [{"fetch": "http://localhost", "name": "test-remote"}],
118 "repo-hooks": {"enabled-list": "a b", "in-project": "repohooks"},
119 }
120
121
122def test_output_format_json_pretty(tmp_path, capsys):
123 """Test writing pretty JSON."""
124 path = tmp_path / "manifest.xml"
125 path.write_text(_EXAMPLE_MANIFEST)
126 cmd = _get_cmd(tmp_path)
127 opt, args = cmd.OptionParser.parse_args(["--format", "json", "--pretty"])
128 cmd.Execute(opt, args)
129 stdout = capsys.readouterr().out
130 assert (
131 stdout
132 == """\
133{
134 "default": {
135 "remote": "test-remote",
136 "revision": "refs/heads/main"
137 },
138 "project": [
139 {
140 "name": "repohooks",
141 "path": "src/repohooks"
142 }
143 ],
144 "remote": [
145 {
146 "fetch": "http://localhost",
147 "name": "test-remote"
148 }
149 ],
150 "repo-hooks": {
151 "enabled-list": "a b",
152 "in-project": "repohooks"
153 }
154}
155"""
156 )