blob: 8769355c10d8845c1bc5d09a5c53cbcd95538087 [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 Tenneti78f4dd32021-06-07 13:27:37 -070035from manifest_xml import LOCAL_MANIFEST_GROUP_PREFIX
Raman Tenneti6a872c92021-01-14 19:17:50 -080036
Raman Tenneti8d43dea2021-02-07 16:30:27 -080037_SUPERPROJECT_GIT_NAME = 'superproject.git'
38_SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml'
39
Raman Tenneti6a872c92021-01-14 19:17:50 -080040
Raman Tenneti784e16f2021-06-11 17:29:45 -070041class SyncResult(NamedTuple):
42 """Return the status of sync and whether caller should exit."""
43
44 # Whether the superproject sync was successful.
45 success: bool
46 # Whether the caller should exit.
47 fatal: bool
48
49
50class CommitIdsResult(NamedTuple):
51 """Return the commit ids and whether caller should exit."""
52
53 # A dictionary with the projects/commit ids on success, otherwise None.
54 commit_ids: dict
55 # Whether the caller should exit.
56 fatal: bool
57
58
59class UpdateProjectsResult(NamedTuple):
60 """Return the overriding manifest file and whether caller should exit."""
61
62 # Path name of the overriding manfiest file if successful, otherwise None.
63 manifest_path: str
64 # Whether the caller should exit.
65 fatal: bool
66
67
Raman Tenneti6a872c92021-01-14 19:17:50 -080068class Superproject(object):
Raman Tenneti21dce3d2021-02-09 00:26:31 -080069 """Get commit ids from superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -080070
Raman Tenneticeba2dd2021-02-22 16:54:56 -080071 Initializes a local copy of a superproject for the manifest. This allows
72 lookup of commit ids for all projects. It contains _project_commit_ids which
73 is a dictionary with project/commit id entries.
Raman Tenneti6a872c92021-01-14 19:17:50 -080074 """
Raman Tenneti784e16f2021-06-11 17:29:45 -070075 def __init__(self, manifest, repodir, git_event_log,
76 superproject_dir='exp-superproject', quiet=False):
Raman Tenneti6a872c92021-01-14 19:17:50 -080077 """Initializes superproject.
78
79 Args:
Raman Tenneti21dce3d2021-02-09 00:26:31 -080080 manifest: A Manifest object that is to be written to a file.
Raman Tenneti6a872c92021-01-14 19:17:50 -080081 repodir: Path to the .repo/ dir for holding all internal checkout state.
Raman Tenneti21dce3d2021-02-09 00:26:31 -080082 It must be in the top directory of the repo client checkout.
Raman Tenneti784e16f2021-06-11 17:29:45 -070083 git_event_log: A git trace2 event log to log events.
Raman Tenneti6a872c92021-01-14 19:17:50 -080084 superproject_dir: Relative path under |repodir| to checkout superproject.
Raman Tennetief99ec02021-03-04 10:29:40 -080085 quiet: If True then only print the progress 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 Tenneti21dce3d2021-02-09 00:26:31 -080091 self._branch = self._GetBranch()
Raman Tenneti6a872c92021-01-14 19:17:50 -080092 self._repodir = os.path.abspath(repodir)
93 self._superproject_dir = superproject_dir
94 self._superproject_path = os.path.join(self._repodir, 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)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080097 git_name = ''
98 if self._manifest.superproject:
99 remote_name = self._manifest.superproject['remote'].name
100 git_name = hashlib.md5(remote_name.encode('utf8')).hexdigest() + '-'
101 self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME
102 self._work_git = os.path.join(self._superproject_path, self._work_git_name)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800103
104 @property
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800105 def project_commit_ids(self):
106 """Returns a dictionary of projects and their commit ids."""
107 return self._project_commit_ids
Raman Tenneti6a872c92021-01-14 19:17:50 -0800108
Raman Tennetiae86a462021-07-27 08:54:59 -0700109 @property
110 def manifest_path(self):
111 """Returns the manifest path if the path exists or None."""
112 return self._manifest_path if os.path.exists(self._manifest_path) else None
113
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800114 def _GetBranch(self):
115 """Returns the branch name for getting the approved manifest."""
116 p = self._manifest.manifestProject
117 b = p.GetBranch(p.CurrentBranch)
118 if not b:
119 return None
120 branch = b.merge
121 if branch and branch.startswith(R_HEADS):
122 branch = branch[len(R_HEADS):]
123 return branch
124
Raman Tenneti8db30d62021-07-06 21:30:06 -0700125 def _LogError(self, message):
126 """Logs message to stderr and _git_event_log."""
127 print(message, file=sys.stderr)
128 self._git_event_log.ErrorEvent(message, '')
129
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800130 def _Init(self):
131 """Sets up a local Git repository to get a copy of a superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800132
133 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800134 True if initialization is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800135 """
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800136 if not os.path.exists(self._superproject_path):
137 os.mkdir(self._superproject_path)
Raman Tennetief99ec02021-03-04 10:29:40 -0800138 if not self._quiet and not os.path.exists(self._work_git):
139 print('%s: Performing initial setup for superproject; this might take '
140 'several minutes.' % self._work_git)
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800141 cmd = ['init', '--bare', self._work_git_name]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800142 p = GitCommand(None,
143 cmd,
144 cwd=self._superproject_path,
145 capture_stdout=True,
146 capture_stderr=True)
147 retval = p.Wait()
148 if retval:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700149 self._LogError(f'repo: error: git init call failed, command: git {cmd}, '
150 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800151 return False
152 return True
153
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800154 def _Fetch(self, url):
155 """Fetches a local copy of a superproject for the manifest based on url.
156
157 Args:
158 url: superproject's url.
Raman Tenneti9e787532021-02-01 11:47:06 -0800159
160 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800161 True if fetch is successful, or False.
Raman Tenneti9e787532021-02-01 11:47:06 -0800162 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800163 if not os.path.exists(self._work_git):
Raman Tenneti8db30d62021-07-06 21:30:06 -0700164 self._LogError(f'git fetch missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800165 return False
Raman Tennetie253b432021-06-02 10:05:54 -0700166 if not git_require((2, 28, 0)):
167 print('superproject requires a git version 2.28 or later', file=sys.stderr)
168 return False
Raman Tenneti83670962021-03-19 13:53:43 -0700169 cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none']
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800170 if self._branch:
171 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800172 p = GitCommand(None,
173 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800174 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800175 capture_stdout=True,
176 capture_stderr=True)
177 retval = p.Wait()
178 if retval:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700179 self._LogError(f'repo: error: git fetch call failed, command: git {cmd}, '
180 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti9e787532021-02-01 11:47:06 -0800181 return False
182 return True
183
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800184 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800185 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800186
187 Works only in git repositories.
188
189 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800190 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800191 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800192 if not os.path.exists(self._work_git):
Raman Tenneti8db30d62021-07-06 21:30:06 -0700193 self._LogError(f'git ls-tree missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800194 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800195 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800196 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800197 cmd = ['ls-tree', '-z', '-r', branch]
198
Raman Tenneti6a872c92021-01-14 19:17:50 -0800199 p = GitCommand(None,
200 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800201 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800202 capture_stdout=True,
203 capture_stderr=True)
204 retval = p.Wait()
205 if retval == 0:
206 data = p.stdout
207 else:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700208 self._LogError(f'repo: error: git ls-tree call failed, command: git {cmd}, '
209 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800210 return data
211
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800212 def Sync(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800213 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800214
215 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700216 SyncResult
Raman Tenneti6a872c92021-01-14 19:17:50 -0800217 """
Raman Tenneti2b37fa32021-06-02 17:46:25 -0700218 print('NOTICE: --use-superproject is in beta; report any issues to the '
219 'address described in `repo version`', file=sys.stderr)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800220
221 if not self._manifest.superproject:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700222 self._LogError(f'repo error: superproject tag is not defined in manifest: '
223 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700224 return SyncResult(False, False)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800225
Raman Tenneti784e16f2021-06-11 17:29:45 -0700226 should_exit = True
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800227 url = self._manifest.superproject['remote'].url
Raman Tenneti6a872c92021-01-14 19:17:50 -0800228 if not url:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700229 self._LogError(f'repo error: superproject URL is not defined in manifest: '
230 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700231 return SyncResult(False, should_exit)
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800232
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800233 if not self._Init():
Raman Tenneti784e16f2021-06-11 17:29:45 -0700234 return SyncResult(False, should_exit)
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800235 if not self._Fetch(url):
Raman Tenneti784e16f2021-06-11 17:29:45 -0700236 return SyncResult(False, should_exit)
Raman Tennetief99ec02021-03-04 10:29:40 -0800237 if not self._quiet:
238 print('%s: Initial setup for superproject completed.' % self._work_git)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700239 return SyncResult(True, False)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800240
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800241 def _GetAllProjectsCommitIds(self):
242 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
243
244 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700245 CommitIdsResult
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800246 """
Raman Tenneti784e16f2021-06-11 17:29:45 -0700247 sync_result = self.Sync()
248 if not sync_result.success:
249 return CommitIdsResult(None, sync_result.fatal)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800250
251 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800252 if not data:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700253 print('warning: git ls-tree failed to return data for superproject',
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800254 file=sys.stderr)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700255 return CommitIdsResult(None, True)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800256
257 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800258 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800259 #
260 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
261 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800262 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800263 for line in data.split('\x00'):
264 ls_data = line.split(None, 3)
265 if not ls_data:
266 break
267 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800268 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800269
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800270 self._project_commit_ids = commit_ids
Raman Tenneti784e16f2021-06-11 17:29:45 -0700271 return CommitIdsResult(commit_ids, False)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800272
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800273 def _WriteManfiestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800274 """Writes manifest to a file.
275
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800276 Returns:
277 manifest_path: Path name of the file into which manifest is written instead of None.
278 """
279 if not os.path.exists(self._superproject_path):
Raman Tenneti8db30d62021-07-06 21:30:06 -0700280 self._LogError(f'error: missing superproject directory: {self._superproject_path}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800281 return None
Raman Tenneti080877e2021-03-09 15:19:06 -0800282 manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800283 manifest_path = self._manifest_path
284 try:
285 with open(manifest_path, 'w', encoding='utf-8') as fp:
286 fp.write(manifest_str)
287 except IOError as e:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700288 self._LogError(f'error: cannot write manifest to : {manifest_path} {e}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800289 return None
290 return manifest_path
291
Raman Tenneti784e16f2021-06-11 17:29:45 -0700292 def _SkipUpdatingProjectRevisionId(self, project):
293 """Checks if a project's revision id needs to be updated or not.
294
295 Revision id for projects from local manifest will not be updated.
296
297 Args:
298 project: project whose revision id is being updated.
299
300 Returns:
301 True if a project's revision id should not be updated, or False,
302 """
303 path = project.relpath
304 if not path:
305 return True
Raman Tenneti1da6f302021-06-28 19:21:38 -0700306 # Skip the project with revisionId.
307 if project.revisionId:
308 return True
Raman Tenneti784e16f2021-06-11 17:29:45 -0700309 # Skip the project if it comes from the local manifest.
310 return any(s.startswith(LOCAL_MANIFEST_GROUP_PREFIX) for s in project.groups)
311
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800312 def UpdateProjectsRevisionId(self, projects):
313 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800314
315 Args:
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800316 projects: List of projects whose revisionId needs to be updated.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800317
318 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700319 UpdateProjectsResult
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800320 """
Raman Tenneti784e16f2021-06-11 17:29:45 -0700321 commit_ids_result = self._GetAllProjectsCommitIds()
322 commit_ids = commit_ids_result.commit_ids
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800323 if not commit_ids:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700324 print('warning: Cannot get project commit ids from manifest', file=sys.stderr)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700325 return UpdateProjectsResult(None, commit_ids_result.fatal)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800326
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800327 projects_missing_commit_ids = []
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800328 for project in projects:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700329 if self._SkipUpdatingProjectRevisionId(project):
330 continue
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800331 path = project.relpath
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800332 commit_id = commit_ids.get(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700333 if not commit_id:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800334 projects_missing_commit_ids.append(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700335
336 # If superproject doesn't have a commit id for a project, then report an
337 # error event and continue as if do not use superproject is specified.
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800338 if projects_missing_commit_ids:
Raman Tenneti8db30d62021-07-06 21:30:06 -0700339 self._LogError(f'error: please file a bug using {self._manifest.contactinfo.bugurl} '
340 f'to report missing commit_ids for: {projects_missing_commit_ids}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700341 return UpdateProjectsResult(None, False)
342
343 for project in projects:
344 if not self._SkipUpdatingProjectRevisionId(project):
345 project.SetRevisionId(commit_ids.get(project.relpath))
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800346
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800347 manifest_path = self._WriteManfiestFile()
Raman Tenneti784e16f2021-06-11 17:29:45 -0700348 return UpdateProjectsResult(manifest_path, False)
Xin Li0cb6e922021-06-16 10:19:00 -0700349
350
351@functools.lru_cache(maxsize=None)
352def _UseSuperprojectFromConfiguration():
353 """Returns the user choice of whether to use superproject."""
354 user_cfg = RepoConfig.ForUser()
355 system_cfg = RepoConfig.ForSystem()
356 time_now = int(time.time())
357
358 user_value = user_cfg.GetBoolean('repo.superprojectChoice')
359 if user_value is not None:
360 user_expiration = user_cfg.GetInt('repo.superprojectChoiceExpire')
361 if user_expiration is not None and (user_expiration <= 0 or user_expiration >= time_now):
362 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
363 # default value.
364 print(('You are currently enrolled in Git submodules experiment '
365 '(go/android-submodules-quickstart). Use --no-use-superproject '
366 'to override.\n'), file=sys.stderr)
367 return user_value
368
369 # We don't have an unexpired choice, ask for one.
370 system_value = system_cfg.GetBoolean('repo.superprojectChoice')
371 if system_value:
372 # The system configuration is proposing that we should enable the
373 # use of superproject. Present this to user for confirmation if we
374 # are on a TTY, or, when we are not on a TTY, accept the system
375 # default for this time only.
376 #
377 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
378 # default value.
379 prompt = ('Repo can now use Git submodules (go/android-submodules-quickstart) '
380 'instead of manifests to represent the state of the Android '
381 'superproject, which results in faster syncs and better atomicity.\n\n')
382 if sys.stdout.isatty():
383 prompt += 'Would you like to opt in for two weeks (y/N)? '
384 response = input(prompt).lower()
385 time_choiceexpire = time_now + (86400 * 14)
386 if response in ('y', 'yes'):
387 userchoice = True
388 elif response in ('a', 'always'):
389 userchoice = True
390 time_choiceexpire = 0
391 elif response == 'never':
392 userchoice = False
393 time_choiceexpire = 0
394 elif response in ('n', 'no'):
395 userchoice = False
396 else:
397 # Unrecognized user response, assume the intention was no, but
398 # only for 2 hours instead of 2 weeks to balance between not
399 # being overly pushy while still retain the opportunity to
400 # enroll.
401 userchoice = False
402 time_choiceexpire = time_now + 7200
403
404 user_cfg.SetString('repo.superprojectChoiceExpire', str(time_choiceexpire))
405 user_cfg.SetBoolean('repo.superprojectChoice', userchoice)
406
407 return userchoice
408 else:
409 print('Accepting once since we are not on a TTY', file=sys.stderr)
410 return True
411
412 # For all other cases, we would not use superproject by default.
413 return False
414
415
416def UseSuperproject(opt, manifest):
417 """Returns a boolean if use-superproject option is enabled."""
418
419 if opt.use_superproject is not None:
420 return opt.use_superproject
421 else:
422 client_value = manifest.manifestProject.config.GetBoolean('repo.superproject')
423 if client_value is not None:
424 return client_value
425 else:
426 return _UseSuperprojectFromConfiguration()