blob: 1dd59435944ab59b27f7b0a113ca4fe82396a0e1 [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
Raman Tennetib55769a2021-08-13 11:47:24 -070062 # Path name of the overriding manifest file if successful, otherwise None.
Raman Tenneti784e16f2021-06-11 17:29:45 -070063 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,
Raman Tennetib55769a2021-08-13 11:47:24 -070076 superproject_dir='exp-superproject', quiet=False, print_messages=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 Tennetib55769a2021-08-13 11:47:24 -070086 print_messages: if True then print error/warning messages.
Raman Tenneti6a872c92021-01-14 19:17:50 -080087 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -080088 self._project_commit_ids = None
89 self._manifest = manifest
Raman Tenneti784e16f2021-06-11 17:29:45 -070090 self._git_event_log = git_event_log
Raman Tennetief99ec02021-03-04 10:29:40 -080091 self._quiet = quiet
Raman Tennetib55769a2021-08-13 11:47:24 -070092 self._print_messages = print_messages
Raman Tenneti21dce3d2021-02-09 00:26:31 -080093 self._branch = self._GetBranch()
Raman Tenneti6a872c92021-01-14 19:17:50 -080094 self._repodir = os.path.abspath(repodir)
95 self._superproject_dir = superproject_dir
96 self._superproject_path = os.path.join(self._repodir, superproject_dir)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -080097 self._manifest_path = os.path.join(self._superproject_path,
Raman Tenneti8d43dea2021-02-07 16:30:27 -080098 _SUPERPROJECT_MANIFEST_NAME)
Raman Tenneticeba2dd2021-02-22 16:54:56 -080099 git_name = ''
100 if self._manifest.superproject:
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700101 remote = self._manifest.superproject['remote']
102 git_name = hashlib.md5(remote.name.encode('utf8')).hexdigest() + '-'
103 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 Tenneti21dce3d2021-02-09 00:26:31 -0800119 def _GetBranch(self):
120 """Returns the branch name for getting the approved manifest."""
121 p = self._manifest.manifestProject
122 b = p.GetBranch(p.CurrentBranch)
123 if not b:
124 return None
125 branch = b.merge
126 if branch and branch.startswith(R_HEADS):
127 branch = branch[len(R_HEADS):]
128 return branch
129
Raman Tenneti5637afc2021-08-11 09:26:30 -0700130 def _LogMessage(self, message):
Raman Tenneti8db30d62021-07-06 21:30:06 -0700131 """Logs message to stderr and _git_event_log."""
Raman Tennetib55769a2021-08-13 11:47:24 -0700132 if self._print_messages:
133 print(message, file=sys.stderr)
Raman Tenneti7f8bd852021-09-02 16:13:06 -0700134 self._git_event_log.ErrorEvent(message, f'{message}')
Raman Tenneti8db30d62021-07-06 21:30:06 -0700135
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700136 def _LogMessagePrefix(self):
137 """Returns the prefix string to be logged in each log message"""
138 return f'repo superproject branch: {self._branch} url: {self._remote_url}'
139
Raman Tenneti5637afc2021-08-11 09:26:30 -0700140 def _LogError(self, message):
141 """Logs error message to stderr and _git_event_log."""
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700142 self._LogMessage(f'{self._LogMessagePrefix()} error: {message}')
Raman Tenneti5637afc2021-08-11 09:26:30 -0700143
144 def _LogWarning(self, message):
145 """Logs warning message to stderr and _git_event_log."""
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700146 self._LogMessage(f'{self._LogMessagePrefix()} warning: {message}')
Raman Tenneti5637afc2021-08-11 09:26:30 -0700147
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800148 def _Init(self):
149 """Sets up a local Git repository to get a copy of a superproject.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800150
151 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800152 True if initialization is successful, or False.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800153 """
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800154 if not os.path.exists(self._superproject_path):
155 os.mkdir(self._superproject_path)
Raman Tennetief99ec02021-03-04 10:29:40 -0800156 if not self._quiet and not os.path.exists(self._work_git):
157 print('%s: Performing initial setup for superproject; this might take '
158 'several minutes.' % self._work_git)
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800159 cmd = ['init', '--bare', self._work_git_name]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800160 p = GitCommand(None,
161 cmd,
162 cwd=self._superproject_path,
163 capture_stdout=True,
164 capture_stderr=True)
165 retval = p.Wait()
166 if retval:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700167 self._LogWarning(f'git init call failed, command: git {cmd}, '
168 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800169 return False
170 return True
171
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700172 def _Fetch(self):
173 """Fetches a local copy of a superproject for the manifest based on |_remote_url|.
Raman Tenneti9e787532021-02-01 11:47:06 -0800174
175 Returns:
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800176 True if fetch is successful, or False.
Raman Tenneti9e787532021-02-01 11:47:06 -0800177 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800178 if not os.path.exists(self._work_git):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700179 self._LogWarning(f'git fetch missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800180 return False
Raman Tennetie253b432021-06-02 10:05:54 -0700181 if not git_require((2, 28, 0)):
Raman Tennetib55769a2021-08-13 11:47:24 -0700182 self._LogWarning('superproject requires a git version 2.28 or later')
Raman Tennetie253b432021-06-02 10:05:54 -0700183 return False
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700184 cmd = ['fetch', self._remote_url, '--depth', '1', '--force', '--no-tags',
185 '--filter', 'blob:none']
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800186 if self._branch:
187 cmd += [self._branch + ':' + self._branch]
Raman Tenneti9e787532021-02-01 11:47:06 -0800188 p = GitCommand(None,
189 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800190 cwd=self._work_git,
Raman Tenneti9e787532021-02-01 11:47:06 -0800191 capture_stdout=True,
192 capture_stderr=True)
193 retval = p.Wait()
194 if retval:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700195 self._LogWarning(f'git fetch call failed, command: git {cmd}, '
196 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti9e787532021-02-01 11:47:06 -0800197 return False
198 return True
199
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800200 def _LsTree(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800201 """Gets the commit ids for all projects.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800202
203 Works only in git repositories.
204
205 Returns:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800206 data: data returned from 'git ls-tree ...' instead of None.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800207 """
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800208 if not os.path.exists(self._work_git):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700209 self._LogWarning(f'git ls-tree missing directory: {self._work_git}')
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800210 return None
Raman Tenneti6a872c92021-01-14 19:17:50 -0800211 data = None
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800212 branch = 'HEAD' if not self._branch else self._branch
Raman Tennetice64e3d2021-02-08 13:27:41 -0800213 cmd = ['ls-tree', '-z', '-r', branch]
214
Raman Tenneti6a872c92021-01-14 19:17:50 -0800215 p = GitCommand(None,
216 cmd,
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800217 cwd=self._work_git,
Raman Tenneti6a872c92021-01-14 19:17:50 -0800218 capture_stdout=True,
219 capture_stderr=True)
220 retval = p.Wait()
221 if retval == 0:
222 data = p.stdout
223 else:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700224 self._LogWarning(f'git ls-tree call failed, command: git {cmd}, '
225 f'return code: {retval}, stderr: {p.stderr}')
Raman Tenneti6a872c92021-01-14 19:17:50 -0800226 return data
227
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800228 def Sync(self):
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800229 """Gets a local copy of a superproject for the manifest.
Raman Tenneti6a872c92021-01-14 19:17:50 -0800230
231 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700232 SyncResult
Raman Tenneti6a872c92021-01-14 19:17:50 -0800233 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800234 if not self._manifest.superproject:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700235 self._LogWarning(f'superproject tag is not defined in manifest: '
236 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700237 return SyncResult(False, False)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800238
Raman Tennetib55769a2021-08-13 11:47:24 -0700239 print('NOTICE: --use-superproject is in beta; report any issues to the '
240 'address described in `repo version`', file=sys.stderr)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700241 should_exit = True
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700242 if not self._remote_url:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700243 self._LogWarning(f'superproject URL is not defined in manifest: '
244 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700245 return SyncResult(False, should_exit)
Raman Tenneti8d43dea2021-02-07 16:30:27 -0800246
Raman Tenneticeba2dd2021-02-22 16:54:56 -0800247 if not self._Init():
Raman Tenneti784e16f2021-06-11 17:29:45 -0700248 return SyncResult(False, should_exit)
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700249 if not self._Fetch():
Raman Tenneti784e16f2021-06-11 17:29:45 -0700250 return SyncResult(False, should_exit)
Raman Tennetief99ec02021-03-04 10:29:40 -0800251 if not self._quiet:
252 print('%s: Initial setup for superproject completed.' % self._work_git)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700253 return SyncResult(True, False)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800254
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800255 def _GetAllProjectsCommitIds(self):
256 """Get commit ids for all projects from superproject and save them in _project_commit_ids.
257
258 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700259 CommitIdsResult
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800260 """
Raman Tenneti784e16f2021-06-11 17:29:45 -0700261 sync_result = self.Sync()
262 if not sync_result.success:
263 return CommitIdsResult(None, sync_result.fatal)
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800264
265 data = self._LsTree()
Raman Tenneti6a872c92021-01-14 19:17:50 -0800266 if not data:
Raman Tennetid8e8ae82021-09-15 16:32:33 -0700267 self._LogWarning(f'git ls-tree failed to return data for manifest: '
Raman Tennetib55769a2021-08-13 11:47:24 -0700268 f'{self._manifest.manifestFile}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700269 return CommitIdsResult(None, True)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800270
271 # Parse lines like the following to select lines starting with '160000' and
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800272 # build a dictionary with project path (last element) and its commit id (3rd element).
Raman Tenneti6a872c92021-01-14 19:17:50 -0800273 #
274 # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00
275 # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800276 commit_ids = {}
Raman Tenneti6a872c92021-01-14 19:17:50 -0800277 for line in data.split('\x00'):
278 ls_data = line.split(None, 3)
279 if not ls_data:
280 break
281 if ls_data[0] == '160000':
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800282 commit_ids[ls_data[3]] = ls_data[2]
Raman Tenneti6a872c92021-01-14 19:17:50 -0800283
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800284 self._project_commit_ids = commit_ids
Raman Tenneti784e16f2021-06-11 17:29:45 -0700285 return CommitIdsResult(commit_ids, False)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800286
Raman Tennetib55769a2021-08-13 11:47:24 -0700287 def _WriteManifestFile(self):
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800288 """Writes manifest to a file.
289
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800290 Returns:
291 manifest_path: Path name of the file into which manifest is written instead of None.
292 """
293 if not os.path.exists(self._superproject_path):
Raman Tenneti5637afc2021-08-11 09:26:30 -0700294 self._LogWarning(f'missing superproject directory: {self._superproject_path}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800295 return None
Raman Tenneti080877e2021-03-09 15:19:06 -0800296 manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml()
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800297 manifest_path = self._manifest_path
298 try:
299 with open(manifest_path, 'w', encoding='utf-8') as fp:
300 fp.write(manifest_str)
301 except IOError as e:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700302 self._LogError(f'cannot write manifest to : {manifest_path} {e}')
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800303 return None
304 return manifest_path
305
Raman Tenneti784e16f2021-06-11 17:29:45 -0700306 def _SkipUpdatingProjectRevisionId(self, project):
307 """Checks if a project's revision id needs to be updated or not.
308
309 Revision id for projects from local manifest will not be updated.
310
311 Args:
312 project: project whose revision id is being updated.
313
314 Returns:
315 True if a project's revision id should not be updated, or False,
316 """
317 path = project.relpath
318 if not path:
319 return True
Raman Tenneti1da6f302021-06-28 19:21:38 -0700320 # Skip the project with revisionId.
321 if project.revisionId:
322 return True
Raman Tenneti784e16f2021-06-11 17:29:45 -0700323 # Skip the project if it comes from the local manifest.
324 return any(s.startswith(LOCAL_MANIFEST_GROUP_PREFIX) for s in project.groups)
325
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800326 def UpdateProjectsRevisionId(self, projects):
327 """Update revisionId of every project in projects with the commit id.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800328
329 Args:
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800330 projects: List of projects whose revisionId needs to be updated.
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800331
332 Returns:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700333 UpdateProjectsResult
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800334 """
Raman Tenneti784e16f2021-06-11 17:29:45 -0700335 commit_ids_result = self._GetAllProjectsCommitIds()
336 commit_ids = commit_ids_result.commit_ids
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800337 if not commit_ids:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700338 return UpdateProjectsResult(None, commit_ids_result.fatal)
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800339
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800340 projects_missing_commit_ids = []
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800341 for project in projects:
Raman Tenneti784e16f2021-06-11 17:29:45 -0700342 if self._SkipUpdatingProjectRevisionId(project):
343 continue
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800344 path = project.relpath
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800345 commit_id = commit_ids.get(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700346 if not commit_id:
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800347 projects_missing_commit_ids.append(path)
Raman Tenneti784e16f2021-06-11 17:29:45 -0700348
349 # If superproject doesn't have a commit id for a project, then report an
350 # error event and continue as if do not use superproject is specified.
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800351 if projects_missing_commit_ids:
Raman Tenneti5637afc2021-08-11 09:26:30 -0700352 self._LogWarning(f'please file a bug using {self._manifest.contactinfo.bugurl} '
353 f'to report missing commit_ids for: {projects_missing_commit_ids}')
Raman Tenneti784e16f2021-06-11 17:29:45 -0700354 return UpdateProjectsResult(None, False)
355
356 for project in projects:
357 if not self._SkipUpdatingProjectRevisionId(project):
358 project.SetRevisionId(commit_ids.get(project.relpath))
Raman Tenneti1fd7bc22021-02-04 14:39:38 -0800359
Raman Tennetib55769a2021-08-13 11:47:24 -0700360 manifest_path = self._WriteManifestFile()
Raman Tenneti784e16f2021-06-11 17:29:45 -0700361 return UpdateProjectsResult(manifest_path, False)
Xin Li0cb6e922021-06-16 10:19:00 -0700362
363
364@functools.lru_cache(maxsize=None)
365def _UseSuperprojectFromConfiguration():
366 """Returns the user choice of whether to use superproject."""
367 user_cfg = RepoConfig.ForUser()
Xin Li0cb6e922021-06-16 10:19:00 -0700368 time_now = int(time.time())
369
370 user_value = user_cfg.GetBoolean('repo.superprojectChoice')
371 if user_value is not None:
372 user_expiration = user_cfg.GetInt('repo.superprojectChoiceExpire')
373 if user_expiration is not None and (user_expiration <= 0 or user_expiration >= time_now):
374 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
375 # default value.
Xin Li1328c352021-09-08 00:25:30 -0700376 if user_value:
377 print(('You are currently enrolled in Git submodules experiment '
378 '(go/android-submodules-quickstart). Use --no-use-superproject '
379 'to override.\n'), file=sys.stderr)
380 else:
381 print(('You are not currently enrolled in Git submodules experiment '
382 '(go/android-submodules-quickstart). Use --use-superproject '
383 'to override.\n'), file=sys.stderr)
Xin Li0cb6e922021-06-16 10:19:00 -0700384 return user_value
385
386 # We don't have an unexpired choice, ask for one.
Raman Tennetib55769a2021-08-13 11:47:24 -0700387 system_cfg = RepoConfig.ForSystem()
Xin Li0cb6e922021-06-16 10:19:00 -0700388 system_value = system_cfg.GetBoolean('repo.superprojectChoice')
389 if system_value:
390 # The system configuration is proposing that we should enable the
391 # use of superproject. Present this to user for confirmation if we
392 # are on a TTY, or, when we are not on a TTY, accept the system
393 # default for this time only.
394 #
395 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
396 # default value.
397 prompt = ('Repo can now use Git submodules (go/android-submodules-quickstart) '
398 'instead of manifests to represent the state of the Android '
399 'superproject, which results in faster syncs and better atomicity.\n\n')
400 if sys.stdout.isatty():
401 prompt += 'Would you like to opt in for two weeks (y/N)? '
402 response = input(prompt).lower()
403 time_choiceexpire = time_now + (86400 * 14)
404 if response in ('y', 'yes'):
405 userchoice = True
406 elif response in ('a', 'always'):
407 userchoice = True
408 time_choiceexpire = 0
409 elif response == 'never':
410 userchoice = False
411 time_choiceexpire = 0
412 elif response in ('n', 'no'):
413 userchoice = False
414 else:
415 # Unrecognized user response, assume the intention was no, but
416 # only for 2 hours instead of 2 weeks to balance between not
417 # being overly pushy while still retain the opportunity to
418 # enroll.
419 userchoice = False
420 time_choiceexpire = time_now + 7200
421
422 user_cfg.SetString('repo.superprojectChoiceExpire', str(time_choiceexpire))
423 user_cfg.SetBoolean('repo.superprojectChoice', userchoice)
424
425 return userchoice
426 else:
427 print('Accepting once since we are not on a TTY', file=sys.stderr)
428 return True
429
430 # For all other cases, we would not use superproject by default.
431 return False
432
433
Raman Tennetib55769a2021-08-13 11:47:24 -0700434def PrintMessages(opt, manifest):
435 """Returns a boolean if error/warning messages are to be printed."""
436 return opt.use_superproject is not None or manifest.superproject
437
438
Xin Li0cb6e922021-06-16 10:19:00 -0700439def UseSuperproject(opt, manifest):
440 """Returns a boolean if use-superproject option is enabled."""
441
442 if opt.use_superproject is not None:
443 return opt.use_superproject
444 else:
445 client_value = manifest.manifestProject.config.GetBoolean('repo.superproject')
446 if client_value is not None:
447 return client_value
448 else:
449 return _UseSuperprojectFromConfiguration()