Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 1 | # 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 Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 15 | """Provide functionality to get all projects and their commit ids from Superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 16 | |
| 17 | For more information on superproject, check out: |
| 18 | https://en.wikibooks.org/wiki/Git/Submodules_and_Superprojects |
| 19 | |
| 20 | Examples: |
| 21 | superproject = Superproject() |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 22 | UpdateProjectsResult = superproject.UpdateProjectsRevisionId(projects) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 23 | """ |
| 24 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 25 | import hashlib |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 26 | import functools |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 27 | import os |
| 28 | import sys |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 29 | import time |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 30 | from typing import NamedTuple |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 31 | |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 32 | from git_command import git_require, GitCommand |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 33 | from git_config import RepoConfig |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 34 | from git_refs import R_HEADS |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 35 | from manifest_xml import LOCAL_MANIFEST_GROUP_PREFIX |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 36 | |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 37 | _SUPERPROJECT_GIT_NAME = 'superproject.git' |
| 38 | _SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml' |
| 39 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 40 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 41 | class 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 | |
| 50 | class 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 | |
| 59 | class 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 Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 68 | class Superproject(object): |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 69 | """Get commit ids from superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 70 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 71 | 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 Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 74 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 75 | def __init__(self, manifest, repodir, git_event_log, |
| 76 | superproject_dir='exp-superproject', quiet=False): |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 77 | """Initializes superproject. |
| 78 | |
| 79 | Args: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 80 | manifest: A Manifest object that is to be written to a file. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 81 | repodir: Path to the .repo/ dir for holding all internal checkout state. |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 82 | It must be in the top directory of the repo client checkout. |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 83 | git_event_log: A git trace2 event log to log events. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 84 | superproject_dir: Relative path under |repodir| to checkout superproject. |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 85 | quiet: If True then only print the progress messages. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 86 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 87 | self._project_commit_ids = None |
| 88 | self._manifest = manifest |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 89 | self._git_event_log = git_event_log |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 90 | self._quiet = quiet |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 91 | self._branch = self._GetBranch() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 92 | self._repodir = os.path.abspath(repodir) |
| 93 | self._superproject_dir = superproject_dir |
| 94 | self._superproject_path = os.path.join(self._repodir, superproject_dir) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 95 | self._manifest_path = os.path.join(self._superproject_path, |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 96 | _SUPERPROJECT_MANIFEST_NAME) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 97 | 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 Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 103 | |
| 104 | @property |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 105 | def project_commit_ids(self): |
| 106 | """Returns a dictionary of projects and their commit ids.""" |
| 107 | return self._project_commit_ids |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 108 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 109 | def _GetBranch(self): |
| 110 | """Returns the branch name for getting the approved manifest.""" |
| 111 | p = self._manifest.manifestProject |
| 112 | b = p.GetBranch(p.CurrentBranch) |
| 113 | if not b: |
| 114 | return None |
| 115 | branch = b.merge |
| 116 | if branch and branch.startswith(R_HEADS): |
| 117 | branch = branch[len(R_HEADS):] |
| 118 | return branch |
| 119 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 120 | def _Init(self): |
| 121 | """Sets up a local Git repository to get a copy of a superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 122 | |
| 123 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 124 | True if initialization is successful, or False. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 125 | """ |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 126 | if not os.path.exists(self._superproject_path): |
| 127 | os.mkdir(self._superproject_path) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 128 | if not self._quiet and not os.path.exists(self._work_git): |
| 129 | print('%s: Performing initial setup for superproject; this might take ' |
| 130 | 'several minutes.' % self._work_git) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 131 | cmd = ['init', '--bare', self._work_git_name] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 132 | p = GitCommand(None, |
| 133 | cmd, |
| 134 | cwd=self._superproject_path, |
| 135 | capture_stdout=True, |
| 136 | capture_stderr=True) |
| 137 | retval = p.Wait() |
| 138 | if retval: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 139 | print('repo: error: git init call failed with return code: %r, stderr: %r' % |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 140 | (retval, p.stderr), file=sys.stderr) |
| 141 | return False |
| 142 | return True |
| 143 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 144 | def _Fetch(self, url): |
| 145 | """Fetches a local copy of a superproject for the manifest based on url. |
| 146 | |
| 147 | Args: |
| 148 | url: superproject's url. |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 149 | |
| 150 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 151 | True if fetch is successful, or False. |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 152 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 153 | if not os.path.exists(self._work_git): |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 154 | print('git fetch missing drectory: %s' % self._work_git, |
| 155 | file=sys.stderr) |
| 156 | return False |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 157 | if not git_require((2, 28, 0)): |
| 158 | print('superproject requires a git version 2.28 or later', file=sys.stderr) |
| 159 | return False |
Raman Tenneti | 8367096 | 2021-03-19 13:53:43 -0700 | [diff] [blame] | 160 | cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none'] |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 161 | if self._branch: |
| 162 | cmd += [self._branch + ':' + self._branch] |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 163 | p = GitCommand(None, |
| 164 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 165 | cwd=self._work_git, |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 166 | capture_stdout=True, |
| 167 | capture_stderr=True) |
| 168 | retval = p.Wait() |
| 169 | if retval: |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 170 | print('repo: error: git fetch call failed with return code: %r, stderr: %r' % |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 171 | (retval, p.stderr), file=sys.stderr) |
| 172 | return False |
| 173 | return True |
| 174 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 175 | def _LsTree(self): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 176 | """Gets the commit ids for all projects. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 177 | |
| 178 | Works only in git repositories. |
| 179 | |
| 180 | Returns: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 181 | data: data returned from 'git ls-tree ...' instead of None. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 182 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 183 | if not os.path.exists(self._work_git): |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 184 | print('git ls-tree missing drectory: %s' % self._work_git, |
| 185 | file=sys.stderr) |
| 186 | return None |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 187 | data = None |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 188 | branch = 'HEAD' if not self._branch else self._branch |
Raman Tenneti | ce64e3d | 2021-02-08 13:27:41 -0800 | [diff] [blame] | 189 | cmd = ['ls-tree', '-z', '-r', branch] |
| 190 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 191 | p = GitCommand(None, |
| 192 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 193 | cwd=self._work_git, |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 194 | capture_stdout=True, |
| 195 | capture_stderr=True) |
| 196 | retval = p.Wait() |
| 197 | if retval == 0: |
| 198 | data = p.stdout |
| 199 | else: |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 200 | print('repo: error: git ls-tree call failed with return code: %r, stderr: %r' % ( |
| 201 | retval, p.stderr), file=sys.stderr) |
| 202 | return data |
| 203 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 204 | def Sync(self): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 205 | """Gets a local copy of a superproject for the manifest. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 206 | |
| 207 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 208 | SyncResult |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 209 | """ |
Raman Tenneti | 2b37fa3 | 2021-06-02 17:46:25 -0700 | [diff] [blame] | 210 | print('NOTICE: --use-superproject is in beta; report any issues to the ' |
| 211 | 'address described in `repo version`', file=sys.stderr) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 212 | |
| 213 | if not self._manifest.superproject: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 214 | msg = (f'repo error: superproject tag is not defined in manifest: ' |
| 215 | f'{self._manifest.manifestFile}') |
| 216 | print(msg, file=sys.stderr) |
| 217 | self._git_event_log.ErrorEvent(msg, '') |
| 218 | return SyncResult(False, False) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 219 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 220 | should_exit = True |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 221 | url = self._manifest.superproject['remote'].url |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 222 | if not url: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 223 | print('error: superproject URL is not defined in manifest', |
| 224 | file=sys.stderr) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 225 | return SyncResult(False, should_exit) |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 226 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 227 | if not self._Init(): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 228 | return SyncResult(False, should_exit) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 229 | if not self._Fetch(url): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 230 | return SyncResult(False, should_exit) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 231 | if not self._quiet: |
| 232 | print('%s: Initial setup for superproject completed.' % self._work_git) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 233 | return SyncResult(True, False) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 234 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 235 | def _GetAllProjectsCommitIds(self): |
| 236 | """Get commit ids for all projects from superproject and save them in _project_commit_ids. |
| 237 | |
| 238 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 239 | CommitIdsResult |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 240 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 241 | sync_result = self.Sync() |
| 242 | if not sync_result.success: |
| 243 | return CommitIdsResult(None, sync_result.fatal) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 244 | |
| 245 | data = self._LsTree() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 246 | if not data: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 247 | print('error: git ls-tree failed to return data for superproject', |
| 248 | file=sys.stderr) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 249 | return CommitIdsResult(None, True) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 250 | |
| 251 | # Parse lines like the following to select lines starting with '160000' and |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 252 | # build a dictionary with project path (last element) and its commit id (3rd element). |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 253 | # |
| 254 | # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00 |
| 255 | # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00 |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 256 | commit_ids = {} |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 257 | for line in data.split('\x00'): |
| 258 | ls_data = line.split(None, 3) |
| 259 | if not ls_data: |
| 260 | break |
| 261 | if ls_data[0] == '160000': |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 262 | commit_ids[ls_data[3]] = ls_data[2] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 263 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 264 | self._project_commit_ids = commit_ids |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 265 | return CommitIdsResult(commit_ids, False) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 266 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 267 | def _WriteManfiestFile(self): |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 268 | """Writes manifest to a file. |
| 269 | |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 270 | Returns: |
| 271 | manifest_path: Path name of the file into which manifest is written instead of None. |
| 272 | """ |
| 273 | if not os.path.exists(self._superproject_path): |
| 274 | print('error: missing superproject directory %s' % |
| 275 | self._superproject_path, |
| 276 | file=sys.stderr) |
| 277 | return None |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 278 | manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml() |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 279 | manifest_path = self._manifest_path |
| 280 | try: |
| 281 | with open(manifest_path, 'w', encoding='utf-8') as fp: |
| 282 | fp.write(manifest_str) |
| 283 | except IOError as e: |
| 284 | print('error: cannot write manifest to %s:\n%s' |
| 285 | % (manifest_path, e), |
| 286 | file=sys.stderr) |
| 287 | return None |
| 288 | return manifest_path |
| 289 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 290 | def _SkipUpdatingProjectRevisionId(self, project): |
| 291 | """Checks if a project's revision id needs to be updated or not. |
| 292 | |
| 293 | Revision id for projects from local manifest will not be updated. |
| 294 | |
| 295 | Args: |
| 296 | project: project whose revision id is being updated. |
| 297 | |
| 298 | Returns: |
| 299 | True if a project's revision id should not be updated, or False, |
| 300 | """ |
| 301 | path = project.relpath |
| 302 | if not path: |
| 303 | return True |
Raman Tenneti | 1da6f30 | 2021-06-28 19:21:38 -0700 | [diff] [blame] | 304 | # Skip the project with revisionId. |
| 305 | if project.revisionId: |
| 306 | return True |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 307 | # Skip the project if it comes from the local manifest. |
| 308 | return any(s.startswith(LOCAL_MANIFEST_GROUP_PREFIX) for s in project.groups) |
| 309 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 310 | def UpdateProjectsRevisionId(self, projects): |
| 311 | """Update revisionId of every project in projects with the commit id. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 312 | |
| 313 | Args: |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 314 | projects: List of projects whose revisionId needs to be updated. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 315 | |
| 316 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 317 | UpdateProjectsResult |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 318 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 319 | commit_ids_result = self._GetAllProjectsCommitIds() |
| 320 | commit_ids = commit_ids_result.commit_ids |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 321 | if not commit_ids: |
| 322 | print('error: Cannot get project commit ids from manifest', file=sys.stderr) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 323 | return UpdateProjectsResult(None, commit_ids_result.fatal) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 324 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 325 | projects_missing_commit_ids = [] |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 326 | for project in projects: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 327 | if self._SkipUpdatingProjectRevisionId(project): |
| 328 | continue |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 329 | path = project.relpath |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 330 | commit_id = commit_ids.get(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 331 | if not commit_id: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 332 | projects_missing_commit_ids.append(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 333 | |
| 334 | # If superproject doesn't have a commit id for a project, then report an |
| 335 | # error event and continue as if do not use superproject is specified. |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 336 | if projects_missing_commit_ids: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 337 | msg = (f'error: please file a bug using {self._manifest.contactinfo.bugurl} ' |
| 338 | f'to report missing commit_ids for: {projects_missing_commit_ids}') |
| 339 | print(msg, file=sys.stderr) |
| 340 | self._git_event_log.ErrorEvent(msg, '') |
| 341 | 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 Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 346 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 347 | manifest_path = self._WriteManfiestFile() |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 348 | return UpdateProjectsResult(manifest_path, False) |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 349 | |
| 350 | |
| 351 | @functools.lru_cache(maxsize=None) |
| 352 | def _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 | |
| 416 | def 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() |