blob: 031f45c9e6db21140440d3423be0322c58c582ac [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 Tenneti6a872c92021-01-14 19:17:50 -080029from git_command import GitCommand
Raman Tenneti21dce3d2021-02-09 00:26:31 -080030from git_refs import R_HEADS
Mike Frysingera1cd7702021-04-20 23:38:04 -040031from wrapper import Wrapper
Raman Tenneti6a872c92021-01-14 19:17:50 -080032
Raman Tenneti8d43dea2021-02-07 16:30:27 -080033_SUPERPROJECT_GIT_NAME = 'superproject.git'
34_SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml'
35
Raman Tenneti6a872c92021-01-14 19:17:50 -080036
37class Superproject(object):
Raman Tenneti21dce3d2021-02-09 00:26:31 -080038 """Get commit ids from superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080039
Raman Tenneticeba2dd2021-02-22 16:54:56 -080040 Initializes a local copy of a superproject for the manifest. This allows
41 lookup of commit ids for all projects. It contains _project_commit_ids which
42 is a dictionary with project/commit id entries.
Raman Tenneti6a872c92021-01-14 19:17:50 -080043 """
Raman Tennetief99ec02021-03-04 10:29:40 -080044 def __init__(self, manifest, repodir, superproject_dir='exp-superproject',
45 quiet=False):
Raman Tenneti6a872c92021-01-14 19:17:50 -080046 """Initializes superproject.
47
48 Args:
Raman Tenneti21dce3d2021-02-09 00:26:31 -080049 manifest: A Manifest object that is to be written to a file.
Raman Tenneti6a872c92021-01-14 19:17:50 -080050 repodir: Path to the .repo/ dir for holding all internal checkout state.
Raman Tenneti21dce3d2021-02-09 00:26:31 -080051 It must be in the top directory of the repo client checkout.
Raman Tenneti6a872c92021-01-14 19:17:50 -080052 superproject_dir: Relative path under |repodir| to checkout superproject.
Raman Tennetief99ec02021-03-04 10:29:40 -080053 quiet: If True then only print the progress messages.
Raman Tenneti6a872c92021-01-14 19:17:50 -080054 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -080055 self._project_commit_ids = None
56 self._manifest = manifest
Raman Tennetief99ec02021-03-04 10:29:40 -080057 self._quiet = quiet
Raman Tenneti21dce3d2021-02-09 00:26:31 -080058 self._branch = self._GetBranch()
Raman Tenneti6a872c92021-01-14 19:17:50 -080059 self._repodir = os.path.abspath(repodir)
60 self._superproject_dir = superproject_dir
61 self._superproject_path = os.path.join(self._repodir, superproject_dir)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -080062 self._manifest_path = os.path.join(self._superproject_path,
Raman Tenneti8d43dea2021-02-07 16:30:27 -080063 _SUPERPROJECT_MANIFEST_NAME)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080064 git_name = ''
65 if self._manifest.superproject:
66 remote_name = self._manifest.superproject['remote'].name
67 git_name = hashlib.md5(remote_name.encode('utf8')).hexdigest() + '-'
68 self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME
69 self._work_git = os.path.join(self._superproject_path, self._work_git_name)
Raman Tenneti6a872c92021-01-14 19:17:50 -080070
71 @property
Raman Tenneti21dce3d2021-02-09 00:26:31 -080072 def project_commit_ids(self):
73 """Returns a dictionary of projects and their commit ids."""
74 return self._project_commit_ids
Raman Tenneti6a872c92021-01-14 19:17:50 -080075
Raman Tenneti21dce3d2021-02-09 00:26:31 -080076 def _GetBranch(self):
77 """Returns the branch name for getting the approved manifest."""
78 p = self._manifest.manifestProject
79 b = p.GetBranch(p.CurrentBranch)
80 if not b:
81 return None
82 branch = b.merge
83 if branch and branch.startswith(R_HEADS):
84 branch = branch[len(R_HEADS):]
85 return branch
86
Raman Tenneticeba2dd2021-02-22 16:54:56 -080087 def _Init(self):
88 """Sets up a local Git repository to get a copy of a superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080089
90 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -080091 True if initialization is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -080092 """
Raman Tenneti8d43dea2021-02-07 16:30:27 -080093 if not os.path.exists(self._superproject_path):
94 os.mkdir(self._superproject_path)
Raman Tennetief99ec02021-03-04 10:29:40 -080095 if not self._quiet and not os.path.exists(self._work_git):
96 print('%s: Performing initial setup for superproject; this might take '
97 'several minutes.' % self._work_git)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080098 cmd = ['init', '--bare', self._work_git_name]
Raman Tenneti6a872c92021-01-14 19:17:50 -080099 p = GitCommand(None,
100 cmd,
101 cwd=self._superproject_path,
102 capture_stdout=True,
103 capture_stderr=True)
104 retval = p.Wait()
105 if retval:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800106 print('repo: error: git init call failed with return code: %r, stderr: %r' %
Raman Tenneti6a872c92021-01-14 19:17:50 -0800107 (retval, p.stderr), file=sys.stderr)
108 return False
109 return True
110
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800111 def _Fetch(self, url):
112 """Fetches a local copy of a superproject for the manifest based on url.
113
114 Args:
115 url: superproject's url.
Raman Tenneti9e787532021-02-01 11:47:06 -0800116
117 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800118 True if fetch is successful, or False.
Raman Tenneti9e787532021-02-01 11:47:06 -0800119 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800120 if not os.path.exists(self._work_git):
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800121 print('git fetch missing drectory: %s' % self._work_git,
122 file=sys.stderr)
123 return False
Raman Tenneti83670962021-03-19 13:53:43 -0700124 cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none']
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800125 if self._branch:
126 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800127 p = GitCommand(None,
128 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800129 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800130 capture_stdout=True,
131 capture_stderr=True)
132 retval = p.Wait()
133 if retval:
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800134 print('repo: error: git fetch call failed with return code: %r, stderr: %r' %
Raman Tenneti9e787532021-02-01 11:47:06 -0800135 (retval, p.stderr), file=sys.stderr)
136 return False
137 return True
138
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800139 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800140 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800141
142 Works only in git repositories.
143
144 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800145 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800146 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800147 if not os.path.exists(self._work_git):
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800148 print('git ls-tree missing drectory: %s' % self._work_git,
149 file=sys.stderr)
150 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800151 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800152 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800153 cmd = ['ls-tree', '-z', '-r', branch]
154
Raman Tenneti6a872c92021-01-14 19:17:50 -0800155 p = GitCommand(None,
156 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800157 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800158 capture_stdout=True,
159 capture_stderr=True)
160 retval = p.Wait()
161 if retval == 0:
162 data = p.stdout
163 else:
Raman Tenneti6a872c92021-01-14 19:17:50 -0800164 print('repo: error: git ls-tree call failed with return code: %r, stderr: %r' % (
165 retval, p.stderr), file=sys.stderr)
166 return data
167
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800168 def Sync(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800169 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800170
171 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800172 True if sync of superproject is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800173 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800174 print('WARNING: --use-superproject is experimental and not '
175 'for general use', file=sys.stderr)
176
177 if not self._manifest.superproject:
178 print('error: superproject tag is not defined in manifest',
179 file=sys.stderr)
180 return False
181
182 url = self._manifest.superproject['remote'].url
Raman Tenneti6a872c92021-01-14 19:17:50 -0800183 if not url:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800184 print('error: superproject URL is not defined in manifest',
185 file=sys.stderr)
186 return False
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800187
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800188 if not self._Init():
189 return False
190 if not self._Fetch(url):
191 return False
Raman Tennetief99ec02021-03-04 10:29:40 -0800192 if not self._quiet:
193 print('%s: Initial setup for superproject completed.' % self._work_git)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800194 return True
Raman Tenneti6a872c92021-01-14 19:17:50 -0800195
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800196 def _GetAllProjectsCommitIds(self):
197 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
198
199 Returns:
200 A dictionary with the projects/commit ids on success, otherwise None.
201 """
202 if not self.Sync():
203 return None
204
205 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800206 if not data:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800207 print('error: git ls-tree failed to return data for superproject',
208 file=sys.stderr)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800209 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800210
211 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800212 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800213 #
214 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
215 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800216 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800217 for line in data.split('\x00'):
218 ls_data = line.split(None, 3)
219 if not ls_data:
220 break
221 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800222 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800223
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800224 self._project_commit_ids = commit_ids
225 return commit_ids
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800226
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800227 def _WriteManfiestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800228 """Writes manifest to a file.
229
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800230 Returns:
231 manifest_path: Path name of the file into which manifest is written instead of None.
232 """
233 if not os.path.exists(self._superproject_path):
234 print('error: missing superproject directory %s' %
235 self._superproject_path,
236 file=sys.stderr)
237 return None
Raman Tenneti080877e2021-03-09 15:19:06 -0800238 manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800239 manifest_path = self._manifest_path
240 try:
241 with open(manifest_path, 'w', encoding='utf-8') as fp:
242 fp.write(manifest_str)
243 except IOError as e:
244 print('error: cannot write manifest to %s:\n%s'
245 % (manifest_path, e),
246 file=sys.stderr)
247 return None
248 return manifest_path
249
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800250 def UpdateProjectsRevisionId(self, projects):
251 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800252
253 Args:
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800254 projects: List of projects whose revisionId needs to be updated.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800255
256 Returns:
257 manifest_path: Path name of the overriding manfiest file instead of None.
258 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800259 commit_ids = self._GetAllProjectsCommitIds()
260 if not commit_ids:
261 print('error: Cannot get project commit ids from manifest', file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800262 return None
263
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800264 projects_missing_commit_ids = []
Raman Tennetifeb28912021-05-02 19:47:29 -0700265 superproject_remote_name = self._manifest.superproject['remote'].name
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800266 for project in projects:
267 path = project.relpath
268 if not path:
269 continue
Raman Tennetifeb28912021-05-02 19:47:29 -0700270 # Some manifests that pull projects from the "chromium" GoB
271 # (remote="chromium"), and have a private manifest that pulls projects
272 # from both the chromium GoB and "chrome-internal" GoB (remote="chrome").
273 # For such projects, one of the remotes will be different from
274 # superproject's remote. Until superproject, supports multiple remotes,
275 # don't update the commit ids of remotes that don't match superproject's
276 # remote.
277 if project.remote.name != superproject_remote_name:
278 continue
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800279 commit_id = commit_ids.get(path)
280 if commit_id:
281 project.SetRevisionId(commit_id)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800282 else:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800283 projects_missing_commit_ids.append(path)
284 if projects_missing_commit_ids:
285 print('error: please file a bug using %s to report missing commit_ids for: %s' %
Mike Frysingera1cd7702021-04-20 23:38:04 -0400286 (Wrapper().BUG_URL, projects_missing_commit_ids), file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800287 return None
288
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800289 manifest_path = self._WriteManfiestFile()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800290 return manifest_path