blob: e951bbf570fec33a5ec3c2916e67bcdd844e0e58 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
3# Copyright (C) 2008 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Colin Cross23acdd32012-04-21 00:33:54 -070018import itertools
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019import os
Conley Owensdb728cd2011-09-26 16:34:01 -070020import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021import sys
David Pursehouse59bbb582013-05-17 10:49:33 +090022import xml.dom.minidom
23
24from pyversion import is_python3
25if is_python3():
Chirayu Desai217ea7d2013-03-01 19:14:38 +053026 import urllib.parse
David Pursehouse59bbb582013-05-17 10:49:33 +090027else:
Chirayu Desai217ea7d2013-03-01 19:14:38 +053028 import imp
29 import urlparse
30 urllib = imp.new_module('urllib')
Chirayu Desaidb2ad9d2013-06-11 13:42:25 +053031 urllib.parse = urlparse
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Simran Basib9a1b732015-08-20 12:19:28 -070033import gitc_utils
David Pursehousee15c65a2012-08-22 10:46:11 +090034from git_config import GitConfig
David Pursehousee00aa6b2012-09-11 14:33:51 +090035from git_refs import R_HEADS, HEAD
Renaud Paquayd5cec5e2016-11-01 11:24:03 -070036import platform_utils
David Pursehousee00aa6b2012-09-11 14:33:51 +090037from project import RemoteSpec, Project, MetaProject
Mike Frysinger04122b72019-07-31 23:32:58 -040038from error import (ManifestParseError, ManifestInvalidPathError,
39 ManifestInvalidRevisionError)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040
41MANIFEST_FILE_NAME = 'manifest.xml'
Shawn O. Pearce5cc66792008-10-23 16:19:27 -070042LOCAL_MANIFEST_NAME = 'local_manifest.xml'
David Pursehouse2d5a0df2012-11-13 02:50:36 +090043LOCAL_MANIFESTS_DIR_NAME = 'local_manifests'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070044
Anthony Kingcb07ba72015-03-28 23:26:04 +000045# urljoin gets confused if the scheme is not known.
Joe Kilner6e310792016-10-27 15:53:53 -070046urllib.parse.uses_relative.extend([
47 'ssh',
48 'git',
49 'persistent-https',
50 'sso',
51 'rpc'])
52urllib.parse.uses_netloc.extend([
53 'ssh',
54 'git',
55 'persistent-https',
56 'sso',
57 'rpc'])
Conley Owensdb728cd2011-09-26 16:34:01 -070058
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059class _Default(object):
60 """Project defaults within the manifest."""
61
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -070062 revisionExpr = None
Conley Owensb6a16e62013-09-25 15:06:09 -070063 destBranchExpr = None
Nasser Grainawida403412018-05-04 12:53:29 -060064 upstreamExpr = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070065 remote = None
Shawn O. Pearce6392c872011-09-22 17:44:31 -070066 sync_j = 1
Anatol Pomazau79770d22012-04-20 14:41:59 -070067 sync_c = False
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +080068 sync_s = False
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +090069 sync_tags = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070070
Julien Campergue74879922013-10-09 14:38:46 +020071 def __eq__(self, other):
72 return self.__dict__ == other.__dict__
73
74 def __ne__(self, other):
75 return self.__dict__ != other.__dict__
76
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070077class _XmlRemote(object):
78 def __init__(self,
79 name,
Yestin Sunb292b982012-07-02 07:32:50 -070080 alias=None,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070081 fetch=None,
Steve Raed6480452016-08-10 15:00:00 -070082 pushUrl=None,
Conley Owensdb728cd2011-09-26 16:34:01 -070083 manifestUrl=None,
Anthony King36ea2fb2014-05-06 11:54:01 +010084 review=None,
Jonathan Nieder93719792015-03-17 11:29:58 -070085 revision=None):
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070086 self.name = name
87 self.fetchUrl = fetch
Steve Raed6480452016-08-10 15:00:00 -070088 self.pushUrl = pushUrl
Conley Owensdb728cd2011-09-26 16:34:01 -070089 self.manifestUrl = manifestUrl
Yestin Sunb292b982012-07-02 07:32:50 -070090 self.remoteAlias = alias
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070091 self.reviewUrl = review
Anthony King36ea2fb2014-05-06 11:54:01 +010092 self.revision = revision
Conley Owensceea3682011-10-20 10:45:47 -070093 self.resolvedFetchUrl = self._resolveFetchUrl()
Shawn O. Pearced1f70d92009-05-19 14:58:02 -070094
David Pursehouse717ece92012-11-13 08:49:16 +090095 def __eq__(self, other):
96 return self.__dict__ == other.__dict__
97
98 def __ne__(self, other):
99 return self.__dict__ != other.__dict__
100
Conley Owensceea3682011-10-20 10:45:47 -0700101 def _resolveFetchUrl(self):
102 url = self.fetchUrl.rstrip('/')
Conley Owensdb728cd2011-09-26 16:34:01 -0700103 manifestUrl = self.manifestUrl.rstrip('/')
Conley Owens2d0f5082014-01-31 15:03:51 -0800104 # urljoin will gets confused over quite a few things. The ones we care
105 # about here are:
106 # * no scheme in the base url, like <hostname:port>
Anthony Kingcb07ba72015-03-28 23:26:04 +0000107 # We handle no scheme by replacing it with an obscure protocol, gopher
108 # and then replacing it with the original when we are done.
109
Conley Owensdb728cd2011-09-26 16:34:01 -0700110 if manifestUrl.find(':') != manifestUrl.find('/') - 1:
Conley Owens4ccad752015-04-29 10:45:37 -0700111 url = urllib.parse.urljoin('gopher://' + manifestUrl, url)
112 url = re.sub(r'^gopher://', '', url)
Anthony Kingcb07ba72015-03-28 23:26:04 +0000113 else:
114 url = urllib.parse.urljoin(manifestUrl, url)
Shawn Pearcea9f11b32013-01-02 15:40:48 -0800115 return url
Conley Owensceea3682011-10-20 10:45:47 -0700116
117 def ToRemoteSpec(self, projectName):
David Rileye0684ad2017-04-05 00:02:59 -0700118 fetchUrl = self.resolvedFetchUrl.rstrip('/')
119 url = fetchUrl + '/' + projectName
Yestin Sunb292b982012-07-02 07:32:50 -0700120 remoteName = self.name
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700121 if self.remoteAlias:
David Pursehouse37128b62013-10-15 10:48:40 +0900122 remoteName = self.remoteAlias
Dan Willemsen96c2d652016-04-06 16:03:54 -0700123 return RemoteSpec(remoteName,
124 url=url,
Steve Raed6480452016-08-10 15:00:00 -0700125 pushUrl=self.pushUrl,
Dan Willemsen96c2d652016-04-06 16:03:54 -0700126 review=self.reviewUrl,
David Rileye0684ad2017-04-05 00:02:59 -0700127 orig_name=self.name,
128 fetchUrl=self.fetchUrl)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700130class XmlManifest(object):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 """manages the repo configuration file"""
132
133 def __init__(self, repodir):
134 self.repodir = os.path.abspath(repodir)
135 self.topdir = os.path.dirname(self.repodir)
136 self.manifestFile = os.path.join(self.repodir, MANIFEST_FILE_NAME)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700137 self.globalConfig = GitConfig.ForUser()
David Pursehouse4eb285c2013-02-14 16:28:44 +0900138 self.localManifestWarning = False
Simran Basib9a1b732015-08-20 12:19:28 -0700139 self.isGitcClient = False
Basil Gelloc7453502018-05-25 20:23:52 +0300140 self._load_local_manifests = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700141
142 self.repoProject = MetaProject(self, 'repo',
143 gitdir = os.path.join(repodir, 'repo/.git'),
144 worktree = os.path.join(repodir, 'repo'))
145
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146 self.manifestProject = MetaProject(self, 'manifests',
Shawn O. Pearcef5c25a62008-11-04 08:11:53 -0800147 gitdir = os.path.join(repodir, 'manifests.git'),
148 worktree = os.path.join(repodir, 'manifests'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700149
150 self._Unload()
151
Basil Gelloc7453502018-05-25 20:23:52 +0300152 def Override(self, name, load_local_manifests=True):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700153 """Use a different manifest, just for the current instantiation.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154 """
Basil Gelloc7453502018-05-25 20:23:52 +0300155 path = None
156
157 # Look for a manifest by path in the filesystem (including the cwd).
158 if not load_local_manifests:
159 local_path = os.path.abspath(name)
160 if os.path.isfile(local_path):
161 path = local_path
162
163 # Look for manifests by name from the manifests repo.
164 if path is None:
165 path = os.path.join(self.manifestProject.worktree, name)
166 if not os.path.isfile(path):
167 raise ManifestParseError('manifest %s not found' % name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168
169 old = self.manifestFile
170 try:
Basil Gelloc7453502018-05-25 20:23:52 +0300171 self._load_local_manifests = load_local_manifests
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700172 self.manifestFile = path
173 self._Unload()
174 self._Load()
175 finally:
176 self.manifestFile = old
177
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700178 def Link(self, name):
179 """Update the repo metadata to use a different manifest.
180 """
181 self.Override(name)
182
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700183 try:
Sebastian Frias223bf962012-11-21 19:09:25 +0100184 if os.path.lexists(self.manifestFile):
Renaud Paquay010fed72016-11-11 14:25:29 -0800185 platform_utils.remove(self.manifestFile)
Renaud Paquayd5cec5e2016-11-01 11:24:03 -0700186 platform_utils.symlink(os.path.join('manifests', name), self.manifestFile)
Sebastian Frias223bf962012-11-21 19:09:25 +0100187 except OSError as e:
188 raise ManifestParseError('cannot link manifest %s: %s' % (name, str(e)))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700189
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800190 def _RemoteToXml(self, r, doc, root):
191 e = doc.createElement('remote')
192 root.appendChild(e)
193 e.setAttribute('name', r.name)
194 e.setAttribute('fetch', r.fetchUrl)
Steve Raed6480452016-08-10 15:00:00 -0700195 if r.pushUrl is not None:
196 e.setAttribute('pushurl', r.pushUrl)
Conley Owens1e7ab2a2013-10-08 17:26:57 -0700197 if r.remoteAlias is not None:
198 e.setAttribute('alias', r.remoteAlias)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800199 if r.reviewUrl is not None:
200 e.setAttribute('review', r.reviewUrl)
Anthony King36ea2fb2014-05-06 11:54:01 +0100201 if r.revision is not None:
202 e.setAttribute('revision', r.revision)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800203
Josh Triplett884a3872014-06-12 14:57:29 -0700204 def _ParseGroups(self, groups):
205 return [x for x in re.split(r'[,\s]+', groups) if x]
206
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700207 def Save(self, fd, peg_rev=False, peg_rev_upstream=True, groups=None):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800208 """Write the current manifest out to the given file descriptor.
209 """
Colin Cross5acde752012-03-28 20:15:45 -0700210 mp = self.manifestProject
211
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700212 if groups is None:
213 groups = mp.config.GetString('manifest.groups')
Matt Gumbel0c635bb2012-12-21 10:14:53 -0800214 if groups:
Josh Triplett884a3872014-06-12 14:57:29 -0700215 groups = self._ParseGroups(groups)
Colin Cross5acde752012-03-28 20:15:45 -0700216
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800217 doc = xml.dom.minidom.Document()
218 root = doc.createElement('manifest')
219 doc.appendChild(root)
220
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700221 # Save out the notice. There's a little bit of work here to give it the
222 # right whitespace, which assumes that the notice is automatically indented
223 # by 4 by minidom.
224 if self.notice:
225 notice_element = root.appendChild(doc.createElement('notice'))
226 notice_lines = self.notice.splitlines()
227 indented_notice = ('\n'.join(" "*4 + line for line in notice_lines))[4:]
228 notice_element.appendChild(doc.createTextNode(indented_notice))
229
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800230 d = self.default
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800231
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530232 for r in sorted(self.remotes):
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800233 self._RemoteToXml(self.remotes[r], doc, root)
234 if self.remotes:
235 root.appendChild(doc.createTextNode(''))
236
237 have_default = False
238 e = doc.createElement('default')
239 if d.remote:
240 have_default = True
241 e.setAttribute('remote', d.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700242 if d.revisionExpr:
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800243 have_default = True
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700244 e.setAttribute('revision', d.revisionExpr)
Simon Ruggier7e59de22015-07-24 12:50:06 +0200245 if d.destBranchExpr:
246 have_default = True
247 e.setAttribute('dest-branch', d.destBranchExpr)
Nasser Grainawida403412018-05-04 12:53:29 -0600248 if d.upstreamExpr:
249 have_default = True
250 e.setAttribute('upstream', d.upstreamExpr)
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700251 if d.sync_j > 1:
252 have_default = True
253 e.setAttribute('sync-j', '%d' % d.sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700254 if d.sync_c:
255 have_default = True
256 e.setAttribute('sync-c', 'true')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800257 if d.sync_s:
258 have_default = True
259 e.setAttribute('sync-s', 'true')
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900260 if not d.sync_tags:
261 have_default = True
262 e.setAttribute('sync-tags', 'false')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800263 if have_default:
264 root.appendChild(e)
265 root.appendChild(doc.createTextNode(''))
266
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700267 if self._manifest_server:
268 e = doc.createElement('manifest-server')
269 e.setAttribute('url', self._manifest_server)
270 root.appendChild(e)
271 root.appendChild(doc.createTextNode(''))
272
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800273 def output_projects(parent, parent_node, projects):
David James8d201162013-10-11 17:03:19 -0700274 for project_name in projects:
275 for project in self._projects[project_name]:
276 output_project(parent, parent_node, project)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800277
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800278 def output_project(parent, parent_node, p):
Colin Cross5acde752012-03-28 20:15:45 -0700279 if not p.MatchesGroups(groups):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800280 return
281
282 name = p.name
283 relpath = p.relpath
284 if parent:
285 name = self._UnjoinName(parent.name, name)
286 relpath = self._UnjoinRelpath(parent.relpath, relpath)
Colin Cross5acde752012-03-28 20:15:45 -0700287
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800288 e = doc.createElement('project')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800289 parent_node.appendChild(e)
290 e.setAttribute('name', name)
291 if relpath != name:
292 e.setAttribute('path', relpath)
Conley Owensa17d7af2013-10-16 14:38:09 -0700293 remoteName = None
294 if d.remote:
Dan Willemsen96c2d652016-04-06 16:03:54 -0700295 remoteName = d.remote.name
296 if not d.remote or p.remote.orig_name != remoteName:
297 remoteName = p.remote.orig_name
Anthony King36ea2fb2014-05-06 11:54:01 +0100298 e.setAttribute('remote', remoteName)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800299 if peg_rev:
300 if self.IsMirror:
Brian Harring14a66742012-09-28 20:21:57 -0700301 value = p.bare_git.rev_parse(p.revisionExpr + '^0')
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800302 else:
Brian Harring14a66742012-09-28 20:21:57 -0700303 value = p.work_git.rev_parse(HEAD + '^0')
304 e.setAttribute('revision', value)
Conley Owens551dfec2015-07-10 14:54:54 -0700305 if peg_rev_upstream:
306 if p.upstream:
307 e.setAttribute('upstream', p.upstream)
308 elif value != p.revisionExpr:
309 # Only save the origin if the origin is not a sha1, and the default
310 # isn't our value
311 e.setAttribute('upstream', p.revisionExpr)
Anthony King36ea2fb2014-05-06 11:54:01 +0100312 else:
Dan Willemsen96c2d652016-04-06 16:03:54 -0700313 revision = self.remotes[p.remote.orig_name].revision or d.revisionExpr
Anthony King36ea2fb2014-05-06 11:54:01 +0100314 if not revision or revision != p.revisionExpr:
315 e.setAttribute('revision', p.revisionExpr)
Nasser Grainawida403412018-05-04 12:53:29 -0600316 if (p.upstream and (p.upstream != p.revisionExpr or
317 p.upstream != d.upstreamExpr)):
Mani Chandel7a91d512014-07-24 16:27:08 +0530318 e.setAttribute('upstream', p.upstream)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800319
Simon Ruggier7e59de22015-07-24 12:50:06 +0200320 if p.dest_branch and p.dest_branch != d.destBranchExpr:
321 e.setAttribute('dest-branch', p.dest_branch)
322
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800323 for c in p.copyfiles:
324 ce = doc.createElement('copyfile')
325 ce.setAttribute('src', c.src)
326 ce.setAttribute('dest', c.dest)
327 e.appendChild(ce)
328
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500329 for l in p.linkfiles:
330 le = doc.createElement('linkfile')
331 le.setAttribute('src', l.src)
332 le.setAttribute('dest', l.dest)
333 e.appendChild(le)
334
Conley Owensbb1b5f52012-08-13 13:11:18 -0700335 default_groups = ['all', 'name:%s' % p.name, 'path:%s' % p.relpath]
Dmitry Fink17f85ea2012-08-06 14:52:29 -0700336 egroups = [g for g in p.groups if g not in default_groups]
Conley Owens971de8e2012-04-16 10:36:08 -0700337 if egroups:
338 e.setAttribute('groups', ','.join(egroups))
Colin Cross5acde752012-03-28 20:15:45 -0700339
James W. Mills24c13082012-04-12 15:04:13 -0500340 for a in p.annotations:
341 if a.keep == "true":
342 ae = doc.createElement('annotation')
343 ae.setAttribute('name', a.name)
344 ae.setAttribute('value', a.value)
345 e.appendChild(ae)
346
Anatol Pomazau79770d22012-04-20 14:41:59 -0700347 if p.sync_c:
348 e.setAttribute('sync-c', 'true')
349
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800350 if p.sync_s:
351 e.setAttribute('sync-s', 'true')
352
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900353 if not p.sync_tags:
354 e.setAttribute('sync-tags', 'false')
355
Dan Willemsen88409222015-08-17 15:29:10 -0700356 if p.clone_depth:
357 e.setAttribute('clone-depth', str(p.clone_depth))
358
Simran Basib9a1b732015-08-20 12:19:28 -0700359 self._output_manifest_project_extras(p, e)
360
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800361 if p.subprojects:
David James8d201162013-10-11 17:03:19 -0700362 subprojects = set(subp.name for subp in p.subprojects)
363 output_projects(p, e, list(sorted(subprojects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800364
David James8d201162013-10-11 17:03:19 -0700365 projects = set(p.name for p in self._paths.values() if not p.parent)
366 output_projects(None, root, list(sorted(projects)))
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800367
Doug Anderson37282b42011-03-04 11:54:18 -0800368 if self._repo_hooks_project:
369 root.appendChild(doc.createTextNode(''))
370 e = doc.createElement('repo-hooks')
371 e.setAttribute('in-project', self._repo_hooks_project.name)
372 e.setAttribute('enabled-list',
373 ' '.join(self._repo_hooks_project.enabled_repo_hooks))
374 root.appendChild(e)
375
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -0800376 doc.writexml(fd, '', ' ', '\n', 'UTF-8')
377
Simran Basib9a1b732015-08-20 12:19:28 -0700378 def _output_manifest_project_extras(self, p, e):
379 """Manifests can modify e if they support extra project attributes."""
380 pass
381
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700382 @property
David James8d201162013-10-11 17:03:19 -0700383 def paths(self):
384 self._Load()
385 return self._paths
386
387 @property
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700388 def projects(self):
389 self._Load()
Anthony Kingd58bfe52014-05-05 23:30:49 +0100390 return list(self._paths.values())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700391
392 @property
393 def remotes(self):
394 self._Load()
395 return self._remotes
396
397 @property
398 def default(self):
399 self._Load()
400 return self._default
401
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800402 @property
Doug Anderson37282b42011-03-04 11:54:18 -0800403 def repo_hooks_project(self):
404 self._Load()
405 return self._repo_hooks_project
406
407 @property
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700408 def notice(self):
409 self._Load()
410 return self._notice
411
412 @property
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700413 def manifest_server(self):
414 self._Load()
Shawn O. Pearce34fb20f2011-11-30 13:41:02 -0800415 return self._manifest_server
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700416
417 @property
Xin Li745be2e2019-06-03 11:24:30 -0700418 def CloneFilter(self):
419 if self.manifestProject.config.GetBoolean('repo.partialclone'):
420 return self.manifestProject.config.GetString('repo.clonefilter')
421 return None
422
423 @property
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800424 def IsMirror(self):
425 return self.manifestProject.config.GetBoolean('repo.mirror')
426
Julien Campergue335f5ef2013-10-16 11:02:35 +0200427 @property
428 def IsArchive(self):
429 return self.manifestProject.config.GetBoolean('repo.archive')
430
Martin Kellye4e94d22017-03-21 16:05:12 -0700431 @property
432 def HasSubmodules(self):
433 return self.manifestProject.config.GetBoolean('repo.submodules')
434
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700435 def _Unload(self):
436 self._loaded = False
437 self._projects = {}
David James8d201162013-10-11 17:03:19 -0700438 self._paths = {}
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700439 self._remotes = {}
440 self._default = None
Doug Anderson37282b42011-03-04 11:54:18 -0800441 self._repo_hooks_project = None
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700442 self._notice = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700443 self.branch = None
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700444 self._manifest_server = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700445
446 def _Load(self):
447 if not self._loaded:
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800448 m = self.manifestProject
449 b = m.GetBranch(m.CurrentBranch).merge
Shawn O. Pearce21c5c342009-06-25 16:47:30 -0700450 if b is not None and b.startswith(R_HEADS):
Shawn O. Pearce2450a292008-11-04 08:22:07 -0800451 b = b[len(R_HEADS):]
452 self.branch = b
453
Colin Cross23acdd32012-04-21 00:33:54 -0700454 nodes = []
Brian Harring475a47d2012-06-07 20:05:35 -0700455 nodes.append(self._ParseManifestXml(self.manifestFile,
456 self.manifestProject.worktree))
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700457
Basil Gelloc7453502018-05-25 20:23:52 +0300458 if self._load_local_manifests:
459 local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
460 if os.path.exists(local):
461 if not self.localManifestWarning:
462 self.localManifestWarning = True
463 print('warning: %s is deprecated; put local manifests '
464 'in `%s` instead' % (LOCAL_MANIFEST_NAME,
465 os.path.join(self.repodir, LOCAL_MANIFESTS_DIR_NAME)),
466 file=sys.stderr)
467 nodes.append(self._ParseManifestXml(local, self.repodir))
Colin Cross23acdd32012-04-21 00:33:54 -0700468
Basil Gelloc7453502018-05-25 20:23:52 +0300469 local_dir = os.path.abspath(os.path.join(self.repodir,
470 LOCAL_MANIFESTS_DIR_NAME))
471 try:
472 for local_file in sorted(platform_utils.listdir(local_dir)):
473 if local_file.endswith('.xml'):
474 local = os.path.join(local_dir, local_file)
475 nodes.append(self._ParseManifestXml(local, self.repodir))
476 except OSError:
477 pass
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900478
Joe Onorato26e24752013-01-11 12:35:53 -0800479 try:
480 self._ParseManifest(nodes)
481 except ManifestParseError as e:
482 # There was a problem parsing, unload ourselves in case they catch
483 # this error and try again later, we will show the correct error
484 self._Unload()
485 raise e
Shawn O. Pearce5cc66792008-10-23 16:19:27 -0700486
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800487 if self.IsMirror:
488 self._AddMetaProjectMirror(self.repoProject)
489 self._AddMetaProjectMirror(self.manifestProject)
490
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700491 self._loaded = True
492
Brian Harring475a47d2012-06-07 20:05:35 -0700493 def _ParseManifestXml(self, path, include_root):
David Pursehousef7fc8a92012-11-13 04:00:28 +0900494 try:
495 root = xml.dom.minidom.parse(path)
David Pursehouse2d5a0df2012-11-13 02:50:36 +0900496 except (OSError, xml.parsers.expat.ExpatError) as e:
David Pursehousef7fc8a92012-11-13 04:00:28 +0900497 raise ManifestParseError("error parsing manifest %s: %s" % (path, e))
498
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700499 if not root or not root.childNodes:
Brian Harring26448742011-04-28 05:04:41 -0700500 raise ManifestParseError("no root node in %s" % (path,))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700501
Jooncheol Park34acdd22012-08-27 02:25:59 +0900502 for manifest in root.childNodes:
503 if manifest.nodeName == 'manifest':
504 break
505 else:
Brian Harring26448742011-04-28 05:04:41 -0700506 raise ManifestParseError("no <manifest> in %s" % (path,))
507
Colin Cross23acdd32012-04-21 00:33:54 -0700508 nodes = []
David Pursehouse65b0ba52018-06-24 16:21:51 +0900509 for node in manifest.childNodes:
David Pursehousec1b86a22012-11-14 11:36:51 +0900510 if node.nodeName == 'include':
511 name = self._reqatt(node, 'name')
512 fp = os.path.join(include_root, name)
513 if not os.path.isfile(fp):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530514 raise ManifestParseError("include %s doesn't exist or isn't a file"
515 % (name,))
David Pursehousec1b86a22012-11-14 11:36:51 +0900516 try:
517 nodes.extend(self._ParseManifestXml(fp, include_root))
518 # should isolate this to the exact exception, but that's
519 # tricky. actual parsing implementation may vary.
520 except (KeyboardInterrupt, RuntimeError, SystemExit):
521 raise
522 except Exception as e:
523 raise ManifestParseError(
Mike Frysingerec558df2019-07-05 01:38:05 -0400524 "failed parsing included manifest %s: %s" % (name, e))
David Pursehousec1b86a22012-11-14 11:36:51 +0900525 else:
526 nodes.append(node)
Colin Cross23acdd32012-04-21 00:33:54 -0700527 return nodes
Brian Harring26448742011-04-28 05:04:41 -0700528
Colin Cross23acdd32012-04-21 00:33:54 -0700529 def _ParseManifest(self, node_list):
530 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700531 if node.nodeName == 'remote':
532 remote = self._ParseRemote(node)
David Pursehouse717ece92012-11-13 08:49:16 +0900533 if remote:
534 if remote.name in self._remotes:
535 if remote != self._remotes[remote.name]:
536 raise ManifestParseError(
537 'remote %s already exists with different attributes' %
538 (remote.name))
539 else:
540 self._remotes[remote.name] = remote
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700541
Colin Cross23acdd32012-04-21 00:33:54 -0700542 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700543 if node.nodeName == 'default':
Julien Campergue74879922013-10-09 14:38:46 +0200544 new_default = self._ParseDefault(node)
545 if self._default is None:
546 self._default = new_default
547 elif new_default != self._default:
David Pursehouse37128b62013-10-15 10:48:40 +0900548 raise ManifestParseError('duplicate default in %s' %
549 (self.manifestFile))
Julien Campergue74879922013-10-09 14:38:46 +0200550
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700551 if self._default is None:
552 self._default = _Default()
553
Colin Cross23acdd32012-04-21 00:33:54 -0700554 for node in itertools.chain(*node_list):
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700555 if node.nodeName == 'notice':
556 if self._notice is not None:
Doug Anderson37282b42011-03-04 11:54:18 -0800557 raise ManifestParseError(
558 'duplicate notice in %s' %
559 (self.manifestFile))
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700560 self._notice = self._ParseNotice(node)
561
Colin Cross23acdd32012-04-21 00:33:54 -0700562 for node in itertools.chain(*node_list):
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700563 if node.nodeName == 'manifest-server':
564 url = self._reqatt(node, 'url')
565 if self._manifest_server is not None:
David Pursehousec1b86a22012-11-14 11:36:51 +0900566 raise ManifestParseError(
567 'duplicate manifest-server in %s' %
568 (self.manifestFile))
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700569 self._manifest_server = url
570
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800571 def recursively_add_projects(project):
David James8d201162013-10-11 17:03:19 -0700572 projects = self._projects.setdefault(project.name, [])
573 if project.relpath is None:
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800574 raise ManifestParseError(
David James8d201162013-10-11 17:03:19 -0700575 'missing path for %s in %s' %
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800576 (project.name, self.manifestFile))
David James8d201162013-10-11 17:03:19 -0700577 if project.relpath in self._paths:
578 raise ManifestParseError(
579 'duplicate path %s in %s' %
580 (project.relpath, self.manifestFile))
581 self._paths[project.relpath] = project
582 projects.append(project)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800583 for subproject in project.subprojects:
584 recursively_add_projects(subproject)
585
Colin Cross23acdd32012-04-21 00:33:54 -0700586 for node in itertools.chain(*node_list):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700587 if node.nodeName == 'project':
588 project = self._ParseProject(node)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800589 recursively_add_projects(project)
Josh Triplett884a3872014-06-12 14:57:29 -0700590 if node.nodeName == 'extend-project':
591 name = self._reqatt(node, 'name')
592
593 if name not in self._projects:
594 raise ManifestParseError('extend-project element specifies non-existent '
595 'project: %s' % name)
596
597 path = node.getAttribute('path')
598 groups = node.getAttribute('groups')
599 if groups:
600 groups = self._ParseGroups(groups)
Luis Hector Chavez7d525852018-03-15 09:54:08 -0700601 revision = node.getAttribute('revision')
Kyunam Jobd0aae92020-02-04 11:38:53 +0900602 remote = node.getAttribute('remote')
603 if remote:
604 remote = self._get_remote(node)
Josh Triplett884a3872014-06-12 14:57:29 -0700605
606 for p in self._projects[name]:
607 if path and p.relpath != path:
608 continue
609 if groups:
610 p.groups.extend(groups)
Luis Hector Chavez7d525852018-03-15 09:54:08 -0700611 if revision:
612 p.revisionExpr = revision
Kyunam Jobd0aae92020-02-04 11:38:53 +0900613 if remote:
614 p.remote = remote.ToRemoteSpec(name)
Doug Anderson37282b42011-03-04 11:54:18 -0800615 if node.nodeName == 'repo-hooks':
616 # Get the name of the project and the (space-separated) list of enabled.
617 repo_hooks_project = self._reqatt(node, 'in-project')
618 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
619
620 # Only one project can be the hooks project
621 if self._repo_hooks_project is not None:
622 raise ManifestParseError(
623 'duplicate repo-hooks in %s' %
624 (self.manifestFile))
625
626 # Store a reference to the Project.
627 try:
David James8d201162013-10-11 17:03:19 -0700628 repo_hooks_projects = self._projects[repo_hooks_project]
Doug Anderson37282b42011-03-04 11:54:18 -0800629 except KeyError:
630 raise ManifestParseError(
631 'project %s not found for repo-hooks' %
632 (repo_hooks_project))
633
David James8d201162013-10-11 17:03:19 -0700634 if len(repo_hooks_projects) != 1:
635 raise ManifestParseError(
636 'internal error parsing repo-hooks in %s' %
637 (self.manifestFile))
638 self._repo_hooks_project = repo_hooks_projects[0]
639
Doug Anderson37282b42011-03-04 11:54:18 -0800640 # Store the enabled hooks in the Project object.
641 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700642 if node.nodeName == 'remove-project':
643 name = self._reqatt(node, 'name')
David Jamesb8433df2014-01-30 10:11:17 -0800644
645 if name not in self._projects:
David Pursehousef9107482012-11-16 19:12:32 +0900646 raise ManifestParseError('remove-project element specifies non-existent '
647 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700648
David Jamesb8433df2014-01-30 10:11:17 -0800649 for p in self._projects[name]:
650 del self._paths[p.relpath]
651 del self._projects[name]
652
Colin Cross23acdd32012-04-21 00:33:54 -0700653 # If the manifest removes the hooks project, treat it as if it deleted
654 # the repo-hooks element too.
655 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
656 self._repo_hooks_project = None
657
Doug Anderson37282b42011-03-04 11:54:18 -0800658
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800659 def _AddMetaProjectMirror(self, m):
660 name = None
661 m_url = m.GetRemote(m.remote.name).url
662 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530663 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800664
665 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700666 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800667 if not url.endswith('/'):
668 url += '/'
669 if m_url.startswith(url):
670 remote = self._default.remote
671 name = m_url[len(url):]
672
673 if name is None:
674 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700675 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700676 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800677 name = m_url[s:]
678
679 if name.endswith('.git'):
680 name = name[:-4]
681
682 if name not in self._projects:
683 m.PreSync()
684 gitdir = os.path.join(self.topdir, '%s.git' % name)
685 project = Project(manifest = self,
686 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700687 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800688 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700689 objdir = gitdir,
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800690 worktree = None,
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900691 relpath = name or None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700692 revisionExpr = m.revisionExpr,
693 revisionId = None)
David James8d201162013-10-11 17:03:19 -0700694 self._projects[project.name] = [project]
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900695 self._paths[project.relpath] = project
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800696
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700697 def _ParseRemote(self, node):
698 """
699 reads a <remote> element from the manifest file
700 """
701 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700702 alias = node.getAttribute('alias')
703 if alias == '':
704 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700705 fetch = self._reqatt(node, 'fetch')
Steve Raed6480452016-08-10 15:00:00 -0700706 pushUrl = node.getAttribute('pushurl')
707 if pushUrl == '':
708 pushUrl = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700709 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800710 if review == '':
711 review = None
Anthony King36ea2fb2014-05-06 11:54:01 +0100712 revision = node.getAttribute('revision')
713 if revision == '':
714 revision = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700715 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Steve Raed6480452016-08-10 15:00:00 -0700716 return _XmlRemote(name, alias, fetch, pushUrl, manifestUrl, review, revision)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700717
718 def _ParseDefault(self, node):
719 """
720 reads a <default> element from the manifest file
721 """
722 d = _Default()
723 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700724 d.revisionExpr = node.getAttribute('revision')
725 if d.revisionExpr == '':
726 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700727
Bryan Jacobsf609f912013-05-06 13:36:24 -0400728 d.destBranchExpr = node.getAttribute('dest-branch') or None
Nasser Grainawida403412018-05-04 12:53:29 -0600729 d.upstreamExpr = node.getAttribute('upstream') or None
Bryan Jacobsf609f912013-05-06 13:36:24 -0400730
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700731 sync_j = node.getAttribute('sync-j')
732 if sync_j == '' or sync_j is None:
733 d.sync_j = 1
734 else:
735 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700736
737 sync_c = node.getAttribute('sync-c')
738 if not sync_c:
739 d.sync_c = False
740 else:
741 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800742
743 sync_s = node.getAttribute('sync-s')
744 if not sync_s:
745 d.sync_s = False
746 else:
747 d.sync_s = sync_s.lower() in ("yes", "true", "1")
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900748
749 sync_tags = node.getAttribute('sync-tags')
750 if not sync_tags:
751 d.sync_tags = True
752 else:
753 d.sync_tags = sync_tags.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700754 return d
755
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700756 def _ParseNotice(self, node):
757 """
758 reads a <notice> element from the manifest file
759
760 The <notice> element is distinct from other tags in the XML in that the
761 data is conveyed between the start and end tag (it's not an empty-element
762 tag).
763
764 The white space (carriage returns, indentation) for the notice element is
765 relevant and is parsed in a way that is based on how python docstrings work.
766 In fact, the code is remarkably similar to here:
767 http://www.python.org/dev/peps/pep-0257/
768 """
769 # Get the data out of the node...
770 notice = node.childNodes[0].data
771
772 # Figure out minimum indentation, skipping the first line (the same line
773 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530774 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700775 lines = notice.splitlines()
776 for line in lines[1:]:
777 lstrippedLine = line.lstrip()
778 if lstrippedLine:
779 indent = len(line) - len(lstrippedLine)
780 minIndent = min(indent, minIndent)
781
782 # Strip leading / trailing blank lines and also indentation.
783 cleanLines = [lines[0].strip()]
784 for line in lines[1:]:
785 cleanLines.append(line[minIndent:].rstrip())
786
787 # Clear completely blank lines from front and back...
788 while cleanLines and not cleanLines[0]:
789 del cleanLines[0]
790 while cleanLines and not cleanLines[-1]:
791 del cleanLines[-1]
792
793 return '\n'.join(cleanLines)
794
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800795 def _JoinName(self, parent_name, name):
796 return os.path.join(parent_name, name)
797
798 def _UnjoinName(self, parent_name, name):
799 return os.path.relpath(name, parent_name)
800
Simran Basib9a1b732015-08-20 12:19:28 -0700801 def _ParseProject(self, node, parent = None, **extra_proj_attrs):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700802 """
803 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700804 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700805 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800806 if parent:
807 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700808
809 remote = self._get_remote(node)
810 if remote is None:
811 remote = self._default.remote
812 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530813 raise ManifestParseError("no remote for project %s within %s" %
814 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700815
Anthony King36ea2fb2014-05-06 11:54:01 +0100816 revisionExpr = node.getAttribute('revision') or remote.revision
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700817 if not revisionExpr:
818 revisionExpr = self._default.revisionExpr
819 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530820 raise ManifestParseError("no revision for project %s within %s" %
821 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700822
823 path = node.getAttribute('path')
824 if not path:
825 path = name
826 if path.startswith('/'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530827 raise ManifestParseError("project %s path cannot be absolute in %s" %
828 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700829
Mike Pontillod3153822012-02-28 11:53:24 -0800830 rebase = node.getAttribute('rebase')
831 if not rebase:
832 rebase = True
833 else:
834 rebase = rebase.lower() in ("yes", "true", "1")
835
Anatol Pomazau79770d22012-04-20 14:41:59 -0700836 sync_c = node.getAttribute('sync-c')
837 if not sync_c:
838 sync_c = False
839 else:
840 sync_c = sync_c.lower() in ("yes", "true", "1")
841
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800842 sync_s = node.getAttribute('sync-s')
843 if not sync_s:
844 sync_s = self._default.sync_s
845 else:
846 sync_s = sync_s.lower() in ("yes", "true", "1")
847
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900848 sync_tags = node.getAttribute('sync-tags')
849 if not sync_tags:
850 sync_tags = self._default.sync_tags
851 else:
852 sync_tags = sync_tags.lower() in ("yes", "true", "1")
853
David Pursehouseede7f122012-11-27 22:25:30 +0900854 clone_depth = node.getAttribute('clone-depth')
855 if clone_depth:
856 try:
857 clone_depth = int(clone_depth)
858 if clone_depth <= 0:
859 raise ValueError()
860 except ValueError:
861 raise ManifestParseError('invalid clone-depth %s in %s' %
862 (clone_depth, self.manifestFile))
863
Bryan Jacobsf609f912013-05-06 13:36:24 -0400864 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
865
Nasser Grainawida403412018-05-04 12:53:29 -0600866 upstream = node.getAttribute('upstream') or self._default.upstreamExpr
Brian Harring14a66742012-09-28 20:21:57 -0700867
Conley Owens971de8e2012-04-16 10:36:08 -0700868 groups = ''
869 if node.hasAttribute('groups'):
870 groups = node.getAttribute('groups')
Josh Triplett884a3872014-06-12 14:57:29 -0700871 groups = self._ParseGroups(groups)
Brian Harring7da13142012-06-15 02:24:20 -0700872
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800873 if parent is None:
David James8d201162013-10-11 17:03:19 -0700874 relpath, worktree, gitdir, objdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700875 else:
David James8d201162013-10-11 17:03:19 -0700876 relpath, worktree, gitdir, objdir = \
877 self.GetSubprojectPaths(parent, name, path)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800878
879 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
880 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700881
Scott Fandb83b1b2013-02-28 09:34:14 +0800882 if self.IsMirror and node.hasAttribute('force-path'):
883 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
884 gitdir = os.path.join(self.topdir, '%s.git' % path)
885
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700886 project = Project(manifest = self,
887 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700888 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700889 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700890 objdir = objdir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700891 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800892 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700893 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800894 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700895 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700896 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700897 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800898 sync_s = sync_s,
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900899 sync_tags = sync_tags,
David Pursehouseede7f122012-11-27 22:25:30 +0900900 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800901 upstream = upstream,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400902 parent = parent,
Simran Basib9a1b732015-08-20 12:19:28 -0700903 dest_branch = dest_branch,
904 **extra_proj_attrs)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700905
906 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700907 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700908 self._ParseCopyFile(project, n)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500909 if n.nodeName == 'linkfile':
910 self._ParseLinkFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500911 if n.nodeName == 'annotation':
912 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800913 if n.nodeName == 'project':
914 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700915
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700916 return project
917
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800918 def GetProjectPaths(self, name, path):
919 relpath = path
920 if self.IsMirror:
921 worktree = None
922 gitdir = os.path.join(self.topdir, '%s.git' % name)
David James8d201162013-10-11 17:03:19 -0700923 objdir = gitdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800924 else:
925 worktree = os.path.join(self.topdir, path).replace('\\', '/')
926 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700927 objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name)
928 return relpath, worktree, gitdir, objdir
929
930 def GetProjectsWithName(self, name):
931 return self._projects.get(name, [])
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800932
933 def GetSubprojectName(self, parent, submodule_path):
934 return os.path.join(parent.name, submodule_path)
935
936 def _JoinRelpath(self, parent_relpath, relpath):
937 return os.path.join(parent_relpath, relpath)
938
939 def _UnjoinRelpath(self, parent_relpath, relpath):
940 return os.path.relpath(relpath, parent_relpath)
941
David James8d201162013-10-11 17:03:19 -0700942 def GetSubprojectPaths(self, parent, name, path):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800943 relpath = self._JoinRelpath(parent.relpath, path)
944 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700945 objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800946 if self.IsMirror:
947 worktree = None
948 else:
949 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
David James8d201162013-10-11 17:03:19 -0700950 return relpath, worktree, gitdir, objdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800951
Mike Frysinger04122b72019-07-31 23:32:58 -0400952 @staticmethod
953 def _CheckLocalPath(path, symlink=False):
954 """Verify |path| is reasonable for use in <copyfile> & <linkfile>."""
955 if '~' in path:
956 return '~ not allowed (due to 8.3 filenames on Windows filesystems)'
957
958 # Some filesystems (like Apple's HFS+) try to normalize Unicode codepoints
959 # which means there are alternative names for ".git". Reject paths with
960 # these in it as there shouldn't be any reasonable need for them here.
961 # The set of codepoints here was cribbed from jgit's implementation:
962 # https://eclipse.googlesource.com/jgit/jgit/+/9110037e3e9461ff4dac22fee84ef3694ed57648/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java#884
963 BAD_CODEPOINTS = {
964 u'\u200C', # ZERO WIDTH NON-JOINER
965 u'\u200D', # ZERO WIDTH JOINER
966 u'\u200E', # LEFT-TO-RIGHT MARK
967 u'\u200F', # RIGHT-TO-LEFT MARK
968 u'\u202A', # LEFT-TO-RIGHT EMBEDDING
969 u'\u202B', # RIGHT-TO-LEFT EMBEDDING
970 u'\u202C', # POP DIRECTIONAL FORMATTING
971 u'\u202D', # LEFT-TO-RIGHT OVERRIDE
972 u'\u202E', # RIGHT-TO-LEFT OVERRIDE
973 u'\u206A', # INHIBIT SYMMETRIC SWAPPING
974 u'\u206B', # ACTIVATE SYMMETRIC SWAPPING
975 u'\u206C', # INHIBIT ARABIC FORM SHAPING
976 u'\u206D', # ACTIVATE ARABIC FORM SHAPING
977 u'\u206E', # NATIONAL DIGIT SHAPES
978 u'\u206F', # NOMINAL DIGIT SHAPES
979 u'\uFEFF', # ZERO WIDTH NO-BREAK SPACE
980 }
981 if BAD_CODEPOINTS & set(path):
982 # This message is more expansive than reality, but should be fine.
983 return 'Unicode combining characters not allowed'
984
985 # Assume paths might be used on case-insensitive filesystems.
986 path = path.lower()
987
Mike Frysingerae625412020-02-10 17:10:03 -0500988 # Some people use src="." to create stable links to projects. Lets allow
989 # that but reject all other uses of "." to keep things simple.
990 parts = path.split(os.path.sep)
991 if parts != ['.']:
992 for part in set(parts):
993 if part in {'.', '..', '.git'} or part.startswith('.repo'):
994 return 'bad component: %s' % (part,)
Mike Frysinger04122b72019-07-31 23:32:58 -0400995
996 if not symlink and path.endswith(os.path.sep):
997 return 'dirs not allowed'
998
999 norm = os.path.normpath(path)
1000 if norm == '..' or norm.startswith('../') or norm.startswith(os.path.sep):
1001 return 'path cannot be outside'
1002
1003 @classmethod
1004 def _ValidateFilePaths(cls, element, src, dest):
1005 """Verify |src| & |dest| are reasonable for <copyfile> & <linkfile>.
1006
1007 We verify the path independent of any filesystem state as we won't have a
1008 checkout available to compare to. i.e. This is for parsing validation
1009 purposes only.
1010
1011 We'll do full/live sanity checking before we do the actual filesystem
1012 modifications in _CopyFile/_LinkFile/etc...
1013 """
1014 # |dest| is the file we write to or symlink we create.
1015 # It is relative to the top of the repo client checkout.
1016 msg = cls._CheckLocalPath(dest)
1017 if msg:
1018 raise ManifestInvalidPathError(
1019 '<%s> invalid "dest": %s: %s' % (element, dest, msg))
1020
1021 # |src| is the file we read from or path we point to for symlinks.
1022 # It is relative to the top of the git project checkout.
1023 msg = cls._CheckLocalPath(src, symlink=element == 'linkfile')
1024 if msg:
1025 raise ManifestInvalidPathError(
1026 '<%s> invalid "src": %s: %s' % (element, src, msg))
1027
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001028 def _ParseCopyFile(self, project, node):
1029 src = self._reqatt(node, 'src')
1030 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001031 if not self.IsMirror:
1032 # src is project relative;
Mike Frysinger04122b72019-07-31 23:32:58 -04001033 # dest is relative to the top of the tree.
1034 # We only validate paths if we actually plan to process them.
1035 self._ValidateFilePaths('copyfile', src, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -04001036 project.AddCopyFile(src, dest, self.topdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001037
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001038 def _ParseLinkFile(self, project, node):
1039 src = self._reqatt(node, 'src')
1040 dest = self._reqatt(node, 'dest')
1041 if not self.IsMirror:
1042 # src is project relative;
Mike Frysinger04122b72019-07-31 23:32:58 -04001043 # dest is relative to the top of the tree.
1044 # We only validate paths if we actually plan to process them.
1045 self._ValidateFilePaths('linkfile', src, dest)
Mike Frysingere6a202f2019-08-02 15:57:57 -04001046 project.AddLinkFile(src, dest, self.topdir)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001047
James W. Mills24c13082012-04-12 15:04:13 -05001048 def _ParseAnnotation(self, project, node):
1049 name = self._reqatt(node, 'name')
1050 value = self._reqatt(node, 'value')
1051 try:
1052 keep = self._reqatt(node, 'keep').lower()
1053 except ManifestParseError:
1054 keep = "true"
1055 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301056 raise ManifestParseError('optional "keep" attribute must be '
1057 '"true" or "false"')
James W. Mills24c13082012-04-12 15:04:13 -05001058 project.AddAnnotation(name, value, keep)
1059
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001060 def _get_remote(self, node):
1061 name = node.getAttribute('remote')
1062 if not name:
1063 return None
1064
1065 v = self._remotes.get(name)
1066 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301067 raise ManifestParseError("remote %s not defined in %s" %
1068 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001069 return v
1070
1071 def _reqatt(self, node, attname):
1072 """
1073 reads a required attribute from the node.
1074 """
1075 v = node.getAttribute(attname)
1076 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301077 raise ManifestParseError("no %s in <%s> within %s" %
1078 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001079 return v
Julien Camperguedd654222014-01-09 16:21:37 +01001080
1081 def projectsDiff(self, manifest):
1082 """return the projects differences between two manifests.
1083
1084 The diff will be from self to given manifest.
1085
1086 """
1087 fromProjects = self.paths
1088 toProjects = manifest.paths
1089
Anthony King7446c592014-05-06 09:19:39 +01001090 fromKeys = sorted(fromProjects.keys())
1091 toKeys = sorted(toProjects.keys())
Julien Camperguedd654222014-01-09 16:21:37 +01001092
1093 diff = {'added': [], 'removed': [], 'changed': [], 'unreachable': []}
1094
1095 for proj in fromKeys:
1096 if not proj in toKeys:
1097 diff['removed'].append(fromProjects[proj])
1098 else:
1099 fromProj = fromProjects[proj]
1100 toProj = toProjects[proj]
1101 try:
1102 fromRevId = fromProj.GetCommitRevisionId()
1103 toRevId = toProj.GetCommitRevisionId()
1104 except ManifestInvalidRevisionError:
1105 diff['unreachable'].append((fromProj, toProj))
1106 else:
1107 if fromRevId != toRevId:
1108 diff['changed'].append((fromProj, toProj))
1109 toKeys.remove(proj)
1110
1111 for proj in toKeys:
1112 diff['added'].append(toProjects[proj])
1113
1114 return diff
Simran Basib9a1b732015-08-20 12:19:28 -07001115
1116
1117class GitcManifest(XmlManifest):
1118
1119 def __init__(self, repodir, gitc_client_name):
1120 """Initialize the GitcManifest object."""
1121 super(GitcManifest, self).__init__(repodir)
1122 self.isGitcClient = True
1123 self.gitc_client_name = gitc_client_name
Simran Basi8ce50412015-08-28 14:25:44 -07001124 self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
Simran Basib9a1b732015-08-20 12:19:28 -07001125 gitc_client_name)
1126 self.manifestFile = os.path.join(self.gitc_client_dir, '.manifest')
1127
1128 def _ParseProject(self, node, parent = None):
1129 """Override _ParseProject and add support for GITC specific attributes."""
1130 return super(GitcManifest, self)._ParseProject(
1131 node, parent=parent, old_revision=node.getAttribute('old-revision'))
1132
1133 def _output_manifest_project_extras(self, p, e):
1134 """Output GITC Specific Project attributes"""
1135 if p.old_revision:
Stefan Beller66851062016-06-17 16:40:08 -07001136 e.setAttribute('old-revision', str(p.old_revision))