blob: 7a4ca16baf861d03c8bf2144f47a96fcc340ab68 [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:
LaMont Jonesff6b1da2022-06-01 21:03:34 +000021 superproject = Superproject(manifest, name, remote, revision)
Raman Tenneti784e16f2021-06-11 17:29:45 -070022 UpdateProjectsResult = superproject.UpdateProjectsRevisionId(projects)
Raman Tenneti6a872c92021-01-14 19:17:50 -080023"""
24
Raman Tenneticeba2dd2021-02-22 16:54:56 -080025import hashlib
Xin Li0cb6e922021-06-16 10:19:00 -070026import functools
Raman Tenneti6a872c92021-01-14 19:17:50 -080027import os
28import sys
Xin Li0cb6e922021-06-16 10:19:00 -070029import time
Raman Tenneti784e16f2021-06-11 17:29:45 -070030from typing import NamedTuple
Raman Tenneti6a872c92021-01-14 19:17:50 -080031
Raman Tennetie253b432021-06-02 10:05:54 -070032from git_command import git_require, GitCommand
Xin Li0cb6e922021-06-16 10:19:00 -070033from git_config import RepoConfig
Joanna Wang0e4f1e72022-12-08 17:46:28 -050034from git_refs import R_HEADS, GitRefs
Raman Tenneti6a872c92021-01-14 19:17:50 -080035
Raman Tenneti8d43dea2021-02-07 16:30:27 -080036_SUPERPROJECT_GIT_NAME = 'superproject.git'
37_SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml'
38
Raman Tenneti6a872c92021-01-14 19:17:50 -080039
Raman Tenneti784e16f2021-06-11 17:29:45 -070040class SyncResult(NamedTuple):
41 """Return the status of sync and whether caller should exit."""
42
43 # Whether the superproject sync was successful.
44 success: bool
45 # Whether the caller should exit.
46 fatal: bool
47
48
49class CommitIdsResult(NamedTuple):
50 """Return the commit ids and whether caller should exit."""
51
52 # A dictionary with the projects/commit ids on success, otherwise None.
53 commit_ids: dict
54 # Whether the caller should exit.
55 fatal: bool
56
57
58class UpdateProjectsResult(NamedTuple):
59 """Return the overriding manifest file and whether caller should exit."""
60
Raman Tennetib55769a2021-08-13 11:47:24 -070061 # Path name of the overriding manifest file if successful, otherwise None.
Raman Tenneti784e16f2021-06-11 17:29:45 -070062 manifest_path: str
63 # Whether the caller should exit.
64 fatal: bool
65
66
Raman Tenneti6a872c92021-01-14 19:17:50 -080067class Superproject(object):
Raman Tenneti21dce3d2021-02-09 00:26:31 -080068 """Get commit ids from superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080069
Raman Tenneticeba2dd2021-02-22 16:54:56 -080070 Initializes a local copy of a superproject for the manifest. This allows
71 lookup of commit ids for all projects. It contains _project_commit_ids which
72 is a dictionary with project/commit id entries.
Raman Tenneti6a872c92021-01-14 19:17:50 -080073 """
LaMont Jonesd56e2eb2022-04-07 18:14:46 +000074 def __init__(self, manifest, name, remote, revision,
75 superproject_dir='exp-superproject'):
Raman Tenneti6a872c92021-01-14 19:17:50 -080076 """Initializes superproject.
77
78 Args:
Raman Tenneti21dce3d2021-02-09 00:26:31 -080079 manifest: A Manifest object that is to be written to a file.
LaMont Jonesd56e2eb2022-04-07 18:14:46 +000080 name: The unique name of the superproject
81 remote: The RemoteSpec for the remote.
82 revision: The name of the git branch to track.
83 superproject_dir: Relative path under |manifest.subdir| to checkout
84 superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080085 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -080086 self._project_commit_ids = None
87 self._manifest = manifest
LaMont Jonesd56e2eb2022-04-07 18:14:46 +000088 self.name = name
89 self.remote = remote
90 self.revision = self._branch = revision
91 self._repodir = manifest.repodir
Raman Tenneti6a872c92021-01-14 19:17:50 -080092 self._superproject_dir = superproject_dir
LaMont Jonescc879a92021-11-18 22:40:18 +000093 self._superproject_path = manifest.SubmanifestInfoDir(manifest.path_prefix,
94 superproject_dir)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -080095 self._manifest_path = os.path.join(self._superproject_path,
Raman Tenneti8d43dea2021-02-07 16:30:27 -080096 _SUPERPROJECT_MANIFEST_NAME)
LaMont Jonesd56e2eb2022-04-07 18:14:46 +000097 git_name = hashlib.md5(remote.name.encode('utf8')).hexdigest() + '-'
98 self._remote_url = remote.url
Raman Tenneticeba2dd2021-02-22 16:54:56 -080099 self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME
100 self._work_git = os.path.join(self._superproject_path, self._work_git_name)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800101
LaMont Jonesff6b1da2022-06-01 21:03:34 +0000102 # The following are command arguemnts, rather than superproject attributes,
103 # and were included here originally. They should eventually become
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000104 # arguments that are passed down from the public methods, instead of being
105 # treated as attributes.
106 self._git_event_log = None
107 self._quiet = False
108 self._print_messages = False
109
110 def SetQuiet(self, value):
111 """Set the _quiet attribute."""
112 self._quiet = value
113
114 def SetPrintMessages(self, value):
115 """Set the _print_messages attribute."""
116 self._print_messages = value
117
Raman Tenneti6a872c92021-01-14 19:17:50 -0800118 @property
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800119 def project_commit_ids(self):
120 """Returns a dictionary of projects and their commit ids."""
121 return self._project_commit_ids
Raman Tenneti6a872c92021-01-14 19:17:50 -0800122
Raman Tennetiae86a462021-07-27 08:54:59 -0700123 @property
124 def manifest_path(self):
125 """Returns the manifest path if the path exists or None."""
126 return self._manifest_path if os.path.exists(self._manifest_path) else None
127
Raman Tenneti5637afc2021-08-11 09:26:30 -0700128 def _LogMessage(self, message):
Raman Tenneti8db30d62021-07-06 21:30:06 -0700129 """Logs message to stderr and _git_event_log."""
Raman Tennetib55769a2021-08-13 11:47:24 -0700130 if self._print_messages:
131 print(message, file=sys.stderr)
Raman Tenneti7f8bd852021-09-02 16:13:06 -0700132 self._git_event_log.ErrorEvent(message, f'{message}')
Raman Tenneti8db30d62021-07-06 21:30:06 -0700133
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700134 def _LogMessagePrefix(self):
135 """Returns the prefix string to be logged in each log message"""
136 return f'repo superproject branch: {self._branch} url: {self._remote_url}'
137
Raman Tenneti5637afc2021-08-11 09:26:30 -0700138 def _LogError(self, message):
139 """Logs error message to stderr and _git_event_log."""
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700140 self._LogMessage(f'{self._LogMessagePrefix()} error: {message}')
Raman Tenneti5637afc2021-08-11 09:26:30 -0700141
142 def _LogWarning(self, message):
143 """Logs warning message to stderr and _git_event_log."""
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700144 self._LogMessage(f'{self._LogMessagePrefix()} warning: {message}')
Raman Tenneti5637afc2021-08-11 09:26:30 -0700145
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800146 def _Init(self):
147 """Sets up a local Git repository to get a copy of a superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800148
149 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800150 True if initialization is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800151 """
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800152 if not os.path.exists(self._superproject_path):
153 os.mkdir(self._superproject_path)
Raman Tennetief99ec02021-03-04 10:29:40 -0800154 if not self._quiet and not os.path.exists(self._work_git):
155 print('%s: Performing initial setup for superproject; this might take '
156 'several minutes.' % self._work_git)
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800157 cmd = ['init', '--bare', self._work_git_name]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800158 p = GitCommand(None,
159 cmd,
160 cwd=self._superproject_path,
161 capture_stdout=True,
162 capture_stderr=True)
163 retval = p.Wait()
164 if retval:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700165 self._LogWarning(f'git init call failed, command: git {cmd}, '
166 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800167 return False
168 return True
169
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700170 def _Fetch(self):
171 """Fetches a local copy of a superproject for the manifest based on |_remote_url|.
Raman Tenneti9e787532021-02-01 11:47:06 -0800172
173 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800174 True if fetch is successful, or False.
Raman Tenneti9e787532021-02-01 11:47:06 -0800175 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800176 if not os.path.exists(self._work_git):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700177 self._LogWarning(f'git fetch missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800178 return False
Raman Tennetie253b432021-06-02 10:05:54 -0700179 if not git_require((2, 28, 0)):
Raman Tennetib55769a2021-08-13 11:47:24 -0700180 self._LogWarning('superproject requires a git version 2.28 or later')
Raman Tennetie253b432021-06-02 10:05:54 -0700181 return False
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700182 cmd = ['fetch', self._remote_url, '--depth', '1', '--force', '--no-tags',
183 '--filter', 'blob:none']
Joanna Wang0e4f1e72022-12-08 17:46:28 -0500184
185 # Check if there is a local ref that we can pass to --negotiation-tip.
186 # If this is the first fetch, it does not exist yet.
187 # We use --negotiation-tip to speed up the fetch. Superproject branches do
188 # not share commits. So this lets git know it only needs to send commits
189 # reachable from the specified local refs.
190 rev_commit = GitRefs(self._work_git).get(f'refs/heads/{self.revision}')
191 if rev_commit:
192 cmd.extend(['--negotiation-tip', rev_commit])
193
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800194 if self._branch:
195 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800196 p = GitCommand(None,
197 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800198 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800199 capture_stdout=True,
200 capture_stderr=True)
201 retval = p.Wait()
202 if retval:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700203 self._LogWarning(f'git fetch call failed, command: git {cmd}, '
204 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti9e787532021-02-01 11:47:06 -0800205 return False
206 return True
207
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800208 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800209 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800210
211 Works only in git repositories.
212
213 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800214 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800215 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800216 if not os.path.exists(self._work_git):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700217 self._LogWarning(f'git ls-tree missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800218 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800219 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800220 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800221 cmd = ['ls-tree', '-z', '-r', branch]
222
Raman Tenneti6a872c92021-01-14 19:17:50 -0800223 p = GitCommand(None,
224 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800225 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800226 capture_stdout=True,
227 capture_stderr=True)
228 retval = p.Wait()
229 if retval == 0:
230 data = p.stdout
231 else:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700232 self._LogWarning(f'git ls-tree call failed, command: git {cmd}, '
233 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800234 return data
235
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000236 def Sync(self, git_event_log):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800237 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800238
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000239 Args:
240 git_event_log: an EventLog, for git tracing.
241
Raman Tenneti6a872c92021-01-14 19:17:50 -0800242 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700243 SyncResult
Raman Tenneti6a872c92021-01-14 19:17:50 -0800244 """
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000245 self._git_event_log = git_event_log
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800246 if not self._manifest.superproject:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700247 self._LogWarning(f'superproject tag is not defined in manifest: '
248 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700249 return SyncResult(False, False)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800250
LaMont Jones2cc3ab72022-04-13 15:58:58 +0000251 _PrintBetaNotice()
252
Raman Tenneti784e16f2021-06-11 17:29:45 -0700253 should_exit = True
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700254 if not self._remote_url:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700255 self._LogWarning(f'superproject URL is not defined in manifest: '
256 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700257 return SyncResult(False, should_exit)
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800258
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800259 if not self._Init():
Raman Tenneti784e16f2021-06-11 17:29:45 -0700260 return SyncResult(False, should_exit)
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700261 if not self._Fetch():
Raman Tenneti784e16f2021-06-11 17:29:45 -0700262 return SyncResult(False, should_exit)
Raman Tennetief99ec02021-03-04 10:29:40 -0800263 if not self._quiet:
264 print('%s: Initial setup for superproject completed.' % self._work_git)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700265 return SyncResult(True, False)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800266
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800267 def _GetAllProjectsCommitIds(self):
268 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
269
270 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700271 CommitIdsResult
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800272 """
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000273 sync_result = self.Sync(self._git_event_log)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700274 if not sync_result.success:
275 return CommitIdsResult(None, sync_result.fatal)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800276
277 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800278 if not data:
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700279 self._LogWarning(f'git ls-tree failed to return data for manifest: '
Raman Tennetib55769a2021-08-13 11:47:24 -0700280 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700281 return CommitIdsResult(None, True)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800282
283 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800284 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800285 #
286 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
287 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800288 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800289 for line in data.split('\x00'):
290 ls_data = line.split(None, 3)
291 if not ls_data:
292 break
293 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800294 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800295
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800296 self._project_commit_ids = commit_ids
Raman Tenneti784e16f2021-06-11 17:29:45 -0700297 return CommitIdsResult(commit_ids, False)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800298
Raman Tennetib55769a2021-08-13 11:47:24 -0700299 def _WriteManifestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800300 """Writes manifest to a file.
301
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800302 Returns:
303 manifest_path: Path name of the file into which manifest is written instead of None.
304 """
305 if not os.path.exists(self._superproject_path):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700306 self._LogWarning(f'missing superproject directory: {self._superproject_path}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800307 return None
LaMont Jonesa8cf5752022-07-15 20:31:33 +0000308 manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr(),
309 omit_local=True).toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800310 manifest_path = self._manifest_path
311 try:
312 with open(manifest_path, 'w', encoding='utf-8') as fp:
313 fp.write(manifest_str)
314 except IOError as e:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700315 self._LogError(f'cannot write manifest to : {manifest_path} {e}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800316 return None
317 return manifest_path
318
Raman Tenneti784e16f2021-06-11 17:29:45 -0700319 def _SkipUpdatingProjectRevisionId(self, project):
320 """Checks if a project's revision id needs to be updated or not.
321
322 Revision id for projects from local manifest will not be updated.
323
324 Args:
325 project: project whose revision id is being updated.
326
327 Returns:
328 True if a project's revision id should not be updated, or False,
329 """
330 path = project.relpath
331 if not path:
332 return True
Raman Tenneti1da6f302021-06-28 19:21:38 -0700333 # Skip the project with revisionId.
334 if project.revisionId:
335 return True
Raman Tenneti784e16f2021-06-11 17:29:45 -0700336 # Skip the project if it comes from the local manifest.
LaMont Jones87cce682022-02-14 17:48:31 +0000337 return project.manifest.IsFromLocalManifest(project)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700338
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000339 def UpdateProjectsRevisionId(self, projects, git_event_log):
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800340 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800341
342 Args:
LaMont Jonesff6b1da2022-06-01 21:03:34 +0000343 projects: a list of projects whose revisionId needs to be updated.
344 git_event_log: an EventLog, for git tracing.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800345
346 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700347 UpdateProjectsResult
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800348 """
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000349 self._git_event_log = git_event_log
Raman Tenneti784e16f2021-06-11 17:29:45 -0700350 commit_ids_result = self._GetAllProjectsCommitIds()
351 commit_ids = commit_ids_result.commit_ids
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800352 if not commit_ids:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700353 return UpdateProjectsResult(None, commit_ids_result.fatal)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800354
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800355 projects_missing_commit_ids = []
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800356 for project in projects:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700357 if self._SkipUpdatingProjectRevisionId(project):
358 continue
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800359 path = project.relpath
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800360 commit_id = commit_ids.get(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700361 if not commit_id:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800362 projects_missing_commit_ids.append(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700363
364 # If superproject doesn't have a commit id for a project, then report an
365 # error event and continue as if do not use superproject is specified.
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800366 if projects_missing_commit_ids:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700367 self._LogWarning(f'please file a bug using {self._manifest.contactinfo.bugurl} '
368 f'to report missing commit_ids for: {projects_missing_commit_ids}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700369 return UpdateProjectsResult(None, False)
370
371 for project in projects:
372 if not self._SkipUpdatingProjectRevisionId(project):
373 project.SetRevisionId(commit_ids.get(project.relpath))
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800374
Raman Tennetib55769a2021-08-13 11:47:24 -0700375 manifest_path = self._WriteManifestFile()
Raman Tenneti784e16f2021-06-11 17:29:45 -0700376 return UpdateProjectsResult(manifest_path, False)
Xin Li0cb6e922021-06-16 10:19:00 -0700377
378
LaMont Jones2cc3ab72022-04-13 15:58:58 +0000379@functools.lru_cache(maxsize=10)
380def _PrintBetaNotice():
381 """Print the notice of beta status."""
382 print('NOTICE: --use-superproject is in beta; report any issues to the '
383 'address described in `repo version`', file=sys.stderr)
384
385
Xin Li0cb6e922021-06-16 10:19:00 -0700386@functools.lru_cache(maxsize=None)
387def _UseSuperprojectFromConfiguration():
388 """Returns the user choice of whether to use superproject."""
389 user_cfg = RepoConfig.ForUser()
Xin Li0cb6e922021-06-16 10:19:00 -0700390 time_now = int(time.time())
391
392 user_value = user_cfg.GetBoolean('repo.superprojectChoice')
393 if user_value is not None:
394 user_expiration = user_cfg.GetInt('repo.superprojectChoiceExpire')
Xin Li0ec20292021-09-14 16:42:37 -0700395 if user_expiration is None or user_expiration <= 0 or user_expiration >= time_now:
Xin Li0cb6e922021-06-16 10:19:00 -0700396 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
397 # default value.
Xin Li1328c352021-09-08 00:25:30 -0700398 if user_value:
399 print(('You are currently enrolled in Git submodules experiment '
400 '(go/android-submodules-quickstart). Use --no-use-superproject '
401 'to override.\n'), file=sys.stderr)
402 else:
403 print(('You are not currently enrolled in Git submodules experiment '
404 '(go/android-submodules-quickstart). Use --use-superproject '
405 'to override.\n'), file=sys.stderr)
Xin Li6f8c1bf2021-09-24 02:15:39 +0000406 return user_value
Xin Li0cb6e922021-06-16 10:19:00 -0700407
408 # We don't have an unexpired choice, ask for one.
Raman Tennetib55769a2021-08-13 11:47:24 -0700409 system_cfg = RepoConfig.ForSystem()
Xin Li0cb6e922021-06-16 10:19:00 -0700410 system_value = system_cfg.GetBoolean('repo.superprojectChoice')
411 if system_value:
412 # The system configuration is proposing that we should enable the
Xin Li0ec20292021-09-14 16:42:37 -0700413 # use of superproject. Treat the user as enrolled for two weeks.
Xin Li0cb6e922021-06-16 10:19:00 -0700414 #
415 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
416 # default value.
Xin Li0ec20292021-09-14 16:42:37 -0700417 userchoice = True
418 time_choiceexpire = time_now + (86400 * 14)
419 user_cfg.SetString('repo.superprojectChoiceExpire', str(time_choiceexpire))
420 user_cfg.SetBoolean('repo.superprojectChoice', userchoice)
421 print('You are automatically enrolled in Git submodules experiment '
422 '(go/android-submodules-quickstart) for another two weeks.\n',
423 file=sys.stderr)
424 return True
Xin Li0cb6e922021-06-16 10:19:00 -0700425
426 # For all other cases, we would not use superproject by default.
427 return False
428
429
LaMont Jones5fa912b2022-04-14 14:41:13 +0000430def PrintMessages(use_superproject, manifest):
431 """Returns a boolean if error/warning messages are to be printed.
432
433 Args:
434 use_superproject: option value from optparse.
435 manifest: manifest to use.
436 """
437 return use_superproject is not None or bool(manifest.superproject)
Raman Tennetib55769a2021-08-13 11:47:24 -0700438
439
LaMont Jones5fa912b2022-04-14 14:41:13 +0000440def UseSuperproject(use_superproject, manifest):
441 """Returns a boolean if use-superproject option is enabled.
Xin Li0cb6e922021-06-16 10:19:00 -0700442
LaMont Jones5fa912b2022-04-14 14:41:13 +0000443 Args:
444 use_superproject: option value from optparse.
445 manifest: manifest to use.
LaMont Jonesff6b1da2022-06-01 21:03:34 +0000446
447 Returns:
448 Whether the superproject should be used.
LaMont Jones5fa912b2022-04-14 14:41:13 +0000449 """
450
LaMont Jonesff6b1da2022-06-01 21:03:34 +0000451 if not manifest.superproject:
452 # This (sub) manifest does not have a superproject definition.
453 return False
454 elif use_superproject is not None:
LaMont Jones5fa912b2022-04-14 14:41:13 +0000455 return use_superproject
Xin Li0cb6e922021-06-16 10:19:00 -0700456 else:
LaMont Jonesd82be3e2022-04-05 19:30:46 +0000457 client_value = manifest.manifestProject.use_superproject
Xin Li0cb6e922021-06-16 10:19:00 -0700458 if client_value is not None:
459 return client_value
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000460 elif manifest.superproject:
Xin Li0cb6e922021-06-16 10:19:00 -0700461 return _UseSuperprojectFromConfiguration()
LaMont Jonesd56e2eb2022-04-07 18:14:46 +0000462 else:
463 return False