blob: a09edc15b6d56a075d6bebc908b72df3a520e907 [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 Tenneti21dce3d2021-02-09 00:26:31 -080029from error import BUG_REPORT_URL
Raman Tenneti6a872c92021-01-14 19:17:50 -080030from git_command import GitCommand
Raman Tenneti21dce3d2021-02-09 00:26:31 -080031from git_refs import R_HEADS
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 Tenneti21dce3d2021-02-09 00:26:31 -080044 def __init__(self, manifest, repodir, superproject_dir='exp-superproject'):
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.
52 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -080053 self._project_commit_ids = None
54 self._manifest = manifest
55 self._branch = self._GetBranch()
Raman Tenneti6a872c92021-01-14 19:17:50 -080056 self._repodir = os.path.abspath(repodir)
57 self._superproject_dir = superproject_dir
58 self._superproject_path = os.path.join(self._repodir, superproject_dir)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -080059 self._manifest_path = os.path.join(self._superproject_path,
Raman Tenneti8d43dea2021-02-07 16:30:27 -080060 _SUPERPROJECT_MANIFEST_NAME)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080061 git_name = ''
62 if self._manifest.superproject:
63 remote_name = self._manifest.superproject['remote'].name
64 git_name = hashlib.md5(remote_name.encode('utf8')).hexdigest() + '-'
65 self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME
66 self._work_git = os.path.join(self._superproject_path, self._work_git_name)
Raman Tenneti6a872c92021-01-14 19:17:50 -080067
68 @property
Raman Tenneti21dce3d2021-02-09 00:26:31 -080069 def project_commit_ids(self):
70 """Returns a dictionary of projects and their commit ids."""
71 return self._project_commit_ids
Raman Tenneti6a872c92021-01-14 19:17:50 -080072
Raman Tenneti21dce3d2021-02-09 00:26:31 -080073 def _GetBranch(self):
74 """Returns the branch name for getting the approved manifest."""
75 p = self._manifest.manifestProject
76 b = p.GetBranch(p.CurrentBranch)
77 if not b:
78 return None
79 branch = b.merge
80 if branch and branch.startswith(R_HEADS):
81 branch = branch[len(R_HEADS):]
82 return branch
83
Raman Tenneticeba2dd2021-02-22 16:54:56 -080084 def _Init(self):
85 """Sets up a local Git repository to get a copy of a superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080086
87 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -080088 True if initialization is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -080089 """
Raman Tenneti8d43dea2021-02-07 16:30:27 -080090 if not os.path.exists(self._superproject_path):
91 os.mkdir(self._superproject_path)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080092 cmd = ['init', '--bare', self._work_git_name]
Raman Tenneti6a872c92021-01-14 19:17:50 -080093 p = GitCommand(None,
94 cmd,
95 cwd=self._superproject_path,
96 capture_stdout=True,
97 capture_stderr=True)
98 retval = p.Wait()
99 if retval:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800100 print('repo: error: git init call failed with return code: %r, stderr: %r' %
Raman Tenneti6a872c92021-01-14 19:17:50 -0800101 (retval, p.stderr), file=sys.stderr)
102 return False
103 return True
104
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800105 def _Fetch(self, url):
106 """Fetches a local copy of a superproject for the manifest based on url.
107
108 Args:
109 url: superproject's url.
Raman Tenneti9e787532021-02-01 11:47:06 -0800110
111 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800112 True if fetch is successful, or False.
Raman Tenneti9e787532021-02-01 11:47:06 -0800113 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800114 if not os.path.exists(self._work_git):
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800115 print('git fetch missing drectory: %s' % self._work_git,
116 file=sys.stderr)
117 return False
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800118 cmd = ['fetch', url, '--force', '--no-tags', '--filter', 'blob:none']
119 if self._branch:
120 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800121 p = GitCommand(None,
122 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800123 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800124 capture_stdout=True,
125 capture_stderr=True)
126 retval = p.Wait()
127 if retval:
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800128 print('repo: error: git fetch call failed with return code: %r, stderr: %r' %
Raman Tenneti9e787532021-02-01 11:47:06 -0800129 (retval, p.stderr), file=sys.stderr)
130 return False
131 return True
132
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800133 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800134 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800135
136 Works only in git repositories.
137
138 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800139 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800140 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800141 if not os.path.exists(self._work_git):
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800142 print('git ls-tree missing drectory: %s' % self._work_git,
143 file=sys.stderr)
144 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800145 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800146 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800147 cmd = ['ls-tree', '-z', '-r', branch]
148
Raman Tenneti6a872c92021-01-14 19:17:50 -0800149 p = GitCommand(None,
150 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800151 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800152 capture_stdout=True,
153 capture_stderr=True)
154 retval = p.Wait()
155 if retval == 0:
156 data = p.stdout
157 else:
Raman Tenneti6a872c92021-01-14 19:17:50 -0800158 print('repo: error: git ls-tree call failed with return code: %r, stderr: %r' % (
159 retval, p.stderr), file=sys.stderr)
160 return data
161
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800162 def Sync(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800163 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800164
165 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800166 True if sync of superproject is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800167 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800168 print('WARNING: --use-superproject is experimental and not '
169 'for general use', file=sys.stderr)
170
171 if not self._manifest.superproject:
172 print('error: superproject tag is not defined in manifest',
173 file=sys.stderr)
174 return False
175
176 url = self._manifest.superproject['remote'].url
Raman Tenneti6a872c92021-01-14 19:17:50 -0800177 if not url:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800178 print('error: superproject URL is not defined in manifest',
179 file=sys.stderr)
180 return False
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800181
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800182 if not self._Init():
183 return False
184 if not self._Fetch(url):
185 return False
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800186 return True
Raman Tenneti6a872c92021-01-14 19:17:50 -0800187
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800188 def _GetAllProjectsCommitIds(self):
189 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
190
191 Returns:
192 A dictionary with the projects/commit ids on success, otherwise None.
193 """
194 if not self.Sync():
195 return None
196
197 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800198 if not data:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800199 print('error: git ls-tree failed to return data for superproject',
200 file=sys.stderr)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800201 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800202
203 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800204 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800205 #
206 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
207 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800208 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800209 for line in data.split('\x00'):
210 ls_data = line.split(None, 3)
211 if not ls_data:
212 break
213 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800214 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800215
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800216 self._project_commit_ids = commit_ids
217 return commit_ids
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800218
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800219 def _WriteManfiestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800220 """Writes manifest to a file.
221
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800222 Returns:
223 manifest_path: Path name of the file into which manifest is written instead of None.
224 """
225 if not os.path.exists(self._superproject_path):
226 print('error: missing superproject directory %s' %
227 self._superproject_path,
228 file=sys.stderr)
229 return None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800230 manifest_str = self._manifest.ToXml().toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800231 manifest_path = self._manifest_path
232 try:
233 with open(manifest_path, 'w', encoding='utf-8') as fp:
234 fp.write(manifest_str)
235 except IOError as e:
236 print('error: cannot write manifest to %s:\n%s'
237 % (manifest_path, e),
238 file=sys.stderr)
239 return None
240 return manifest_path
241
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800242 def UpdateProjectsRevisionId(self, projects):
243 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800244
245 Args:
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800246 projects: List of projects whose revisionId needs to be updated.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800247
248 Returns:
249 manifest_path: Path name of the overriding manfiest file instead of None.
250 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800251 commit_ids = self._GetAllProjectsCommitIds()
252 if not commit_ids:
253 print('error: Cannot get project commit ids from manifest', file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800254 return None
255
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800256 projects_missing_commit_ids = []
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800257 for project in projects:
258 path = project.relpath
259 if not path:
260 continue
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800261 commit_id = commit_ids.get(path)
262 if commit_id:
263 project.SetRevisionId(commit_id)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800264 else:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800265 projects_missing_commit_ids.append(path)
266 if projects_missing_commit_ids:
267 print('error: please file a bug using %s to report missing commit_ids for: %s' %
268 (BUG_REPORT_URL, projects_missing_commit_ids), file=sys.stderr)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800269 return None
270
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800271 manifest_path = self._WriteManfiestFile()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800272 return manifest_path