The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 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 | |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 15 | import collections |
Colin Cross | 23acdd3 | 2012-04-21 00:33:54 -0700 | [diff] [blame] | 16 | import itertools |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import os |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 18 | import platform |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 19 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import sys |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 21 | import urllib.parse |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 22 | import xml.dom.minidom |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 24 | from error import ManifestInvalidPathError |
| 25 | from error import ManifestInvalidRevisionError |
| 26 | from error import ManifestParseError |
Daniel Kutik | 035f22a | 2022-12-13 12:34:23 +0100 | [diff] [blame] | 27 | from git_config import GitConfig |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 28 | from git_refs import HEAD |
| 29 | from git_refs import R_HEADS |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 30 | from git_superproject import Superproject |
Renaud Paquay | d5cec5e | 2016-11-01 11:24:03 -0700 | [diff] [blame] | 31 | import platform_utils |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 32 | from project import Annotation |
| 33 | from project import ManifestProject |
| 34 | from project import Project |
| 35 | from project import RemoteSpec |
| 36 | from project import RepoProject |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 37 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 39 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 40 | MANIFEST_FILE_NAME = "manifest.xml" |
| 41 | LOCAL_MANIFEST_NAME = "local_manifest.xml" |
| 42 | LOCAL_MANIFESTS_DIR_NAME = "local_manifests" |
| 43 | SUBMANIFEST_DIR = "submanifests" |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 44 | # Limit submanifests to an arbitrary depth for loop detection. |
| 45 | MAX_SUBMANIFEST_DEPTH = 8 |
LaMont Jones | b308db1 | 2022-02-25 17:05:21 +0000 | [diff] [blame] | 46 | # Add all projects from sub manifest into a group. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 47 | SUBMANIFEST_GROUP_PREFIX = "submanifest:" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 49 | # Add all projects from local manifest into a group. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 50 | LOCAL_MANIFEST_GROUP_PREFIX = "local:" |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 51 | |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 52 | # ContactInfo has the self-registered bug url, supplied by the manifest authors. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 53 | ContactInfo = collections.namedtuple("ContactInfo", "bugurl") |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 54 | |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 55 | # urljoin gets confused if the scheme is not known. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 56 | urllib.parse.uses_relative.extend( |
| 57 | ["ssh", "git", "persistent-https", "sso", "rpc"] |
| 58 | ) |
| 59 | urllib.parse.uses_netloc.extend( |
| 60 | ["ssh", "git", "persistent-https", "sso", "rpc"] |
| 61 | ) |
Conley Owens | db728cd | 2011-09-26 16:34:01 -0700 | [diff] [blame] | 62 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 63 | |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 64 | def XmlBool(node, attr, default=None): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 65 | """Determine boolean value of |node|'s |attr|. |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 66 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 67 | Invalid values will issue a non-fatal warning. |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 68 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 69 | Args: |
| 70 | node: XML node whose attributes we access. |
| 71 | attr: The attribute to access. |
| 72 | default: If the attribute is not set (value is empty), then use this. |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 73 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 74 | Returns: |
| 75 | True if the attribute is a valid string representing true. |
| 76 | False if the attribute is a valid string representing false. |
| 77 | |default| otherwise. |
| 78 | """ |
| 79 | value = node.getAttribute(attr) |
| 80 | s = value.lower() |
| 81 | if s == "": |
| 82 | return default |
| 83 | elif s in {"yes", "true", "1"}: |
| 84 | return True |
| 85 | elif s in {"no", "false", "0"}: |
| 86 | return False |
| 87 | else: |
| 88 | print( |
| 89 | 'warning: manifest: %s="%s": ignoring invalid XML boolean' |
| 90 | % (attr, value), |
| 91 | file=sys.stderr, |
| 92 | ) |
| 93 | return default |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def XmlInt(node, attr, default=None): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 97 | """Determine integer value of |node|'s |attr|. |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 98 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 99 | Args: |
| 100 | node: XML node whose attributes we access. |
| 101 | attr: The attribute to access. |
| 102 | default: If the attribute is not set (value is empty), then use this. |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 103 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 104 | Returns: |
| 105 | The number if the attribute is a valid number. |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 106 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 107 | Raises: |
| 108 | ManifestParseError: The number is invalid. |
| 109 | """ |
| 110 | value = node.getAttribute(attr) |
| 111 | if not value: |
| 112 | return default |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 113 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 114 | try: |
| 115 | return int(value) |
| 116 | except ValueError: |
| 117 | raise ManifestParseError( |
| 118 | 'manifest: invalid %s="%s" integer' % (attr, value) |
| 119 | ) |
Mike Frysinger | bb8ee7f | 2020-02-22 05:30:12 -0500 | [diff] [blame] | 120 | |
| 121 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 122 | class _Default: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 123 | """Project defaults within the manifest.""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 125 | revisionExpr = None |
| 126 | destBranchExpr = None |
| 127 | upstreamExpr = None |
| 128 | remote = None |
| 129 | sync_j = None |
| 130 | sync_c = False |
| 131 | sync_s = False |
| 132 | sync_tags = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 134 | def __eq__(self, other): |
| 135 | if not isinstance(other, _Default): |
| 136 | return False |
| 137 | return self.__dict__ == other.__dict__ |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 138 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 139 | def __ne__(self, other): |
| 140 | if not isinstance(other, _Default): |
| 141 | return True |
| 142 | return self.__dict__ != other.__dict__ |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 143 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 144 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 145 | class _XmlRemote: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 146 | def __init__( |
| 147 | self, |
| 148 | name, |
| 149 | alias=None, |
| 150 | fetch=None, |
| 151 | pushUrl=None, |
| 152 | manifestUrl=None, |
| 153 | review=None, |
| 154 | revision=None, |
| 155 | ): |
| 156 | self.name = name |
| 157 | self.fetchUrl = fetch |
| 158 | self.pushUrl = pushUrl |
| 159 | self.manifestUrl = manifestUrl |
| 160 | self.remoteAlias = alias |
| 161 | self.reviewUrl = review |
| 162 | self.revision = revision |
| 163 | self.resolvedFetchUrl = self._resolveFetchUrl() |
| 164 | self.annotations = [] |
Shawn O. Pearce | d1f70d9 | 2009-05-19 14:58:02 -0700 | [diff] [blame] | 165 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 166 | def __eq__(self, other): |
| 167 | if not isinstance(other, _XmlRemote): |
| 168 | return False |
| 169 | return ( |
| 170 | sorted(self.annotations) == sorted(other.annotations) |
| 171 | and self.name == other.name |
| 172 | and self.fetchUrl == other.fetchUrl |
| 173 | and self.pushUrl == other.pushUrl |
| 174 | and self.remoteAlias == other.remoteAlias |
| 175 | and self.reviewUrl == other.reviewUrl |
| 176 | and self.revision == other.revision |
| 177 | ) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 178 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 179 | def __ne__(self, other): |
| 180 | return not self.__eq__(other) |
David Pursehouse | 717ece9 | 2012-11-13 08:49:16 +0900 | [diff] [blame] | 181 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 182 | def _resolveFetchUrl(self): |
| 183 | if self.fetchUrl is None: |
| 184 | return "" |
| 185 | url = self.fetchUrl.rstrip("/") |
| 186 | manifestUrl = self.manifestUrl.rstrip("/") |
| 187 | # urljoin will gets confused over quite a few things. The ones we care |
| 188 | # about here are: |
| 189 | # * no scheme in the base url, like <hostname:port> |
| 190 | # We handle no scheme by replacing it with an obscure protocol, gopher |
| 191 | # and then replacing it with the original when we are done. |
Anthony King | cb07ba7 | 2015-03-28 23:26:04 +0000 | [diff] [blame] | 192 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 193 | if manifestUrl.find(":") != manifestUrl.find("/") - 1: |
| 194 | url = urllib.parse.urljoin("gopher://" + manifestUrl, url) |
| 195 | url = re.sub(r"^gopher://", "", url) |
| 196 | else: |
| 197 | url = urllib.parse.urljoin(manifestUrl, url) |
| 198 | return url |
Conley Owens | ceea368 | 2011-10-20 10:45:47 -0700 | [diff] [blame] | 199 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 200 | def ToRemoteSpec(self, projectName): |
| 201 | fetchUrl = self.resolvedFetchUrl.rstrip("/") |
| 202 | url = fetchUrl + "/" + projectName |
| 203 | remoteName = self.name |
| 204 | if self.remoteAlias: |
| 205 | remoteName = self.remoteAlias |
| 206 | return RemoteSpec( |
| 207 | remoteName, |
| 208 | url=url, |
| 209 | pushUrl=self.pushUrl, |
| 210 | review=self.reviewUrl, |
| 211 | orig_name=self.name, |
| 212 | fetchUrl=self.fetchUrl, |
| 213 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 215 | def AddAnnotation(self, name, value, keep): |
| 216 | self.annotations.append(Annotation(name, value, keep)) |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 217 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 218 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 219 | class _XmlSubmanifest: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 220 | """Manage the <submanifest> element specified in the manifest. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 221 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 222 | Attributes: |
| 223 | name: a string, the name for this submanifest. |
| 224 | remote: a string, the remote.name for this submanifest. |
| 225 | project: a string, the name of the manifest project. |
| 226 | revision: a string, the commitish. |
| 227 | manifestName: a string, the submanifest file name. |
| 228 | groups: a list of strings, the groups to add to all projects in the |
| 229 | submanifest. |
| 230 | default_groups: a list of strings, the default groups to sync. |
| 231 | path: a string, the relative path for the submanifest checkout. |
| 232 | parent: an XmlManifest, the parent manifest. |
| 233 | annotations: (derived) a list of annotations. |
| 234 | present: (derived) a boolean, whether the sub manifest file is present. |
| 235 | """ |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 236 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 237 | def __init__( |
| 238 | self, |
| 239 | name, |
| 240 | remote=None, |
| 241 | project=None, |
| 242 | revision=None, |
| 243 | manifestName=None, |
| 244 | groups=None, |
| 245 | default_groups=None, |
| 246 | path=None, |
| 247 | parent=None, |
| 248 | ): |
| 249 | self.name = name |
| 250 | self.remote = remote |
| 251 | self.project = project |
| 252 | self.revision = revision |
| 253 | self.manifestName = manifestName |
| 254 | self.groups = groups |
| 255 | self.default_groups = default_groups |
| 256 | self.path = path |
| 257 | self.parent = parent |
| 258 | self.annotations = [] |
| 259 | outer_client = parent._outer_client or parent |
| 260 | if self.remote and not self.project: |
| 261 | raise ManifestParseError( |
| 262 | f"Submanifest {name}: must specify project when remote is " |
| 263 | "given." |
| 264 | ) |
| 265 | # Construct the absolute path to the manifest file using the parent's |
| 266 | # method, so that we can correctly create our repo_client. |
| 267 | manifestFile = parent.SubmanifestInfoDir( |
| 268 | os.path.join(parent.path_prefix, self.relpath), |
| 269 | os.path.join("manifests", manifestName or "default.xml"), |
| 270 | ) |
| 271 | linkFile = parent.SubmanifestInfoDir( |
| 272 | os.path.join(parent.path_prefix, self.relpath), MANIFEST_FILE_NAME |
| 273 | ) |
| 274 | self.repo_client = RepoClient( |
| 275 | parent.repodir, |
| 276 | linkFile, |
| 277 | parent_groups=",".join(groups) or "", |
| 278 | submanifest_path=self.relpath, |
| 279 | outer_client=outer_client, |
| 280 | default_groups=default_groups, |
| 281 | ) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 282 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 283 | self.present = os.path.exists(manifestFile) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 284 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 285 | def __eq__(self, other): |
| 286 | if not isinstance(other, _XmlSubmanifest): |
| 287 | return False |
| 288 | return ( |
| 289 | self.name == other.name |
| 290 | and self.remote == other.remote |
| 291 | and self.project == other.project |
| 292 | and self.revision == other.revision |
| 293 | and self.manifestName == other.manifestName |
| 294 | and self.groups == other.groups |
| 295 | and self.default_groups == other.default_groups |
| 296 | and self.path == other.path |
| 297 | and sorted(self.annotations) == sorted(other.annotations) |
| 298 | ) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 299 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 300 | def __ne__(self, other): |
| 301 | return not self.__eq__(other) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 302 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 303 | def ToSubmanifestSpec(self): |
| 304 | """Return a SubmanifestSpec object, populating attributes""" |
| 305 | mp = self.parent.manifestProject |
| 306 | remote = self.parent.remotes[ |
| 307 | self.remote or self.parent.default.remote.name |
| 308 | ] |
| 309 | # If a project was given, generate the url from the remote and project. |
| 310 | # If not, use this manifestProject's url. |
| 311 | if self.project: |
| 312 | manifestUrl = remote.ToRemoteSpec(self.project).url |
| 313 | else: |
| 314 | manifestUrl = mp.GetRemote().url |
| 315 | manifestName = self.manifestName or "default.xml" |
| 316 | revision = self.revision or self.name |
| 317 | path = self.path or revision.split("/")[-1] |
| 318 | groups = self.groups or [] |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 319 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 320 | return SubmanifestSpec( |
| 321 | self.name, manifestUrl, manifestName, revision, path, groups |
| 322 | ) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 323 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 324 | @property |
| 325 | def relpath(self): |
| 326 | """The path of this submanifest relative to the parent manifest.""" |
| 327 | revision = self.revision or self.name |
| 328 | return self.path or revision.split("/")[-1] |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 329 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 330 | def GetGroupsStr(self): |
| 331 | """Returns the `groups` given for this submanifest.""" |
| 332 | if self.groups: |
| 333 | return ",".join(self.groups) |
| 334 | return "" |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 335 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 336 | def GetDefaultGroupsStr(self): |
| 337 | """Returns the `default-groups` given for this submanifest.""" |
| 338 | return ",".join(self.default_groups or []) |
| 339 | |
| 340 | def AddAnnotation(self, name, value, keep): |
| 341 | """Add annotations to the submanifest.""" |
| 342 | self.annotations.append(Annotation(name, value, keep)) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 343 | |
| 344 | |
| 345 | class SubmanifestSpec: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 346 | """The submanifest element, with all fields expanded.""" |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 347 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 348 | def __init__(self, name, manifestUrl, manifestName, revision, path, groups): |
| 349 | self.name = name |
| 350 | self.manifestUrl = manifestUrl |
| 351 | self.manifestName = manifestName |
| 352 | self.revision = revision |
| 353 | self.path = path |
| 354 | self.groups = groups or [] |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 355 | |
| 356 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 357 | class XmlManifest: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 358 | """manages the repo configuration file""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 359 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 360 | def __init__( |
| 361 | self, |
| 362 | repodir, |
| 363 | manifest_file, |
| 364 | local_manifests=None, |
| 365 | outer_client=None, |
| 366 | parent_groups="", |
| 367 | submanifest_path="", |
| 368 | default_groups=None, |
| 369 | ): |
| 370 | """Initialize. |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 371 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 372 | Args: |
| 373 | repodir: Path to the .repo/ dir for holding all internal checkout |
| 374 | state. It must be in the top directory of the repo client |
| 375 | checkout. |
| 376 | manifest_file: Full path to the manifest file to parse. This will |
| 377 | usually be |repodir|/|MANIFEST_FILE_NAME|. |
| 378 | local_manifests: Full path to the directory of local override |
| 379 | manifests. This will usually be |
| 380 | |repodir|/|LOCAL_MANIFESTS_DIR_NAME|. |
| 381 | outer_client: RepoClient of the outer manifest. |
| 382 | parent_groups: a string, the groups to apply to this projects. |
| 383 | submanifest_path: The submanifest root relative to the repo root. |
| 384 | default_groups: a string, the default manifest groups to use. |
| 385 | """ |
| 386 | # TODO(vapier): Move this out of this class. |
| 387 | self.globalConfig = GitConfig.ForUser() |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 388 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 389 | self.repodir = os.path.abspath(repodir) |
| 390 | self._CheckLocalPath(submanifest_path) |
| 391 | self.topdir = os.path.dirname(self.repodir) |
| 392 | if submanifest_path: |
| 393 | # This avoids a trailing os.path.sep when submanifest_path is empty. |
| 394 | self.topdir = os.path.join(self.topdir, submanifest_path) |
| 395 | if manifest_file != os.path.abspath(manifest_file): |
| 396 | raise ManifestParseError("manifest_file must be abspath") |
| 397 | self.manifestFile = manifest_file |
| 398 | if not outer_client or outer_client == self: |
| 399 | # manifestFileOverrides only exists in the outer_client's manifest, |
| 400 | # since that is the only instance left when Unload() is called on |
| 401 | # the outer manifest. |
| 402 | self.manifestFileOverrides = {} |
| 403 | self.local_manifests = local_manifests |
| 404 | self._load_local_manifests = True |
| 405 | self.parent_groups = parent_groups |
| 406 | self.default_groups = default_groups |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 407 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 408 | if outer_client and self.isGitcClient: |
| 409 | raise ManifestParseError( |
| 410 | "Multi-manifest is incompatible with `gitc-init`" |
| 411 | ) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 412 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 413 | if submanifest_path and not outer_client: |
| 414 | # If passing a submanifest_path, there must be an outer_client. |
| 415 | raise ManifestParseError(f"Bad call to {self.__class__.__name__}") |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 416 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 417 | # If self._outer_client is None, this is not a checkout that supports |
| 418 | # multi-tree. |
| 419 | self._outer_client = outer_client or self |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 420 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 421 | self.repoProject = RepoProject( |
| 422 | self, |
| 423 | "repo", |
| 424 | gitdir=os.path.join(repodir, "repo/.git"), |
| 425 | worktree=os.path.join(repodir, "repo"), |
| 426 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 427 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 428 | mp = self.SubmanifestProject(self.path_prefix) |
| 429 | self.manifestProject = mp |
Mike Frysinger | 979d5bd | 2020-02-09 02:28:34 -0500 | [diff] [blame] | 430 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 431 | # This is a bit hacky, but we're in a chicken & egg situation: all the |
| 432 | # normal repo settings live in the manifestProject which we just setup |
| 433 | # above, so we couldn't easily query before that. We assume Project() |
| 434 | # init doesn't care if this changes afterwards. |
| 435 | if os.path.exists(mp.gitdir) and mp.use_worktree: |
| 436 | mp.use_git_worktrees = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 438 | self.Unload() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 439 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 440 | def Override(self, name, load_local_manifests=True): |
| 441 | """Use a different manifest, just for the current instantiation.""" |
| 442 | path = None |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 443 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 444 | # Look for a manifest by path in the filesystem (including the cwd). |
| 445 | if not load_local_manifests: |
| 446 | local_path = os.path.abspath(name) |
| 447 | if os.path.isfile(local_path): |
| 448 | path = local_path |
Basil Gello | c745350 | 2018-05-25 20:23:52 +0300 | [diff] [blame] | 449 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 450 | # Look for manifests by name from the manifests repo. |
| 451 | if path is None: |
| 452 | path = os.path.join(self.manifestProject.worktree, name) |
| 453 | if not os.path.isfile(path): |
| 454 | raise ManifestParseError("manifest %s not found" % name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 455 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 456 | self._load_local_manifests = load_local_manifests |
| 457 | self._outer_client.manifestFileOverrides[self.path_prefix] = path |
| 458 | self.Unload() |
| 459 | self._Load() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 460 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 461 | def Link(self, name): |
| 462 | """Update the repo metadata to use a different manifest.""" |
| 463 | self.Override(name) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 464 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 465 | # Old versions of repo would generate symlinks we need to clean up. |
| 466 | platform_utils.remove(self.manifestFile, missing_ok=True) |
| 467 | # This file is interpreted as if it existed inside the manifest repo. |
| 468 | # That allows us to use <include> with the relative file name. |
| 469 | with open(self.manifestFile, "w") as fp: |
| 470 | fp.write( |
| 471 | """<?xml version="1.0" encoding="UTF-8"?> |
Mike Frysinger | a269b1c | 2020-02-21 00:49:41 -0500 | [diff] [blame] | 472 | <!-- |
| 473 | DO NOT EDIT THIS FILE! It is generated by repo and changes will be discarded. |
| 474 | If you want to use a different manifest, use `repo init -m <file>` instead. |
| 475 | |
| 476 | If you want to customize your checkout by overriding manifest settings, use |
| 477 | the local_manifests/ directory instead. |
| 478 | |
| 479 | For more information on repo manifests, check out: |
| 480 | https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md |
| 481 | --> |
| 482 | <manifest> |
| 483 | <include name="%s" /> |
| 484 | </manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 485 | """ |
| 486 | % (name,) |
| 487 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 489 | def _RemoteToXml(self, r, doc, root): |
| 490 | e = doc.createElement("remote") |
| 491 | root.appendChild(e) |
| 492 | e.setAttribute("name", r.name) |
| 493 | e.setAttribute("fetch", r.fetchUrl) |
| 494 | if r.pushUrl is not None: |
| 495 | e.setAttribute("pushurl", r.pushUrl) |
| 496 | if r.remoteAlias is not None: |
| 497 | e.setAttribute("alias", r.remoteAlias) |
| 498 | if r.reviewUrl is not None: |
| 499 | e.setAttribute("review", r.reviewUrl) |
| 500 | if r.revision is not None: |
| 501 | e.setAttribute("revision", r.revision) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 502 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 503 | for a in r.annotations: |
| 504 | if a.keep == "true": |
| 505 | ae = doc.createElement("annotation") |
| 506 | ae.setAttribute("name", a.name) |
| 507 | ae.setAttribute("value", a.value) |
| 508 | e.appendChild(ae) |
Jack Neus | 6ea0cae | 2021-07-20 20:52:33 +0000 | [diff] [blame] | 509 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 510 | def _SubmanifestToXml(self, r, doc, root): |
| 511 | """Generate XML <submanifest/> node.""" |
| 512 | e = doc.createElement("submanifest") |
| 513 | root.appendChild(e) |
| 514 | e.setAttribute("name", r.name) |
| 515 | if r.remote is not None: |
| 516 | e.setAttribute("remote", r.remote) |
| 517 | if r.project is not None: |
| 518 | e.setAttribute("project", r.project) |
| 519 | if r.manifestName is not None: |
| 520 | e.setAttribute("manifest-name", r.manifestName) |
| 521 | if r.revision is not None: |
| 522 | e.setAttribute("revision", r.revision) |
| 523 | if r.path is not None: |
| 524 | e.setAttribute("path", r.path) |
| 525 | if r.groups: |
| 526 | e.setAttribute("groups", r.GetGroupsStr()) |
| 527 | if r.default_groups: |
| 528 | e.setAttribute("default-groups", r.GetDefaultGroupsStr()) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 529 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 530 | for a in r.annotations: |
| 531 | if a.keep == "true": |
| 532 | ae = doc.createElement("annotation") |
| 533 | ae.setAttribute("name", a.name) |
| 534 | ae.setAttribute("value", a.value) |
| 535 | e.appendChild(ae) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 536 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 537 | def _ParseList(self, field): |
| 538 | """Parse fields that contain flattened lists. |
Mike Frysinger | 51e39d5 | 2020-12-04 05:32:06 -0500 | [diff] [blame] | 539 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 540 | These are whitespace & comma separated. Empty elements will be |
| 541 | discarded. |
| 542 | """ |
| 543 | return [x for x in re.split(r"[,\s]+", field) if x] |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 544 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 545 | def ToXml( |
| 546 | self, |
| 547 | peg_rev=False, |
| 548 | peg_rev_upstream=True, |
| 549 | peg_rev_dest_branch=True, |
| 550 | groups=None, |
| 551 | omit_local=False, |
| 552 | ): |
| 553 | """Return the current manifest XML.""" |
| 554 | mp = self.manifestProject |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 555 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 556 | if groups is None: |
| 557 | groups = mp.manifest_groups |
| 558 | if groups: |
| 559 | groups = self._ParseList(groups) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 560 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 561 | doc = xml.dom.minidom.Document() |
| 562 | root = doc.createElement("manifest") |
| 563 | if self.is_submanifest: |
| 564 | root.setAttribute("path", self.path_prefix) |
| 565 | doc.appendChild(root) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 566 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 567 | # Save out the notice. There's a little bit of work here to give it the |
| 568 | # right whitespace, which assumes that the notice is automatically |
| 569 | # indented by 4 by minidom. |
| 570 | if self.notice: |
| 571 | notice_element = root.appendChild(doc.createElement("notice")) |
| 572 | notice_lines = self.notice.splitlines() |
| 573 | indented_notice = ( |
| 574 | "\n".join(" " * 4 + line for line in notice_lines) |
| 575 | )[4:] |
| 576 | notice_element.appendChild(doc.createTextNode(indented_notice)) |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 577 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 578 | d = self.default |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 579 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 580 | for r in sorted(self.remotes): |
| 581 | self._RemoteToXml(self.remotes[r], doc, root) |
| 582 | if self.remotes: |
| 583 | root.appendChild(doc.createTextNode("")) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 584 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 585 | have_default = False |
| 586 | e = doc.createElement("default") |
| 587 | if d.remote: |
| 588 | have_default = True |
| 589 | e.setAttribute("remote", d.remote.name) |
| 590 | if d.revisionExpr: |
| 591 | have_default = True |
| 592 | e.setAttribute("revision", d.revisionExpr) |
| 593 | if d.destBranchExpr: |
| 594 | have_default = True |
| 595 | e.setAttribute("dest-branch", d.destBranchExpr) |
| 596 | if d.upstreamExpr: |
| 597 | have_default = True |
| 598 | e.setAttribute("upstream", d.upstreamExpr) |
| 599 | if d.sync_j is not None: |
| 600 | have_default = True |
| 601 | e.setAttribute("sync-j", "%d" % d.sync_j) |
| 602 | if d.sync_c: |
| 603 | have_default = True |
| 604 | e.setAttribute("sync-c", "true") |
| 605 | if d.sync_s: |
| 606 | have_default = True |
| 607 | e.setAttribute("sync-s", "true") |
| 608 | if not d.sync_tags: |
| 609 | have_default = True |
| 610 | e.setAttribute("sync-tags", "false") |
| 611 | if have_default: |
| 612 | root.appendChild(e) |
| 613 | root.appendChild(doc.createTextNode("")) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 614 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 615 | if self._manifest_server: |
| 616 | e = doc.createElement("manifest-server") |
| 617 | e.setAttribute("url", self._manifest_server) |
| 618 | root.appendChild(e) |
| 619 | root.appendChild(doc.createTextNode("")) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 620 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 621 | for r in sorted(self.submanifests): |
| 622 | self._SubmanifestToXml(self.submanifests[r], doc, root) |
| 623 | if self.submanifests: |
| 624 | root.appendChild(doc.createTextNode("")) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 625 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 626 | def output_projects(parent, parent_node, projects): |
| 627 | for project_name in projects: |
| 628 | for project in self._projects[project_name]: |
| 629 | output_project(parent, parent_node, project) |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 630 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 631 | def output_project(parent, parent_node, p): |
| 632 | if not p.MatchesGroups(groups): |
| 633 | return |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 634 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 635 | if omit_local and self.IsFromLocalManifest(p): |
| 636 | return |
LaMont Jones | a8cf575 | 2022-07-15 20:31:33 +0000 | [diff] [blame] | 637 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 638 | name = p.name |
| 639 | relpath = p.relpath |
| 640 | if parent: |
| 641 | name = self._UnjoinName(parent.name, name) |
| 642 | relpath = self._UnjoinRelpath(parent.relpath, relpath) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 643 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 644 | e = doc.createElement("project") |
| 645 | parent_node.appendChild(e) |
| 646 | e.setAttribute("name", name) |
| 647 | if relpath != name: |
| 648 | e.setAttribute("path", relpath) |
| 649 | remoteName = None |
| 650 | if d.remote: |
| 651 | remoteName = d.remote.name |
| 652 | if not d.remote or p.remote.orig_name != remoteName: |
| 653 | remoteName = p.remote.orig_name |
| 654 | e.setAttribute("remote", remoteName) |
| 655 | if peg_rev: |
| 656 | if self.IsMirror: |
| 657 | value = p.bare_git.rev_parse(p.revisionExpr + "^0") |
| 658 | else: |
| 659 | value = p.work_git.rev_parse(HEAD + "^0") |
| 660 | e.setAttribute("revision", value) |
| 661 | if peg_rev_upstream: |
| 662 | if p.upstream: |
| 663 | e.setAttribute("upstream", p.upstream) |
| 664 | elif value != p.revisionExpr: |
| 665 | # Only save the origin if the origin is not a sha1, and |
| 666 | # the default isn't our value |
| 667 | e.setAttribute("upstream", p.revisionExpr) |
| 668 | |
| 669 | if peg_rev_dest_branch: |
| 670 | if p.dest_branch: |
| 671 | e.setAttribute("dest-branch", p.dest_branch) |
| 672 | elif value != p.revisionExpr: |
| 673 | e.setAttribute("dest-branch", p.revisionExpr) |
| 674 | |
| 675 | else: |
| 676 | revision = ( |
| 677 | self.remotes[p.remote.orig_name].revision or d.revisionExpr |
| 678 | ) |
| 679 | if not revision or revision != p.revisionExpr: |
| 680 | e.setAttribute("revision", p.revisionExpr) |
| 681 | elif p.revisionId: |
| 682 | e.setAttribute("revision", p.revisionId) |
| 683 | if p.upstream and ( |
| 684 | p.upstream != p.revisionExpr or p.upstream != d.upstreamExpr |
| 685 | ): |
| 686 | e.setAttribute("upstream", p.upstream) |
| 687 | |
| 688 | if p.dest_branch and p.dest_branch != d.destBranchExpr: |
| 689 | e.setAttribute("dest-branch", p.dest_branch) |
| 690 | |
| 691 | for c in p.copyfiles: |
| 692 | ce = doc.createElement("copyfile") |
| 693 | ce.setAttribute("src", c.src) |
| 694 | ce.setAttribute("dest", c.dest) |
| 695 | e.appendChild(ce) |
| 696 | |
| 697 | for lf in p.linkfiles: |
| 698 | le = doc.createElement("linkfile") |
| 699 | le.setAttribute("src", lf.src) |
| 700 | le.setAttribute("dest", lf.dest) |
| 701 | e.appendChild(le) |
| 702 | |
| 703 | default_groups = ["all", "name:%s" % p.name, "path:%s" % p.relpath] |
| 704 | egroups = [g for g in p.groups if g not in default_groups] |
| 705 | if egroups: |
| 706 | e.setAttribute("groups", ",".join(egroups)) |
| 707 | |
| 708 | for a in p.annotations: |
| 709 | if a.keep == "true": |
| 710 | ae = doc.createElement("annotation") |
| 711 | ae.setAttribute("name", a.name) |
| 712 | ae.setAttribute("value", a.value) |
| 713 | e.appendChild(ae) |
| 714 | |
| 715 | if p.sync_c: |
| 716 | e.setAttribute("sync-c", "true") |
| 717 | |
| 718 | if p.sync_s: |
| 719 | e.setAttribute("sync-s", "true") |
| 720 | |
| 721 | if not p.sync_tags: |
| 722 | e.setAttribute("sync-tags", "false") |
| 723 | |
| 724 | if p.clone_depth: |
| 725 | e.setAttribute("clone-depth", str(p.clone_depth)) |
| 726 | |
| 727 | self._output_manifest_project_extras(p, e) |
| 728 | |
| 729 | if p.subprojects: |
Jason R. Coombs | 0bcffd8 | 2023-10-20 23:29:42 +0545 | [diff] [blame] | 730 | subprojects = {subp.name for subp in p.subprojects} |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 731 | output_projects(p, e, list(sorted(subprojects))) |
| 732 | |
Jason R. Coombs | 0bcffd8 | 2023-10-20 23:29:42 +0545 | [diff] [blame] | 733 | projects = {p.name for p in self._paths.values() if not p.parent} |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 734 | output_projects(None, root, list(sorted(projects))) |
| 735 | |
| 736 | if self._repo_hooks_project: |
| 737 | root.appendChild(doc.createTextNode("")) |
| 738 | e = doc.createElement("repo-hooks") |
| 739 | e.setAttribute("in-project", self._repo_hooks_project.name) |
| 740 | e.setAttribute( |
| 741 | "enabled-list", |
| 742 | " ".join(self._repo_hooks_project.enabled_repo_hooks), |
| 743 | ) |
| 744 | root.appendChild(e) |
| 745 | |
| 746 | if self._superproject: |
| 747 | root.appendChild(doc.createTextNode("")) |
| 748 | e = doc.createElement("superproject") |
| 749 | e.setAttribute("name", self._superproject.name) |
| 750 | remoteName = None |
| 751 | if d.remote: |
| 752 | remoteName = d.remote.name |
| 753 | remote = self._superproject.remote |
| 754 | if not d.remote or remote.orig_name != remoteName: |
| 755 | remoteName = remote.orig_name |
| 756 | e.setAttribute("remote", remoteName) |
| 757 | revision = remote.revision or d.revisionExpr |
| 758 | if not revision or revision != self._superproject.revision: |
| 759 | e.setAttribute("revision", self._superproject.revision) |
| 760 | root.appendChild(e) |
| 761 | |
| 762 | if self._contactinfo.bugurl != Wrapper().BUG_URL: |
| 763 | root.appendChild(doc.createTextNode("")) |
| 764 | e = doc.createElement("contactinfo") |
| 765 | e.setAttribute("bugurl", self._contactinfo.bugurl) |
| 766 | root.appendChild(e) |
| 767 | |
| 768 | return doc |
| 769 | |
| 770 | def ToDict(self, **kwargs): |
| 771 | """Return the current manifest as a dictionary.""" |
| 772 | # Elements that may only appear once. |
| 773 | SINGLE_ELEMENTS = { |
| 774 | "notice", |
| 775 | "default", |
| 776 | "manifest-server", |
| 777 | "repo-hooks", |
| 778 | "superproject", |
| 779 | "contactinfo", |
| 780 | } |
| 781 | # Elements that may be repeated. |
| 782 | MULTI_ELEMENTS = { |
| 783 | "remote", |
| 784 | "remove-project", |
| 785 | "project", |
| 786 | "extend-project", |
| 787 | "include", |
| 788 | "submanifest", |
| 789 | # These are children of 'project' nodes. |
| 790 | "annotation", |
| 791 | "project", |
| 792 | "copyfile", |
| 793 | "linkfile", |
| 794 | } |
| 795 | |
| 796 | doc = self.ToXml(**kwargs) |
| 797 | ret = {} |
| 798 | |
| 799 | def append_children(ret, node): |
| 800 | for child in node.childNodes: |
| 801 | if child.nodeType == xml.dom.Node.ELEMENT_NODE: |
| 802 | attrs = child.attributes |
Jason R. Coombs | 0bcffd8 | 2023-10-20 23:29:42 +0545 | [diff] [blame] | 803 | element = { |
| 804 | attrs.item(i).localName: attrs.item(i).value |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 805 | for i in range(attrs.length) |
Jason R. Coombs | 0bcffd8 | 2023-10-20 23:29:42 +0545 | [diff] [blame] | 806 | } |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 807 | if child.nodeName in SINGLE_ELEMENTS: |
| 808 | ret[child.nodeName] = element |
| 809 | elif child.nodeName in MULTI_ELEMENTS: |
| 810 | ret.setdefault(child.nodeName, []).append(element) |
| 811 | else: |
| 812 | raise ManifestParseError( |
| 813 | 'Unhandled element "%s"' % (child.nodeName,) |
| 814 | ) |
| 815 | |
| 816 | append_children(element, child) |
| 817 | |
| 818 | append_children(ret, doc.firstChild) |
| 819 | |
| 820 | return ret |
| 821 | |
| 822 | def Save(self, fd, **kwargs): |
| 823 | """Write the current manifest out to the given file descriptor.""" |
| 824 | doc = self.ToXml(**kwargs) |
| 825 | doc.writexml(fd, "", " ", "\n", "UTF-8") |
| 826 | |
| 827 | def _output_manifest_project_extras(self, p, e): |
| 828 | """Manifests can modify e if they support extra project attributes.""" |
| 829 | |
| 830 | @property |
| 831 | def is_multimanifest(self): |
| 832 | """Whether this is a multimanifest checkout. |
| 833 | |
| 834 | This is safe to use as long as the outermost manifest XML has been |
| 835 | parsed. |
| 836 | """ |
| 837 | return bool(self._outer_client._submanifests) |
| 838 | |
| 839 | @property |
| 840 | def is_submanifest(self): |
| 841 | """Whether this manifest is a submanifest. |
| 842 | |
| 843 | This is safe to use as long as the outermost manifest XML has been |
| 844 | parsed. |
| 845 | """ |
| 846 | return self._outer_client and self._outer_client != self |
| 847 | |
| 848 | @property |
| 849 | def outer_client(self): |
| 850 | """The instance of the outermost manifest client.""" |
| 851 | self._Load() |
| 852 | return self._outer_client |
| 853 | |
| 854 | @property |
| 855 | def all_manifests(self): |
| 856 | """Generator yielding all (sub)manifests, in depth-first order.""" |
| 857 | self._Load() |
| 858 | outer = self._outer_client |
| 859 | yield outer |
Jason R. Coombs | 8dd8521 | 2023-10-20 06:48:20 -0400 | [diff] [blame] | 860 | yield from outer.all_children |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 861 | |
| 862 | @property |
| 863 | def all_children(self): |
| 864 | """Generator yielding all (present) child submanifests.""" |
| 865 | self._Load() |
| 866 | for child in self._submanifests.values(): |
| 867 | if child.repo_client: |
| 868 | yield child.repo_client |
Jason R. Coombs | 8dd8521 | 2023-10-20 06:48:20 -0400 | [diff] [blame] | 869 | yield from child.repo_client.all_children |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 870 | |
| 871 | @property |
| 872 | def path_prefix(self): |
| 873 | """The path of this submanifest, relative to the outermost manifest.""" |
| 874 | if not self._outer_client or self == self._outer_client: |
| 875 | return "" |
| 876 | return os.path.relpath(self.topdir, self._outer_client.topdir) |
| 877 | |
| 878 | @property |
| 879 | def all_paths(self): |
| 880 | """All project paths for all (sub)manifests. |
| 881 | |
| 882 | See also `paths`. |
| 883 | |
| 884 | Returns: |
| 885 | A dictionary of {path: Project()}. `path` is relative to the outer |
| 886 | manifest. |
| 887 | """ |
| 888 | ret = {} |
| 889 | for tree in self.all_manifests: |
| 890 | prefix = tree.path_prefix |
| 891 | ret.update( |
| 892 | {os.path.join(prefix, k): v for k, v in tree.paths.items()} |
| 893 | ) |
| 894 | return ret |
| 895 | |
| 896 | @property |
| 897 | def all_projects(self): |
| 898 | """All projects for all (sub)manifests. See `projects`.""" |
| 899 | return list( |
| 900 | itertools.chain.from_iterable( |
| 901 | x._paths.values() for x in self.all_manifests |
| 902 | ) |
| 903 | ) |
| 904 | |
| 905 | @property |
| 906 | def paths(self): |
| 907 | """Return all paths for this manifest. |
| 908 | |
| 909 | Returns: |
| 910 | A dictionary of {path: Project()}. `path` is relative to this |
| 911 | manifest. |
| 912 | """ |
| 913 | self._Load() |
| 914 | return self._paths |
| 915 | |
| 916 | @property |
| 917 | def projects(self): |
| 918 | """Return a list of all Projects in this manifest.""" |
| 919 | self._Load() |
| 920 | return list(self._paths.values()) |
| 921 | |
| 922 | @property |
| 923 | def remotes(self): |
| 924 | """Return a list of remotes for this manifest.""" |
| 925 | self._Load() |
| 926 | return self._remotes |
| 927 | |
| 928 | @property |
| 929 | def default(self): |
| 930 | """Return default values for this manifest.""" |
| 931 | self._Load() |
| 932 | return self._default |
| 933 | |
| 934 | @property |
| 935 | def submanifests(self): |
| 936 | """All submanifests in this manifest.""" |
| 937 | self._Load() |
| 938 | return self._submanifests |
| 939 | |
| 940 | @property |
| 941 | def repo_hooks_project(self): |
| 942 | self._Load() |
| 943 | return self._repo_hooks_project |
| 944 | |
| 945 | @property |
| 946 | def superproject(self): |
| 947 | self._Load() |
| 948 | return self._superproject |
| 949 | |
| 950 | @property |
| 951 | def contactinfo(self): |
| 952 | self._Load() |
| 953 | return self._contactinfo |
| 954 | |
| 955 | @property |
| 956 | def notice(self): |
| 957 | self._Load() |
| 958 | return self._notice |
| 959 | |
| 960 | @property |
| 961 | def manifest_server(self): |
| 962 | self._Load() |
| 963 | return self._manifest_server |
| 964 | |
| 965 | @property |
| 966 | def CloneBundle(self): |
| 967 | clone_bundle = self.manifestProject.clone_bundle |
| 968 | if clone_bundle is None: |
| 969 | return False if self.manifestProject.partial_clone else True |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 970 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 971 | return clone_bundle |
Sean McAllister | af908cb | 2020-04-20 08:41:58 -0600 | [diff] [blame] | 972 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 973 | @property |
| 974 | def CloneFilter(self): |
| 975 | if self.manifestProject.partial_clone: |
| 976 | return self.manifestProject.clone_filter |
| 977 | return None |
Sean McAllister | af908cb | 2020-04-20 08:41:58 -0600 | [diff] [blame] | 978 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 979 | @property |
Jason Chang | 1783332 | 2023-05-23 13:06:55 -0700 | [diff] [blame] | 980 | def CloneFilterForDepth(self): |
| 981 | if self.manifestProject.clone_filter_for_depth: |
| 982 | return self.manifestProject.clone_filter_for_depth |
| 983 | return None |
| 984 | |
| 985 | @property |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 986 | def PartialCloneExclude(self): |
| 987 | exclude = self.manifest.manifestProject.partial_clone_exclude or "" |
Jason R. Coombs | 0bcffd8 | 2023-10-20 23:29:42 +0545 | [diff] [blame] | 988 | return {x.strip() for x in exclude.split(",")} |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 989 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 990 | def SetManifestOverride(self, path): |
| 991 | """Override manifestFile. The caller must call Unload()""" |
| 992 | self._outer_client.manifest.manifestFileOverrides[ |
| 993 | self.path_prefix |
| 994 | ] = path |
Simon Ruggier | 7e59de2 | 2015-07-24 12:50:06 +0200 | [diff] [blame] | 995 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 996 | @property |
| 997 | def UseLocalManifests(self): |
| 998 | return self._load_local_manifests |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 999 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1000 | def SetUseLocalManifests(self, value): |
| 1001 | self._load_local_manifests = value |
Jeff Hamilton | e0df232 | 2014-04-21 17:10:59 -0500 | [diff] [blame] | 1002 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1003 | @property |
| 1004 | def HasLocalManifests(self): |
| 1005 | return self._load_local_manifests and self.local_manifests |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 1006 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1007 | def IsFromLocalManifest(self, project): |
| 1008 | """Is the project from a local manifest?""" |
| 1009 | return any( |
| 1010 | x.startswith(LOCAL_MANIFEST_GROUP_PREFIX) for x in project.groups |
| 1011 | ) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 1012 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1013 | @property |
| 1014 | def IsMirror(self): |
| 1015 | return self.manifestProject.mirror |
Anatol Pomazau | 79770d2 | 2012-04-20 14:41:59 -0700 | [diff] [blame] | 1016 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1017 | @property |
| 1018 | def UseGitWorktrees(self): |
| 1019 | return self.manifestProject.use_worktree |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1020 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1021 | @property |
| 1022 | def IsArchive(self): |
| 1023 | return self.manifestProject.archive |
YOUNG HO CHA | a32c92c | 2018-02-14 16:57:31 +0900 | [diff] [blame] | 1024 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1025 | @property |
| 1026 | def HasSubmodules(self): |
| 1027 | return self.manifestProject.submodules |
Dan Willemsen | 8840922 | 2015-08-17 15:29:10 -0700 | [diff] [blame] | 1028 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1029 | @property |
| 1030 | def EnableGitLfs(self): |
| 1031 | return self.manifestProject.git_lfs |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1032 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1033 | def FindManifestByPath(self, path): |
| 1034 | """Returns the manifest containing path.""" |
| 1035 | path = os.path.abspath(path) |
| 1036 | manifest = self._outer_client or self |
| 1037 | old = None |
| 1038 | while manifest._submanifests and manifest != old: |
| 1039 | old = manifest |
| 1040 | for name in manifest._submanifests: |
| 1041 | tree = manifest._submanifests[name] |
| 1042 | if path.startswith(tree.repo_client.manifest.topdir): |
| 1043 | manifest = tree.repo_client |
| 1044 | break |
| 1045 | return manifest |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1046 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1047 | @property |
| 1048 | def subdir(self): |
| 1049 | """Returns the path for per-submanifest objects for this manifest.""" |
| 1050 | return self.SubmanifestInfoDir(self.path_prefix) |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1051 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1052 | def SubmanifestInfoDir(self, submanifest_path, object_path=""): |
| 1053 | """Return the path to submanifest-specific info for a submanifest. |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 1054 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1055 | Return the full path of the directory in which to put per-manifest |
| 1056 | objects. |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 1057 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1058 | Args: |
| 1059 | submanifest_path: a string, the path of the submanifest, relative to |
| 1060 | the outermost topdir. If empty, then repodir is returned. |
| 1061 | object_path: a string, relative path to append to the submanifest |
| 1062 | info directory path. |
| 1063 | """ |
| 1064 | if submanifest_path: |
| 1065 | return os.path.join( |
| 1066 | self.repodir, SUBMANIFEST_DIR, submanifest_path, object_path |
| 1067 | ) |
| 1068 | else: |
| 1069 | return os.path.join(self.repodir, object_path) |
Raman Tenneti | 1c3f57e | 2021-05-04 12:32:13 -0700 | [diff] [blame] | 1070 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1071 | def SubmanifestProject(self, submanifest_path): |
| 1072 | """Return a manifestProject for a submanifest.""" |
| 1073 | subdir = self.SubmanifestInfoDir(submanifest_path) |
| 1074 | mp = ManifestProject( |
| 1075 | self, |
| 1076 | "manifests", |
| 1077 | gitdir=os.path.join(subdir, "manifests.git"), |
| 1078 | worktree=os.path.join(subdir, "manifests"), |
| 1079 | ) |
| 1080 | return mp |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 1081 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1082 | def GetDefaultGroupsStr(self, with_platform=True): |
| 1083 | """Returns the default group string to use. |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 1084 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1085 | Args: |
| 1086 | with_platform: a boolean, whether to include the group for the |
| 1087 | underlying platform. |
| 1088 | """ |
| 1089 | groups = ",".join(self.default_groups or ["default"]) |
| 1090 | if with_platform: |
| 1091 | groups += f",platform-{platform.system().lower()}" |
| 1092 | return groups |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 1093 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1094 | def GetGroupsStr(self): |
| 1095 | """Returns the manifest group string that should be synced.""" |
| 1096 | return ( |
| 1097 | self.manifestProject.manifest_groups or self.GetDefaultGroupsStr() |
| 1098 | ) |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 1099 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1100 | def Unload(self): |
| 1101 | """Unload the manifest. |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 1102 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1103 | If the manifest files have been changed since Load() was called, this |
| 1104 | will cause the new/updated manifest to be used. |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 1105 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1106 | """ |
| 1107 | self._loaded = False |
| 1108 | self._projects = {} |
| 1109 | self._paths = {} |
| 1110 | self._remotes = {} |
| 1111 | self._default = None |
| 1112 | self._submanifests = {} |
| 1113 | self._repo_hooks_project = None |
| 1114 | self._superproject = None |
| 1115 | self._contactinfo = ContactInfo(Wrapper().BUG_URL) |
| 1116 | self._notice = None |
| 1117 | self.branch = None |
| 1118 | self._manifest_server = None |
Mike Frysinger | 23411d3 | 2020-09-02 04:31:10 -0400 | [diff] [blame] | 1119 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1120 | def Load(self): |
| 1121 | """Read the manifest into memory.""" |
| 1122 | # Do not expose internal arguments. |
| 1123 | self._Load() |
Shawn O. Pearce | c7a4eef | 2009-03-05 10:32:38 -0800 | [diff] [blame] | 1124 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1125 | def _Load(self, initial_client=None, submanifest_depth=0): |
| 1126 | if submanifest_depth > MAX_SUBMANIFEST_DEPTH: |
| 1127 | raise ManifestParseError( |
| 1128 | "maximum submanifest depth %d exceeded." % MAX_SUBMANIFEST_DEPTH |
| 1129 | ) |
| 1130 | if not self._loaded: |
| 1131 | if self._outer_client and self._outer_client != self: |
| 1132 | # This will load all clients. |
| 1133 | self._outer_client._Load(initial_client=self) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1134 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1135 | savedManifestFile = self.manifestFile |
| 1136 | override = self._outer_client.manifestFileOverrides.get( |
| 1137 | self.path_prefix |
| 1138 | ) |
| 1139 | if override: |
| 1140 | self.manifestFile = override |
Mike Frysinger | 1d00a7e | 2021-12-21 00:40:31 -0500 | [diff] [blame] | 1141 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1142 | try: |
| 1143 | m = self.manifestProject |
| 1144 | b = m.GetBranch(m.CurrentBranch).merge |
| 1145 | if b is not None and b.startswith(R_HEADS): |
| 1146 | b = b[len(R_HEADS) :] |
| 1147 | self.branch = b |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1148 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1149 | parent_groups = self.parent_groups |
| 1150 | if self.path_prefix: |
| 1151 | parent_groups = ( |
| 1152 | f"{SUBMANIFEST_GROUP_PREFIX}:path:" |
| 1153 | f"{self.path_prefix},{parent_groups}" |
| 1154 | ) |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 1155 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1156 | # The manifestFile was specified by the user which is why we |
| 1157 | # allow include paths to point anywhere. |
| 1158 | nodes = [] |
| 1159 | nodes.append( |
| 1160 | self._ParseManifestXml( |
| 1161 | self.manifestFile, |
| 1162 | self.manifestProject.worktree, |
| 1163 | parent_groups=parent_groups, |
| 1164 | restrict_includes=False, |
| 1165 | ) |
| 1166 | ) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1167 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1168 | if self._load_local_manifests and self.local_manifests: |
| 1169 | try: |
| 1170 | for local_file in sorted( |
| 1171 | platform_utils.listdir(self.local_manifests) |
| 1172 | ): |
| 1173 | if local_file.endswith(".xml"): |
| 1174 | local = os.path.join( |
| 1175 | self.local_manifests, local_file |
| 1176 | ) |
| 1177 | # Since local manifests are entirely managed by |
| 1178 | # the user, allow them to point anywhere the |
| 1179 | # user wants. |
| 1180 | local_group = ( |
| 1181 | f"{LOCAL_MANIFEST_GROUP_PREFIX}:" |
| 1182 | f"{local_file[:-4]}" |
| 1183 | ) |
| 1184 | nodes.append( |
| 1185 | self._ParseManifestXml( |
| 1186 | local, |
| 1187 | self.subdir, |
| 1188 | parent_groups=( |
| 1189 | f"{local_group},{parent_groups}" |
| 1190 | ), |
| 1191 | restrict_includes=False, |
| 1192 | ) |
| 1193 | ) |
| 1194 | except OSError: |
| 1195 | pass |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 1196 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1197 | try: |
| 1198 | self._ParseManifest(nodes) |
| 1199 | except ManifestParseError as e: |
| 1200 | # There was a problem parsing, unload ourselves in case they |
| 1201 | # catch this error and try again later, we will show the |
| 1202 | # correct error |
| 1203 | self.Unload() |
| 1204 | raise e |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 1205 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1206 | if self.IsMirror: |
| 1207 | self._AddMetaProjectMirror(self.repoProject) |
| 1208 | self._AddMetaProjectMirror(self.manifestProject) |
LaMont Jones | a2ff20d | 2022-04-07 16:49:06 +0000 | [diff] [blame] | 1209 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1210 | self._loaded = True |
| 1211 | finally: |
| 1212 | if override: |
| 1213 | self.manifestFile = savedManifestFile |
LaMont Jones | a2ff20d | 2022-04-07 16:49:06 +0000 | [diff] [blame] | 1214 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1215 | # Now that we have loaded this manifest, load any submanifests as |
| 1216 | # well. We need to do this after self._loaded is set to avoid |
| 1217 | # looping. |
| 1218 | for name in self._submanifests: |
| 1219 | tree = self._submanifests[name] |
| 1220 | tree.ToSubmanifestSpec() |
| 1221 | present = os.path.exists( |
| 1222 | os.path.join(self.subdir, MANIFEST_FILE_NAME) |
| 1223 | ) |
| 1224 | if present and tree.present and not tree.repo_client: |
| 1225 | if initial_client and initial_client.topdir == self.topdir: |
| 1226 | tree.repo_client = self |
| 1227 | tree.present = present |
| 1228 | elif not os.path.exists(self.subdir): |
| 1229 | tree.present = False |
| 1230 | if present and tree.present: |
| 1231 | tree.repo_client._Load( |
| 1232 | initial_client=initial_client, |
| 1233 | submanifest_depth=submanifest_depth + 1, |
| 1234 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1235 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1236 | def _ParseManifestXml( |
Shuchuan Zeng | 3e3340d | 2023-04-18 10:36:50 +0800 | [diff] [blame] | 1237 | self, |
| 1238 | path, |
| 1239 | include_root, |
| 1240 | parent_groups="", |
| 1241 | restrict_includes=True, |
| 1242 | parent_node=None, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1243 | ): |
| 1244 | """Parse a manifest XML and return the computed nodes. |
LaMont Jones | a2ff20d | 2022-04-07 16:49:06 +0000 | [diff] [blame] | 1245 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1246 | Args: |
| 1247 | path: The XML file to read & parse. |
| 1248 | include_root: The path to interpret include "name"s relative to. |
| 1249 | parent_groups: The groups to apply to this projects. |
| 1250 | restrict_includes: Whether to constrain the "name" attribute of |
| 1251 | includes. |
Shuchuan Zeng | 3e3340d | 2023-04-18 10:36:50 +0800 | [diff] [blame] | 1252 | parent_node: The parent include node, to apply attribute to this |
| 1253 | projects. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1254 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1255 | Returns: |
| 1256 | List of XML nodes. |
| 1257 | """ |
| 1258 | try: |
| 1259 | root = xml.dom.minidom.parse(path) |
| 1260 | except (OSError, xml.parsers.expat.ExpatError) as e: |
| 1261 | raise ManifestParseError( |
| 1262 | "error parsing manifest %s: %s" % (path, e) |
| 1263 | ) |
David Pursehouse | 2d5a0df | 2012-11-13 02:50:36 +0900 | [diff] [blame] | 1264 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1265 | if not root or not root.childNodes: |
| 1266 | raise ManifestParseError("no root node in %s" % (path,)) |
Shawn O. Pearce | 5cc6679 | 2008-10-23 16:19:27 -0700 | [diff] [blame] | 1267 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1268 | for manifest in root.childNodes: |
Chris Allen | 7393f6b | 2023-10-20 16:35:39 +0100 | [diff] [blame] | 1269 | if ( |
| 1270 | manifest.nodeType == manifest.ELEMENT_NODE |
| 1271 | and manifest.nodeName == "manifest" |
| 1272 | ): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1273 | break |
| 1274 | else: |
| 1275 | raise ManifestParseError("no <manifest> in %s" % (path,)) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 1276 | |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1277 | nodes = [] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1278 | for node in manifest.childNodes: |
| 1279 | if node.nodeName == "include": |
| 1280 | name = self._reqatt(node, "name") |
| 1281 | if restrict_includes: |
| 1282 | msg = self._CheckLocalPath(name) |
| 1283 | if msg: |
| 1284 | raise ManifestInvalidPathError( |
| 1285 | '<include> invalid "name": %s: %s' % (name, msg) |
| 1286 | ) |
| 1287 | include_groups = "" |
| 1288 | if parent_groups: |
| 1289 | include_groups = parent_groups |
| 1290 | if node.hasAttribute("groups"): |
| 1291 | include_groups = ( |
| 1292 | node.getAttribute("groups") + "," + include_groups |
| 1293 | ) |
| 1294 | fp = os.path.join(include_root, name) |
| 1295 | if not os.path.isfile(fp): |
| 1296 | raise ManifestParseError( |
| 1297 | "include [%s/]%s doesn't exist or isn't a file" |
| 1298 | % (include_root, name) |
| 1299 | ) |
| 1300 | try: |
| 1301 | nodes.extend( |
Shuchuan Zeng | 3e3340d | 2023-04-18 10:36:50 +0800 | [diff] [blame] | 1302 | self._ParseManifestXml( |
| 1303 | fp, include_root, include_groups, parent_node=node |
| 1304 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1305 | ) |
| 1306 | # should isolate this to the exact exception, but that's |
| 1307 | # tricky. actual parsing implementation may vary. |
| 1308 | except ( |
| 1309 | KeyboardInterrupt, |
| 1310 | RuntimeError, |
| 1311 | SystemExit, |
| 1312 | ManifestParseError, |
| 1313 | ): |
| 1314 | raise |
| 1315 | except Exception as e: |
| 1316 | raise ManifestParseError( |
| 1317 | "failed parsing included manifest %s: %s" % (name, e) |
| 1318 | ) |
| 1319 | else: |
| 1320 | if parent_groups and node.nodeName == "project": |
| 1321 | nodeGroups = parent_groups |
| 1322 | if node.hasAttribute("groups"): |
| 1323 | nodeGroups = ( |
| 1324 | node.getAttribute("groups") + "," + nodeGroups |
| 1325 | ) |
| 1326 | node.setAttribute("groups", nodeGroups) |
Shuchuan Zeng | 3e3340d | 2023-04-18 10:36:50 +0800 | [diff] [blame] | 1327 | if ( |
| 1328 | parent_node |
| 1329 | and node.nodeName == "project" |
| 1330 | and not node.hasAttribute("revision") |
| 1331 | ): |
| 1332 | node.setAttribute( |
| 1333 | "revision", parent_node.getAttribute("revision") |
| 1334 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1335 | nodes.append(node) |
| 1336 | return nodes |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1337 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1338 | def _ParseManifest(self, node_list): |
| 1339 | for node in itertools.chain(*node_list): |
| 1340 | if node.nodeName == "remote": |
| 1341 | remote = self._ParseRemote(node) |
| 1342 | if remote: |
| 1343 | if remote.name in self._remotes: |
| 1344 | if remote != self._remotes[remote.name]: |
| 1345 | raise ManifestParseError( |
| 1346 | "remote %s already exists with different " |
| 1347 | "attributes" % (remote.name) |
| 1348 | ) |
| 1349 | else: |
| 1350 | self._remotes[remote.name] = remote |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1351 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1352 | for node in itertools.chain(*node_list): |
| 1353 | if node.nodeName == "default": |
| 1354 | new_default = self._ParseDefault(node) |
| 1355 | emptyDefault = ( |
| 1356 | not node.hasAttributes() and not node.hasChildNodes() |
| 1357 | ) |
| 1358 | if self._default is None: |
| 1359 | self._default = new_default |
| 1360 | elif not emptyDefault and new_default != self._default: |
| 1361 | raise ManifestParseError( |
| 1362 | "duplicate default in %s" % (self.manifestFile) |
| 1363 | ) |
LaMont Jones | b90a422 | 2022-04-14 15:00:09 +0000 | [diff] [blame] | 1364 | |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 1365 | if self._default is None: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1366 | self._default = _Default() |
Julien Campergue | 7487992 | 2013-10-09 14:38:46 +0200 | [diff] [blame] | 1367 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1368 | submanifest_paths = set() |
| 1369 | for node in itertools.chain(*node_list): |
| 1370 | if node.nodeName == "submanifest": |
| 1371 | submanifest = self._ParseSubmanifest(node) |
| 1372 | if submanifest: |
| 1373 | if submanifest.name in self._submanifests: |
| 1374 | if submanifest != self._submanifests[submanifest.name]: |
| 1375 | raise ManifestParseError( |
| 1376 | "submanifest %s already exists with different " |
| 1377 | "attributes" % (submanifest.name) |
| 1378 | ) |
| 1379 | else: |
| 1380 | self._submanifests[submanifest.name] = submanifest |
| 1381 | submanifest_paths.add(submanifest.relpath) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1382 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1383 | for node in itertools.chain(*node_list): |
| 1384 | if node.nodeName == "notice": |
| 1385 | if self._notice is not None: |
| 1386 | raise ManifestParseError( |
| 1387 | "duplicate notice in %s" % (self.manifestFile) |
| 1388 | ) |
| 1389 | self._notice = self._ParseNotice(node) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1390 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1391 | for node in itertools.chain(*node_list): |
| 1392 | if node.nodeName == "manifest-server": |
| 1393 | url = self._reqatt(node, "url") |
| 1394 | if self._manifest_server is not None: |
| 1395 | raise ManifestParseError( |
| 1396 | "duplicate manifest-server in %s" % (self.manifestFile) |
| 1397 | ) |
| 1398 | self._manifest_server = url |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1399 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1400 | def recursively_add_projects(project): |
| 1401 | projects = self._projects.setdefault(project.name, []) |
| 1402 | if project.relpath is None: |
| 1403 | raise ManifestParseError( |
| 1404 | "missing path for %s in %s" |
| 1405 | % (project.name, self.manifestFile) |
| 1406 | ) |
| 1407 | if project.relpath in self._paths: |
| 1408 | raise ManifestParseError( |
| 1409 | "duplicate path %s in %s" |
| 1410 | % (project.relpath, self.manifestFile) |
| 1411 | ) |
| 1412 | for tree in submanifest_paths: |
| 1413 | if project.relpath.startswith(tree): |
| 1414 | raise ManifestParseError( |
| 1415 | "project %s conflicts with submanifest path %s" |
| 1416 | % (project.relpath, tree) |
| 1417 | ) |
| 1418 | self._paths[project.relpath] = project |
| 1419 | projects.append(project) |
| 1420 | for subproject in project.subprojects: |
| 1421 | recursively_add_projects(subproject) |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 1422 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1423 | repo_hooks_project = None |
| 1424 | enabled_repo_hooks = None |
| 1425 | for node in itertools.chain(*node_list): |
| 1426 | if node.nodeName == "project": |
| 1427 | project = self._ParseProject(node) |
| 1428 | recursively_add_projects(project) |
| 1429 | if node.nodeName == "extend-project": |
| 1430 | name = self._reqatt(node, "name") |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1431 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1432 | if name not in self._projects: |
| 1433 | raise ManifestParseError( |
| 1434 | "extend-project element specifies non-existent " |
| 1435 | "project: %s" % name |
| 1436 | ) |
| 1437 | |
| 1438 | path = node.getAttribute("path") |
| 1439 | dest_path = node.getAttribute("dest-path") |
| 1440 | groups = node.getAttribute("groups") |
| 1441 | if groups: |
| 1442 | groups = self._ParseList(groups) |
| 1443 | revision = node.getAttribute("revision") |
| 1444 | remote_name = node.getAttribute("remote") |
| 1445 | if not remote_name: |
| 1446 | remote = self._default.remote |
| 1447 | else: |
| 1448 | remote = self._get_remote(node) |
| 1449 | dest_branch = node.getAttribute("dest-branch") |
| 1450 | upstream = node.getAttribute("upstream") |
| 1451 | |
| 1452 | named_projects = self._projects[name] |
| 1453 | if dest_path and not path and len(named_projects) > 1: |
| 1454 | raise ManifestParseError( |
| 1455 | "extend-project cannot use dest-path when " |
| 1456 | "matching multiple projects: %s" % name |
| 1457 | ) |
| 1458 | for p in self._projects[name]: |
| 1459 | if path and p.relpath != path: |
| 1460 | continue |
| 1461 | if groups: |
| 1462 | p.groups.extend(groups) |
| 1463 | if revision: |
| 1464 | p.SetRevision(revision) |
| 1465 | |
| 1466 | if remote_name: |
| 1467 | p.remote = remote.ToRemoteSpec(name) |
| 1468 | if dest_branch: |
| 1469 | p.dest_branch = dest_branch |
| 1470 | if upstream: |
| 1471 | p.upstream = upstream |
| 1472 | |
| 1473 | if dest_path: |
| 1474 | del self._paths[p.relpath] |
| 1475 | ( |
| 1476 | relpath, |
| 1477 | worktree, |
| 1478 | gitdir, |
| 1479 | objdir, |
| 1480 | _, |
| 1481 | ) = self.GetProjectPaths(name, dest_path, remote.name) |
| 1482 | p.UpdatePaths(relpath, worktree, gitdir, objdir) |
| 1483 | self._paths[p.relpath] = p |
| 1484 | |
| 1485 | if node.nodeName == "repo-hooks": |
| 1486 | # Only one project can be the hooks project |
| 1487 | if repo_hooks_project is not None: |
| 1488 | raise ManifestParseError( |
| 1489 | "duplicate repo-hooks in %s" % (self.manifestFile) |
| 1490 | ) |
| 1491 | |
| 1492 | # Get the name of the project and the (space-separated) list of |
| 1493 | # enabled. |
| 1494 | repo_hooks_project = self._reqatt(node, "in-project") |
| 1495 | enabled_repo_hooks = self._ParseList( |
| 1496 | self._reqatt(node, "enabled-list") |
| 1497 | ) |
| 1498 | if node.nodeName == "superproject": |
| 1499 | name = self._reqatt(node, "name") |
| 1500 | # There can only be one superproject. |
| 1501 | if self._superproject: |
| 1502 | raise ManifestParseError( |
| 1503 | "duplicate superproject in %s" % (self.manifestFile) |
| 1504 | ) |
| 1505 | remote_name = node.getAttribute("remote") |
| 1506 | if not remote_name: |
| 1507 | remote = self._default.remote |
| 1508 | else: |
| 1509 | remote = self._get_remote(node) |
| 1510 | if remote is None: |
| 1511 | raise ManifestParseError( |
| 1512 | "no remote for superproject %s within %s" |
| 1513 | % (name, self.manifestFile) |
| 1514 | ) |
| 1515 | revision = node.getAttribute("revision") or remote.revision |
| 1516 | if not revision: |
| 1517 | revision = self._default.revisionExpr |
| 1518 | if not revision: |
| 1519 | raise ManifestParseError( |
| 1520 | "no revision for superproject %s within %s" |
| 1521 | % (name, self.manifestFile) |
| 1522 | ) |
| 1523 | self._superproject = Superproject( |
| 1524 | self, |
| 1525 | name=name, |
| 1526 | remote=remote.ToRemoteSpec(name), |
| 1527 | revision=revision, |
| 1528 | ) |
| 1529 | if node.nodeName == "contactinfo": |
| 1530 | bugurl = self._reqatt(node, "bugurl") |
| 1531 | # This element can be repeated, later entries will clobber |
| 1532 | # earlier ones. |
| 1533 | self._contactinfo = ContactInfo(bugurl) |
| 1534 | |
| 1535 | if node.nodeName == "remove-project": |
Fredrik de Groot | be71c2f | 2023-05-31 16:56:34 +0200 | [diff] [blame] | 1536 | name = node.getAttribute("name") |
| 1537 | path = node.getAttribute("path") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1538 | |
Fredrik de Groot | be71c2f | 2023-05-31 16:56:34 +0200 | [diff] [blame] | 1539 | # Name or path needed. |
| 1540 | if not name and not path: |
| 1541 | raise ManifestParseError( |
| 1542 | "remove-project must have name and/or path" |
| 1543 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1544 | |
Fredrik de Groot | be71c2f | 2023-05-31 16:56:34 +0200 | [diff] [blame] | 1545 | removed_project = "" |
| 1546 | |
| 1547 | # Find and remove projects based on name and/or path. |
| 1548 | for projname, projects in list(self._projects.items()): |
| 1549 | for p in projects: |
| 1550 | if name == projname and not path: |
| 1551 | del self._paths[p.relpath] |
| 1552 | if not removed_project: |
| 1553 | del self._projects[name] |
| 1554 | removed_project = name |
| 1555 | elif path == p.relpath and ( |
| 1556 | name == projname or not name |
| 1557 | ): |
| 1558 | self._projects[projname].remove(p) |
| 1559 | del self._paths[p.relpath] |
| 1560 | removed_project = p.name |
| 1561 | |
| 1562 | # If the manifest removes the hooks project, treat it as if |
| 1563 | # it deleted the repo-hooks element too. |
| 1564 | if ( |
| 1565 | removed_project |
| 1566 | and removed_project not in self._projects |
| 1567 | and repo_hooks_project == removed_project |
| 1568 | ): |
| 1569 | repo_hooks_project = None |
| 1570 | |
| 1571 | if not removed_project and not XmlBool(node, "optional", False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1572 | raise ManifestParseError( |
| 1573 | "remove-project element specifies non-existent " |
Fredrik de Groot | be71c2f | 2023-05-31 16:56:34 +0200 | [diff] [blame] | 1574 | "project: %s" % node.toxml() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1575 | ) |
| 1576 | |
| 1577 | # Store repo hooks project information. |
| 1578 | if repo_hooks_project: |
| 1579 | # Store a reference to the Project. |
| 1580 | try: |
| 1581 | repo_hooks_projects = self._projects[repo_hooks_project] |
| 1582 | except KeyError: |
| 1583 | raise ManifestParseError( |
| 1584 | "project %s not found for repo-hooks" % (repo_hooks_project) |
| 1585 | ) |
| 1586 | |
| 1587 | if len(repo_hooks_projects) != 1: |
| 1588 | raise ManifestParseError( |
| 1589 | "internal error parsing repo-hooks in %s" |
| 1590 | % (self.manifestFile) |
| 1591 | ) |
| 1592 | self._repo_hooks_project = repo_hooks_projects[0] |
| 1593 | # Store the enabled hooks in the Project object. |
| 1594 | self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks |
| 1595 | |
| 1596 | def _AddMetaProjectMirror(self, m): |
| 1597 | name = None |
| 1598 | m_url = m.GetRemote().url |
| 1599 | if m_url.endswith("/.git"): |
| 1600 | raise ManifestParseError("refusing to mirror %s" % m_url) |
| 1601 | |
| 1602 | if self._default and self._default.remote: |
| 1603 | url = self._default.remote.resolvedFetchUrl |
| 1604 | if not url.endswith("/"): |
| 1605 | url += "/" |
| 1606 | if m_url.startswith(url): |
| 1607 | remote = self._default.remote |
| 1608 | name = m_url[len(url) :] |
| 1609 | |
| 1610 | if name is None: |
| 1611 | s = m_url.rindex("/") + 1 |
| 1612 | manifestUrl = self.manifestProject.config.GetString( |
| 1613 | "remote.origin.url" |
| 1614 | ) |
| 1615 | remote = _XmlRemote( |
| 1616 | "origin", fetch=m_url[:s], manifestUrl=manifestUrl |
| 1617 | ) |
| 1618 | name = m_url[s:] |
| 1619 | |
| 1620 | if name.endswith(".git"): |
| 1621 | name = name[:-4] |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 1622 | |
| 1623 | if name not in self._projects: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1624 | m.PreSync() |
| 1625 | gitdir = os.path.join(self.topdir, "%s.git" % name) |
| 1626 | project = Project( |
| 1627 | manifest=self, |
| 1628 | name=name, |
| 1629 | remote=remote.ToRemoteSpec(name), |
| 1630 | gitdir=gitdir, |
| 1631 | objdir=gitdir, |
| 1632 | worktree=None, |
| 1633 | relpath=name or None, |
| 1634 | revisionExpr=m.revisionExpr, |
| 1635 | revisionId=None, |
| 1636 | ) |
| 1637 | self._projects[project.name] = [project] |
| 1638 | self._paths[project.relpath] = project |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 1639 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1640 | def _ParseRemote(self, node): |
| 1641 | """ |
| 1642 | reads a <remote> element from the manifest file |
| 1643 | """ |
| 1644 | name = self._reqatt(node, "name") |
| 1645 | alias = node.getAttribute("alias") |
| 1646 | if alias == "": |
| 1647 | alias = None |
| 1648 | fetch = self._reqatt(node, "fetch") |
| 1649 | pushUrl = node.getAttribute("pushurl") |
| 1650 | if pushUrl == "": |
| 1651 | pushUrl = None |
| 1652 | review = node.getAttribute("review") |
| 1653 | if review == "": |
| 1654 | review = None |
| 1655 | revision = node.getAttribute("revision") |
| 1656 | if revision == "": |
| 1657 | revision = None |
| 1658 | manifestUrl = self.manifestProject.config.GetString("remote.origin.url") |
| 1659 | |
| 1660 | remote = _XmlRemote( |
| 1661 | name, alias, fetch, pushUrl, manifestUrl, review, revision |
| 1662 | ) |
| 1663 | |
| 1664 | for n in node.childNodes: |
| 1665 | if n.nodeName == "annotation": |
| 1666 | self._ParseAnnotation(remote, n) |
| 1667 | |
| 1668 | return remote |
| 1669 | |
| 1670 | def _ParseDefault(self, node): |
| 1671 | """ |
| 1672 | reads a <default> element from the manifest file |
| 1673 | """ |
| 1674 | d = _Default() |
| 1675 | d.remote = self._get_remote(node) |
| 1676 | d.revisionExpr = node.getAttribute("revision") |
| 1677 | if d.revisionExpr == "": |
| 1678 | d.revisionExpr = None |
| 1679 | |
| 1680 | d.destBranchExpr = node.getAttribute("dest-branch") or None |
| 1681 | d.upstreamExpr = node.getAttribute("upstream") or None |
| 1682 | |
| 1683 | d.sync_j = XmlInt(node, "sync-j", None) |
| 1684 | if d.sync_j is not None and d.sync_j <= 0: |
| 1685 | raise ManifestParseError( |
| 1686 | '%s: sync-j must be greater than 0, not "%s"' |
| 1687 | % (self.manifestFile, d.sync_j) |
| 1688 | ) |
| 1689 | |
| 1690 | d.sync_c = XmlBool(node, "sync-c", False) |
| 1691 | d.sync_s = XmlBool(node, "sync-s", False) |
| 1692 | d.sync_tags = XmlBool(node, "sync-tags", True) |
| 1693 | return d |
| 1694 | |
| 1695 | def _ParseNotice(self, node): |
| 1696 | """ |
| 1697 | reads a <notice> element from the manifest file |
| 1698 | |
| 1699 | The <notice> element is distinct from other tags in the XML in that the |
| 1700 | data is conveyed between the start and end tag (it's not an |
| 1701 | empty-element tag). |
| 1702 | |
| 1703 | The white space (carriage returns, indentation) for the notice element |
| 1704 | is relevant and is parsed in a way that is based on how python |
| 1705 | docstrings work. In fact, the code is remarkably similar to here: |
| 1706 | http://www.python.org/dev/peps/pep-0257/ |
| 1707 | """ |
| 1708 | # Get the data out of the node... |
| 1709 | notice = node.childNodes[0].data |
| 1710 | |
| 1711 | # Figure out minimum indentation, skipping the first line (the same line |
| 1712 | # as the <notice> tag)... |
| 1713 | minIndent = sys.maxsize |
| 1714 | lines = notice.splitlines() |
| 1715 | for line in lines[1:]: |
| 1716 | lstrippedLine = line.lstrip() |
| 1717 | if lstrippedLine: |
| 1718 | indent = len(line) - len(lstrippedLine) |
| 1719 | minIndent = min(indent, minIndent) |
| 1720 | |
| 1721 | # Strip leading / trailing blank lines and also indentation. |
| 1722 | cleanLines = [lines[0].strip()] |
| 1723 | for line in lines[1:]: |
| 1724 | cleanLines.append(line[minIndent:].rstrip()) |
| 1725 | |
| 1726 | # Clear completely blank lines from front and back... |
| 1727 | while cleanLines and not cleanLines[0]: |
| 1728 | del cleanLines[0] |
| 1729 | while cleanLines and not cleanLines[-1]: |
| 1730 | del cleanLines[-1] |
| 1731 | |
| 1732 | return "\n".join(cleanLines) |
| 1733 | |
| 1734 | def _ParseSubmanifest(self, node): |
| 1735 | """Reads a <submanifest> element from the manifest file.""" |
| 1736 | name = self._reqatt(node, "name") |
| 1737 | remote = node.getAttribute("remote") |
| 1738 | if remote == "": |
| 1739 | remote = None |
| 1740 | project = node.getAttribute("project") |
| 1741 | if project == "": |
| 1742 | project = None |
| 1743 | revision = node.getAttribute("revision") |
| 1744 | if revision == "": |
| 1745 | revision = None |
| 1746 | manifestName = node.getAttribute("manifest-name") |
| 1747 | if manifestName == "": |
| 1748 | manifestName = None |
| 1749 | groups = "" |
| 1750 | if node.hasAttribute("groups"): |
| 1751 | groups = node.getAttribute("groups") |
| 1752 | groups = self._ParseList(groups) |
| 1753 | default_groups = self._ParseList(node.getAttribute("default-groups")) |
| 1754 | path = node.getAttribute("path") |
| 1755 | if path == "": |
| 1756 | path = None |
| 1757 | if revision: |
| 1758 | msg = self._CheckLocalPath(revision.split("/")[-1]) |
| 1759 | if msg: |
| 1760 | raise ManifestInvalidPathError( |
| 1761 | '<submanifest> invalid "revision": %s: %s' |
| 1762 | % (revision, msg) |
| 1763 | ) |
| 1764 | else: |
| 1765 | msg = self._CheckLocalPath(name) |
| 1766 | if msg: |
| 1767 | raise ManifestInvalidPathError( |
| 1768 | '<submanifest> invalid "name": %s: %s' % (name, msg) |
| 1769 | ) |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 1770 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1771 | msg = self._CheckLocalPath(path) |
| 1772 | if msg: |
| 1773 | raise ManifestInvalidPathError( |
| 1774 | '<submanifest> invalid "path": %s: %s' % (path, msg) |
| 1775 | ) |
Josh Triplett | 884a387 | 2014-06-12 14:57:29 -0700 | [diff] [blame] | 1776 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1777 | submanifest = _XmlSubmanifest( |
| 1778 | name, |
| 1779 | remote, |
| 1780 | project, |
| 1781 | revision, |
| 1782 | manifestName, |
| 1783 | groups, |
| 1784 | default_groups, |
| 1785 | path, |
| 1786 | self, |
| 1787 | ) |
Michael Kelly | 2f3c331 | 2020-07-21 19:40:38 -0700 | [diff] [blame] | 1788 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1789 | for n in node.childNodes: |
| 1790 | if n.nodeName == "annotation": |
| 1791 | self._ParseAnnotation(submanifest, n) |
Michael Kelly | 2f3c331 | 2020-07-21 19:40:38 -0700 | [diff] [blame] | 1792 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1793 | return submanifest |
Michael Kelly | 37c21c2 | 2020-06-13 02:10:40 -0700 | [diff] [blame] | 1794 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1795 | def _JoinName(self, parent_name, name): |
| 1796 | return os.path.join(parent_name, name) |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 1797 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1798 | def _UnjoinName(self, parent_name, name): |
| 1799 | return os.path.relpath(name, parent_name) |
| 1800 | |
| 1801 | def _ParseProject(self, node, parent=None, **extra_proj_attrs): |
| 1802 | """ |
| 1803 | reads a <project> element from the manifest file |
| 1804 | """ |
| 1805 | name = self._reqatt(node, "name") |
| 1806 | msg = self._CheckLocalPath(name, dir_ok=True) |
| 1807 | if msg: |
| 1808 | raise ManifestInvalidPathError( |
| 1809 | '<project> invalid "name": %s: %s' % (name, msg) |
| 1810 | ) |
| 1811 | if parent: |
| 1812 | name = self._JoinName(parent.name, name) |
| 1813 | |
| 1814 | remote = self._get_remote(node) |
Raman Tenneti | 1bb4fb2 | 2021-01-07 16:50:45 -0800 | [diff] [blame] | 1815 | if remote is None: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1816 | remote = self._default.remote |
| 1817 | if remote is None: |
| 1818 | raise ManifestParseError( |
| 1819 | "no remote for project %s within %s" % (name, self.manifestFile) |
| 1820 | ) |
Raman Tenneti | 993af5e | 2021-05-12 12:00:31 -0700 | [diff] [blame] | 1821 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1822 | revisionExpr = node.getAttribute("revision") or remote.revision |
| 1823 | if not revisionExpr: |
| 1824 | revisionExpr = self._default.revisionExpr |
| 1825 | if not revisionExpr: |
| 1826 | raise ManifestParseError( |
| 1827 | "no revision for project %s within %s" |
| 1828 | % (name, self.manifestFile) |
| 1829 | ) |
David James | b8433df | 2014-01-30 10:11:17 -0800 | [diff] [blame] | 1830 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1831 | path = node.getAttribute("path") |
| 1832 | if not path: |
| 1833 | path = name |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1834 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1835 | # NB: The "." project is handled specially in |
| 1836 | # Project.Sync_LocalHalf. |
| 1837 | msg = self._CheckLocalPath(path, dir_ok=True, cwd_dot_ok=True) |
| 1838 | if msg: |
| 1839 | raise ManifestInvalidPathError( |
| 1840 | '<project> invalid "path": %s: %s' % (path, msg) |
| 1841 | ) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1842 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1843 | rebase = XmlBool(node, "rebase", True) |
| 1844 | sync_c = XmlBool(node, "sync-c", False) |
| 1845 | sync_s = XmlBool(node, "sync-s", self._default.sync_s) |
| 1846 | sync_tags = XmlBool(node, "sync-tags", self._default.sync_tags) |
Julien Campergue | dd65422 | 2014-01-09 16:21:37 +0100 | [diff] [blame] | 1847 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1848 | clone_depth = XmlInt(node, "clone-depth") |
| 1849 | if clone_depth is not None and clone_depth <= 0: |
| 1850 | raise ManifestParseError( |
| 1851 | '%s: clone-depth must be greater than 0, not "%s"' |
| 1852 | % (self.manifestFile, clone_depth) |
| 1853 | ) |
| 1854 | |
| 1855 | dest_branch = ( |
| 1856 | node.getAttribute("dest-branch") or self._default.destBranchExpr |
| 1857 | ) |
| 1858 | |
| 1859 | upstream = node.getAttribute("upstream") or self._default.upstreamExpr |
| 1860 | |
| 1861 | groups = "" |
| 1862 | if node.hasAttribute("groups"): |
| 1863 | groups = node.getAttribute("groups") |
| 1864 | groups = self._ParseList(groups) |
| 1865 | |
| 1866 | if parent is None: |
| 1867 | ( |
| 1868 | relpath, |
| 1869 | worktree, |
| 1870 | gitdir, |
| 1871 | objdir, |
| 1872 | use_git_worktrees, |
| 1873 | ) = self.GetProjectPaths(name, path, remote.name) |
| 1874 | else: |
| 1875 | use_git_worktrees = False |
| 1876 | relpath, worktree, gitdir, objdir = self.GetSubprojectPaths( |
| 1877 | parent, name, path |
| 1878 | ) |
| 1879 | |
| 1880 | default_groups = ["all", "name:%s" % name, "path:%s" % relpath] |
| 1881 | groups.extend(set(default_groups).difference(groups)) |
| 1882 | |
| 1883 | if self.IsMirror and node.hasAttribute("force-path"): |
| 1884 | if XmlBool(node, "force-path", False): |
| 1885 | gitdir = os.path.join(self.topdir, "%s.git" % path) |
| 1886 | |
| 1887 | project = Project( |
| 1888 | manifest=self, |
| 1889 | name=name, |
| 1890 | remote=remote.ToRemoteSpec(name), |
| 1891 | gitdir=gitdir, |
| 1892 | objdir=objdir, |
| 1893 | worktree=worktree, |
| 1894 | relpath=relpath, |
| 1895 | revisionExpr=revisionExpr, |
| 1896 | revisionId=None, |
| 1897 | rebase=rebase, |
| 1898 | groups=groups, |
| 1899 | sync_c=sync_c, |
| 1900 | sync_s=sync_s, |
| 1901 | sync_tags=sync_tags, |
| 1902 | clone_depth=clone_depth, |
| 1903 | upstream=upstream, |
| 1904 | parent=parent, |
| 1905 | dest_branch=dest_branch, |
| 1906 | use_git_worktrees=use_git_worktrees, |
| 1907 | **extra_proj_attrs, |
| 1908 | ) |
| 1909 | |
| 1910 | for n in node.childNodes: |
| 1911 | if n.nodeName == "copyfile": |
| 1912 | self._ParseCopyFile(project, n) |
| 1913 | if n.nodeName == "linkfile": |
| 1914 | self._ParseLinkFile(project, n) |
| 1915 | if n.nodeName == "annotation": |
| 1916 | self._ParseAnnotation(project, n) |
| 1917 | if n.nodeName == "project": |
| 1918 | project.subprojects.append( |
| 1919 | self._ParseProject(n, parent=project) |
| 1920 | ) |
| 1921 | |
| 1922 | return project |
| 1923 | |
| 1924 | def GetProjectPaths(self, name, path, remote): |
| 1925 | """Return the paths for a project. |
| 1926 | |
| 1927 | Args: |
| 1928 | name: a string, the name of the project. |
| 1929 | path: a string, the path of the project. |
| 1930 | remote: a string, the remote.name of the project. |
| 1931 | |
| 1932 | Returns: |
| 1933 | A tuple of (relpath, worktree, gitdir, objdir, use_git_worktrees) |
| 1934 | for the project with |name| and |path|. |
| 1935 | """ |
| 1936 | # The manifest entries might have trailing slashes. Normalize them to |
| 1937 | # avoid unexpected filesystem behavior since we do string concatenation |
| 1938 | # below. |
| 1939 | path = path.rstrip("/") |
| 1940 | name = name.rstrip("/") |
| 1941 | remote = remote.rstrip("/") |
| 1942 | use_git_worktrees = False |
| 1943 | use_remote_name = self.is_multimanifest |
| 1944 | relpath = path |
| 1945 | if self.IsMirror: |
| 1946 | worktree = None |
| 1947 | gitdir = os.path.join(self.topdir, "%s.git" % name) |
| 1948 | objdir = gitdir |
| 1949 | else: |
| 1950 | if use_remote_name: |
| 1951 | namepath = os.path.join(remote, f"{name}.git") |
| 1952 | else: |
| 1953 | namepath = f"{name}.git" |
| 1954 | worktree = os.path.join(self.topdir, path).replace("\\", "/") |
| 1955 | gitdir = os.path.join(self.subdir, "projects", "%s.git" % path) |
| 1956 | # We allow people to mix git worktrees & non-git worktrees for now. |
| 1957 | # This allows for in situ migration of repo clients. |
| 1958 | if os.path.exists(gitdir) or not self.UseGitWorktrees: |
| 1959 | objdir = os.path.join(self.repodir, "project-objects", namepath) |
| 1960 | else: |
| 1961 | use_git_worktrees = True |
| 1962 | gitdir = os.path.join(self.repodir, "worktrees", namepath) |
| 1963 | objdir = gitdir |
| 1964 | return relpath, worktree, gitdir, objdir, use_git_worktrees |
| 1965 | |
| 1966 | def GetProjectsWithName(self, name, all_manifests=False): |
| 1967 | """All projects with |name|. |
| 1968 | |
| 1969 | Args: |
| 1970 | name: a string, the name of the project. |
| 1971 | all_manifests: a boolean, if True, then all manifests are searched. |
| 1972 | If False, then only this manifest is searched. |
| 1973 | |
| 1974 | Returns: |
| 1975 | A list of Project instances with name |name|. |
| 1976 | """ |
| 1977 | if all_manifests: |
| 1978 | return list( |
| 1979 | itertools.chain.from_iterable( |
| 1980 | x._projects.get(name, []) for x in self.all_manifests |
| 1981 | ) |
| 1982 | ) |
| 1983 | return self._projects.get(name, []) |
| 1984 | |
| 1985 | def GetSubprojectName(self, parent, submodule_path): |
| 1986 | return os.path.join(parent.name, submodule_path) |
| 1987 | |
| 1988 | def _JoinRelpath(self, parent_relpath, relpath): |
| 1989 | return os.path.join(parent_relpath, relpath) |
| 1990 | |
| 1991 | def _UnjoinRelpath(self, parent_relpath, relpath): |
| 1992 | return os.path.relpath(relpath, parent_relpath) |
| 1993 | |
| 1994 | def GetSubprojectPaths(self, parent, name, path): |
| 1995 | # The manifest entries might have trailing slashes. Normalize them to |
| 1996 | # avoid unexpected filesystem behavior since we do string concatenation |
| 1997 | # below. |
| 1998 | path = path.rstrip("/") |
| 1999 | name = name.rstrip("/") |
| 2000 | relpath = self._JoinRelpath(parent.relpath, path) |
| 2001 | gitdir = os.path.join(parent.gitdir, "subprojects", "%s.git" % path) |
| 2002 | objdir = os.path.join( |
| 2003 | parent.gitdir, "subproject-objects", "%s.git" % name |
| 2004 | ) |
| 2005 | if self.IsMirror: |
| 2006 | worktree = None |
| 2007 | else: |
| 2008 | worktree = os.path.join(parent.worktree, path).replace("\\", "/") |
| 2009 | return relpath, worktree, gitdir, objdir |
| 2010 | |
| 2011 | @staticmethod |
| 2012 | def _CheckLocalPath(path, dir_ok=False, cwd_dot_ok=False): |
| 2013 | """Verify |path| is reasonable for use in filesystem paths. |
| 2014 | |
| 2015 | Used with <copyfile> & <linkfile> & <project> elements. |
| 2016 | |
| 2017 | This only validates the |path| in isolation: it does not check against |
| 2018 | the current filesystem state. Thus it is suitable as a first-past in a |
| 2019 | parser. |
| 2020 | |
| 2021 | It enforces a number of constraints: |
| 2022 | * No empty paths. |
| 2023 | * No "~" in paths. |
| 2024 | * No Unicode codepoints that filesystems might elide when normalizing. |
| 2025 | * No relative path components like "." or "..". |
| 2026 | * No absolute paths. |
| 2027 | * No ".git" or ".repo*" path components. |
| 2028 | |
| 2029 | Args: |
| 2030 | path: The path name to validate. |
| 2031 | dir_ok: Whether |path| may force a directory (e.g. end in a /). |
| 2032 | cwd_dot_ok: Whether |path| may be just ".". |
| 2033 | |
| 2034 | Returns: |
| 2035 | None if |path| is OK, a failure message otherwise. |
| 2036 | """ |
| 2037 | if not path: |
| 2038 | return "empty paths not allowed" |
| 2039 | |
| 2040 | if "~" in path: |
| 2041 | return "~ not allowed (due to 8.3 filenames on Windows filesystems)" |
| 2042 | |
| 2043 | path_codepoints = set(path) |
| 2044 | |
| 2045 | # Some filesystems (like Apple's HFS+) try to normalize Unicode |
| 2046 | # codepoints which means there are alternative names for ".git". Reject |
| 2047 | # paths with these in it as there shouldn't be any reasonable need for |
| 2048 | # them here. The set of codepoints here was cribbed from jgit's |
| 2049 | # implementation: |
| 2050 | # https://eclipse.googlesource.com/jgit/jgit/+/9110037e3e9461ff4dac22fee84ef3694ed57648/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java#884 |
| 2051 | BAD_CODEPOINTS = { |
| 2052 | "\u200C", # ZERO WIDTH NON-JOINER |
| 2053 | "\u200D", # ZERO WIDTH JOINER |
| 2054 | "\u200E", # LEFT-TO-RIGHT MARK |
| 2055 | "\u200F", # RIGHT-TO-LEFT MARK |
| 2056 | "\u202A", # LEFT-TO-RIGHT EMBEDDING |
| 2057 | "\u202B", # RIGHT-TO-LEFT EMBEDDING |
| 2058 | "\u202C", # POP DIRECTIONAL FORMATTING |
| 2059 | "\u202D", # LEFT-TO-RIGHT OVERRIDE |
| 2060 | "\u202E", # RIGHT-TO-LEFT OVERRIDE |
| 2061 | "\u206A", # INHIBIT SYMMETRIC SWAPPING |
| 2062 | "\u206B", # ACTIVATE SYMMETRIC SWAPPING |
| 2063 | "\u206C", # INHIBIT ARABIC FORM SHAPING |
| 2064 | "\u206D", # ACTIVATE ARABIC FORM SHAPING |
| 2065 | "\u206E", # NATIONAL DIGIT SHAPES |
| 2066 | "\u206F", # NOMINAL DIGIT SHAPES |
| 2067 | "\uFEFF", # ZERO WIDTH NO-BREAK SPACE |
| 2068 | } |
| 2069 | if BAD_CODEPOINTS & path_codepoints: |
| 2070 | # This message is more expansive than reality, but should be fine. |
| 2071 | return "Unicode combining characters not allowed" |
| 2072 | |
| 2073 | # Reject newlines as there shouldn't be any legitmate use for them, |
| 2074 | # they'll be confusing to users, and they can easily break tools that |
| 2075 | # expect to be able to iterate over newline delimited lists. This even |
| 2076 | # applies to our own code like .repo/project.list. |
| 2077 | if {"\r", "\n"} & path_codepoints: |
| 2078 | return "Newlines not allowed" |
| 2079 | |
| 2080 | # Assume paths might be used on case-insensitive filesystems. |
| 2081 | path = path.lower() |
| 2082 | |
| 2083 | # Split up the path by its components. We can't use os.path.sep |
| 2084 | # exclusively as some platforms (like Windows) will convert / to \ and |
| 2085 | # that bypasses all our constructed logic here. Especially since |
| 2086 | # manifest authors only use / in their paths. |
| 2087 | resep = re.compile(r"[/%s]" % re.escape(os.path.sep)) |
| 2088 | # Strip off trailing slashes as those only produce '' elements, and we |
| 2089 | # use parts to look for individual bad components. |
| 2090 | parts = resep.split(path.rstrip("/")) |
| 2091 | |
| 2092 | # Some people use src="." to create stable links to projects. Lets |
| 2093 | # allow that but reject all other uses of "." to keep things simple. |
| 2094 | if not cwd_dot_ok or parts != ["."]: |
| 2095 | for part in set(parts): |
| 2096 | if part in {".", "..", ".git"} or part.startswith(".repo"): |
| 2097 | return "bad component: %s" % (part,) |
| 2098 | |
| 2099 | if not dir_ok and resep.match(path[-1]): |
| 2100 | return "dirs not allowed" |
| 2101 | |
| 2102 | # NB: The two abspath checks here are to handle platforms with multiple |
| 2103 | # filesystem path styles (e.g. Windows). |
| 2104 | norm = os.path.normpath(path) |
| 2105 | if ( |
| 2106 | norm == ".." |
| 2107 | or ( |
| 2108 | len(norm) >= 3 |
| 2109 | and norm.startswith("..") |
| 2110 | and resep.match(norm[0]) |
| 2111 | ) |
| 2112 | or os.path.isabs(norm) |
| 2113 | or norm.startswith("/") |
| 2114 | ): |
| 2115 | return "path cannot be outside" |
| 2116 | |
| 2117 | @classmethod |
| 2118 | def _ValidateFilePaths(cls, element, src, dest): |
| 2119 | """Verify |src| & |dest| are reasonable for <copyfile> & <linkfile>. |
| 2120 | |
| 2121 | We verify the path independent of any filesystem state as we won't have |
| 2122 | a checkout available to compare to. i.e. This is for parsing validation |
| 2123 | purposes only. |
| 2124 | |
| 2125 | We'll do full/live sanity checking before we do the actual filesystem |
| 2126 | modifications in _CopyFile/_LinkFile/etc... |
| 2127 | """ |
| 2128 | # |dest| is the file we write to or symlink we create. |
| 2129 | # It is relative to the top of the repo client checkout. |
| 2130 | msg = cls._CheckLocalPath(dest) |
| 2131 | if msg: |
| 2132 | raise ManifestInvalidPathError( |
| 2133 | '<%s> invalid "dest": %s: %s' % (element, dest, msg) |
| 2134 | ) |
| 2135 | |
| 2136 | # |src| is the file we read from or path we point to for symlinks. |
| 2137 | # It is relative to the top of the git project checkout. |
| 2138 | is_linkfile = element == "linkfile" |
| 2139 | msg = cls._CheckLocalPath( |
| 2140 | src, dir_ok=is_linkfile, cwd_dot_ok=is_linkfile |
| 2141 | ) |
| 2142 | if msg: |
| 2143 | raise ManifestInvalidPathError( |
| 2144 | '<%s> invalid "src": %s: %s' % (element, src, msg) |
| 2145 | ) |
| 2146 | |
| 2147 | def _ParseCopyFile(self, project, node): |
| 2148 | src = self._reqatt(node, "src") |
| 2149 | dest = self._reqatt(node, "dest") |
| 2150 | if not self.IsMirror: |
| 2151 | # src is project relative; |
| 2152 | # dest is relative to the top of the tree. |
| 2153 | # We only validate paths if we actually plan to process them. |
| 2154 | self._ValidateFilePaths("copyfile", src, dest) |
| 2155 | project.AddCopyFile(src, dest, self.topdir) |
| 2156 | |
| 2157 | def _ParseLinkFile(self, project, node): |
| 2158 | src = self._reqatt(node, "src") |
| 2159 | dest = self._reqatt(node, "dest") |
| 2160 | if not self.IsMirror: |
| 2161 | # src is project relative; |
| 2162 | # dest is relative to the top of the tree. |
| 2163 | # We only validate paths if we actually plan to process them. |
| 2164 | self._ValidateFilePaths("linkfile", src, dest) |
| 2165 | project.AddLinkFile(src, dest, self.topdir) |
| 2166 | |
| 2167 | def _ParseAnnotation(self, element, node): |
| 2168 | name = self._reqatt(node, "name") |
| 2169 | value = self._reqatt(node, "value") |
| 2170 | try: |
| 2171 | keep = self._reqatt(node, "keep").lower() |
| 2172 | except ManifestParseError: |
| 2173 | keep = "true" |
| 2174 | if keep != "true" and keep != "false": |
| 2175 | raise ManifestParseError( |
| 2176 | 'optional "keep" attribute must be ' '"true" or "false"' |
| 2177 | ) |
| 2178 | element.AddAnnotation(name, value, keep) |
| 2179 | |
| 2180 | def _get_remote(self, node): |
| 2181 | name = node.getAttribute("remote") |
| 2182 | if not name: |
| 2183 | return None |
| 2184 | |
| 2185 | v = self._remotes.get(name) |
| 2186 | if not v: |
| 2187 | raise ManifestParseError( |
| 2188 | "remote %s not defined in %s" % (name, self.manifestFile) |
| 2189 | ) |
| 2190 | return v |
| 2191 | |
| 2192 | def _reqatt(self, node, attname): |
| 2193 | """ |
| 2194 | reads a required attribute from the node. |
| 2195 | """ |
| 2196 | v = node.getAttribute(attname) |
| 2197 | if not v: |
| 2198 | raise ManifestParseError( |
| 2199 | "no %s in <%s> within %s" |
| 2200 | % (attname, node.nodeName, self.manifestFile) |
| 2201 | ) |
| 2202 | return v |
| 2203 | |
| 2204 | def projectsDiff(self, manifest): |
| 2205 | """return the projects differences between two manifests. |
| 2206 | |
| 2207 | The diff will be from self to given manifest. |
| 2208 | |
| 2209 | """ |
| 2210 | fromProjects = self.paths |
| 2211 | toProjects = manifest.paths |
| 2212 | |
| 2213 | fromKeys = sorted(fromProjects.keys()) |
Sylvain | 25d6c7c | 2023-08-19 23:21:49 +0200 | [diff] [blame] | 2214 | toKeys = set(toProjects.keys()) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2215 | |
| 2216 | diff = { |
| 2217 | "added": [], |
| 2218 | "removed": [], |
| 2219 | "missing": [], |
| 2220 | "changed": [], |
| 2221 | "unreachable": [], |
| 2222 | } |
| 2223 | |
| 2224 | for proj in fromKeys: |
Sylvain | 25d6c7c | 2023-08-19 23:21:49 +0200 | [diff] [blame] | 2225 | fromProj = fromProjects[proj] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2226 | if proj not in toKeys: |
Sylvain | 25d6c7c | 2023-08-19 23:21:49 +0200 | [diff] [blame] | 2227 | diff["removed"].append(fromProj) |
| 2228 | elif not fromProj.Exists: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2229 | diff["missing"].append(toProjects[proj]) |
| 2230 | toKeys.remove(proj) |
| 2231 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2232 | toProj = toProjects[proj] |
| 2233 | try: |
| 2234 | fromRevId = fromProj.GetCommitRevisionId() |
| 2235 | toRevId = toProj.GetCommitRevisionId() |
| 2236 | except ManifestInvalidRevisionError: |
| 2237 | diff["unreachable"].append((fromProj, toProj)) |
| 2238 | else: |
| 2239 | if fromRevId != toRevId: |
| 2240 | diff["changed"].append((fromProj, toProj)) |
| 2241 | toKeys.remove(proj) |
| 2242 | |
Sylvain | 25d6c7c | 2023-08-19 23:21:49 +0200 | [diff] [blame] | 2243 | diff["added"].extend(toProjects[proj] for proj in sorted(toKeys)) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2244 | |
| 2245 | return diff |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 2246 | |
| 2247 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2248 | class RepoClient(XmlManifest): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2249 | """Manages a repo client checkout.""" |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2250 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2251 | def __init__( |
| 2252 | self, repodir, manifest_file=None, submanifest_path="", **kwargs |
| 2253 | ): |
| 2254 | """Initialize. |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 2255 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2256 | Args: |
| 2257 | repodir: Path to the .repo/ dir for holding all internal checkout |
| 2258 | state. It must be in the top directory of the repo client |
| 2259 | checkout. |
| 2260 | manifest_file: Full path to the manifest file to parse. This will |
| 2261 | usually be |repodir|/|MANIFEST_FILE_NAME|. |
| 2262 | submanifest_path: The submanifest root relative to the repo root. |
| 2263 | **kwargs: Additional keyword arguments, passed to XmlManifest. |
| 2264 | """ |
| 2265 | self.isGitcClient = False |
| 2266 | submanifest_path = submanifest_path or "" |
| 2267 | if submanifest_path: |
| 2268 | self._CheckLocalPath(submanifest_path) |
| 2269 | prefix = os.path.join(repodir, SUBMANIFEST_DIR, submanifest_path) |
| 2270 | else: |
| 2271 | prefix = repodir |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2272 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2273 | if os.path.exists(os.path.join(prefix, LOCAL_MANIFEST_NAME)): |
| 2274 | print( |
| 2275 | "error: %s is not supported; put local manifests in `%s` " |
| 2276 | "instead" |
| 2277 | % ( |
| 2278 | LOCAL_MANIFEST_NAME, |
| 2279 | os.path.join(prefix, LOCAL_MANIFESTS_DIR_NAME), |
| 2280 | ), |
| 2281 | file=sys.stderr, |
| 2282 | ) |
| 2283 | sys.exit(1) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2284 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2285 | if manifest_file is None: |
| 2286 | manifest_file = os.path.join(prefix, MANIFEST_FILE_NAME) |
| 2287 | local_manifests = os.path.abspath( |
| 2288 | os.path.join(prefix, LOCAL_MANIFESTS_DIR_NAME) |
| 2289 | ) |
| 2290 | super().__init__( |
| 2291 | repodir, |
| 2292 | manifest_file, |
| 2293 | local_manifests, |
| 2294 | submanifest_path=submanifest_path, |
| 2295 | **kwargs, |
| 2296 | ) |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 2297 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2298 | # TODO: Completely separate manifest logic out of the client. |
| 2299 | self.manifest = self |