blob: 3c4144dda09d3050513e42d71fa0bf83d9c48559 [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 Tenneti78f4dd32021-06-07 13:27:37 -070031from manifest_xml import LOCAL_MANIFEST_GROUP_PREFIX
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 Tennetie253b432021-06-02 10:05:54 -0700124 if not git_require((2, 28, 0)):
125 print('superproject requires a git version 2.28 or later', file=sys.stderr)
126 return False
Raman Tenneti83670962021-03-19 13:53:43 -0700127 cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none']
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800128 if self._branch:
129 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800130 p = GitCommand(None,
131 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800132 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800133 capture_stdout=True,
134 capture_stderr=True)
135 retval = p.Wait()
136 if retval:
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800137 print('repo: error: git fetch call failed with return code: %r, stderr: %r' %
Raman Tenneti9e787532021-02-01 11:47:06 -0800138 (retval, p.stderr), file=sys.stderr)
139 return False
140 return True
141
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800142 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800143 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800144
145 Works only in git repositories.
146
147 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800148 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800149 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800150 if not os.path.exists(self._work_git):
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800151 print('git ls-tree missing drectory: %s' % self._work_git,
152 file=sys.stderr)
153 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800154 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800155 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800156 cmd = ['ls-tree', '-z', '-r', branch]
157
Raman Tenneti6a872c92021-01-14 19:17:50 -0800158 p = GitCommand(None,
159 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800160 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800161 capture_stdout=True,
162 capture_stderr=True)
163 retval = p.Wait()
164 if retval == 0:
165 data = p.stdout
166 else:
Raman Tenneti6a872c92021-01-14 19:17:50 -0800167 print('repo: error: git ls-tree call failed with return code: %r, stderr: %r' % (
168 retval, p.stderr), file=sys.stderr)
169 return data
170
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800171 def Sync(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800172 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800173
174 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800175 True if sync of superproject is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800176 """
Raman Tenneti2b37fa32021-06-02 17:46:25 -0700177 print('NOTICE: --use-superproject is in beta; report any issues to the '
178 'address described in `repo version`', file=sys.stderr)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800179
180 if not self._manifest.superproject:
181 print('error: superproject tag is not defined in manifest',
182 file=sys.stderr)
183 return False
184
185 url = self._manifest.superproject['remote'].url
Raman Tenneti6a872c92021-01-14 19:17:50 -0800186 if not url:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800187 print('error: superproject URL is not defined in manifest',
188 file=sys.stderr)
189 return False
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800190
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800191 if not self._Init():
192 return False
193 if not self._Fetch(url):
194 return False
Raman Tennetief99ec02021-03-04 10:29:40 -0800195 if not self._quiet:
196 print('%s: Initial setup for superproject completed.' % self._work_git)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800197 return True
Raman Tenneti6a872c92021-01-14 19:17:50 -0800198
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800199 def _GetAllProjectsCommitIds(self):
200 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
201
202 Returns:
203 A dictionary with the projects/commit ids on success, otherwise None.
204 """
205 if not self.Sync():
206 return None
207
208 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800209 if not data:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800210 print('error: git ls-tree failed to return data for superproject',
211 file=sys.stderr)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800212 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800213
214 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800215 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800216 #
217 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
218 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800219 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800220 for line in data.split('\x00'):
221 ls_data = line.split(None, 3)
222 if not ls_data:
223 break
224 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800225 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800226
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800227 self._project_commit_ids = commit_ids
228 return commit_ids
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800229
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800230 def _WriteManfiestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800231 """Writes manifest to a file.
232
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800233 Returns:
234 manifest_path: Path name of the file into which manifest is written instead of None.
235 """
236 if not os.path.exists(self._superproject_path):
237 print('error: missing superproject directory %s' %
238 self._superproject_path,
239 file=sys.stderr)
240 return None
Raman Tenneti080877e2021-03-09 15:19:06 -0800241 manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800242 manifest_path = self._manifest_path
243 try:
244 with open(manifest_path, 'w', encoding='utf-8') as fp:
245 fp.write(manifest_str)
246 except IOError as e:
247 print('error: cannot write manifest to %s:\n%s'
248 % (manifest_path, e),
249 file=sys.stderr)
250 return None
251 return manifest_path
252
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800253 def UpdateProjectsRevisionId(self, projects):
254 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800255
256 Args:
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800257 projects: List of projects whose revisionId needs to be updated.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800258
259 Returns:
260 manifest_path: Path name of the overriding manfiest file instead of None.
261 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800262 commit_ids = self._GetAllProjectsCommitIds()
263 if not commit_ids:
264 print('error: Cannot get project commit ids from manifest', file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800265 return None
266
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800267 projects_missing_commit_ids = []
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800268 for project in projects:
269 path = project.relpath
270 if not path:
271 continue
Raman Tenneti78f4dd32021-06-07 13:27:37 -0700272 # Skip the project if it comes from local manifest.
273 if any(s.startswith(LOCAL_MANIFEST_GROUP_PREFIX) for s in project.groups):
274 continue
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800275 commit_id = commit_ids.get(path)
276 if commit_id:
277 project.SetRevisionId(commit_id)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800278 else:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800279 projects_missing_commit_ids.append(path)
280 if projects_missing_commit_ids:
281 print('error: please file a bug using %s to report missing commit_ids for: %s' %
Raman Tenneti993af5e2021-05-12 12:00:31 -0700282 (self._manifest.contactinfo.bugurl, projects_missing_commit_ids), file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800283 return None
284
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800285 manifest_path = self._WriteManfiestFile()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800286 return manifest_path