blob: 69105c9e728a0d08ac7b3b46694fe6b8c8814111 [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')
Josh Triplett884a3872014-06-12 14:57:29 -0700602
603 for p in self._projects[name]:
604 if path and p.relpath != path:
605 continue
606 if groups:
607 p.groups.extend(groups)
Luis Hector Chavez7d525852018-03-15 09:54:08 -0700608 if revision:
609 p.revisionExpr = revision
Doug Anderson37282b42011-03-04 11:54:18 -0800610 if node.nodeName == 'repo-hooks':
611 # Get the name of the project and the (space-separated) list of enabled.
612 repo_hooks_project = self._reqatt(node, 'in-project')
613 enabled_repo_hooks = self._reqatt(node, 'enabled-list').split()
614
615 # Only one project can be the hooks project
616 if self._repo_hooks_project is not None:
617 raise ManifestParseError(
618 'duplicate repo-hooks in %s' %
619 (self.manifestFile))
620
621 # Store a reference to the Project.
622 try:
David James8d201162013-10-11 17:03:19 -0700623 repo_hooks_projects = self._projects[repo_hooks_project]
Doug Anderson37282b42011-03-04 11:54:18 -0800624 except KeyError:
625 raise ManifestParseError(
626 'project %s not found for repo-hooks' %
627 (repo_hooks_project))
628
David James8d201162013-10-11 17:03:19 -0700629 if len(repo_hooks_projects) != 1:
630 raise ManifestParseError(
631 'internal error parsing repo-hooks in %s' %
632 (self.manifestFile))
633 self._repo_hooks_project = repo_hooks_projects[0]
634
Doug Anderson37282b42011-03-04 11:54:18 -0800635 # Store the enabled hooks in the Project object.
636 self._repo_hooks_project.enabled_repo_hooks = enabled_repo_hooks
Colin Cross23acdd32012-04-21 00:33:54 -0700637 if node.nodeName == 'remove-project':
638 name = self._reqatt(node, 'name')
David Jamesb8433df2014-01-30 10:11:17 -0800639
640 if name not in self._projects:
David Pursehousef9107482012-11-16 19:12:32 +0900641 raise ManifestParseError('remove-project element specifies non-existent '
642 'project: %s' % name)
Colin Cross23acdd32012-04-21 00:33:54 -0700643
David Jamesb8433df2014-01-30 10:11:17 -0800644 for p in self._projects[name]:
645 del self._paths[p.relpath]
646 del self._projects[name]
647
Colin Cross23acdd32012-04-21 00:33:54 -0700648 # If the manifest removes the hooks project, treat it as if it deleted
649 # the repo-hooks element too.
650 if self._repo_hooks_project and (self._repo_hooks_project.name == name):
651 self._repo_hooks_project = None
652
Doug Anderson37282b42011-03-04 11:54:18 -0800653
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800654 def _AddMetaProjectMirror(self, m):
655 name = None
656 m_url = m.GetRemote(m.remote.name).url
657 if m_url.endswith('/.git'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530658 raise ManifestParseError('refusing to mirror %s' % m_url)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800659
660 if self._default and self._default.remote:
Conley Owensceea3682011-10-20 10:45:47 -0700661 url = self._default.remote.resolvedFetchUrl
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800662 if not url.endswith('/'):
663 url += '/'
664 if m_url.startswith(url):
665 remote = self._default.remote
666 name = m_url[len(url):]
667
668 if name is None:
669 s = m_url.rindex('/') + 1
Conley Owensdb728cd2011-09-26 16:34:01 -0700670 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Shawn O. Pearcef35b2d92012-08-02 11:46:22 -0700671 remote = _XmlRemote('origin', fetch=m_url[:s], manifestUrl=manifestUrl)
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800672 name = m_url[s:]
673
674 if name.endswith('.git'):
675 name = name[:-4]
676
677 if name not in self._projects:
678 m.PreSync()
679 gitdir = os.path.join(self.topdir, '%s.git' % name)
680 project = Project(manifest = self,
681 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700682 remote = remote.ToRemoteSpec(name),
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800683 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700684 objdir = gitdir,
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800685 worktree = None,
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900686 relpath = name or None,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700687 revisionExpr = m.revisionExpr,
688 revisionId = None)
David James8d201162013-10-11 17:03:19 -0700689 self._projects[project.name] = [project]
Kwanhong Leeccd218c2014-02-17 13:07:32 +0900690 self._paths[project.relpath] = project
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800691
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700692 def _ParseRemote(self, node):
693 """
694 reads a <remote> element from the manifest file
695 """
696 name = self._reqatt(node, 'name')
Yestin Sunb292b982012-07-02 07:32:50 -0700697 alias = node.getAttribute('alias')
698 if alias == '':
699 alias = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700700 fetch = self._reqatt(node, 'fetch')
Steve Raed6480452016-08-10 15:00:00 -0700701 pushUrl = node.getAttribute('pushurl')
702 if pushUrl == '':
703 pushUrl = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700704 review = node.getAttribute('review')
Shawn O. Pearceae6e0942008-11-06 10:25:35 -0800705 if review == '':
706 review = None
Anthony King36ea2fb2014-05-06 11:54:01 +0100707 revision = node.getAttribute('revision')
708 if revision == '':
709 revision = None
Conley Owensdb728cd2011-09-26 16:34:01 -0700710 manifestUrl = self.manifestProject.config.GetString('remote.origin.url')
Steve Raed6480452016-08-10 15:00:00 -0700711 return _XmlRemote(name, alias, fetch, pushUrl, manifestUrl, review, revision)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700712
713 def _ParseDefault(self, node):
714 """
715 reads a <default> element from the manifest file
716 """
717 d = _Default()
718 d.remote = self._get_remote(node)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700719 d.revisionExpr = node.getAttribute('revision')
720 if d.revisionExpr == '':
721 d.revisionExpr = None
Anatol Pomazau79770d22012-04-20 14:41:59 -0700722
Bryan Jacobsf609f912013-05-06 13:36:24 -0400723 d.destBranchExpr = node.getAttribute('dest-branch') or None
Nasser Grainawida403412018-05-04 12:53:29 -0600724 d.upstreamExpr = node.getAttribute('upstream') or None
Bryan Jacobsf609f912013-05-06 13:36:24 -0400725
Shawn O. Pearce6392c872011-09-22 17:44:31 -0700726 sync_j = node.getAttribute('sync-j')
727 if sync_j == '' or sync_j is None:
728 d.sync_j = 1
729 else:
730 d.sync_j = int(sync_j)
Anatol Pomazau79770d22012-04-20 14:41:59 -0700731
732 sync_c = node.getAttribute('sync-c')
733 if not sync_c:
734 d.sync_c = False
735 else:
736 d.sync_c = sync_c.lower() in ("yes", "true", "1")
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800737
738 sync_s = node.getAttribute('sync-s')
739 if not sync_s:
740 d.sync_s = False
741 else:
742 d.sync_s = sync_s.lower() in ("yes", "true", "1")
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900743
744 sync_tags = node.getAttribute('sync-tags')
745 if not sync_tags:
746 d.sync_tags = True
747 else:
748 d.sync_tags = sync_tags.lower() in ("yes", "true", "1")
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700749 return d
750
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700751 def _ParseNotice(self, node):
752 """
753 reads a <notice> element from the manifest file
754
755 The <notice> element is distinct from other tags in the XML in that the
756 data is conveyed between the start and end tag (it's not an empty-element
757 tag).
758
759 The white space (carriage returns, indentation) for the notice element is
760 relevant and is parsed in a way that is based on how python docstrings work.
761 In fact, the code is remarkably similar to here:
762 http://www.python.org/dev/peps/pep-0257/
763 """
764 # Get the data out of the node...
765 notice = node.childNodes[0].data
766
767 # Figure out minimum indentation, skipping the first line (the same line
768 # as the <notice> tag)...
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530769 minIndent = sys.maxsize
Doug Anderson2b8db3c2010-11-01 15:08:06 -0700770 lines = notice.splitlines()
771 for line in lines[1:]:
772 lstrippedLine = line.lstrip()
773 if lstrippedLine:
774 indent = len(line) - len(lstrippedLine)
775 minIndent = min(indent, minIndent)
776
777 # Strip leading / trailing blank lines and also indentation.
778 cleanLines = [lines[0].strip()]
779 for line in lines[1:]:
780 cleanLines.append(line[minIndent:].rstrip())
781
782 # Clear completely blank lines from front and back...
783 while cleanLines and not cleanLines[0]:
784 del cleanLines[0]
785 while cleanLines and not cleanLines[-1]:
786 del cleanLines[-1]
787
788 return '\n'.join(cleanLines)
789
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800790 def _JoinName(self, parent_name, name):
791 return os.path.join(parent_name, name)
792
793 def _UnjoinName(self, parent_name, name):
794 return os.path.relpath(name, parent_name)
795
Simran Basib9a1b732015-08-20 12:19:28 -0700796 def _ParseProject(self, node, parent = None, **extra_proj_attrs):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700797 """
798 reads a <project> element from the manifest file
Nico Sallembiena1bfd2c2010-04-06 10:40:01 -0700799 """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700800 name = self._reqatt(node, 'name')
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800801 if parent:
802 name = self._JoinName(parent.name, name)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700803
804 remote = self._get_remote(node)
805 if remote is None:
806 remote = self._default.remote
807 if remote is None:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530808 raise ManifestParseError("no remote for project %s within %s" %
809 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700810
Anthony King36ea2fb2014-05-06 11:54:01 +0100811 revisionExpr = node.getAttribute('revision') or remote.revision
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700812 if not revisionExpr:
813 revisionExpr = self._default.revisionExpr
814 if not revisionExpr:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530815 raise ManifestParseError("no revision for project %s within %s" %
816 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700817
818 path = node.getAttribute('path')
819 if not path:
820 path = name
821 if path.startswith('/'):
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530822 raise ManifestParseError("project %s path cannot be absolute in %s" %
823 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700824
Mike Pontillod3153822012-02-28 11:53:24 -0800825 rebase = node.getAttribute('rebase')
826 if not rebase:
827 rebase = True
828 else:
829 rebase = rebase.lower() in ("yes", "true", "1")
830
Anatol Pomazau79770d22012-04-20 14:41:59 -0700831 sync_c = node.getAttribute('sync-c')
832 if not sync_c:
833 sync_c = False
834 else:
835 sync_c = sync_c.lower() in ("yes", "true", "1")
836
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800837 sync_s = node.getAttribute('sync-s')
838 if not sync_s:
839 sync_s = self._default.sync_s
840 else:
841 sync_s = sync_s.lower() in ("yes", "true", "1")
842
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900843 sync_tags = node.getAttribute('sync-tags')
844 if not sync_tags:
845 sync_tags = self._default.sync_tags
846 else:
847 sync_tags = sync_tags.lower() in ("yes", "true", "1")
848
David Pursehouseede7f122012-11-27 22:25:30 +0900849 clone_depth = node.getAttribute('clone-depth')
850 if clone_depth:
851 try:
852 clone_depth = int(clone_depth)
853 if clone_depth <= 0:
854 raise ValueError()
855 except ValueError:
856 raise ManifestParseError('invalid clone-depth %s in %s' %
857 (clone_depth, self.manifestFile))
858
Bryan Jacobsf609f912013-05-06 13:36:24 -0400859 dest_branch = node.getAttribute('dest-branch') or self._default.destBranchExpr
860
Nasser Grainawida403412018-05-04 12:53:29 -0600861 upstream = node.getAttribute('upstream') or self._default.upstreamExpr
Brian Harring14a66742012-09-28 20:21:57 -0700862
Conley Owens971de8e2012-04-16 10:36:08 -0700863 groups = ''
864 if node.hasAttribute('groups'):
865 groups = node.getAttribute('groups')
Josh Triplett884a3872014-06-12 14:57:29 -0700866 groups = self._ParseGroups(groups)
Brian Harring7da13142012-06-15 02:24:20 -0700867
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800868 if parent is None:
David James8d201162013-10-11 17:03:19 -0700869 relpath, worktree, gitdir, objdir = self.GetProjectPaths(name, path)
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700870 else:
David James8d201162013-10-11 17:03:19 -0700871 relpath, worktree, gitdir, objdir = \
872 self.GetSubprojectPaths(parent, name, path)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800873
874 default_groups = ['all', 'name:%s' % name, 'path:%s' % relpath]
875 groups.extend(set(default_groups).difference(groups))
Shawn O. Pearcecd81dd62012-10-26 12:18:00 -0700876
Scott Fandb83b1b2013-02-28 09:34:14 +0800877 if self.IsMirror and node.hasAttribute('force-path'):
878 if node.getAttribute('force-path').lower() in ("yes", "true", "1"):
879 gitdir = os.path.join(self.topdir, '%s.git' % path)
880
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700881 project = Project(manifest = self,
882 name = name,
Shawn O. Pearced1f70d92009-05-19 14:58:02 -0700883 remote = remote.ToRemoteSpec(name),
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700884 gitdir = gitdir,
David James8d201162013-10-11 17:03:19 -0700885 objdir = objdir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700886 worktree = worktree,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800887 relpath = relpath,
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700888 revisionExpr = revisionExpr,
Mike Pontillod3153822012-02-28 11:53:24 -0800889 revisionId = None,
Colin Cross5acde752012-03-28 20:15:45 -0700890 rebase = rebase,
Anatol Pomazau79770d22012-04-20 14:41:59 -0700891 groups = groups,
Brian Harring14a66742012-09-28 20:21:57 -0700892 sync_c = sync_c,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800893 sync_s = sync_s,
YOUNG HO CHAa32c92c2018-02-14 16:57:31 +0900894 sync_tags = sync_tags,
David Pursehouseede7f122012-11-27 22:25:30 +0900895 clone_depth = clone_depth,
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800896 upstream = upstream,
Bryan Jacobsf609f912013-05-06 13:36:24 -0400897 parent = parent,
Simran Basib9a1b732015-08-20 12:19:28 -0700898 dest_branch = dest_branch,
899 **extra_proj_attrs)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700900
901 for n in node.childNodes:
Shawn O. Pearce242b5262009-05-19 13:00:29 -0700902 if n.nodeName == 'copyfile':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700903 self._ParseCopyFile(project, n)
Jeff Hamiltone0df2322014-04-21 17:10:59 -0500904 if n.nodeName == 'linkfile':
905 self._ParseLinkFile(project, n)
James W. Mills24c13082012-04-12 15:04:13 -0500906 if n.nodeName == 'annotation':
907 self._ParseAnnotation(project, n)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800908 if n.nodeName == 'project':
909 project.subprojects.append(self._ParseProject(n, parent = project))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700910
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700911 return project
912
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800913 def GetProjectPaths(self, name, path):
914 relpath = path
915 if self.IsMirror:
916 worktree = None
917 gitdir = os.path.join(self.topdir, '%s.git' % name)
David James8d201162013-10-11 17:03:19 -0700918 objdir = gitdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800919 else:
920 worktree = os.path.join(self.topdir, path).replace('\\', '/')
921 gitdir = os.path.join(self.repodir, 'projects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700922 objdir = os.path.join(self.repodir, 'project-objects', '%s.git' % name)
923 return relpath, worktree, gitdir, objdir
924
925 def GetProjectsWithName(self, name):
926 return self._projects.get(name, [])
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800927
928 def GetSubprojectName(self, parent, submodule_path):
929 return os.path.join(parent.name, submodule_path)
930
931 def _JoinRelpath(self, parent_relpath, relpath):
932 return os.path.join(parent_relpath, relpath)
933
934 def _UnjoinRelpath(self, parent_relpath, relpath):
935 return os.path.relpath(relpath, parent_relpath)
936
David James8d201162013-10-11 17:03:19 -0700937 def GetSubprojectPaths(self, parent, name, path):
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800938 relpath = self._JoinRelpath(parent.relpath, path)
939 gitdir = os.path.join(parent.gitdir, 'subprojects', '%s.git' % path)
David James8d201162013-10-11 17:03:19 -0700940 objdir = os.path.join(parent.gitdir, 'subproject-objects', '%s.git' % name)
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800941 if self.IsMirror:
942 worktree = None
943 else:
944 worktree = os.path.join(parent.worktree, path).replace('\\', '/')
David James8d201162013-10-11 17:03:19 -0700945 return relpath, worktree, gitdir, objdir
Che-Liang Chioub2bd91c2012-01-11 11:28:42 +0800946
Mike Frysinger04122b72019-07-31 23:32:58 -0400947 @staticmethod
948 def _CheckLocalPath(path, symlink=False):
949 """Verify |path| is reasonable for use in <copyfile> & <linkfile>."""
950 if '~' in path:
951 return '~ not allowed (due to 8.3 filenames on Windows filesystems)'
952
953 # Some filesystems (like Apple's HFS+) try to normalize Unicode codepoints
954 # which means there are alternative names for ".git". Reject paths with
955 # these in it as there shouldn't be any reasonable need for them here.
956 # The set of codepoints here was cribbed from jgit's implementation:
957 # https://eclipse.googlesource.com/jgit/jgit/+/9110037e3e9461ff4dac22fee84ef3694ed57648/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectChecker.java#884
958 BAD_CODEPOINTS = {
959 u'\u200C', # ZERO WIDTH NON-JOINER
960 u'\u200D', # ZERO WIDTH JOINER
961 u'\u200E', # LEFT-TO-RIGHT MARK
962 u'\u200F', # RIGHT-TO-LEFT MARK
963 u'\u202A', # LEFT-TO-RIGHT EMBEDDING
964 u'\u202B', # RIGHT-TO-LEFT EMBEDDING
965 u'\u202C', # POP DIRECTIONAL FORMATTING
966 u'\u202D', # LEFT-TO-RIGHT OVERRIDE
967 u'\u202E', # RIGHT-TO-LEFT OVERRIDE
968 u'\u206A', # INHIBIT SYMMETRIC SWAPPING
969 u'\u206B', # ACTIVATE SYMMETRIC SWAPPING
970 u'\u206C', # INHIBIT ARABIC FORM SHAPING
971 u'\u206D', # ACTIVATE ARABIC FORM SHAPING
972 u'\u206E', # NATIONAL DIGIT SHAPES
973 u'\u206F', # NOMINAL DIGIT SHAPES
974 u'\uFEFF', # ZERO WIDTH NO-BREAK SPACE
975 }
976 if BAD_CODEPOINTS & set(path):
977 # This message is more expansive than reality, but should be fine.
978 return 'Unicode combining characters not allowed'
979
980 # Assume paths might be used on case-insensitive filesystems.
981 path = path.lower()
982
983 # We don't really need to reject '.' here, but there shouldn't really be a
984 # need to ever use it, so no need to accept it either.
985 for part in set(path.split(os.path.sep)):
986 if part in {'.', '..', '.git'} or part.startswith('.repo'):
987 return 'bad component: %s' % (part,)
988
989 if not symlink and path.endswith(os.path.sep):
990 return 'dirs not allowed'
991
992 norm = os.path.normpath(path)
993 if norm == '..' or norm.startswith('../') or norm.startswith(os.path.sep):
994 return 'path cannot be outside'
995
996 @classmethod
997 def _ValidateFilePaths(cls, element, src, dest):
998 """Verify |src| & |dest| are reasonable for <copyfile> & <linkfile>.
999
1000 We verify the path independent of any filesystem state as we won't have a
1001 checkout available to compare to. i.e. This is for parsing validation
1002 purposes only.
1003
1004 We'll do full/live sanity checking before we do the actual filesystem
1005 modifications in _CopyFile/_LinkFile/etc...
1006 """
1007 # |dest| is the file we write to or symlink we create.
1008 # It is relative to the top of the repo client checkout.
1009 msg = cls._CheckLocalPath(dest)
1010 if msg:
1011 raise ManifestInvalidPathError(
1012 '<%s> invalid "dest": %s: %s' % (element, dest, msg))
1013
1014 # |src| is the file we read from or path we point to for symlinks.
1015 # It is relative to the top of the git project checkout.
1016 msg = cls._CheckLocalPath(src, symlink=element == 'linkfile')
1017 if msg:
1018 raise ManifestInvalidPathError(
1019 '<%s> invalid "src": %s: %s' % (element, src, msg))
1020
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001021 def _ParseCopyFile(self, project, node):
1022 src = self._reqatt(node, 'src')
1023 dest = self._reqatt(node, 'dest')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -08001024 if not self.IsMirror:
1025 # src is project relative;
Mike Frysinger04122b72019-07-31 23:32:58 -04001026 # dest is relative to the top of the tree.
1027 # We only validate paths if we actually plan to process them.
1028 self._ValidateFilePaths('copyfile', src, dest)
Shawn O. Pearcec7a4eef2009-03-05 10:32:38 -08001029 project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001030
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001031 def _ParseLinkFile(self, project, node):
1032 src = self._reqatt(node, 'src')
1033 dest = self._reqatt(node, 'dest')
1034 if not self.IsMirror:
1035 # src is project relative;
Mike Frysinger04122b72019-07-31 23:32:58 -04001036 # dest is relative to the top of the tree.
1037 # We only validate paths if we actually plan to process them.
1038 self._ValidateFilePaths('linkfile', src, dest)
Jeff Hamiltone0df2322014-04-21 17:10:59 -05001039 project.AddLinkFile(src, dest, os.path.join(self.topdir, dest))
1040
James W. Mills24c13082012-04-12 15:04:13 -05001041 def _ParseAnnotation(self, project, node):
1042 name = self._reqatt(node, 'name')
1043 value = self._reqatt(node, 'value')
1044 try:
1045 keep = self._reqatt(node, 'keep').lower()
1046 except ManifestParseError:
1047 keep = "true"
1048 if keep != "true" and keep != "false":
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301049 raise ManifestParseError('optional "keep" attribute must be '
1050 '"true" or "false"')
James W. Mills24c13082012-04-12 15:04:13 -05001051 project.AddAnnotation(name, value, keep)
1052
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001053 def _get_remote(self, node):
1054 name = node.getAttribute('remote')
1055 if not name:
1056 return None
1057
1058 v = self._remotes.get(name)
1059 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301060 raise ManifestParseError("remote %s not defined in %s" %
1061 (name, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001062 return v
1063
1064 def _reqatt(self, node, attname):
1065 """
1066 reads a required attribute from the node.
1067 """
1068 v = node.getAttribute(attname)
1069 if not v:
Chirayu Desai217ea7d2013-03-01 19:14:38 +05301070 raise ManifestParseError("no %s in <%s> within %s" %
1071 (attname, node.nodeName, self.manifestFile))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001072 return v
Julien Camperguedd654222014-01-09 16:21:37 +01001073
1074 def projectsDiff(self, manifest):
1075 """return the projects differences between two manifests.
1076
1077 The diff will be from self to given manifest.
1078
1079 """
1080 fromProjects = self.paths
1081 toProjects = manifest.paths
1082
Anthony King7446c592014-05-06 09:19:39 +01001083 fromKeys = sorted(fromProjects.keys())
1084 toKeys = sorted(toProjects.keys())
Julien Camperguedd654222014-01-09 16:21:37 +01001085
1086 diff = {'added': [], 'removed': [], 'changed': [], 'unreachable': []}
1087
1088 for proj in fromKeys:
1089 if not proj in toKeys:
1090 diff['removed'].append(fromProjects[proj])
1091 else:
1092 fromProj = fromProjects[proj]
1093 toProj = toProjects[proj]
1094 try:
1095 fromRevId = fromProj.GetCommitRevisionId()
1096 toRevId = toProj.GetCommitRevisionId()
1097 except ManifestInvalidRevisionError:
1098 diff['unreachable'].append((fromProj, toProj))
1099 else:
1100 if fromRevId != toRevId:
1101 diff['changed'].append((fromProj, toProj))
1102 toKeys.remove(proj)
1103
1104 for proj in toKeys:
1105 diff['added'].append(toProjects[proj])
1106
1107 return diff
Simran Basib9a1b732015-08-20 12:19:28 -07001108
1109
1110class GitcManifest(XmlManifest):
1111
1112 def __init__(self, repodir, gitc_client_name):
1113 """Initialize the GitcManifest object."""
1114 super(GitcManifest, self).__init__(repodir)
1115 self.isGitcClient = True
1116 self.gitc_client_name = gitc_client_name
Simran Basi8ce50412015-08-28 14:25:44 -07001117 self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
Simran Basib9a1b732015-08-20 12:19:28 -07001118 gitc_client_name)
1119 self.manifestFile = os.path.join(self.gitc_client_dir, '.manifest')
1120
1121 def _ParseProject(self, node, parent = None):
1122 """Override _ParseProject and add support for GITC specific attributes."""
1123 return super(GitcManifest, self)._ParseProject(
1124 node, parent=parent, old_revision=node.getAttribute('old-revision'))
1125
1126 def _output_manifest_project_extras(self, p, e):
1127 """Output GITC Specific Project attributes"""
1128 if p.old_revision:
Stefan Beller66851062016-06-17 16:40:08 -07001129 e.setAttribute('old-revision', str(p.old_revision))
Simran Basib9a1b732015-08-20 12:19:28 -07001130