blob: 62f2d52fb34cbe989856e16924fbd5098cccf05f [file] [log] [blame]
Raman Tenneti6a872c92021-01-14 19:17:50 -08001# Copyright (C) 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Raman Tenneti21dce3d2021-02-09 00:26:31 -080015"""Provide functionality to get all projects and their commit ids from Superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080016
17For more information on superproject, check out:
18https://en.wikibooks.org/wiki/Git/Submodules_and_Superprojects
19
20Examples:
21 superproject = Superproject()
Raman Tenneti21dce3d2021-02-09 00:26:31 -080022 project_commit_ids = superproject.UpdateProjectsRevisionId(projects)
Raman Tenneti6a872c92021-01-14 19:17:50 -080023"""
24
Raman Tenneticeba2dd2021-02-22 16:54:56 -080025import hashlib
Raman Tenneti6a872c92021-01-14 19:17:50 -080026import os
27import sys
28
Raman Tennetie253b432021-06-02 10:05:54 -070029from git_command import git_require, GitCommand
Raman Tenneti21dce3d2021-02-09 00:26:31 -080030from git_refs import R_HEADS
Raman Tenneti6a872c92021-01-14 19:17:50 -080031
Raman Tenneti8d43dea2021-02-07 16:30:27 -080032_SUPERPROJECT_GIT_NAME = 'superproject.git'
33_SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml'
34
Raman Tenneti6a872c92021-01-14 19:17:50 -080035
36class Superproject(object):
Raman Tenneti21dce3d2021-02-09 00:26:31 -080037 """Get commit ids from superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080038
Raman Tenneticeba2dd2021-02-22 16:54:56 -080039 Initializes a local copy of a superproject for the manifest. This allows
40 lookup of commit ids for all projects. It contains _project_commit_ids which
41 is a dictionary with project/commit id entries.
Raman Tenneti6a872c92021-01-14 19:17:50 -080042 """
Raman Tennetief99ec02021-03-04 10:29:40 -080043 def __init__(self, manifest, repodir, superproject_dir='exp-superproject',
44 quiet=False):
Raman Tenneti6a872c92021-01-14 19:17:50 -080045 """Initializes superproject.
46
47 Args:
Raman Tenneti21dce3d2021-02-09 00:26:31 -080048 manifest: A Manifest object that is to be written to a file.
Raman Tenneti6a872c92021-01-14 19:17:50 -080049 repodir: Path to the .repo/ dir for holding all internal checkout state.
Raman Tenneti21dce3d2021-02-09 00:26:31 -080050 It must be in the top directory of the repo client checkout.
Raman Tenneti6a872c92021-01-14 19:17:50 -080051 superproject_dir: Relative path under |repodir| to checkout superproject.
Raman Tennetief99ec02021-03-04 10:29:40 -080052 quiet: If True then only print the progress messages.
Raman Tenneti6a872c92021-01-14 19:17:50 -080053 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -080054 self._project_commit_ids = None
55 self._manifest = manifest
Raman Tennetief99ec02021-03-04 10:29:40 -080056 self._quiet = quiet
Raman Tenneti21dce3d2021-02-09 00:26:31 -080057 self._branch = self._GetBranch()
Raman Tenneti6a872c92021-01-14 19:17:50 -080058 self._repodir = os.path.abspath(repodir)
59 self._superproject_dir = superproject_dir
60 self._superproject_path = os.path.join(self._repodir, superproject_dir)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -080061 self._manifest_path = os.path.join(self._superproject_path,
Raman Tenneti8d43dea2021-02-07 16:30:27 -080062 _SUPERPROJECT_MANIFEST_NAME)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080063 git_name = ''
64 if self._manifest.superproject:
65 remote_name = self._manifest.superproject['remote'].name
66 git_name = hashlib.md5(remote_name.encode('utf8')).hexdigest() + '-'
67 self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME
68 self._work_git = os.path.join(self._superproject_path, self._work_git_name)
Raman Tenneti6a872c92021-01-14 19:17:50 -080069
70 @property
Raman Tenneti21dce3d2021-02-09 00:26:31 -080071 def project_commit_ids(self):
72 """Returns a dictionary of projects and their commit ids."""
73 return self._project_commit_ids
Raman Tenneti6a872c92021-01-14 19:17:50 -080074
Raman Tenneti21dce3d2021-02-09 00:26:31 -080075 def _GetBranch(self):
76 """Returns the branch name for getting the approved manifest."""
77 p = self._manifest.manifestProject
78 b = p.GetBranch(p.CurrentBranch)
79 if not b:
80 return None
81 branch = b.merge
82 if branch and branch.startswith(R_HEADS):
83 branch = branch[len(R_HEADS):]
84 return branch
85
Raman Tenneticeba2dd2021-02-22 16:54:56 -080086 def _Init(self):
87 """Sets up a local Git repository to get a copy of a superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080088
89 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -080090 True if initialization is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -080091 """
Raman Tenneti8d43dea2021-02-07 16:30:27 -080092 if not os.path.exists(self._superproject_path):
93 os.mkdir(self._superproject_path)
Raman Tennetief99ec02021-03-04 10:29:40 -080094 if not self._quiet and not os.path.exists(self._work_git):
95 print('%s: Performing initial setup for superproject; this might take '
96 'several minutes.' % self._work_git)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080097 cmd = ['init', '--bare', self._work_git_name]
Raman Tenneti6a872c92021-01-14 19:17:50 -080098 p = GitCommand(None,
99 cmd,
100 cwd=self._superproject_path,
101 capture_stdout=True,
102 capture_stderr=True)
103 retval = p.Wait()
104 if retval:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800105 print('repo: error: git init call failed with return code: %r, stderr: %r' %
Raman Tenneti6a872c92021-01-14 19:17:50 -0800106 (retval, p.stderr), file=sys.stderr)
107 return False
108 return True
109
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800110 def _Fetch(self, url):
111 """Fetches a local copy of a superproject for the manifest based on url.
112
113 Args:
114 url: superproject's url.
Raman Tenneti9e787532021-02-01 11:47:06 -0800115
116 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800117 True if fetch is successful, or False.
Raman Tenneti9e787532021-02-01 11:47:06 -0800118 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800119 if not os.path.exists(self._work_git):
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800120 print('git fetch missing drectory: %s' % self._work_git,
121 file=sys.stderr)
122 return False
Raman Tennetie253b432021-06-02 10:05:54 -0700123 if not git_require((2, 28, 0)):
124 print('superproject requires a git version 2.28 or later', file=sys.stderr)
125 return False
Raman Tenneti83670962021-03-19 13:53:43 -0700126 cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none']
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800127 if self._branch:
128 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800129 p = GitCommand(None,
130 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800131 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800132 capture_stdout=True,
133 capture_stderr=True)
134 retval = p.Wait()
135 if retval:
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800136 print('repo: error: git fetch call failed with return code: %r, stderr: %r' %
Raman Tenneti9e787532021-02-01 11:47:06 -0800137 (retval, p.stderr), file=sys.stderr)
138 return False
139 return True
140
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800141 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800142 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800143
144 Works only in git repositories.
145
146 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800147 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800148 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800149 if not os.path.exists(self._work_git):
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800150 print('git ls-tree missing drectory: %s' % self._work_git,
151 file=sys.stderr)
152 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800153 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800154 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800155 cmd = ['ls-tree', '-z', '-r', branch]
156
Raman Tenneti6a872c92021-01-14 19:17:50 -0800157 p = GitCommand(None,
158 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800159 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800160 capture_stdout=True,
161 capture_stderr=True)
162 retval = p.Wait()
163 if retval == 0:
164 data = p.stdout
165 else:
Raman Tenneti6a872c92021-01-14 19:17:50 -0800166 print('repo: error: git ls-tree call failed with return code: %r, stderr: %r' % (
167 retval, p.stderr), file=sys.stderr)
168 return data
169
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800170 def Sync(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800171 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800172
173 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800174 True if sync of superproject is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800175 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800176 print('WARNING: --use-superproject is experimental and not '
177 'for general use', file=sys.stderr)
178
179 if not self._manifest.superproject:
180 print('error: superproject tag is not defined in manifest',
181 file=sys.stderr)
182 return False
183
184 url = self._manifest.superproject['remote'].url
Raman Tenneti6a872c92021-01-14 19:17:50 -0800185 if not url:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800186 print('error: superproject URL is not defined in manifest',
187 file=sys.stderr)
188 return False
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800189
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800190 if not self._Init():
191 return False
192 if not self._Fetch(url):
193 return False
Raman Tennetief99ec02021-03-04 10:29:40 -0800194 if not self._quiet:
195 print('%s: Initial setup for superproject completed.' % self._work_git)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800196 return True
Raman Tenneti6a872c92021-01-14 19:17:50 -0800197
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800198 def _GetAllProjectsCommitIds(self):
199 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
200
201 Returns:
202 A dictionary with the projects/commit ids on success, otherwise None.
203 """
204 if not self.Sync():
205 return None
206
207 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800208 if not data:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800209 print('error: git ls-tree failed to return data for superproject',
210 file=sys.stderr)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800211 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800212
213 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800214 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800215 #
216 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
217 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800218 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800219 for line in data.split('\x00'):
220 ls_data = line.split(None, 3)
221 if not ls_data:
222 break
223 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800224 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800225
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800226 self._project_commit_ids = commit_ids
227 return commit_ids
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800228
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800229 def _WriteManfiestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800230 """Writes manifest to a file.
231
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800232 Returns:
233 manifest_path: Path name of the file into which manifest is written instead of None.
234 """
235 if not os.path.exists(self._superproject_path):
236 print('error: missing superproject directory %s' %
237 self._superproject_path,
238 file=sys.stderr)
239 return None
Raman Tenneti080877e2021-03-09 15:19:06 -0800240 manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800241 manifest_path = self._manifest_path
242 try:
243 with open(manifest_path, 'w', encoding='utf-8') as fp:
244 fp.write(manifest_str)
245 except IOError as e:
246 print('error: cannot write manifest to %s:\n%s'
247 % (manifest_path, e),
248 file=sys.stderr)
249 return None
250 return manifest_path
251
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800252 def UpdateProjectsRevisionId(self, projects):
253 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800254
255 Args:
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800256 projects: List of projects whose revisionId needs to be updated.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800257
258 Returns:
259 manifest_path: Path name of the overriding manfiest file instead of None.
260 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800261 commit_ids = self._GetAllProjectsCommitIds()
262 if not commit_ids:
263 print('error: Cannot get project commit ids from manifest', file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800264 return None
265
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800266 projects_missing_commit_ids = []
Raman Tenneti5a41b0b2021-05-02 22:55:26 -0700267 superproject_fetchUrl = self._manifest.superproject['remote'].fetchUrl
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800268 for project in projects:
269 path = project.relpath
270 if not path:
271 continue
Raman Tennetifeb28912021-05-02 19:47:29 -0700272 # Some manifests that pull projects from the "chromium" GoB
273 # (remote="chromium"), and have a private manifest that pulls projects
274 # from both the chromium GoB and "chrome-internal" GoB (remote="chrome").
275 # For such projects, one of the remotes will be different from
276 # superproject's remote. Until superproject, supports multiple remotes,
277 # don't update the commit ids of remotes that don't match superproject's
278 # remote.
Raman Tenneti5a41b0b2021-05-02 22:55:26 -0700279 if project.remote.fetchUrl != superproject_fetchUrl:
Raman Tennetifeb28912021-05-02 19:47:29 -0700280 continue
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800281 commit_id = commit_ids.get(path)
282 if commit_id:
283 project.SetRevisionId(commit_id)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800284 else:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800285 projects_missing_commit_ids.append(path)
286 if projects_missing_commit_ids:
287 print('error: please file a bug using %s to report missing commit_ids for: %s' %
Raman Tenneti993af5e2021-05-12 12:00:31 -0700288 (self._manifest.contactinfo.bugurl, projects_missing_commit_ids), file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800289 return None
290
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800291 manifest_path = self._WriteManfiestFile()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800292 return manifest_path