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 | |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 62 | # Path name of the overriding manifest file if successful, otherwise None. |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 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, |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 76 | superproject_dir='exp-superproject', quiet=False, print_messages=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 | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 86 | print_messages: if True then print error/warning messages. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 87 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 88 | self._project_commit_ids = None |
| 89 | self._manifest = manifest |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 90 | self._git_event_log = git_event_log |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 91 | self._quiet = quiet |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 92 | self._print_messages = print_messages |
Xin Li | e0b16a2 | 2021-09-26 23:20:32 -0700 | [diff] [blame] | 93 | self._branch = manifest.branch |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 94 | self._repodir = os.path.abspath(repodir) |
| 95 | self._superproject_dir = superproject_dir |
| 96 | self._superproject_path = os.path.join(self._repodir, superproject_dir) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 97 | self._manifest_path = os.path.join(self._superproject_path, |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 98 | _SUPERPROJECT_MANIFEST_NAME) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 99 | git_name = '' |
| 100 | if self._manifest.superproject: |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 101 | remote = self._manifest.superproject['remote'] |
| 102 | git_name = hashlib.md5(remote.name.encode('utf8')).hexdigest() + '-' |
Xin Li | e0b16a2 | 2021-09-26 23:20:32 -0700 | [diff] [blame] | 103 | self._branch = self._manifest.superproject['revision'] |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 104 | self._remote_url = remote.url |
| 105 | else: |
| 106 | self._remote_url = None |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 107 | self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME |
| 108 | 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] | 109 | |
| 110 | @property |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 111 | def project_commit_ids(self): |
| 112 | """Returns a dictionary of projects and their commit ids.""" |
| 113 | return self._project_commit_ids |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 114 | |
Raman Tenneti | ae86a46 | 2021-07-27 08:54:59 -0700 | [diff] [blame] | 115 | @property |
| 116 | def manifest_path(self): |
| 117 | """Returns the manifest path if the path exists or None.""" |
| 118 | return self._manifest_path if os.path.exists(self._manifest_path) else None |
| 119 | |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 120 | def _LogMessage(self, message): |
Raman Tenneti | 8db30d6 | 2021-07-06 21:30:06 -0700 | [diff] [blame] | 121 | """Logs message to stderr and _git_event_log.""" |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 122 | if self._print_messages: |
| 123 | print(message, file=sys.stderr) |
Raman Tenneti | 7f8bd85 | 2021-09-02 16:13:06 -0700 | [diff] [blame] | 124 | self._git_event_log.ErrorEvent(message, f'{message}') |
Raman Tenneti | 8db30d6 | 2021-07-06 21:30:06 -0700 | [diff] [blame] | 125 | |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 126 | def _LogMessagePrefix(self): |
| 127 | """Returns the prefix string to be logged in each log message""" |
| 128 | return f'repo superproject branch: {self._branch} url: {self._remote_url}' |
| 129 | |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 130 | def _LogError(self, message): |
| 131 | """Logs error message to stderr and _git_event_log.""" |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 132 | self._LogMessage(f'{self._LogMessagePrefix()} error: {message}') |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 133 | |
| 134 | def _LogWarning(self, message): |
| 135 | """Logs warning message to stderr and _git_event_log.""" |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 136 | self._LogMessage(f'{self._LogMessagePrefix()} warning: {message}') |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 137 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 138 | def _Init(self): |
| 139 | """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] | 140 | |
| 141 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 142 | True if initialization is successful, or False. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 143 | """ |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 144 | if not os.path.exists(self._superproject_path): |
| 145 | os.mkdir(self._superproject_path) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 146 | if not self._quiet and not os.path.exists(self._work_git): |
| 147 | print('%s: Performing initial setup for superproject; this might take ' |
| 148 | 'several minutes.' % self._work_git) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 149 | cmd = ['init', '--bare', self._work_git_name] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 150 | p = GitCommand(None, |
| 151 | cmd, |
| 152 | cwd=self._superproject_path, |
| 153 | capture_stdout=True, |
| 154 | capture_stderr=True) |
| 155 | retval = p.Wait() |
| 156 | if retval: |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 157 | self._LogWarning(f'git init call failed, command: git {cmd}, ' |
| 158 | f'return code: {retval}, stderr: {p.stderr}') |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 159 | return False |
| 160 | return True |
| 161 | |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 162 | def _Fetch(self): |
| 163 | """Fetches a local copy of a superproject for the manifest based on |_remote_url|. |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 164 | |
| 165 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 166 | True if fetch is successful, or False. |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 167 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 168 | if not os.path.exists(self._work_git): |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 169 | self._LogWarning(f'git fetch missing directory: {self._work_git}') |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 170 | return False |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 171 | if not git_require((2, 28, 0)): |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 172 | self._LogWarning('superproject requires a git version 2.28 or later') |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 173 | return False |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 174 | cmd = ['fetch', self._remote_url, '--depth', '1', '--force', '--no-tags', |
| 175 | '--filter', 'blob:none'] |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 176 | if self._branch: |
| 177 | cmd += [self._branch + ':' + self._branch] |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 178 | p = GitCommand(None, |
| 179 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 180 | cwd=self._work_git, |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 181 | capture_stdout=True, |
| 182 | capture_stderr=True) |
| 183 | retval = p.Wait() |
| 184 | if retval: |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 185 | self._LogWarning(f'git fetch call failed, command: git {cmd}, ' |
| 186 | f'return code: {retval}, stderr: {p.stderr}') |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 187 | return False |
| 188 | return True |
| 189 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 190 | def _LsTree(self): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 191 | """Gets the commit ids for all projects. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 192 | |
| 193 | Works only in git repositories. |
| 194 | |
| 195 | Returns: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 196 | data: data returned from 'git ls-tree ...' instead of None. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 197 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 198 | if not os.path.exists(self._work_git): |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 199 | self._LogWarning(f'git ls-tree missing directory: {self._work_git}') |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 200 | return None |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 201 | data = None |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 202 | branch = 'HEAD' if not self._branch else self._branch |
Raman Tenneti | ce64e3d | 2021-02-08 13:27:41 -0800 | [diff] [blame] | 203 | cmd = ['ls-tree', '-z', '-r', branch] |
| 204 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 205 | p = GitCommand(None, |
| 206 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 207 | cwd=self._work_git, |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 208 | capture_stdout=True, |
| 209 | capture_stderr=True) |
| 210 | retval = p.Wait() |
| 211 | if retval == 0: |
| 212 | data = p.stdout |
| 213 | else: |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 214 | self._LogWarning(f'git ls-tree call failed, command: git {cmd}, ' |
| 215 | f'return code: {retval}, stderr: {p.stderr}') |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 216 | return data |
| 217 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 218 | def Sync(self): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 219 | """Gets a local copy of a superproject for the manifest. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 220 | |
| 221 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 222 | SyncResult |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 223 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 224 | if not self._manifest.superproject: |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 225 | self._LogWarning(f'superproject tag is not defined in manifest: ' |
| 226 | f'{self._manifest.manifestFile}') |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 227 | return SyncResult(False, False) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 228 | |
Raman Tenneti | 71e48b7 | 2022-01-04 22:33:19 +0000 | [diff] [blame] | 229 | print('NOTICE: --use-superproject is in beta; report any issues to the ' |
| 230 | 'address described in `repo version`', file=sys.stderr) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 231 | should_exit = True |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 232 | if not self._remote_url: |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 233 | self._LogWarning(f'superproject URL is not defined in manifest: ' |
| 234 | f'{self._manifest.manifestFile}') |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 235 | return SyncResult(False, should_exit) |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 236 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 237 | if not self._Init(): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 238 | return SyncResult(False, should_exit) |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 239 | if not self._Fetch(): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 240 | return SyncResult(False, should_exit) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 241 | if not self._quiet: |
| 242 | print('%s: Initial setup for superproject completed.' % self._work_git) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 243 | return SyncResult(True, False) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 244 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 245 | def _GetAllProjectsCommitIds(self): |
| 246 | """Get commit ids for all projects from superproject and save them in _project_commit_ids. |
| 247 | |
| 248 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 249 | CommitIdsResult |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 250 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 251 | sync_result = self.Sync() |
| 252 | if not sync_result.success: |
| 253 | return CommitIdsResult(None, sync_result.fatal) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 254 | |
| 255 | data = self._LsTree() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 256 | if not data: |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 257 | self._LogWarning(f'git ls-tree failed to return data for manifest: ' |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 258 | f'{self._manifest.manifestFile}') |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 259 | return CommitIdsResult(None, True) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 260 | |
| 261 | # Parse lines like the following to select lines starting with '160000' and |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 262 | # 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] | 263 | # |
| 264 | # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00 |
| 265 | # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00 |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 266 | commit_ids = {} |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 267 | for line in data.split('\x00'): |
| 268 | ls_data = line.split(None, 3) |
| 269 | if not ls_data: |
| 270 | break |
| 271 | if ls_data[0] == '160000': |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 272 | commit_ids[ls_data[3]] = ls_data[2] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 273 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 274 | self._project_commit_ids = commit_ids |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 275 | return CommitIdsResult(commit_ids, False) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 276 | |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 277 | def _WriteManifestFile(self): |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 278 | """Writes manifest to a file. |
| 279 | |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 280 | Returns: |
| 281 | manifest_path: Path name of the file into which manifest is written instead of None. |
| 282 | """ |
| 283 | if not os.path.exists(self._superproject_path): |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 284 | self._LogWarning(f'missing superproject directory: {self._superproject_path}') |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 285 | return None |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 286 | manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr()).toxml() |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 287 | manifest_path = self._manifest_path |
| 288 | try: |
| 289 | with open(manifest_path, 'w', encoding='utf-8') as fp: |
| 290 | fp.write(manifest_str) |
| 291 | except IOError as e: |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 292 | self._LogError(f'cannot write manifest to : {manifest_path} {e}') |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 293 | return None |
| 294 | return manifest_path |
| 295 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 296 | def _SkipUpdatingProjectRevisionId(self, project): |
| 297 | """Checks if a project's revision id needs to be updated or not. |
| 298 | |
| 299 | Revision id for projects from local manifest will not be updated. |
| 300 | |
| 301 | Args: |
| 302 | project: project whose revision id is being updated. |
| 303 | |
| 304 | Returns: |
| 305 | True if a project's revision id should not be updated, or False, |
| 306 | """ |
| 307 | path = project.relpath |
| 308 | if not path: |
| 309 | return True |
Raman Tenneti | 1da6f30 | 2021-06-28 19:21:38 -0700 | [diff] [blame] | 310 | # Skip the project with revisionId. |
| 311 | if project.revisionId: |
| 312 | return True |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 313 | # Skip the project if it comes from the local manifest. |
| 314 | return any(s.startswith(LOCAL_MANIFEST_GROUP_PREFIX) for s in project.groups) |
| 315 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 316 | def UpdateProjectsRevisionId(self, projects): |
| 317 | """Update revisionId of every project in projects with the commit id. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 318 | |
| 319 | Args: |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 320 | projects: List of projects whose revisionId needs to be updated. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 321 | |
| 322 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 323 | UpdateProjectsResult |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 324 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 325 | commit_ids_result = self._GetAllProjectsCommitIds() |
| 326 | commit_ids = commit_ids_result.commit_ids |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 327 | if not commit_ids: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 328 | return UpdateProjectsResult(None, commit_ids_result.fatal) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 329 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 330 | projects_missing_commit_ids = [] |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 331 | for project in projects: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 332 | if self._SkipUpdatingProjectRevisionId(project): |
| 333 | continue |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 334 | path = project.relpath |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 335 | commit_id = commit_ids.get(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 336 | if not commit_id: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 337 | projects_missing_commit_ids.append(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 338 | |
| 339 | # If superproject doesn't have a commit id for a project, then report an |
| 340 | # error event and continue as if do not use superproject is specified. |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 341 | if projects_missing_commit_ids: |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 342 | self._LogWarning(f'please file a bug using {self._manifest.contactinfo.bugurl} ' |
| 343 | f'to report missing commit_ids for: {projects_missing_commit_ids}') |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 344 | return UpdateProjectsResult(None, False) |
| 345 | |
| 346 | for project in projects: |
| 347 | if not self._SkipUpdatingProjectRevisionId(project): |
| 348 | project.SetRevisionId(commit_ids.get(project.relpath)) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 349 | |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 350 | manifest_path = self._WriteManifestFile() |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 351 | return UpdateProjectsResult(manifest_path, False) |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 352 | |
| 353 | |
| 354 | @functools.lru_cache(maxsize=None) |
| 355 | def _UseSuperprojectFromConfiguration(): |
| 356 | """Returns the user choice of whether to use superproject.""" |
| 357 | user_cfg = RepoConfig.ForUser() |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 358 | time_now = int(time.time()) |
| 359 | |
| 360 | user_value = user_cfg.GetBoolean('repo.superprojectChoice') |
| 361 | if user_value is not None: |
| 362 | user_expiration = user_cfg.GetInt('repo.superprojectChoiceExpire') |
Xin Li | 0ec2029 | 2021-09-14 16:42:37 -0700 | [diff] [blame] | 363 | if user_expiration is None or user_expiration <= 0 or user_expiration >= time_now: |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 364 | # TODO(b/190688390) - Remove prompt when we are comfortable with the new |
| 365 | # default value. |
Xin Li | 1328c35 | 2021-09-08 00:25:30 -0700 | [diff] [blame] | 366 | if user_value: |
| 367 | print(('You are currently enrolled in Git submodules experiment ' |
| 368 | '(go/android-submodules-quickstart). Use --no-use-superproject ' |
| 369 | 'to override.\n'), file=sys.stderr) |
| 370 | else: |
| 371 | print(('You are not currently enrolled in Git submodules experiment ' |
| 372 | '(go/android-submodules-quickstart). Use --use-superproject ' |
| 373 | 'to override.\n'), file=sys.stderr) |
Xin Li | 6f8c1bf | 2021-09-24 02:15:39 +0000 | [diff] [blame] | 374 | return user_value |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 375 | |
| 376 | # We don't have an unexpired choice, ask for one. |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 377 | system_cfg = RepoConfig.ForSystem() |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 378 | system_value = system_cfg.GetBoolean('repo.superprojectChoice') |
| 379 | if system_value: |
| 380 | # The system configuration is proposing that we should enable the |
Xin Li | 0ec2029 | 2021-09-14 16:42:37 -0700 | [diff] [blame] | 381 | # use of superproject. Treat the user as enrolled for two weeks. |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 382 | # |
| 383 | # TODO(b/190688390) - Remove prompt when we are comfortable with the new |
| 384 | # default value. |
Xin Li | 0ec2029 | 2021-09-14 16:42:37 -0700 | [diff] [blame] | 385 | userchoice = True |
| 386 | time_choiceexpire = time_now + (86400 * 14) |
| 387 | user_cfg.SetString('repo.superprojectChoiceExpire', str(time_choiceexpire)) |
| 388 | user_cfg.SetBoolean('repo.superprojectChoice', userchoice) |
| 389 | print('You are automatically enrolled in Git submodules experiment ' |
| 390 | '(go/android-submodules-quickstart) for another two weeks.\n', |
| 391 | file=sys.stderr) |
| 392 | return True |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 393 | |
| 394 | # For all other cases, we would not use superproject by default. |
| 395 | return False |
| 396 | |
| 397 | |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 398 | def PrintMessages(opt, manifest): |
| 399 | """Returns a boolean if error/warning messages are to be printed.""" |
| 400 | return opt.use_superproject is not None or manifest.superproject |
| 401 | |
| 402 | |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 403 | def UseSuperproject(opt, manifest): |
| 404 | """Returns a boolean if use-superproject option is enabled.""" |
| 405 | |
| 406 | if opt.use_superproject is not None: |
| 407 | return opt.use_superproject |
| 408 | else: |
| 409 | client_value = manifest.manifestProject.config.GetBoolean('repo.superproject') |
| 410 | if client_value is not None: |
| 411 | return client_value |
| 412 | else: |
Xin Li | b12c369 | 2021-09-28 16:55:24 +0000 | [diff] [blame] | 413 | if not manifest.superproject: |
| 414 | return False |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 415 | return _UseSuperprojectFromConfiguration() |