blob: 86f20202e41497f2ef0d15d08ed8eebeff7f9791 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# 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 Tenneti993af5e2021-05-12 12:00:31 -070015import collections
Colin Cross23acdd32012-04-21 00:33:54 -070016import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
Raman Tenneti080877e2021-03-09 15:19:06 -080018import platform
Conley Owensdb728cd2011-09-26 16:34:01 -070019import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import sys
David Pursehouse59bbb582013-05-17 10:49:33 +090021import xml.dom.minidom
Mike Frysingeracf63b22019-06-13 02:24:21 -040022import urllib.parse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023
Simran Basib9a1b732015-08-20 12:19:28 -070024import gitc_utils
Miguel Gaio1f207762020-07-17 14:09:13 +020025from git_config import GitConfig, IsId
David Pursehousee00aa6b2012-09-11 14:33:51 +090026from git_refs import R_HEADS, HEAD
Renaud Paquayd5cec5e2016-11-01 11:24:03 -070027import platform_utils
Jack Neus6ea0cae2021-07-20 20:52:33 +000028from project import Annotation, RemoteSpec, Project, MetaProject
Mike Frysinger04122b72019-07-31 23:32:58 -040029from error import (ManifestParseError, ManifestInvalidPathError,
30 ManifestInvalidRevisionError)
Raman Tenneti993af5e2021-05-12 12:00:31 -070031from wrapper import Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
33MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070034LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090035LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036
Raman Tenneti78f4dd32021-06-07 13:27:37 -070037# Add all projects from local manifest into a group.
38LOCAL_MANIFEST_GROUP_PREFIX = 'local:'
39
Raman Tenneti993af5e2021-05-12 12:00:31 -070040# ContactInfo has the self-registered bug url, supplied by the manifest authors.
41ContactInfo = collections.namedtuple('ContactInfo', 'bugurl')
42
Anthony Kingcb07ba72015-03-28 23:26:04 +000043# urljoin gets confused if the scheme is not known.
Joe Kilner6e310792016-10-27 15:53:53 -070044urllib.parse.uses_relative.extend([
45 'ssh',
46 'git',
47 'persistent-https',
48 'sso',
49 'rpc'])
50urllib.parse.uses_netloc.extend([
51 'ssh',
52 'git',
53 'persistent-https',
54 'sso',
55 'rpc'])
Conley Owensdb728cd2011-09-26 16:34:01 -070056
David Pursehouse819827a2020-02-12 15:20:19 +090057
Mike Frysingerbb8ee7f2020-02-22 05:30:12 -050058def XmlBool(node, attr, default=None):
59 """Determine boolean value of |node|'s |attr|.
60
61 Invalid values will issue a non-fatal warning.
62
63 Args:
64 node: XML node whose attributes we access.
65 attr: The attribute to access.
66 default: If the attribute is not set (value is empty), then use this.
67
68 Returns:
69 True if the attribute is a valid string representing true.
70 False if the attribute is a valid string representing false.
71 |default| otherwise.
72 """
73 value = node.getAttribute(attr)
74 s = value.lower()
75 if s == '':
76 return default
77 elif s in {'yes', 'true', '1'}:
78 return True
79 elif s in {'no', 'false', '0'}:
80 return False
81 else:
82 print('warning: manifest: %s="%s": ignoring invalid XML boolean' %
83 (attr, value), file=sys.stderr)
84 return default
85
86
87def XmlInt(node, attr, default=None):
88 """Determine integer value of |node|'s |attr|.
89
90 Args:
91 node: XML node whose attributes we access.
92 attr: The attribute to access.
93 default: If the attribute is not set (value is empty), then use this.
94
95 Returns:
96 The number if the attribute is a valid number.
97
98 Raises:
99 ManifestParseError: The number is invalid.
100 """
101 value = node.getAttribute(attr)
102 if not value:
103 return default
104
105 try:
106 return int(value)
107 except ValueError:
108 raise ManifestParseError('manifest: invalid %s="%s" integer' %
109 (attr, value))
110
111
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700112class _Default(object):
113 """Project defaults within the manifest."""
114
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700115 revisionExpr = None
Conley Owensb6a16e62013-09-25 15:06:09 -0700116 destBranchExpr = None
Nasser Grainawida403412018-05-04 12:53:29 -0600117 upstreamExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700119 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -0700120 sync_c = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800121 sync_s = False
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900122 sync_tags = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700123
Julien Campergue74879922013-10-09 14:38:46 +0200124 def __eq__(self, other):
Jack Neus5ba21202021-06-09 15:21:25 +0000125 if not isinstance(other, _Default):
126 return False
Julien Campergue74879922013-10-09 14:38:46 +0200127 return self.__dict__ == other.__dict__
128
129 def __ne__(self, other):
Jack Neus5ba21202021-06-09 15:21:25 +0000130 if not isinstance(other, _Default):
131 return True
Julien Campergue74879922013-10-09 14:38:46 +0200132 return self.__dict__ != other.__dict__
133
David Pursehouse819827a2020-02-12 15:20:19 +0900134
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700135class _XmlRemote(object):
136 def __init__(self,
137 name,
Yestin Sunb292b982012-07-02 07:32:50 -0700138 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700139 fetch=None,
Steve Raed6480452016-08-10 15:00:00 -0700140 pushUrl=None,
Conley Owensdb728cd2011-09-26 16:34:01 -0700141 manifestUrl=None,
Anthony King36ea2fb2014-05-06 11:54:01 +0100142 review=None,
Jonathan Nieder93719792015-03-17 11:29:58 -0700143 revision=None):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700144 self.name = name
145 self.fetchUrl = fetch
Steve Raed6480452016-08-10 15:00:00 -0700146 self.pushUrl = pushUrl
Conley Owensdb728cd2011-09-26 16:34:01 -0700147 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -0700148 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700149 self.reviewUrl = review
Anthony King36ea2fb2014-05-06 11:54:01 +0100150 self.revision = revision
Conley Owensceea3682011-10-20 10:45:47 -0700151 self.resolvedFetchUrl = self._resolveFetchUrl()
Jack Neus6ea0cae2021-07-20 20:52:33 +0000152 self.annotations = []
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700153
David Pursehouse717ece92012-11-13 08:49:16 +0900154 def __eq__(self, other):
Jack Neus5ba21202021-06-09 15:21:25 +0000155 if not isinstance(other, _XmlRemote):
156 return False
Jack Neus6ea0cae2021-07-20 20:52:33 +0000157 return (sorted(self.annotations) == sorted(other.annotations) and
158 self.name == other.name and self.fetchUrl == other.fetchUrl and
159 self.pushUrl == other.pushUrl and self.remoteAlias == other.remoteAlias
160 and self.reviewUrl == other.reviewUrl and self.revision == other.revision)
David Pursehouse717ece92012-11-13 08:49:16 +0900161
162 def __ne__(self, other):
Jack Neus6ea0cae2021-07-20 20:52:33 +0000163 return not self.__eq__(other)
David Pursehouse717ece92012-11-13 08:49:16 +0900164
Conley Owensceea3682011-10-20 10:45:47 -0700165 def _resolveFetchUrl(self):
Jack Neus5ba21202021-06-09 15:21:25 +0000166 if self.fetchUrl is None:
167 return ''
Conley Owensceea3682011-10-20 10:45:47 -0700168 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -0700169 manifestUrl = self.manifestUrl.rstrip('/')
Conley Owens2d0f5082014-01-31 15:03:51 -0800170 # urljoin will gets confused over quite a few things. The ones we care
171 # about here are:
172 # * no scheme in the base url, like <hostname:port>
Anthony Kingcb07ba72015-03-28 23:26:04 +0000173 # We handle no scheme by replacing it with an obscure protocol, gopher
174 # and then replacing it with the original when we are done.
175
Conley Owensdb728cd2011-09-26 16:34:01 -0700176 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
Conley Owens4ccad752015-04-29 10:45:37 -0700177 url = urllib.parse.urljoin('gopher://' + manifestUrl, url)
178 url = re.sub(r'^gopher://', '', url)
Anthony Kingcb07ba72015-03-28 23:26:04 +0000179 else:
180 url = urllib.parse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -0800181 return url
Conley Owensceea3682011-10-20 10:45:47 -0700182
183 def ToRemoteSpec(self, projectName):
David Rileye0684ad2017-04-05 00:02:59 -0700184 fetchUrl = self.resolvedFetchUrl.rstrip('/')
185 url = fetchUrl + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -0700186 remoteName = self.name
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700187 if self.remoteAlias:
David Pursehouse37128b62013-10-15 10:48:40 +0900188 remoteName = self.remoteAlias
Dan Willemsen96c2d652016-04-06 16:03:54 -0700189 return RemoteSpec(remoteName,
190 url=url,
Steve Raed6480452016-08-10 15:00:00 -0700191 pushUrl=self.pushUrl,
Dan Willemsen96c2d652016-04-06 16:03:54 -0700192 review=self.reviewUrl,
David Rileye0684ad2017-04-05 00:02:59 -0700193 orig_name=self.name,
194 fetchUrl=self.fetchUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700195
Jack Neus6ea0cae2021-07-20 20:52:33 +0000196 def AddAnnotation(self, name, value, keep):
197 self.annotations.append(Annotation(name, value, keep))
198
David Pursehouse819827a2020-02-12 15:20:19 +0900199
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700200class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700201 """manages the repo configuration file"""
202
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400203 def __init__(self, repodir, manifest_file, local_manifests=None):
204 """Initialize.
205
206 Args:
207 repodir: Path to the .repo/ dir for holding all internal checkout state.
208 It must be in the top directory of the repo client checkout.
209 manifest_file: Full path to the manifest file to parse. This will usually
210 be |repodir|/|MANIFEST_FILE_NAME|.
211 local_manifests: Full path to the directory of local override manifests.
212 This will usually be |repodir|/|LOCAL_MANIFESTS_DIR_NAME|.
213 """
214 # TODO(vapier): Move this out of this class.
215 self.globalConfig = GitConfig.ForUser()
216
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700217 self.repodir = os.path.abspath(repodir)
218 self.topdir = os.path.dirname(self.repodir)
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400219 self.manifestFile = manifest_file
220 self.local_manifests = local_manifests
Basil Gelloc7453502018-05-25 20:23:52 +0300221 self._load_local_manifests = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222
223 self.repoProject = MetaProject(self, 'repo',
David Pursehouseabdf7502020-02-12 14:58:39 +0900224 gitdir=os.path.join(repodir, 'repo/.git'),
225 worktree=os.path.join(repodir, 'repo'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700226
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500227 mp = MetaProject(self, 'manifests',
228 gitdir=os.path.join(repodir, 'manifests.git'),
229 worktree=os.path.join(repodir, 'manifests'))
230 self.manifestProject = mp
231
232 # This is a bit hacky, but we're in a chicken & egg situation: all the
233 # normal repo settings live in the manifestProject which we just setup
234 # above, so we couldn't easily query before that. We assume Project()
235 # init doesn't care if this changes afterwards.
Mike Frysingerd957ec62020-02-24 14:40:25 -0500236 if os.path.exists(mp.gitdir) and mp.config.GetBoolean('repo.worktree'):
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500237 mp.use_git_worktrees = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700238
239 self._Unload()
240
Basil Gelloc7453502018-05-25 20:23:52 +0300241 def Override(self, name, load_local_manifests=True):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700242 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700243 """
Basil Gelloc7453502018-05-25 20:23:52 +0300244 path = None
245
246 # Look for a manifest by path in the filesystem (including the cwd).
247 if not load_local_manifests:
248 local_path = os.path.abspath(name)
249 if os.path.isfile(local_path):
250 path = local_path
251
252 # Look for manifests by name from the manifests repo.
253 if path is None:
254 path = os.path.join(self.manifestProject.worktree, name)
255 if not os.path.isfile(path):
256 raise ManifestParseError('manifest %s not found' % name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700257
258 old = self.manifestFile
259 try:
Basil Gelloc7453502018-05-25 20:23:52 +0300260 self._load_local_manifests = load_local_manifests
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700261 self.manifestFile = path
262 self._Unload()
263 self._Load()
264 finally:
265 self.manifestFile = old
266
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700267 def Link(self, name):
268 """Update the repo metadata to use a different manifest.
269 """
270 self.Override(name)
271
Mike Frysingera269b1c2020-02-21 00:49:41 -0500272 # Old versions of repo would generate symlinks we need to clean up.
Mike Frysinger9d96f582021-09-28 11:27:24 -0400273 platform_utils.remove(self.manifestFile, missing_ok=True)
Mike Frysingera269b1c2020-02-21 00:49:41 -0500274 # This file is interpreted as if it existed inside the manifest repo.
275 # That allows us to use <include> with the relative file name.
276 with open(self.manifestFile, 'w') as fp:
277 fp.write("""<?xml version="1.0" encoding="UTF-8"?>
278<!--
279DO NOT EDIT THIS FILE! It is generated by repo and changes will be discarded.
280If you want to use a different manifest, use `repo init -m <file>` instead.
281
282If you want to customize your checkout by overriding manifest settings, use
283the local_manifests/ directory instead.
284
285For more information on repo manifests, check out:
286https://gerrit.googlesource.com/git-repo/+/HEAD/docs/manifest-format.md
287-->
288<manifest>
289 <include name="%s" />
290</manifest>
291""" % (name,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700292
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800293 def _RemoteToXml(self, r, doc, root):
294 e = doc.createElement('remote')
295 root.appendChild(e)
296 e.setAttribute('name', r.name)
297 e.setAttribute('fetch', r.fetchUrl)
Steve Raed6480452016-08-10 15:00:00 -0700298 if r.pushUrl is not None:
299 e.setAttribute('pushurl', r.pushUrl)
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700300 if r.remoteAlias is not None:
301 e.setAttribute('alias', r.remoteAlias)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800302 if r.reviewUrl is not None:
303 e.setAttribute('review', r.reviewUrl)
Anthony King36ea2fb2014-05-06 11:54:01 +0100304 if r.revision is not None:
305 e.setAttribute('revision', r.revision)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800306
Jack Neus6ea0cae2021-07-20 20:52:33 +0000307 for a in r.annotations:
308 if a.keep == 'true':
309 ae = doc.createElement('annotation')
310 ae.setAttribute('name', a.name)
311 ae.setAttribute('value', a.value)
312 e.appendChild(ae)
313
Mike Frysinger51e39d52020-12-04 05:32:06 -0500314 def _ParseList(self, field):
315 """Parse fields that contain flattened lists.
316
317 These are whitespace & comma separated. Empty elements will be discarded.
318 """
319 return [x for x in re.split(r'[,\s]+', field) if x]
Josh Triplett884a3872014-06-12 14:57:29 -0700320
Mike Frysinger23411d32020-09-02 04:31:10 -0400321 def ToXml(self, peg_rev=False, peg_rev_upstream=True, peg_rev_dest_branch=True, groups=None):
322 """Return the current manifest XML."""
Colin Cross5acde752012-03-28 20:15:45 -0700323 mp = self.manifestProject
324
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700325 if groups is None:
326 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800327 if groups:
Mike Frysinger51e39d52020-12-04 05:32:06 -0500328 groups = self._ParseList(groups)
Colin Cross5acde752012-03-28 20:15:45 -0700329
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800330 doc = xml.dom.minidom.Document()
331 root = doc.createElement('manifest')
332 doc.appendChild(root)
333
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700334 # Save out the notice. There's a little bit of work here to give it the
335 # right whitespace, which assumes that the notice is automatically indented
336 # by 4 by minidom.
337 if self.notice:
338 notice_element = root.appendChild(doc.createElement('notice'))
339 notice_lines = self.notice.splitlines()
David Pursehouse54a4e602020-02-12 14:31:05 +0900340 indented_notice = ('\n'.join(" " * 4 + line for line in notice_lines))[4:]
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700341 notice_element.appendChild(doc.createTextNode(indented_notice))
342
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800343 d = self.default
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800344
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530345 for r in sorted(self.remotes):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800346 self._RemoteToXml(self.remotes[r], doc, root)
347 if self.remotes:
348 root.appendChild(doc.createTextNode(''))
349
350 have_default = False
351 e = doc.createElement('default')
352 if d.remote:
353 have_default = True
354 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700355 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800356 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700357 e.setAttribute('revision', d.revisionExpr)
Simon Ruggier7e59de22015-07-24 12:50:06 +0200358 if d.destBranchExpr:
359 have_default = True
360 e.setAttribute('dest-branch', d.destBranchExpr)
Nasser Grainawida403412018-05-04 12:53:29 -0600361 if d.upstreamExpr:
362 have_default = True
363 e.setAttribute('upstream', d.upstreamExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700364 if d.sync_j > 1:
365 have_default = True
366 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700367 if d.sync_c:
368 have_default = True
369 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800370 if d.sync_s:
371 have_default = True
372 e.setAttribute('sync-s', 'true')
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900373 if not d.sync_tags:
374 have_default = True
375 e.setAttribute('sync-tags', 'false')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800376 if have_default:
377 root.appendChild(e)
378 root.appendChild(doc.createTextNode(''))
379
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700380 if self._manifest_server:
381 e = doc.createElement('manifest-server')
382 e.setAttribute('url', self._manifest_server)
383 root.appendChild(e)
384 root.appendChild(doc.createTextNode(''))
385
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800386 def output_projects(parent, parent_node, projects):
David James8d201162013-10-11 17:03:19 -0700387 for project_name in projects:
388 for project in self._projects[project_name]:
389 output_project(parent, parent_node, project)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800390
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800391 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700392 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800393 return
394
395 name = p.name
396 relpath = p.relpath
397 if parent:
398 name = self._UnjoinName(parent.name, name)
399 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700400
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800401 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800402 parent_node.appendChild(e)
403 e.setAttribute('name', name)
404 if relpath != name:
405 e.setAttribute('path', relpath)
Conley Owensa17d7af2013-10-16 14:38:09 -0700406 remoteName = None
407 if d.remote:
Dan Willemsen96c2d652016-04-06 16:03:54 -0700408 remoteName = d.remote.name
409 if not d.remote or p.remote.orig_name != remoteName:
410 remoteName = p.remote.orig_name
Anthony King36ea2fb2014-05-06 11:54:01 +0100411 e.setAttribute('remote', remoteName)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800412 if peg_rev:
413 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700414 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800415 else:
Brian Harring14a66742012-09-28 20:21:57 -0700416 value = p.work_git.rev_parse(HEAD + '^0')
417 e.setAttribute('revision', value)
Conley Owens551dfec2015-07-10 14:54:54 -0700418 if peg_rev_upstream:
419 if p.upstream:
420 e.setAttribute('upstream', p.upstream)
421 elif value != p.revisionExpr:
422 # Only save the origin if the origin is not a sha1, and the default
423 # isn't our value
424 e.setAttribute('upstream', p.revisionExpr)
Sean McAllisteraf908cb2020-04-20 08:41:58 -0600425
426 if peg_rev_dest_branch:
427 if p.dest_branch:
428 e.setAttribute('dest-branch', p.dest_branch)
429 elif value != p.revisionExpr:
430 e.setAttribute('dest-branch', p.revisionExpr)
431
Anthony King36ea2fb2014-05-06 11:54:01 +0100432 else:
Dan Willemsen96c2d652016-04-06 16:03:54 -0700433 revision = self.remotes[p.remote.orig_name].revision or d.revisionExpr
Anthony King36ea2fb2014-05-06 11:54:01 +0100434 if not revision or revision != p.revisionExpr:
435 e.setAttribute('revision', p.revisionExpr)
Raman Tennetib5c5a5e2021-02-06 09:44:15 -0800436 elif p.revisionId:
437 e.setAttribute('revision', p.revisionId)
Nasser Grainawida403412018-05-04 12:53:29 -0600438 if (p.upstream and (p.upstream != p.revisionExpr or
439 p.upstream != d.upstreamExpr)):
Mani Chandel7a91d512014-07-24 16:27:08 +0530440 e.setAttribute('upstream', p.upstream)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800441
Simon Ruggier7e59de22015-07-24 12:50:06 +0200442 if p.dest_branch and p.dest_branch != d.destBranchExpr:
443 e.setAttribute('dest-branch', p.dest_branch)
444
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800445 for c in p.copyfiles:
446 ce = doc.createElement('copyfile')
447 ce.setAttribute('src', c.src)
448 ce.setAttribute('dest', c.dest)
449 e.appendChild(ce)
450
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500451 for l in p.linkfiles:
452 le = doc.createElement('linkfile')
453 le.setAttribute('src', l.src)
454 le.setAttribute('dest', l.dest)
455 e.appendChild(le)
456
Conley Owensbb1b5f52012-08-13 13:11:18 -0700457 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700458 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700459 if egroups:
460 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700461
James W. Mills24c13082012-04-12 15:04:13 -0500462 for a in p.annotations:
463 if a.keep == "true":
464 ae = doc.createElement('annotation')
465 ae.setAttribute('name', a.name)
466 ae.setAttribute('value', a.value)
467 e.appendChild(ae)
468
Anatol Pomazau79770d22012-04-20 14:41:59 -0700469 if p.sync_c:
470 e.setAttribute('sync-c', 'true')
471
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800472 if p.sync_s:
473 e.setAttribute('sync-s', 'true')
474
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900475 if not p.sync_tags:
476 e.setAttribute('sync-tags', 'false')
477
Dan Willemsen88409222015-08-17 15:29:10 -0700478 if p.clone_depth:
479 e.setAttribute('clone-depth', str(p.clone_depth))
480
Simran Basib9a1b732015-08-20 12:19:28 -0700481 self._output_manifest_project_extras(p, e)
482
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800483 if p.subprojects:
David James8d201162013-10-11 17:03:19 -0700484 subprojects = set(subp.name for subp in p.subprojects)
485 output_projects(p, e, list(sorted(subprojects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800486
David James8d201162013-10-11 17:03:19 -0700487 projects = set(p.name for p in self._paths.values() if not p.parent)
488 output_projects(None, root, list(sorted(projects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800489
Doug Anderson37282b42011-03-04 11:54:18 -0800490 if self._repo_hooks_project:
491 root.appendChild(doc.createTextNode(''))
492 e = doc.createElement('repo-hooks')
493 e.setAttribute('in-project', self._repo_hooks_project.name)
494 e.setAttribute('enabled-list',
495 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
496 root.appendChild(e)
497
Raman Tenneti1bb4fb22021-01-07 16:50:45 -0800498 if self._superproject:
499 root.appendChild(doc.createTextNode(''))
500 e = doc.createElement('superproject')
501 e.setAttribute('name', self._superproject['name'])
502 remoteName = None
503 if d.remote:
504 remoteName = d.remote.name
505 remote = self._superproject.get('remote')
506 if not d.remote or remote.orig_name != remoteName:
507 remoteName = remote.orig_name
508 e.setAttribute('remote', remoteName)
Xin Lie0b16a22021-09-26 23:20:32 -0700509 revision = remote.revision or d.revisionExpr
510 if not revision or revision != self._superproject['revision']:
511 e.setAttribute('revision', self._superproject['revision'])
Raman Tenneti1bb4fb22021-01-07 16:50:45 -0800512 root.appendChild(e)
513
Raman Tenneti993af5e2021-05-12 12:00:31 -0700514 if self._contactinfo.bugurl != Wrapper().BUG_URL:
Raman Tenneti1c3f57e2021-05-04 12:32:13 -0700515 root.appendChild(doc.createTextNode(''))
516 e = doc.createElement('contactinfo')
Raman Tenneti993af5e2021-05-12 12:00:31 -0700517 e.setAttribute('bugurl', self._contactinfo.bugurl)
Raman Tenneti1c3f57e2021-05-04 12:32:13 -0700518 root.appendChild(e)
519
Mike Frysinger23411d32020-09-02 04:31:10 -0400520 return doc
521
522 def ToDict(self, **kwargs):
523 """Return the current manifest as a dictionary."""
524 # Elements that may only appear once.
525 SINGLE_ELEMENTS = {
526 'notice',
527 'default',
528 'manifest-server',
529 'repo-hooks',
Raman Tenneti1bb4fb22021-01-07 16:50:45 -0800530 'superproject',
Raman Tenneti1c3f57e2021-05-04 12:32:13 -0700531 'contactinfo',
Mike Frysinger23411d32020-09-02 04:31:10 -0400532 }
533 # Elements that may be repeated.
534 MULTI_ELEMENTS = {
535 'remote',
536 'remove-project',
537 'project',
538 'extend-project',
539 'include',
540 # These are children of 'project' nodes.
541 'annotation',
542 'project',
543 'copyfile',
544 'linkfile',
545 }
546
547 doc = self.ToXml(**kwargs)
548 ret = {}
549
550 def append_children(ret, node):
551 for child in node.childNodes:
552 if child.nodeType == xml.dom.Node.ELEMENT_NODE:
553 attrs = child.attributes
554 element = dict((attrs.item(i).localName, attrs.item(i).value)
555 for i in range(attrs.length))
556 if child.nodeName in SINGLE_ELEMENTS:
557 ret[child.nodeName] = element
558 elif child.nodeName in MULTI_ELEMENTS:
559 ret.setdefault(child.nodeName, []).append(element)
560 else:
561 raise ManifestParseError('Unhandled element "%s"' % (child.nodeName,))
562
563 append_children(element, child)
564
565 append_children(ret, doc.firstChild)
566
567 return ret
568
569 def Save(self, fd, **kwargs):
570 """Write the current manifest out to the given file descriptor."""
571 doc = self.ToXml(**kwargs)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800572 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
573
Simran Basib9a1b732015-08-20 12:19:28 -0700574 def _output_manifest_project_extras(self, p, e):
575 """Manifests can modify e if they support extra project attributes."""
Simran Basib9a1b732015-08-20 12:19:28 -0700576
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700577 @property
David James8d201162013-10-11 17:03:19 -0700578 def paths(self):
579 self._Load()
580 return self._paths
581
582 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700583 def projects(self):
584 self._Load()
Anthony Kingd58bfe52014-05-05 23:30:49 +0100585 return list(self._paths.values())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700586
587 @property
588 def remotes(self):
589 self._Load()
590 return self._remotes
591
592 @property
593 def default(self):
594 self._Load()
595 return self._default
596
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800597 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800598 def repo_hooks_project(self):
599 self._Load()
600 return self._repo_hooks_project
601
602 @property
Raman Tenneti1bb4fb22021-01-07 16:50:45 -0800603 def superproject(self):
604 self._Load()
605 return self._superproject
606
607 @property
Raman Tenneti1c3f57e2021-05-04 12:32:13 -0700608 def contactinfo(self):
609 self._Load()
610 return self._contactinfo
611
612 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700613 def notice(self):
614 self._Load()
615 return self._notice
616
617 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700618 def manifest_server(self):
619 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800620 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700621
622 @property
Xin Lid79a4bc2020-05-20 16:03:45 -0700623 def CloneBundle(self):
624 clone_bundle = self.manifestProject.config.GetBoolean('repo.clonebundle')
625 if clone_bundle is None:
626 return False if self.manifestProject.config.GetBoolean('repo.partialclone') else True
627 else:
628 return clone_bundle
629
630 @property
Xin Li745be2e2019-06-03 11:24:30 -0700631 def CloneFilter(self):
632 if self.manifestProject.config.GetBoolean('repo.partialclone'):
633 return self.manifestProject.config.GetString('repo.clonefilter')
634 return None
635
636 @property
Raman Tennetif32f2432021-04-12 20:57:25 -0700637 def PartialCloneExclude(self):
638 exclude = self.manifest.manifestProject.config.GetString(
639 'repo.partialcloneexclude') or ''
640 return set(x.strip() for x in exclude.split(','))
641
642 @property
Michael Kellyc34b91c2021-07-02 09:25:48 -0700643 def UseLocalManifests(self):
644 return self._load_local_manifests
645
646 def SetUseLocalManifests(self, value):
647 self._load_local_manifests = value
648
649 @property
Raman Tennetifeb28912021-05-02 19:47:29 -0700650 def HasLocalManifests(self):
651 return self._load_local_manifests and self.local_manifests
652
653 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800654 def IsMirror(self):
655 return self.manifestProject.config.GetBoolean('repo.mirror')
656
Julien Campergue335f5ef2013-10-16 11:02:35 +0200657 @property
Mike Frysinger979d5bd2020-02-09 02:28:34 -0500658 def UseGitWorktrees(self):
659 return self.manifestProject.config.GetBoolean('repo.worktree')
660
661 @property
Julien Campergue335f5ef2013-10-16 11:02:35 +0200662 def IsArchive(self):
663 return self.manifestProject.config.GetBoolean('repo.archive')
664
Martin Kellye4e94d22017-03-21 16:05:12 -0700665 @property
666 def HasSubmodules(self):
667 return self.manifestProject.config.GetBoolean('repo.submodules')
668
Raman Tenneti080877e2021-03-09 15:19:06 -0800669 def GetDefaultGroupsStr(self):
670 """Returns the default group string for the platform."""
671 return 'default,platform-' + platform.system().lower()
672
673 def GetGroupsStr(self):
674 """Returns the manifest group string that should be synced."""
675 groups = self.manifestProject.config.GetString('manifest.groups')
676 if not groups:
677 groups = self.GetDefaultGroupsStr()
678 return groups
679
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700680 def _Unload(self):
681 self._loaded = False
682 self._projects = {}
David James8d201162013-10-11 17:03:19 -0700683 self._paths = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700684 self._remotes = {}
685 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800686 self._repo_hooks_project = None
Raman Tenneti1bb4fb22021-01-07 16:50:45 -0800687 self._superproject = {}
Raman Tenneti993af5e2021-05-12 12:00:31 -0700688 self._contactinfo = ContactInfo(Wrapper().BUG_URL)
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700689 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700690 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700691 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700692
693 def _Load(self):
694 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800695 m = self.manifestProject
696 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700697 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800698 b = b[len(R_HEADS):]
699 self.branch = b
700
Mike Frysinger54133972021-03-01 21:38:08 -0500701 # The manifestFile was specified by the user which is why we allow include
702 # paths to point anywhere.
Colin Cross23acdd32012-04-21 00:33:54 -0700703 nodes = []
Mike Frysinger54133972021-03-01 21:38:08 -0500704 nodes.append(self._ParseManifestXml(
705 self.manifestFile, self.manifestProject.worktree,
706 restrict_includes=False))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700707
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400708 if self._load_local_manifests and self.local_manifests:
Basil Gelloc7453502018-05-25 20:23:52 +0300709 try:
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400710 for local_file in sorted(platform_utils.listdir(self.local_manifests)):
Basil Gelloc7453502018-05-25 20:23:52 +0300711 if local_file.endswith('.xml'):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -0400712 local = os.path.join(self.local_manifests, local_file)
Mike Frysinger54133972021-03-01 21:38:08 -0500713 # Since local manifests are entirely managed by the user, allow
714 # them to point anywhere the user wants.
715 nodes.append(self._ParseManifestXml(
Raman Tenneti78f4dd32021-06-07 13:27:37 -0700716 local, self.repodir,
717 parent_groups=f'{LOCAL_MANIFEST_GROUP_PREFIX}:{local_file[:-4]}',
718 restrict_includes=False))
Basil Gelloc7453502018-05-25 20:23:52 +0300719 except OSError:
720 pass
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900721
Joe Onorato26e24752013-01-11 12:35:53 -0800722 try:
723 self._ParseManifest(nodes)
724 except ManifestParseError as e:
725 # There was a problem parsing, unload ourselves in case they catch
726 # this error and try again later, we will show the correct error
727 self._Unload()
728 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700729
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800730 if self.IsMirror:
731 self._AddMetaProjectMirror(self.repoProject)
732 self._AddMetaProjectMirror(self.manifestProject)
733
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700734 self._loaded = True
735
Mike Frysinger54133972021-03-01 21:38:08 -0500736 def _ParseManifestXml(self, path, include_root, parent_groups='',
737 restrict_includes=True):
738 """Parse a manifest XML and return the computed nodes.
739
740 Args:
741 path: The XML file to read & parse.
742 include_root: The path to interpret include "name"s relative to.
743 parent_groups: The groups to apply to this projects.
744 restrict_includes: Whether to constrain the "name" attribute of includes.
745
746 Returns:
747 List of XML nodes.
748 """
David Pursehousef7fc8a92012-11-13 04:00:28 +0900749 try:
750 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900751 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900752 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
753
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700754 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700755 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700756
Jooncheol Park34acdd22012-08-27 02:25:59 +0900757 for manifest in root.childNodes:
758 if manifest.nodeName == 'manifest':
759 break
760 else:
Brian Harring26448742011-04-28 05:04:41 -0700761 raise ManifestParseError("no <manifest> in %s" % (path,))
762
Colin Cross23acdd32012-04-21 00:33:54 -0700763 nodes = []
David Pursehouse65b0ba52018-06-24 16:21:51 +0900764 for node in manifest.childNodes:
David Pursehousec1b86a22012-11-14 11:36:51 +0900765 if node.nodeName == 'include':
766 name = self._reqatt(node, 'name')
Mike Frysinger54133972021-03-01 21:38:08 -0500767 if restrict_includes:
768 msg = self._CheckLocalPath(name)
769 if msg:
770 raise ManifestInvalidPathError(
771 '<include> invalid "name": %s: %s' % (name, msg))
Fredrik de Groot352c93b2020-10-06 12:55:14 +0200772 include_groups = ''
773 if parent_groups:
774 include_groups = parent_groups
775 if node.hasAttribute('groups'):
776 include_groups = node.getAttribute('groups') + ',' + include_groups
David Pursehousec1b86a22012-11-14 11:36:51 +0900777 fp = os.path.join(include_root, name)
778 if not os.path.isfile(fp):
Mike Frysinger54133972021-03-01 21:38:08 -0500779 raise ManifestParseError("include [%s/]%s doesn't exist or isn't a file"
780 % (include_root, name))
David Pursehousec1b86a22012-11-14 11:36:51 +0900781 try:
Fredrik de Groot352c93b2020-10-06 12:55:14 +0200782 nodes.extend(self._ParseManifestXml(fp, include_root, include_groups))
David Pursehousec1b86a22012-11-14 11:36:51 +0900783 # should isolate this to the exact exception, but that's
784 # tricky. actual parsing implementation may vary.
Mike Frysinger54133972021-03-01 21:38:08 -0500785 except (KeyboardInterrupt, RuntimeError, SystemExit, ManifestParseError):
David Pursehousec1b86a22012-11-14 11:36:51 +0900786 raise
787 except Exception as e:
788 raise ManifestParseError(
Mike Frysingerec558df2019-07-05 01:38:05 -0400789 "failed parsing included manifest %s: %s" % (name, e))
David Pursehousec1b86a22012-11-14 11:36:51 +0900790 else:
Fredrik de Groot352c93b2020-10-06 12:55:14 +0200791 if parent_groups and node.nodeName == 'project':
792 nodeGroups = parent_groups
793 if node.hasAttribute('groups'):
794 nodeGroups = node.getAttribute('groups') + ',' + nodeGroups
795 node.setAttribute('groups', nodeGroups)
David Pursehousec1b86a22012-11-14 11:36:51 +0900796 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700797 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700798
Colin Cross23acdd32012-04-21 00:33:54 -0700799 def _ParseManifest(self, node_list):
800 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700801 if node.nodeName == 'remote':
802 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900803 if remote:
804 if remote.name in self._remotes:
805 if remote != self._remotes[remote.name]:
806 raise ManifestParseError(
807 'remote %s already exists with different attributes' %
808 (remote.name))
809 else:
810 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700811
Colin Cross23acdd32012-04-21 00:33:54 -0700812 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700813 if node.nodeName == 'default':
Julien Campergue74879922013-10-09 14:38:46 +0200814 new_default = self._ParseDefault(node)
Jack Neusb8c84482021-06-15 14:28:30 +0000815 emptyDefault = not node.hasAttributes() and not node.hasChildNodes()
Julien Campergue74879922013-10-09 14:38:46 +0200816 if self._default is None:
817 self._default = new_default
Jack Neusb8c84482021-06-15 14:28:30 +0000818 elif not emptyDefault and new_default != self._default:
David Pursehouse37128b62013-10-15 10:48:40 +0900819 raise ManifestParseError('duplicate default in %s' %
820 (self.manifestFile))
Julien Campergue74879922013-10-09 14:38:46 +0200821
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700822 if self._default is None:
823 self._default = _Default()
824
Colin Cross23acdd32012-04-21 00:33:54 -0700825 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700826 if node.nodeName == 'notice':
827 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800828 raise ManifestParseError(
829 'duplicate notice in %s' %
830 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700831 self._notice = self._ParseNotice(node)
832
Colin Cross23acdd32012-04-21 00:33:54 -0700833 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700834 if node.nodeName == 'manifest-server':
835 url = self._reqatt(node, 'url')
836 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900837 raise ManifestParseError(
838 'duplicate manifest-server in %s' %
839 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700840 self._manifest_server = url
841
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800842 def recursively_add_projects(project):
David James8d201162013-10-11 17:03:19 -0700843 projects = self._projects.setdefault(project.name, [])
844 if project.relpath is None:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800845 raise ManifestParseError(
David James8d201162013-10-11 17:03:19 -0700846 'missing path for %s in %s' %
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800847 (project.name, self.manifestFile))
David James8d201162013-10-11 17:03:19 -0700848 if project.relpath in self._paths:
849 raise ManifestParseError(
850 'duplicate path %s in %s' %
851 (project.relpath, self.manifestFile))
852 self._paths[project.relpath] = project
853 projects.append(project)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800854 for subproject in project.subprojects:
855 recursively_add_projects(subproject)
856
Jack Neusa84f43a2021-09-21 22:23:55 +0000857 repo_hooks_project = None
858 enabled_repo_hooks = None
Colin Cross23acdd32012-04-21 00:33:54 -0700859 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700860 if node.nodeName == 'project':
861 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800862 recursively_add_projects(project)
Josh Triplett884a3872014-06-12 14:57:29 -0700863 if node.nodeName == 'extend-project':
864 name = self._reqatt(node, 'name')
865
866 if name not in self._projects:
867 raise ManifestParseError('extend-project element specifies non-existent '
868 'project: %s' % name)
869
870 path = node.getAttribute('path')
871 groups = node.getAttribute('groups')
872 if groups:
Mike Frysinger51e39d52020-12-04 05:32:06 -0500873 groups = self._ParseList(groups)
Luis Hector Chavez7d525852018-03-15 09:54:08 -0700874 revision = node.getAttribute('revision')
Kyunam Jobd0aae92020-02-04 11:38:53 +0900875 remote = node.getAttribute('remote')
876 if remote:
877 remote = self._get_remote(node)
Josh Triplett884a3872014-06-12 14:57:29 -0700878
879 for p in self._projects[name]:
880 if path and p.relpath != path:
881 continue
882 if groups:
883 p.groups.extend(groups)
Luis Hector Chavez7d525852018-03-15 09:54:08 -0700884 if revision:
885 p.revisionExpr = revision
Miguel Gaio1f207762020-07-17 14:09:13 +0200886 if IsId(revision):
887 p.revisionId = revision
888 else:
889 p.revisionId = None
Kyunam Jobd0aae92020-02-04 11:38:53 +0900890 if remote:
891 p.remote = remote.ToRemoteSpec(name)
Doug Anderson37282b42011-03-04 11:54:18 -0800892 if node.nodeName == 'repo-hooks':
Doug Anderson37282b42011-03-04 11:54:18 -0800893 # Only one project can be the hooks project
Jack Neusa84f43a2021-09-21 22:23:55 +0000894 if repo_hooks_project is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800895 raise ManifestParseError(
896 'duplicate repo-hooks in %s' %
897 (self.manifestFile))
898
Jack Neusa84f43a2021-09-21 22:23:55 +0000899 # Get the name of the project and the (space-separated) list of enabled.
900 repo_hooks_project = self._reqatt(node, 'in-project')
901 enabled_repo_hooks = self._ParseList(self._reqatt(node, 'enabled-list'))
Raman Tenneti1bb4fb22021-01-07 16:50:45 -0800902 if node.nodeName == 'superproject':
903 name = self._reqatt(node, 'name')
904 # There can only be one superproject.
905 if self._superproject.get('name'):
906 raise ManifestParseError(
907 'duplicate superproject in %s' %
908 (self.manifestFile))
909 self._superproject['name'] = name
910 remote_name = node.getAttribute('remote')
911 if not remote_name:
912 remote = self._default.remote
913 else:
914 remote = self._get_remote(node)
915 if remote is None:
916 raise ManifestParseError("no remote for superproject %s within %s" %
917 (name, self.manifestFile))
918 self._superproject['remote'] = remote.ToRemoteSpec(name)
Xin Lie0b16a22021-09-26 23:20:32 -0700919 revision = node.getAttribute('revision') or remote.revision
920 if not revision:
921 revision = self._default.revisionExpr
922 if not revision:
923 raise ManifestParseError('no revision for superproject %s within %s' %
924 (name, self.manifestFile))
925 self._superproject['revision'] = revision
Raman Tenneti1c3f57e2021-05-04 12:32:13 -0700926 if node.nodeName == 'contactinfo':
927 bugurl = self._reqatt(node, 'bugurl')
928 # This element can be repeated, later entries will clobber earlier ones.
Raman Tenneti993af5e2021-05-12 12:00:31 -0700929 self._contactinfo = ContactInfo(bugurl)
930
Colin Cross23acdd32012-04-21 00:33:54 -0700931 if node.nodeName == 'remove-project':
932 name = self._reqatt(node, 'name')
David Jamesb8433df2014-01-30 10:11:17 -0800933
Michael Kelly06da9982021-06-30 01:58:28 -0700934 if name in self._projects:
935 for p in self._projects[name]:
936 del self._paths[p.relpath]
937 del self._projects[name]
938
939 # If the manifest removes the hooks project, treat it as if it deleted
940 # the repo-hooks element too.
Jack Neusa84f43a2021-09-21 22:23:55 +0000941 if repo_hooks_project == name:
942 repo_hooks_project = None
Michael Kelly06da9982021-06-30 01:58:28 -0700943 elif not XmlBool(node, 'optional', False):
David Pursehousef9107482012-11-16 19:12:32 +0900944 raise ManifestParseError('remove-project element specifies non-existent '
945 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700946
Jack Neusa84f43a2021-09-21 22:23:55 +0000947 # Store repo hooks project information.
948 if repo_hooks_project:
949 # Store a reference to the Project.
950 try:
951 repo_hooks_projects = self._projects[repo_hooks_project]
952 except KeyError:
953 raise ManifestParseError(
954 'project %s not found for repo-hooks' %
955 (repo_hooks_project))
956
957 if len(repo_hooks_projects) != 1:
958 raise ManifestParseError(
959 'internal error parsing repo-hooks in %s' %
960 (self.manifestFile))
961 self._repo_hooks_project = repo_hooks_projects[0]
962 # Store the enabled hooks in the Project object.
963 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
964
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800965 def _AddMetaProjectMirror(self, m):
966 name = None
967 m_url = m.GetRemote(m.remote.name).url
968 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530969 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800970
971 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700972 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800973 if not url.endswith('/'):
974 url += '/'
975 if m_url.startswith(url):
976 remote = self._default.remote
977 name = m_url[len(url):]
978
979 if name is None:
980 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700981 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700982 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800983 name = m_url[s:]
984
985 if name.endswith('.git'):
986 name = name[:-4]
987
988 if name not in self._projects:
989 m.PreSync()
990 gitdir = os.path.join(self.topdir, '%s.git' % name)
David Pursehousee5913ae2020-02-12 13:56:59 +0900991 project = Project(manifest=self,
992 name=name,
993 remote=remote.ToRemoteSpec(name),
994 gitdir=gitdir,
995 objdir=gitdir,
996 worktree=None,
997 relpath=name or None,
998 revisionExpr=m.revisionExpr,
999 revisionId=None)
David James8d201162013-10-11 17:03:19 -07001000 self._projects[project.name] = [project]
Kwanhong Leeccd218c2014-02-17 13:07:32 +09001001 self._paths[project.relpath] = project
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001002
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001003 def _ParseRemote(self, node):
1004 """
1005 reads a <remote> element from the manifest file
1006 """
1007 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -07001008 alias = node.getAttribute('alias')
1009 if alias == '':
1010 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001011 fetch = self._reqatt(node, 'fetch')
Steve Raed6480452016-08-10 15:00:00 -07001012 pushUrl = node.getAttribute('pushurl')
1013 if pushUrl == '':
1014 pushUrl = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001015 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -08001016 if review == '':
1017 review = None
Anthony King36ea2fb2014-05-06 11:54:01 +01001018 revision = node.getAttribute('revision')
1019 if revision == '':
1020 revision = None
Conley Owensdb728cd2011-09-26 16:34:01 -07001021 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Jack Neus6ea0cae2021-07-20 20:52:33 +00001022
1023 remote = _XmlRemote(name, alias, fetch, pushUrl, manifestUrl, review, revision)
1024
1025 for n in node.childNodes:
1026 if n.nodeName == 'annotation':
1027 self._ParseAnnotation(remote, n)
1028
1029 return remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001030
1031 def _ParseDefault(self, node):
1032 """
1033 reads a <default> element from the manifest file
1034 """
1035 d = _Default()
1036 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001037 d.revisionExpr = node.getAttribute('revision')
1038 if d.revisionExpr == '':
1039 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -07001040
Bryan Jacobsf609f912013-05-06 13:36:24 -04001041 d.destBranchExpr = node.getAttribute('dest-branch') or None
Nasser Grainawida403412018-05-04 12:53:29 -06001042 d.upstreamExpr = node.getAttribute('upstream') or None
Bryan Jacobsf609f912013-05-06 13:36:24 -04001043
Mike Frysingerbb8ee7f2020-02-22 05:30:12 -05001044 d.sync_j = XmlInt(node, 'sync-j', 1)
1045 if d.sync_j <= 0:
1046 raise ManifestParseError('%s: sync-j must be greater than 0, not "%s"' %
1047 (self.manifestFile, d.sync_j))
Anatol Pomazau79770d22012-04-20 14:41:59 -07001048
Mike Frysingerbb8ee7f2020-02-22 05:30:12 -05001049 d.sync_c = XmlBool(node, 'sync-c', False)
1050 d.sync_s = XmlBool(node, 'sync-s', False)
1051 d.sync_tags = XmlBool(node, 'sync-tags', True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001052 return d
1053
Doug Anderson2b8db3c2010-11-01 15:08:06 -07001054 def _ParseNotice(self, node):
1055 """
1056 reads a <notice> element from the manifest file
1057
1058 The <notice> element is distinct from other tags in the XML in that the
1059 data is conveyed between the start and end tag (it's not an empty-element
1060 tag).
1061
1062 The white space (carriage returns, indentation) for the notice element is
1063 relevant and is parsed in a way that is based on how python docstrings work.
1064 In fact, the code is remarkably similar to here:
1065 http://www.python.org/dev/peps/pep-0257/
1066 """
1067 # Get the data out of the node...
1068 notice = node.childNodes[0].data
1069
1070 # Figure out minimum indentation, skipping the first line (the same line
1071 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301072 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -07001073 lines = notice.splitlines()
1074 for line in lines[1:]:
1075 lstrippedLine = line.lstrip()
1076 if lstrippedLine:
1077 indent = len(line) - len(lstrippedLine)
1078 minIndent = min(indent, minIndent)
1079
1080 # Strip leading / trailing blank lines and also indentation.
1081 cleanLines = [lines[0].strip()]
1082 for line in lines[1:]:
1083 cleanLines.append(line[minIndent:].rstrip())
1084
1085 # Clear completely blank lines from front and back...
1086 while cleanLines and not cleanLines[0]:
1087 del cleanLines[0]
1088 while cleanLines and not cleanLines[-1]:
1089 del cleanLines[-1]
1090
1091 return '\n'.join(cleanLines)
1092
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001093 def _JoinName(self, parent_name, name):
1094 return os.path.join(parent_name, name)
1095
1096 def _UnjoinName(self, parent_name, name):
1097 return os.path.relpath(name, parent_name)
1098
David Pursehousee5913ae2020-02-12 13:56:59 +09001099 def _ParseProject(self, node, parent=None, **extra_proj_attrs):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001100 """
1101 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -07001102 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001103 name = self._reqatt(node, 'name')
Mike Frysingera29424e2021-02-25 21:53:49 -05001104 msg = self._CheckLocalPath(name, dir_ok=True)
1105 if msg:
1106 raise ManifestInvalidPathError(
1107 '<project> invalid "name": %s: %s' % (name, msg))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001108 if parent:
1109 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001110
1111 remote = self._get_remote(node)
1112 if remote is None:
1113 remote = self._default.remote
1114 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301115 raise ManifestParseError("no remote for project %s within %s" %
David Pursehouseabdf7502020-02-12 14:58:39 +09001116 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001117
Anthony King36ea2fb2014-05-06 11:54:01 +01001118 revisionExpr = node.getAttribute('revision') or remote.revision
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -07001119 if not revisionExpr:
1120 revisionExpr = self._default.revisionExpr
1121 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301122 raise ManifestParseError("no revision for project %s within %s" %
David Pursehouseabdf7502020-02-12 14:58:39 +09001123 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001124
1125 path = node.getAttribute('path')
1126 if not path:
1127 path = name
Mike Frysingera29424e2021-02-25 21:53:49 -05001128 else:
Mike Frysinger0458faa2021-03-10 23:35:44 -05001129 # NB: The "." project is handled specially in Project.Sync_LocalHalf.
1130 msg = self._CheckLocalPath(path, dir_ok=True, cwd_dot_ok=True)
Mike Frysingera29424e2021-02-25 21:53:49 -05001131 if msg:
1132 raise ManifestInvalidPathError(
1133 '<project> invalid "path": %s: %s' % (path, msg))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001134
Mike Frysingerbb8ee7f2020-02-22 05:30:12 -05001135 rebase = XmlBool(node, 'rebase', True)
1136 sync_c = XmlBool(node, 'sync-c', False)
1137 sync_s = XmlBool(node, 'sync-s', self._default.sync_s)
1138 sync_tags = XmlBool(node, 'sync-tags', self._default.sync_tags)
Mike Pontillod3153822012-02-28 11:53:24 -08001139
Mike Frysingerbb8ee7f2020-02-22 05:30:12 -05001140 clone_depth = XmlInt(node, 'clone-depth')
1141 if clone_depth is not None and clone_depth <= 0:
1142 raise ManifestParseError('%s: clone-depth must be greater than 0, not "%s"' %
1143 (self.manifestFile, clone_depth))
David Pursehouseede7f122012-11-27 22:25:30 +09001144
Bryan Jacobsf609f912013-05-06 13:36:24 -04001145 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
1146
Nasser Grainawida403412018-05-04 12:53:29 -06001147 upstream = node.getAttribute('upstream') or self._default.upstreamExpr
Brian Harring14a66742012-09-28 20:21:57 -07001148
Conley Owens971de8e2012-04-16 10:36:08 -07001149 groups = ''
1150 if node.hasAttribute('groups'):
1151 groups = node.getAttribute('groups')
Mike Frysinger51e39d52020-12-04 05:32:06 -05001152 groups = self._ParseList(groups)
Brian Harring7da13142012-06-15 02:24:20 -07001153
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001154 if parent is None:
Mike Frysinger979d5bd2020-02-09 02:28:34 -05001155 relpath, worktree, gitdir, objdir, use_git_worktrees = \
1156 self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -07001157 else:
Mike Frysinger979d5bd2020-02-09 02:28:34 -05001158 use_git_worktrees = False
David James8d201162013-10-11 17:03:19 -07001159 relpath, worktree, gitdir, objdir = \
1160 self.GetSubprojectPaths(parent, name, path)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001161
1162 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
1163 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -07001164
Scott Fandb83b1b2013-02-28 09:34:14 +08001165 if self.IsMirror and node.hasAttribute('force-path'):
Mike Frysingerbb8ee7f2020-02-22 05:30:12 -05001166 if XmlBool(node, 'force-path', False):
Scott Fandb83b1b2013-02-28 09:34:14 +08001167 gitdir = os.path.join(self.topdir, '%s.git' % path)
1168
David Pursehousee5913ae2020-02-12 13:56:59 +09001169 project = Project(manifest=self,
1170 name=name,
1171 remote=remote.ToRemoteSpec(name),
1172 gitdir=gitdir,
1173 objdir=objdir,
1174 worktree=worktree,
1175 relpath=relpath,
1176 revisionExpr=revisionExpr,
1177 revisionId=None,
1178 rebase=rebase,
1179 groups=groups,
1180 sync_c=sync_c,
1181 sync_s=sync_s,
1182 sync_tags=sync_tags,
1183 clone_depth=clone_depth,
1184 upstream=upstream,
1185 parent=parent,
1186 dest_branch=dest_branch,
Mike Frysinger979d5bd2020-02-09 02:28:34 -05001187 use_git_worktrees=use_git_worktrees,
Simran Basib9a1b732015-08-20 12:19:28 -07001188 **extra_proj_attrs)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001189
1190 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -07001191 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001192 self._ParseCopyFile(project, n)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001193 if n.nodeName == 'linkfile':
1194 self._ParseLinkFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -05001195 if n.nodeName == 'annotation':
1196 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001197 if n.nodeName == 'project':
David Pursehousee5913ae2020-02-12 13:56:59 +09001198 project.subprojects.append(self._ParseProject(n, parent=project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001199
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001200 return project
1201
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001202 def GetProjectPaths(self, name, path):
Mike Frysingercebf2272020-05-26 01:02:29 -04001203 # The manifest entries might have trailing slashes. Normalize them to avoid
1204 # unexpected filesystem behavior since we do string concatenation below.
1205 path = path.rstrip('/')
1206 name = name.rstrip('/')
Mike Frysinger979d5bd2020-02-09 02:28:34 -05001207 use_git_worktrees = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001208 relpath = path
1209 if self.IsMirror:
1210 worktree = None
1211 gitdir = os.path.join(self.topdir, '%s.git' % name)
David James8d201162013-10-11 17:03:19 -07001212 objdir = gitdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001213 else:
1214 worktree = os.path.join(self.topdir, path).replace('\\', '/')
1215 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
Mike Frysinger979d5bd2020-02-09 02:28:34 -05001216 # We allow people to mix git worktrees & non-git worktrees for now.
1217 # This allows for in situ migration of repo clients.
1218 if os.path.exists(gitdir) or not self.UseGitWorktrees:
1219 objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name)
1220 else:
1221 use_git_worktrees = True
1222 gitdir = os.path.join(self.repodir, 'worktrees', '%s.git' % name)
1223 objdir = gitdir
1224 return relpath, worktree, gitdir, objdir, use_git_worktrees
David James8d201162013-10-11 17:03:19 -07001225
1226 def GetProjectsWithName(self, name):
1227 return self._projects.get(name, [])
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001228
1229 def GetSubprojectName(self, parent, submodule_path):
1230 return os.path.join(parent.name, submodule_path)
1231
1232 def _JoinRelpath(self, parent_relpath, relpath):
1233 return os.path.join(parent_relpath, relpath)
1234
1235 def _UnjoinRelpath(self, parent_relpath, relpath):
1236 return os.path.relpath(relpath, parent_relpath)
1237
David James8d201162013-10-11 17:03:19 -07001238 def GetSubprojectPaths(self, parent, name, path):
Mike Frysingercebf2272020-05-26 01:02:29 -04001239 # The manifest entries might have trailing slashes. Normalize them to avoid
1240 # unexpected filesystem behavior since we do string concatenation below.
1241 path = path.rstrip('/')
1242 name = name.rstrip('/')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001243 relpath = self._JoinRelpath(parent.relpath, path)
1244 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -07001245 objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001246 if self.IsMirror:
1247 worktree = None
1248 else:
1249 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
David James8d201162013-10-11 17:03:19 -07001250 return relpath, worktree, gitdir, objdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +08001251
Mike Frysinger04122b72019-07-31 23:32:58 -04001252 @staticmethod
Mike Frysingera00c5f42021-02-25 18:26:31 -05001253 def _CheckLocalPath(path, dir_ok=False, cwd_dot_ok=False):
1254 """Verify |path| is reasonable for use in filesystem paths.
1255
Mike Frysingera29424e2021-02-25 21:53:49 -05001256 Used with <copyfile> & <linkfile> & <project> elements.
Mike Frysingera00c5f42021-02-25 18:26:31 -05001257
1258 This only validates the |path| in isolation: it does not check against the
1259 current filesystem state. Thus it is suitable as a first-past in a parser.
1260
1261 It enforces a number of constraints:
1262 * No empty paths.
1263 * No "~" in paths.
1264 * No Unicode codepoints that filesystems might elide when normalizing.
1265 * No relative path components like "." or "..".
1266 * No absolute paths.
1267 * No ".git" or ".repo*" path components.
1268
1269 Args:
1270 path: The path name to validate.
1271 dir_ok: Whether |path| may force a directory (e.g. end in a /).
1272 cwd_dot_ok: Whether |path| may be just ".".
1273
1274 Returns:
1275 None if |path| is OK, a failure message otherwise.
1276 """
1277 if not path:
1278 return 'empty paths not allowed'
1279
Mike Frysinger04122b72019-07-31 23:32:58 -04001280 if '~' in path:
1281 return '~ not allowed (due to 8.3 filenames on Windows filesystems)'
1282
Mike Frysingerf69c7ee2021-04-29 23:15:31 -04001283 path_codepoints = set(path)
1284
Mike Frysinger04122b72019-07-31 23:32:58 -04001285 # Some filesystems (like Apple's HFS+) try to normalize Unicode codepoints
1286 # which means there are alternative names for ".git". Reject paths with
1287 # these in it as there shouldn't be any reasonable need for them here.
1288 # The set of codepoints here was cribbed from jgit's implementation:
1289 # https://eclipse.googlesource.com/jgit/jgit/+/9110037e3e9461ff4dac22fee84ef3694ed57648/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java#884
1290 BAD_CODEPOINTS = {
1291 u'\u200C', # ZERO WIDTH NON-JOINER
1292 u'\u200D', # ZERO WIDTH JOINER
1293 u'\u200E', # LEFT-TO-RIGHT MARK
1294 u'\u200F', # RIGHT-TO-LEFT MARK
1295 u'\u202A', # LEFT-TO-RIGHT EMBEDDING
1296 u'\u202B', # RIGHT-TO-LEFT EMBEDDING
1297 u'\u202C', # POP DIRECTIONAL FORMATTING
1298 u'\u202D', # LEFT-TO-RIGHT OVERRIDE
1299 u'\u202E', # RIGHT-TO-LEFT OVERRIDE
1300 u'\u206A', # INHIBIT SYMMETRIC SWAPPING
1301 u'\u206B', # ACTIVATE SYMMETRIC SWAPPING
1302 u'\u206C', # INHIBIT ARABIC FORM SHAPING
1303 u'\u206D', # ACTIVATE ARABIC FORM SHAPING
1304 u'\u206E', # NATIONAL DIGIT SHAPES
1305 u'\u206F', # NOMINAL DIGIT SHAPES
1306 u'\uFEFF', # ZERO WIDTH NO-BREAK SPACE
1307 }
Mike Frysingerf69c7ee2021-04-29 23:15:31 -04001308 if BAD_CODEPOINTS & path_codepoints:
Mike Frysinger04122b72019-07-31 23:32:58 -04001309 # This message is more expansive than reality, but should be fine.
1310 return 'Unicode combining characters not allowed'
1311
Mike Frysingerf69c7ee2021-04-29 23:15:31 -04001312 # Reject newlines as there shouldn't be any legitmate use for them, they'll
1313 # be confusing to users, and they can easily break tools that expect to be
1314 # able to iterate over newline delimited lists. This even applies to our
1315 # own code like .repo/project.list.
1316 if {'\r', '\n'} & path_codepoints:
1317 return 'Newlines not allowed'
1318
Mike Frysinger04122b72019-07-31 23:32:58 -04001319 # Assume paths might be used on case-insensitive filesystems.
1320 path = path.lower()
1321
Mike Frysingerd9254592020-02-19 22:36:26 -05001322 # Split up the path by its components. We can't use os.path.sep exclusively
1323 # as some platforms (like Windows) will convert / to \ and that bypasses all
1324 # our constructed logic here. Especially since manifest authors only use
1325 # / in their paths.
1326 resep = re.compile(r'[/%s]' % re.escape(os.path.sep))
Mike Frysinger0458faa2021-03-10 23:35:44 -05001327 # Strip off trailing slashes as those only produce '' elements, and we use
1328 # parts to look for individual bad components.
1329 parts = resep.split(path.rstrip('/'))
Mike Frysingerd9254592020-02-19 22:36:26 -05001330
Mike Frysingerae625412020-02-10 17:10:03 -05001331 # Some people use src="." to create stable links to projects. Lets allow
1332 # that but reject all other uses of "." to keep things simple.
Mike Frysingera00c5f42021-02-25 18:26:31 -05001333 if not cwd_dot_ok or parts != ['.']:
Mike Frysingerae625412020-02-10 17:10:03 -05001334 for part in set(parts):
1335 if part in {'.', '..', '.git'} or part.startswith('.repo'):
1336 return 'bad component: %s' % (part,)
Mike Frysinger04122b72019-07-31 23:32:58 -04001337
Mike Frysingera00c5f42021-02-25 18:26:31 -05001338 if not dir_ok and resep.match(path[-1]):
Mike Frysinger04122b72019-07-31 23:32:58 -04001339 return 'dirs not allowed'
1340
Mike Frysingerd9254592020-02-19 22:36:26 -05001341 # NB: The two abspath checks here are to handle platforms with multiple
1342 # filesystem path styles (e.g. Windows).
Mike Frysinger04122b72019-07-31 23:32:58 -04001343 norm = os.path.normpath(path)
Mike Frysingerd9254592020-02-19 22:36:26 -05001344 if (norm == '..' or
1345 (len(norm) >= 3 and norm.startswith('..') and resep.match(norm[0])) or
1346 os.path.isabs(norm) or
1347 norm.startswith('/')):
Mike Frysinger04122b72019-07-31 23:32:58 -04001348 return 'path cannot be outside'
1349
1350 @classmethod
1351 def _ValidateFilePaths(cls, element, src, dest):
1352 """Verify |src| & |dest| are reasonable for <copyfile> & <linkfile>.
1353
1354 We verify the path independent of any filesystem state as we won't have a
1355 checkout available to compare to. i.e. This is for parsing validation
1356 purposes only.
1357
1358 We'll do full/live sanity checking before we do the actual filesystem
1359 modifications in _CopyFile/_LinkFile/etc...
1360 """
1361 # |dest| is the file we write to or symlink we create.
1362 # It is relative to the top of the repo client checkout.
1363 msg = cls._CheckLocalPath(dest)
1364 if msg:
1365 raise ManifestInvalidPathError(
1366 '<%s> invalid "dest": %s: %s' % (element, dest, msg))
1367
1368 # |src| is the file we read from or path we point to for symlinks.
1369 # It is relative to the top of the git project checkout.
Mike Frysingera00c5f42021-02-25 18:26:31 -05001370 is_linkfile = element == 'linkfile'
1371 msg = cls._CheckLocalPath(src, dir_ok=is_linkfile, cwd_dot_ok=is_linkfile)
Mike Frysinger04122b72019-07-31 23:32:58 -04001372 if msg:
1373 raise ManifestInvalidPathError(
1374 '<%s> invalid "src": %s: %s' % (element, src, msg))
1375
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001376 def _ParseCopyFile(self, project, node):
1377 src = self._reqatt(node, 'src')
1378 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001379 if not self.IsMirror:
1380 # src is project relative;
Mike Frysinger04122b72019-07-31 23:32:58 -04001381 # dest is relative to the top of the tree.
1382 # We only validate paths if we actually plan to process them.
1383 self._ValidateFilePaths('copyfile', src, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -04001384 project.AddCopyFile(src, dest, self.topdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001385
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001386 def _ParseLinkFile(self, project, node):
1387 src = self._reqatt(node, 'src')
1388 dest = self._reqatt(node, 'dest')
1389 if not self.IsMirror:
1390 # src is project relative;
Mike Frysinger04122b72019-07-31 23:32:58 -04001391 # dest is relative to the top of the tree.
1392 # We only validate paths if we actually plan to process them.
1393 self._ValidateFilePaths('linkfile', src, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -04001394 project.AddLinkFile(src, dest, self.topdir)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001395
Jack Neus6ea0cae2021-07-20 20:52:33 +00001396 def _ParseAnnotation(self, element, node):
James W. Mills24c13082012-04-12 15:04:13 -05001397 name = self._reqatt(node, 'name')
1398 value = self._reqatt(node, 'value')
1399 try:
1400 keep = self._reqatt(node, 'keep').lower()
1401 except ManifestParseError:
1402 keep = "true"
1403 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301404 raise ManifestParseError('optional "keep" attribute must be '
David Pursehouseabdf7502020-02-12 14:58:39 +09001405 '"true" or "false"')
Jack Neus6ea0cae2021-07-20 20:52:33 +00001406 element.AddAnnotation(name, value, keep)
James W. Mills24c13082012-04-12 15:04:13 -05001407
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001408 def _get_remote(self, node):
1409 name = node.getAttribute('remote')
1410 if not name:
1411 return None
1412
1413 v = self._remotes.get(name)
1414 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301415 raise ManifestParseError("remote %s not defined in %s" %
David Pursehouseabdf7502020-02-12 14:58:39 +09001416 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001417 return v
1418
1419 def _reqatt(self, node, attname):
1420 """
1421 reads a required attribute from the node.
1422 """
1423 v = node.getAttribute(attname)
1424 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301425 raise ManifestParseError("no %s in <%s> within %s" %
David Pursehouseabdf7502020-02-12 14:58:39 +09001426 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001427 return v
Julien Camperguedd654222014-01-09 16:21:37 +01001428
1429 def projectsDiff(self, manifest):
1430 """return the projects differences between two manifests.
1431
1432 The diff will be from self to given manifest.
1433
1434 """
1435 fromProjects = self.paths
1436 toProjects = manifest.paths
1437
Anthony King7446c592014-05-06 09:19:39 +01001438 fromKeys = sorted(fromProjects.keys())
1439 toKeys = sorted(toProjects.keys())
Julien Camperguedd654222014-01-09 16:21:37 +01001440
1441 diff = {'added': [], 'removed': [], 'changed': [], 'unreachable': []}
1442
1443 for proj in fromKeys:
David Pursehouseeeff3532020-02-12 11:24:10 +09001444 if proj not in toKeys:
Julien Camperguedd654222014-01-09 16:21:37 +01001445 diff['removed'].append(fromProjects[proj])
1446 else:
1447 fromProj = fromProjects[proj]
1448 toProj = toProjects[proj]
1449 try:
1450 fromRevId = fromProj.GetCommitRevisionId()
1451 toRevId = toProj.GetCommitRevisionId()
1452 except ManifestInvalidRevisionError:
1453 diff['unreachable'].append((fromProj, toProj))
1454 else:
1455 if fromRevId != toRevId:
1456 diff['changed'].append((fromProj, toProj))
1457 toKeys.remove(proj)
1458
1459 for proj in toKeys:
1460 diff['added'].append(toProjects[proj])
1461
1462 return diff
Simran Basib9a1b732015-08-20 12:19:28 -07001463
1464
1465class GitcManifest(XmlManifest):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -04001466 """Parser for GitC (git-in-the-cloud) manifests."""
Simran Basib9a1b732015-08-20 12:19:28 -07001467
David Pursehousee5913ae2020-02-12 13:56:59 +09001468 def _ParseProject(self, node, parent=None):
Simran Basib9a1b732015-08-20 12:19:28 -07001469 """Override _ParseProject and add support for GITC specific attributes."""
Mike Frysinger5d9c4972021-02-19 13:34:09 -05001470 return super()._ParseProject(
Simran Basib9a1b732015-08-20 12:19:28 -07001471 node, parent=parent, old_revision=node.getAttribute('old-revision'))
1472
1473 def _output_manifest_project_extras(self, p, e):
1474 """Output GITC Specific Project attributes"""
1475 if p.old_revision:
Stefan Beller66851062016-06-17 16:40:08 -07001476 e.setAttribute('old-revision', str(p.old_revision))
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -04001477
1478
1479class RepoClient(XmlManifest):
1480 """Manages a repo client checkout."""
1481
1482 def __init__(self, repodir, manifest_file=None):
1483 self.isGitcClient = False
1484
1485 if os.path.exists(os.path.join(repodir, LOCAL_MANIFEST_NAME)):
1486 print('error: %s is not supported; put local manifests in `%s` instead' %
1487 (LOCAL_MANIFEST_NAME, os.path.join(repodir, LOCAL_MANIFESTS_DIR_NAME)),
1488 file=sys.stderr)
1489 sys.exit(1)
1490
1491 if manifest_file is None:
1492 manifest_file = os.path.join(repodir, MANIFEST_FILE_NAME)
1493 local_manifests = os.path.abspath(os.path.join(repodir, LOCAL_MANIFESTS_DIR_NAME))
Mike Frysinger5d9c4972021-02-19 13:34:09 -05001494 super().__init__(repodir, manifest_file, local_manifests)
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -04001495
1496 # TODO: Completely separate manifest logic out of the client.
1497 self.manifest = self
1498
1499
1500class GitcClient(RepoClient, GitcManifest):
1501 """Manages a GitC client checkout."""
1502
1503 def __init__(self, repodir, gitc_client_name):
1504 """Initialize the GitcManifest object."""
1505 self.gitc_client_name = gitc_client_name
1506 self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
1507 gitc_client_name)
1508
Mike Frysinger5d9c4972021-02-19 13:34:09 -05001509 super().__init__(repodir, os.path.join(self.gitc_client_dir, '.manifest'))
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -04001510 self.isGitcClient = True