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: |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 21 | superproject = Superproject(manifest, name, remote, revision) |
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 |
Daniel Kutik | 035f22a | 2022-12-13 12:34:23 +0100 | [diff] [blame] | 34 | from git_refs import GitRefs |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 35 | |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 36 | _SUPERPROJECT_GIT_NAME = 'superproject.git' |
| 37 | _SUPERPROJECT_MANIFEST_NAME = 'superproject_override.xml' |
| 38 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 39 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 40 | class SyncResult(NamedTuple): |
| 41 | """Return the status of sync and whether caller should exit.""" |
| 42 | |
| 43 | # Whether the superproject sync was successful. |
| 44 | success: bool |
| 45 | # Whether the caller should exit. |
| 46 | fatal: bool |
| 47 | |
| 48 | |
| 49 | class CommitIdsResult(NamedTuple): |
| 50 | """Return the commit ids and whether caller should exit.""" |
| 51 | |
| 52 | # A dictionary with the projects/commit ids on success, otherwise None. |
| 53 | commit_ids: dict |
| 54 | # Whether the caller should exit. |
| 55 | fatal: bool |
| 56 | |
| 57 | |
| 58 | class UpdateProjectsResult(NamedTuple): |
| 59 | """Return the overriding manifest file and whether caller should exit.""" |
| 60 | |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 61 | # Path name of the overriding manifest file if successful, otherwise None. |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 62 | manifest_path: str |
| 63 | # Whether the caller should exit. |
| 64 | fatal: bool |
| 65 | |
| 66 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 67 | class Superproject(object): |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 68 | """Get commit ids from superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 69 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 70 | Initializes a local copy of a superproject for the manifest. This allows |
| 71 | lookup of commit ids for all projects. It contains _project_commit_ids which |
| 72 | is a dictionary with project/commit id entries. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 73 | """ |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 74 | def __init__(self, manifest, name, remote, revision, |
| 75 | superproject_dir='exp-superproject'): |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 76 | """Initializes superproject. |
| 77 | |
| 78 | Args: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 79 | manifest: A Manifest object that is to be written to a file. |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 80 | name: The unique name of the superproject |
| 81 | remote: The RemoteSpec for the remote. |
| 82 | revision: The name of the git branch to track. |
| 83 | superproject_dir: Relative path under |manifest.subdir| to checkout |
| 84 | superproject. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 85 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 86 | self._project_commit_ids = None |
| 87 | self._manifest = manifest |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 88 | self.name = name |
| 89 | self.remote = remote |
| 90 | self.revision = self._branch = revision |
| 91 | self._repodir = manifest.repodir |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 92 | self._superproject_dir = superproject_dir |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 93 | self._superproject_path = manifest.SubmanifestInfoDir(manifest.path_prefix, |
| 94 | 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) |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 97 | git_name = hashlib.md5(remote.name.encode('utf8')).hexdigest() + '-' |
| 98 | self._remote_url = remote.url |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 99 | self._work_git_name = git_name + _SUPERPROJECT_GIT_NAME |
| 100 | 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] | 101 | |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 102 | # The following are command arguemnts, rather than superproject attributes, |
| 103 | # and were included here originally. They should eventually become |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 104 | # arguments that are passed down from the public methods, instead of being |
| 105 | # treated as attributes. |
| 106 | self._git_event_log = None |
| 107 | self._quiet = False |
| 108 | self._print_messages = False |
| 109 | |
| 110 | def SetQuiet(self, value): |
| 111 | """Set the _quiet attribute.""" |
| 112 | self._quiet = value |
| 113 | |
| 114 | def SetPrintMessages(self, value): |
| 115 | """Set the _print_messages attribute.""" |
| 116 | self._print_messages = value |
| 117 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 118 | @property |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 119 | def project_commit_ids(self): |
| 120 | """Returns a dictionary of projects and their commit ids.""" |
| 121 | return self._project_commit_ids |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 122 | |
Raman Tenneti | ae86a46 | 2021-07-27 08:54:59 -0700 | [diff] [blame] | 123 | @property |
| 124 | def manifest_path(self): |
| 125 | """Returns the manifest path if the path exists or None.""" |
| 126 | return self._manifest_path if os.path.exists(self._manifest_path) else None |
| 127 | |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 128 | def _LogMessage(self, fmt, *inputs): |
Raman Tenneti | 8db30d6 | 2021-07-06 21:30:06 -0700 | [diff] [blame] | 129 | """Logs message to stderr and _git_event_log.""" |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 130 | message = f'{self._LogMessagePrefix()} {fmt.format(*inputs)}' |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 131 | if self._print_messages: |
| 132 | print(message, file=sys.stderr) |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 133 | self._git_event_log.ErrorEvent(message, fmt) |
Raman Tenneti | 8db30d6 | 2021-07-06 21:30:06 -0700 | [diff] [blame] | 134 | |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 135 | def _LogMessagePrefix(self): |
| 136 | """Returns the prefix string to be logged in each log message""" |
| 137 | return f'repo superproject branch: {self._branch} url: {self._remote_url}' |
| 138 | |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 139 | def _LogError(self, fmt, *inputs): |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 140 | """Logs error message to stderr and _git_event_log.""" |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 141 | self._LogMessage(f'error: {fmt}', *inputs) |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 142 | |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 143 | def _LogWarning(self, fmt, *inputs): |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 144 | """Logs warning message to stderr and _git_event_log.""" |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 145 | self._LogMessage(f'warning: {fmt}', *inputs) |
Raman Tenneti | 5637afc | 2021-08-11 09:26:30 -0700 | [diff] [blame] | 146 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 147 | def _Init(self): |
| 148 | """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] | 149 | |
| 150 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 151 | True if initialization is successful, or False. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 152 | """ |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 153 | if not os.path.exists(self._superproject_path): |
| 154 | os.mkdir(self._superproject_path) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 155 | if not self._quiet and not os.path.exists(self._work_git): |
| 156 | print('%s: Performing initial setup for superproject; this might take ' |
| 157 | 'several minutes.' % self._work_git) |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 158 | cmd = ['init', '--bare', self._work_git_name] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 159 | p = GitCommand(None, |
| 160 | cmd, |
| 161 | cwd=self._superproject_path, |
| 162 | capture_stdout=True, |
| 163 | capture_stderr=True) |
| 164 | retval = p.Wait() |
| 165 | if retval: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 166 | self._LogWarning('git init call failed, command: git {}, ' |
| 167 | 'return code: {}, stderr: {}', cmd, retval, p.stderr) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 168 | return False |
| 169 | return True |
| 170 | |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 171 | def _Fetch(self): |
| 172 | """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] | 173 | |
| 174 | Returns: |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 175 | True if fetch is successful, or False. |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 176 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 177 | if not os.path.exists(self._work_git): |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 178 | self._LogWarning('git fetch missing directory: {}', self._work_git) |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 179 | return False |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 180 | if not git_require((2, 28, 0)): |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 181 | self._LogWarning('superproject requires a git version 2.28 or later') |
Raman Tenneti | e253b43 | 2021-06-02 10:05:54 -0700 | [diff] [blame] | 182 | return False |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 183 | cmd = ['fetch', self._remote_url, '--depth', '1', '--force', '--no-tags', |
| 184 | '--filter', 'blob:none'] |
Joanna Wang | 0e4f1e7 | 2022-12-08 17:46:28 -0500 | [diff] [blame] | 185 | |
| 186 | # Check if there is a local ref that we can pass to --negotiation-tip. |
| 187 | # If this is the first fetch, it does not exist yet. |
| 188 | # We use --negotiation-tip to speed up the fetch. Superproject branches do |
| 189 | # not share commits. So this lets git know it only needs to send commits |
| 190 | # reachable from the specified local refs. |
| 191 | rev_commit = GitRefs(self._work_git).get(f'refs/heads/{self.revision}') |
| 192 | if rev_commit: |
| 193 | cmd.extend(['--negotiation-tip', rev_commit]) |
| 194 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 195 | if self._branch: |
| 196 | cmd += [self._branch + ':' + self._branch] |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 197 | p = GitCommand(None, |
| 198 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 199 | cwd=self._work_git, |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 200 | capture_stdout=True, |
| 201 | capture_stderr=True) |
| 202 | retval = p.Wait() |
| 203 | if retval: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 204 | self._LogWarning('git fetch call failed, command: git {}, ' |
| 205 | 'return code: {}, stderr: {}', cmd, retval, p.stderr) |
Raman Tenneti | 9e78753 | 2021-02-01 11:47:06 -0800 | [diff] [blame] | 206 | return False |
| 207 | return True |
| 208 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 209 | def _LsTree(self): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 210 | """Gets the commit ids for all projects. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 211 | |
| 212 | Works only in git repositories. |
| 213 | |
| 214 | Returns: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 215 | data: data returned from 'git ls-tree ...' instead of None. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 216 | """ |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 217 | if not os.path.exists(self._work_git): |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 218 | self._LogWarning('git ls-tree missing directory: {}', self._work_git) |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 219 | return None |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 220 | data = None |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 221 | branch = 'HEAD' if not self._branch else self._branch |
Raman Tenneti | ce64e3d | 2021-02-08 13:27:41 -0800 | [diff] [blame] | 222 | cmd = ['ls-tree', '-z', '-r', branch] |
| 223 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 224 | p = GitCommand(None, |
| 225 | cmd, |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 226 | cwd=self._work_git, |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 227 | capture_stdout=True, |
| 228 | capture_stderr=True) |
| 229 | retval = p.Wait() |
| 230 | if retval == 0: |
| 231 | data = p.stdout |
| 232 | else: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 233 | self._LogWarning('git ls-tree call failed, command: git {}, ' |
| 234 | 'return code: {}, stderr: {}', cmd, retval, p.stderr) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 235 | return data |
| 236 | |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 237 | def Sync(self, git_event_log): |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 238 | """Gets a local copy of a superproject for the manifest. |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 239 | |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 240 | Args: |
| 241 | git_event_log: an EventLog, for git tracing. |
| 242 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 243 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 244 | SyncResult |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 245 | """ |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 246 | self._git_event_log = git_event_log |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 247 | if not self._manifest.superproject: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 248 | self._LogWarning('superproject tag is not defined in manifest: {}', |
| 249 | self._manifest.manifestFile) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 250 | return SyncResult(False, False) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 251 | |
LaMont Jones | 2cc3ab7 | 2022-04-13 15:58:58 +0000 | [diff] [blame] | 252 | _PrintBetaNotice() |
| 253 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 254 | should_exit = True |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 255 | if not self._remote_url: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 256 | self._LogWarning('superproject URL is not defined in manifest: {}', |
| 257 | self._manifest.manifestFile) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 258 | return SyncResult(False, should_exit) |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 259 | |
Raman Tenneti | ceba2dd | 2021-02-22 16:54:56 -0800 | [diff] [blame] | 260 | if not self._Init(): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 261 | return SyncResult(False, should_exit) |
Raman Tenneti | d8e8ae8 | 2021-09-15 16:32:33 -0700 | [diff] [blame] | 262 | if not self._Fetch(): |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 263 | return SyncResult(False, should_exit) |
Raman Tenneti | ef99ec0 | 2021-03-04 10:29:40 -0800 | [diff] [blame] | 264 | if not self._quiet: |
| 265 | print('%s: Initial setup for superproject completed.' % self._work_git) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 266 | return SyncResult(True, False) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 267 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 268 | def _GetAllProjectsCommitIds(self): |
| 269 | """Get commit ids for all projects from superproject and save them in _project_commit_ids. |
| 270 | |
| 271 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 272 | CommitIdsResult |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 273 | """ |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 274 | sync_result = self.Sync(self._git_event_log) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 275 | if not sync_result.success: |
| 276 | return CommitIdsResult(None, sync_result.fatal) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 277 | |
| 278 | data = self._LsTree() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 279 | if not data: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 280 | self._LogWarning('git ls-tree failed to return data for manifest: {}', |
| 281 | self._manifest.manifestFile) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 282 | return CommitIdsResult(None, True) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 283 | |
| 284 | # Parse lines like the following to select lines starting with '160000' and |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 285 | # 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] | 286 | # |
| 287 | # 160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00 |
| 288 | # 120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00 |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 289 | commit_ids = {} |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 290 | for line in data.split('\x00'): |
| 291 | ls_data = line.split(None, 3) |
| 292 | if not ls_data: |
| 293 | break |
| 294 | if ls_data[0] == '160000': |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 295 | commit_ids[ls_data[3]] = ls_data[2] |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 296 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 297 | self._project_commit_ids = commit_ids |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 298 | return CommitIdsResult(commit_ids, False) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 299 | |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 300 | def _WriteManifestFile(self): |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 301 | """Writes manifest to a file. |
| 302 | |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 303 | Returns: |
| 304 | manifest_path: Path name of the file into which manifest is written instead of None. |
| 305 | """ |
| 306 | if not os.path.exists(self._superproject_path): |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 307 | self._LogWarning('missing superproject directory: {}', self._superproject_path) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 308 | return None |
LaMont Jones | a8cf575 | 2022-07-15 20:31:33 +0000 | [diff] [blame] | 309 | manifest_str = self._manifest.ToXml(groups=self._manifest.GetGroupsStr(), |
| 310 | omit_local=True).toxml() |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 311 | manifest_path = self._manifest_path |
| 312 | try: |
| 313 | with open(manifest_path, 'w', encoding='utf-8') as fp: |
| 314 | fp.write(manifest_str) |
| 315 | except IOError as e: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 316 | self._LogError('cannot write manifest to : {} {}', |
| 317 | manifest_path, e) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 318 | return None |
| 319 | return manifest_path |
| 320 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 321 | def _SkipUpdatingProjectRevisionId(self, project): |
| 322 | """Checks if a project's revision id needs to be updated or not. |
| 323 | |
| 324 | Revision id for projects from local manifest will not be updated. |
| 325 | |
| 326 | Args: |
| 327 | project: project whose revision id is being updated. |
| 328 | |
| 329 | Returns: |
| 330 | True if a project's revision id should not be updated, or False, |
| 331 | """ |
| 332 | path = project.relpath |
| 333 | if not path: |
| 334 | return True |
Raman Tenneti | 1da6f30 | 2021-06-28 19:21:38 -0700 | [diff] [blame] | 335 | # Skip the project with revisionId. |
| 336 | if project.revisionId: |
| 337 | return True |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 338 | # Skip the project if it comes from the local manifest. |
LaMont Jones | 87cce68 | 2022-02-14 17:48:31 +0000 | [diff] [blame] | 339 | return project.manifest.IsFromLocalManifest(project) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 340 | |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 341 | def UpdateProjectsRevisionId(self, projects, git_event_log): |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 342 | """Update revisionId of every project in projects with the commit id. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 343 | |
| 344 | Args: |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 345 | projects: a list of projects whose revisionId needs to be updated. |
| 346 | git_event_log: an EventLog, for git tracing. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 347 | |
| 348 | Returns: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 349 | UpdateProjectsResult |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 350 | """ |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 351 | self._git_event_log = git_event_log |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 352 | commit_ids_result = self._GetAllProjectsCommitIds() |
| 353 | commit_ids = commit_ids_result.commit_ids |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 354 | if not commit_ids: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 355 | return UpdateProjectsResult(None, commit_ids_result.fatal) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 356 | |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 357 | projects_missing_commit_ids = [] |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 358 | for project in projects: |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 359 | if self._SkipUpdatingProjectRevisionId(project): |
| 360 | continue |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 361 | path = project.relpath |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 362 | commit_id = commit_ids.get(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 363 | if not commit_id: |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 364 | projects_missing_commit_ids.append(path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 365 | |
| 366 | # If superproject doesn't have a commit id for a project, then report an |
| 367 | # error event and continue as if do not use superproject is specified. |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 368 | if projects_missing_commit_ids: |
Joanna Wang | 016a254 | 2023-02-01 15:15:00 -0500 | [diff] [blame] | 369 | self._LogWarning('please file a bug using {} to report missing ' |
| 370 | 'commit_ids for: {}', self._manifest.contactinfo.bugurl, |
| 371 | projects_missing_commit_ids) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 372 | return UpdateProjectsResult(None, False) |
| 373 | |
| 374 | for project in projects: |
| 375 | if not self._SkipUpdatingProjectRevisionId(project): |
| 376 | project.SetRevisionId(commit_ids.get(project.relpath)) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 377 | |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 378 | manifest_path = self._WriteManifestFile() |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 379 | return UpdateProjectsResult(manifest_path, False) |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 380 | |
| 381 | |
LaMont Jones | 2cc3ab7 | 2022-04-13 15:58:58 +0000 | [diff] [blame] | 382 | @functools.lru_cache(maxsize=10) |
| 383 | def _PrintBetaNotice(): |
| 384 | """Print the notice of beta status.""" |
| 385 | print('NOTICE: --use-superproject is in beta; report any issues to the ' |
| 386 | 'address described in `repo version`', file=sys.stderr) |
| 387 | |
| 388 | |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 389 | @functools.lru_cache(maxsize=None) |
| 390 | def _UseSuperprojectFromConfiguration(): |
| 391 | """Returns the user choice of whether to use superproject.""" |
| 392 | user_cfg = RepoConfig.ForUser() |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 393 | time_now = int(time.time()) |
| 394 | |
| 395 | user_value = user_cfg.GetBoolean('repo.superprojectChoice') |
| 396 | if user_value is not None: |
| 397 | user_expiration = user_cfg.GetInt('repo.superprojectChoiceExpire') |
Xin Li | 0ec2029 | 2021-09-14 16:42:37 -0700 | [diff] [blame] | 398 | 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] | 399 | # TODO(b/190688390) - Remove prompt when we are comfortable with the new |
| 400 | # default value. |
Xin Li | 1328c35 | 2021-09-08 00:25:30 -0700 | [diff] [blame] | 401 | if user_value: |
| 402 | print(('You are currently enrolled in Git submodules experiment ' |
| 403 | '(go/android-submodules-quickstart). Use --no-use-superproject ' |
| 404 | 'to override.\n'), file=sys.stderr) |
| 405 | else: |
| 406 | print(('You are not currently enrolled in Git submodules experiment ' |
| 407 | '(go/android-submodules-quickstart). Use --use-superproject ' |
| 408 | 'to override.\n'), file=sys.stderr) |
Xin Li | 6f8c1bf | 2021-09-24 02:15:39 +0000 | [diff] [blame] | 409 | return user_value |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 410 | |
| 411 | # We don't have an unexpired choice, ask for one. |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 412 | system_cfg = RepoConfig.ForSystem() |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 413 | system_value = system_cfg.GetBoolean('repo.superprojectChoice') |
| 414 | if system_value: |
| 415 | # The system configuration is proposing that we should enable the |
Xin Li | 0ec2029 | 2021-09-14 16:42:37 -0700 | [diff] [blame] | 416 | # use of superproject. Treat the user as enrolled for two weeks. |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 417 | # |
| 418 | # TODO(b/190688390) - Remove prompt when we are comfortable with the new |
| 419 | # default value. |
Xin Li | 0ec2029 | 2021-09-14 16:42:37 -0700 | [diff] [blame] | 420 | userchoice = True |
| 421 | time_choiceexpire = time_now + (86400 * 14) |
| 422 | user_cfg.SetString('repo.superprojectChoiceExpire', str(time_choiceexpire)) |
| 423 | user_cfg.SetBoolean('repo.superprojectChoice', userchoice) |
| 424 | print('You are automatically enrolled in Git submodules experiment ' |
| 425 | '(go/android-submodules-quickstart) for another two weeks.\n', |
| 426 | file=sys.stderr) |
| 427 | return True |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 428 | |
| 429 | # For all other cases, we would not use superproject by default. |
| 430 | return False |
| 431 | |
| 432 | |
LaMont Jones | 5fa912b | 2022-04-14 14:41:13 +0000 | [diff] [blame] | 433 | def PrintMessages(use_superproject, manifest): |
| 434 | """Returns a boolean if error/warning messages are to be printed. |
| 435 | |
| 436 | Args: |
| 437 | use_superproject: option value from optparse. |
| 438 | manifest: manifest to use. |
| 439 | """ |
| 440 | return use_superproject is not None or bool(manifest.superproject) |
Raman Tenneti | b55769a | 2021-08-13 11:47:24 -0700 | [diff] [blame] | 441 | |
| 442 | |
LaMont Jones | 5fa912b | 2022-04-14 14:41:13 +0000 | [diff] [blame] | 443 | def UseSuperproject(use_superproject, manifest): |
| 444 | """Returns a boolean if use-superproject option is enabled. |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 445 | |
LaMont Jones | 5fa912b | 2022-04-14 14:41:13 +0000 | [diff] [blame] | 446 | Args: |
| 447 | use_superproject: option value from optparse. |
| 448 | manifest: manifest to use. |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 449 | |
| 450 | Returns: |
| 451 | Whether the superproject should be used. |
LaMont Jones | 5fa912b | 2022-04-14 14:41:13 +0000 | [diff] [blame] | 452 | """ |
| 453 | |
LaMont Jones | ff6b1da | 2022-06-01 21:03:34 +0000 | [diff] [blame] | 454 | if not manifest.superproject: |
| 455 | # This (sub) manifest does not have a superproject definition. |
| 456 | return False |
| 457 | elif use_superproject is not None: |
LaMont Jones | 5fa912b | 2022-04-14 14:41:13 +0000 | [diff] [blame] | 458 | return use_superproject |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 459 | else: |
LaMont Jones | d82be3e | 2022-04-05 19:30:46 +0000 | [diff] [blame] | 460 | client_value = manifest.manifestProject.use_superproject |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 461 | if client_value is not None: |
| 462 | return client_value |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 463 | elif manifest.superproject: |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 464 | return _UseSuperprojectFromConfiguration() |
LaMont Jones | d56e2eb | 2022-04-07 18:14:46 +0000 | [diff] [blame] | 465 | else: |
| 466 | return False |