blob: 237e57e157eb5ec3d020f3f2462eed9d0b737ab7 [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 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
Raman Tenneti21dce3d2021-02-09 00:26:31 -080034from git_refs import R_HEADS
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 """
Raman Tenneti784e16f2021-06-11 17:29:45 -070074 def __init__(self, manifest, repodir, git_event_log,
Raman Tennetib55769a2021-08-13 11:47:24 -070075 superproject_dir='exp-superproject', quiet=False, print_messages=False):
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.
Raman Tenneti6a872c92021-01-14 19:17:50 -080080 repodir: Path to the .repo/ dir for holding all internal checkout state.
Raman Tenneti21dce3d2021-02-09 00:26:31 -080081 It must be in the top directory of the repo client checkout.
Raman Tenneti784e16f2021-06-11 17:29:45 -070082 git_event_log: A git trace2 event log to log events.
Raman Tenneti6a872c92021-01-14 19:17:50 -080083 superproject_dir: Relative path under |repodir| to checkout superproject.
Raman Tennetief99ec02021-03-04 10:29:40 -080084 quiet: If True then only print the progress messages.
Raman Tennetib55769a2021-08-13 11:47:24 -070085 print_messages: if True then print error/warning messages.
Raman Tenneti6a872c92021-01-14 19:17:50 -080086 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -080087 self._project_commit_ids = None
88 self._manifest = manifest
Raman Tenneti784e16f2021-06-11 17:29:45 -070089 self._git_event_log = git_event_log
Raman Tennetief99ec02021-03-04 10:29:40 -080090 self._quiet = quiet
Raman Tennetib55769a2021-08-13 11:47:24 -070091 self._print_messages = print_messages
Xin Lie0b16a22021-09-26 23:20:32 -070092 self._branch = manifest.branch
Raman Tenneti6a872c92021-01-14 19:17:50 -080093 self._repodir = os.path.abspath(repodir)
94 self._superproject_dir = superproject_dir
95 self._superproject_path = os.path.join(self._repodir, superproject_dir)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -080096 self._manifest_path = os.path.join(self._superproject_path,
Raman Tenneti8d43dea2021-02-07 16:30:27 -080097 _SUPERPROJECT_MANIFEST_NAME)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080098 git_name = ''
99 if self._manifest.superproject:
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700100 remote = self._manifest.superproject['remote']
101 git_name = hashlib.md5(remote.name.encode('utf8')).hexdigest() + '-'
Xin Lie0b16a22021-09-26 23:20:32 -0700102 self._branch = self._manifest.superproject['revision']
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700103 self._remote_url = remote.url
104 else:
105 self._remote_url = None
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800106 self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME
107 self._work_git = os.path.join(self._superproject_path, self._work_git_name)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800108
109 @property
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800110 def project_commit_ids(self):
111 """Returns a dictionary of projects and their commit ids."""
112 return self._project_commit_ids
Raman Tenneti6a872c92021-01-14 19:17:50 -0800113
Raman Tennetiae86a462021-07-27 08:54:59 -0700114 @property
115 def manifest_path(self):
116 """Returns the manifest path if the path exists or None."""
117 return self._manifest_path if os.path.exists(self._manifest_path) else None
118
Raman Tenneti5637afc2021-08-11 09:26:30 -0700119 def _LogMessage(self, message):
Raman Tenneti8db30d62021-07-06 21:30:06 -0700120 """Logs message to stderr and _git_event_log."""
Raman Tennetib55769a2021-08-13 11:47:24 -0700121 if self._print_messages:
122 print(message, file=sys.stderr)
Raman Tenneti7f8bd852021-09-02 16:13:06 -0700123 self._git_event_log.ErrorEvent(message, f'{message}')
Raman Tenneti8db30d62021-07-06 21:30:06 -0700124
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700125 def _LogMessagePrefix(self):
126 """Returns the prefix string to be logged in each log message"""
127 return f'repo superproject branch: {self._branch} url: {self._remote_url}'
128
Raman Tenneti5637afc2021-08-11 09:26:30 -0700129 def _LogError(self, message):
130 """Logs error message to stderr and _git_event_log."""
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700131 self._LogMessage(f'{self._LogMessagePrefix()} error: {message}')
Raman Tenneti5637afc2021-08-11 09:26:30 -0700132
133 def _LogWarning(self, message):
134 """Logs warning message to stderr and _git_event_log."""
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700135 self._LogMessage(f'{self._LogMessagePrefix()} warning: {message}')
Raman Tenneti5637afc2021-08-11 09:26:30 -0700136
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800137 def _Init(self):
138 """Sets up a local Git repository to get a copy of a superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800139
140 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800141 True if initialization is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800142 """
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800143 if not os.path.exists(self._superproject_path):
144 os.mkdir(self._superproject_path)
Raman Tennetief99ec02021-03-04 10:29:40 -0800145 if not self._quiet and not os.path.exists(self._work_git):
146 print('%s: Performing initial setup for superproject; this might take '
147 'several minutes.' % self._work_git)
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800148 cmd = ['init', '--bare', self._work_git_name]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800149 p = GitCommand(None,
150 cmd,
151 cwd=self._superproject_path,
152 capture_stdout=True,
153 capture_stderr=True)
154 retval = p.Wait()
155 if retval:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700156 self._LogWarning(f'git init call failed, command: git {cmd}, '
157 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800158 return False
159 return True
160
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700161 def _Fetch(self):
162 """Fetches a local copy of a superproject for the manifest based on |_remote_url|.
Raman Tenneti9e787532021-02-01 11:47:06 -0800163
164 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800165 True if fetch is successful, or False.
Raman Tenneti9e787532021-02-01 11:47:06 -0800166 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800167 if not os.path.exists(self._work_git):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700168 self._LogWarning(f'git fetch missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800169 return False
Raman Tennetie253b432021-06-02 10:05:54 -0700170 if not git_require((2, 28, 0)):
Raman Tennetib55769a2021-08-13 11:47:24 -0700171 self._LogWarning('superproject requires a git version 2.28 or later')
Raman Tennetie253b432021-06-02 10:05:54 -0700172 return False
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700173 cmd = ['fetch', self._remote_url, '--depth', '1', '--force', '--no-tags',
174 '--filter', 'blob:none']
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800175 if self._branch:
176 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800177 p = GitCommand(None,
178 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800179 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800180 capture_stdout=True,
181 capture_stderr=True)
182 retval = p.Wait()
183 if retval:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700184 self._LogWarning(f'git fetch call failed, command: git {cmd}, '
185 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti9e787532021-02-01 11:47:06 -0800186 return False
187 return True
188
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800189 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800190 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800191
192 Works only in git repositories.
193
194 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800195 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800196 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800197 if not os.path.exists(self._work_git):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700198 self._LogWarning(f'git ls-tree missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800199 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800200 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800201 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800202 cmd = ['ls-tree', '-z', '-r', branch]
203
Raman Tenneti6a872c92021-01-14 19:17:50 -0800204 p = GitCommand(None,
205 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800206 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800207 capture_stdout=True,
208 capture_stderr=True)
209 retval = p.Wait()
210 if retval == 0:
211 data = p.stdout
212 else:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700213 self._LogWarning(f'git ls-tree call failed, command: git {cmd}, '
214 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800215 return data
216
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800217 def Sync(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800218 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800219
220 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700221 SyncResult
Raman Tenneti6a872c92021-01-14 19:17:50 -0800222 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800223 if not self._manifest.superproject:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700224 self._LogWarning(f'superproject tag is not defined in manifest: '
225 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700226 return SyncResult(False, False)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800227
Raman Tenneti71e48b72022-01-04 22:33:19 +0000228 print('NOTICE: --use-superproject is in beta; report any issues to the '
229 'address described in `repo version`', file=sys.stderr)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700230 should_exit = True
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700231 if not self._remote_url:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700232 self._LogWarning(f'superproject URL is not defined in manifest: '
233 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700234 return SyncResult(False, should_exit)
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800235
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800236 if not self._Init():
Raman Tenneti784e16f2021-06-11 17:29:45 -0700237 return SyncResult(False, should_exit)
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700238 if not self._Fetch():
Raman Tenneti784e16f2021-06-11 17:29:45 -0700239 return SyncResult(False, should_exit)
Raman Tennetief99ec02021-03-04 10:29:40 -0800240 if not self._quiet:
241 print('%s: Initial setup for superproject completed.' % self._work_git)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700242 return SyncResult(True, False)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800243
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800244 def _GetAllProjectsCommitIds(self):
245 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
246
247 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700248 CommitIdsResult
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800249 """
Raman Tenneti784e16f2021-06-11 17:29:45 -0700250 sync_result = self.Sync()
251 if not sync_result.success:
252 return CommitIdsResult(None, sync_result.fatal)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800253
254 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800255 if not data:
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700256 self._LogWarning(f'git ls-tree failed to return data for manifest: '
Raman Tennetib55769a2021-08-13 11:47:24 -0700257 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700258 return CommitIdsResult(None, True)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800259
260 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800261 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800262 #
263 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
264 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800265 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800266 for line in data.split('\x00'):
267 ls_data = line.split(None, 3)
268 if not ls_data:
269 break
270 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800271 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800272
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800273 self._project_commit_ids = commit_ids
Raman Tenneti784e16f2021-06-11 17:29:45 -0700274 return CommitIdsResult(commit_ids, False)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800275
Raman Tennetib55769a2021-08-13 11:47:24 -0700276 def _WriteManifestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800277 """Writes manifest to a file.
278
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800279 Returns:
280 manifest_path: Path name of the file into which manifest is written instead of None.
281 """
282 if not os.path.exists(self._superproject_path):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700283 self._LogWarning(f'missing superproject directory: {self._superproject_path}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800284 return None
Raman Tenneti080877e2021-03-09 15:19:06 -0800285 manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800286 manifest_path = self._manifest_path
287 try:
288 with open(manifest_path, 'w', encoding='utf-8') as fp:
289 fp.write(manifest_str)
290 except IOError as e:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700291 self._LogError(f'cannot write manifest to : {manifest_path} {e}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800292 return None
293 return manifest_path
294
Raman Tenneti784e16f2021-06-11 17:29:45 -0700295 def _SkipUpdatingProjectRevisionId(self, project):
296 """Checks if a project's revision id needs to be updated or not.
297
298 Revision id for projects from local manifest will not be updated.
299
300 Args:
301 project: project whose revision id is being updated.
302
303 Returns:
304 True if a project's revision id should not be updated, or False,
305 """
306 path = project.relpath
307 if not path:
308 return True
Raman Tenneti1da6f302021-06-28 19:21:38 -0700309 # Skip the project with revisionId.
310 if project.revisionId:
311 return True
Raman Tenneti784e16f2021-06-11 17:29:45 -0700312 # Skip the project if it comes from the local manifest.
LaMont Jones87cce682022-02-14 17:48:31 +0000313 return project.manifest.IsFromLocalManifest(project)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700314
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800315 def UpdateProjectsRevisionId(self, projects):
316 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800317
318 Args:
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800319 projects: List of projects whose revisionId needs to be updated.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800320
321 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700322 UpdateProjectsResult
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800323 """
Raman Tenneti784e16f2021-06-11 17:29:45 -0700324 commit_ids_result = self._GetAllProjectsCommitIds()
325 commit_ids = commit_ids_result.commit_ids
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800326 if not commit_ids:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700327 return UpdateProjectsResult(None, commit_ids_result.fatal)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800328
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800329 projects_missing_commit_ids = []
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800330 for project in projects:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700331 if self._SkipUpdatingProjectRevisionId(project):
332 continue
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800333 path = project.relpath
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800334 commit_id = commit_ids.get(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700335 if not commit_id:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800336 projects_missing_commit_ids.append(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700337
338 # If superproject doesn't have a commit id for a project, then report an
339 # error event and continue as if do not use superproject is specified.
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800340 if projects_missing_commit_ids:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700341 self._LogWarning(f'please file a bug using {self._manifest.contactinfo.bugurl} '
342 f'to report missing commit_ids for: {projects_missing_commit_ids}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700343 return UpdateProjectsResult(None, False)
344
345 for project in projects:
346 if not self._SkipUpdatingProjectRevisionId(project):
347 project.SetRevisionId(commit_ids.get(project.relpath))
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800348
Raman Tennetib55769a2021-08-13 11:47:24 -0700349 manifest_path = self._WriteManifestFile()
Raman Tenneti784e16f2021-06-11 17:29:45 -0700350 return UpdateProjectsResult(manifest_path, False)
Xin Li0cb6e922021-06-16 10:19:00 -0700351
352
353@functools.lru_cache(maxsize=None)
354def _UseSuperprojectFromConfiguration():
355 """Returns the user choice of whether to use superproject."""
356 user_cfg = RepoConfig.ForUser()
Xin Li0cb6e922021-06-16 10:19:00 -0700357 time_now = int(time.time())
358
359 user_value = user_cfg.GetBoolean('repo.superprojectChoice')
360 if user_value is not None:
361 user_expiration = user_cfg.GetInt('repo.superprojectChoiceExpire')
Xin Li0ec20292021-09-14 16:42:37 -0700362 if user_expiration is None or user_expiration <= 0 or user_expiration >= time_now:
Xin Li0cb6e922021-06-16 10:19:00 -0700363 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
364 # default value.
Xin Li1328c352021-09-08 00:25:30 -0700365 if user_value:
366 print(('You are currently enrolled in Git submodules experiment '
367 '(go/android-submodules-quickstart). Use --no-use-superproject '
368 'to override.\n'), file=sys.stderr)
369 else:
370 print(('You are not currently enrolled in Git submodules experiment '
371 '(go/android-submodules-quickstart). Use --use-superproject '
372 'to override.\n'), file=sys.stderr)
Xin Li6f8c1bf2021-09-24 02:15:39 +0000373 return user_value
Xin Li0cb6e922021-06-16 10:19:00 -0700374
375 # We don't have an unexpired choice, ask for one.
Raman Tennetib55769a2021-08-13 11:47:24 -0700376 system_cfg = RepoConfig.ForSystem()
Xin Li0cb6e922021-06-16 10:19:00 -0700377 system_value = system_cfg.GetBoolean('repo.superprojectChoice')
378 if system_value:
379 # The system configuration is proposing that we should enable the
Xin Li0ec20292021-09-14 16:42:37 -0700380 # use of superproject. Treat the user as enrolled for two weeks.
Xin Li0cb6e922021-06-16 10:19:00 -0700381 #
382 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
383 # default value.
Xin Li0ec20292021-09-14 16:42:37 -0700384 userchoice = True
385 time_choiceexpire = time_now + (86400 * 14)
386 user_cfg.SetString('repo.superprojectChoiceExpire', str(time_choiceexpire))
387 user_cfg.SetBoolean('repo.superprojectChoice', userchoice)
388 print('You are automatically enrolled in Git submodules experiment '
389 '(go/android-submodules-quickstart) for another two weeks.\n',
390 file=sys.stderr)
391 return True
Xin Li0cb6e922021-06-16 10:19:00 -0700392
393 # For all other cases, we would not use superproject by default.
394 return False
395
396
Raman Tennetib55769a2021-08-13 11:47:24 -0700397def PrintMessages(opt, manifest):
398 """Returns a boolean if error/warning messages are to be printed."""
399 return opt.use_superproject is not None or manifest.superproject
400
401
Xin Li0cb6e922021-06-16 10:19:00 -0700402def UseSuperproject(opt, manifest):
403 """Returns a boolean if use-superproject option is enabled."""
404
405 if opt.use_superproject is not None:
406 return opt.use_superproject
407 else:
408 client_value = manifest.manifestProject.config.GetBoolean('repo.superproject')
409 if client_value is not None:
410 return client_value
411 else:
Xin Lib12c3692021-09-28 16:55:24 +0000412 if not manifest.superproject:
413 return False
Xin Li0cb6e922021-06-16 10:19:00 -0700414 return _UseSuperprojectFromConfiguration()