The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 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 | |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 15 | import collections |
Mike Frysinger | ebf04a4 | 2021-02-23 20:48:04 -0500 | [diff] [blame] | 16 | import functools |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 17 | import http.cookiejar as cookielib |
Mike Frysinger | 7b586f2 | 2021-02-23 18:38:39 -0500 | [diff] [blame] | 18 | import io |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 19 | import json |
Mike Frysinger | ebf04a4 | 2021-02-23 20:48:04 -0500 | [diff] [blame] | 20 | import multiprocessing |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 21 | import netrc |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 22 | import optparse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import os |
Josip Sokcevic | 5554572 | 2024-02-22 16:38:00 -0800 | [diff] [blame] | 24 | from pathlib import Path |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 25 | import sys |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 26 | import tempfile |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 27 | import time |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 28 | from typing import List, NamedTuple, Set, Union |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 29 | import urllib.error |
| 30 | import urllib.parse |
| 31 | import urllib.request |
Mike Frysinger | 5951e30 | 2022-05-20 23:34:44 -0400 | [diff] [blame] | 32 | import xml.parsers.expat |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 33 | import xmlrpc.client |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 35 | |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 36 | try: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 37 | import threading as _threading |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 38 | except ImportError: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 39 | import dummy_threading as _threading |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 40 | |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 41 | try: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 42 | import resource |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 43 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 44 | def _rlimit_nofile(): |
| 45 | return resource.getrlimit(resource.RLIMIT_NOFILE) |
| 46 | |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 47 | except ImportError: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 48 | |
| 49 | def _rlimit_nofile(): |
| 50 | return (256, 256) |
| 51 | |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 52 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 53 | from command import Command |
| 54 | from command import DEFAULT_LOCAL_JOBS |
| 55 | from command import MirrorSafeCommand |
| 56 | from command import WORKER_BATCH_SIZE |
| 57 | from error import GitError |
| 58 | from error import RepoChangedException |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 59 | from error import RepoError |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 60 | from error import RepoExitError |
| 61 | from error import RepoUnhandledExceptionError |
| 62 | from error import SyncError |
| 63 | from error import UpdateManifestError |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 64 | import event_log |
Mike Frysinger | 347f9ed | 2021-03-15 14:58:52 -0400 | [diff] [blame] | 65 | from git_command import git_require |
David Pursehouse | ba7bc73 | 2015-08-20 16:55:42 +0900 | [diff] [blame] | 66 | from git_config import GetUrlCookieFile |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 67 | from git_refs import HEAD |
| 68 | from git_refs import R_HEADS |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 69 | import git_superproject |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 70 | import platform_utils |
| 71 | from progress import elapsed_str |
| 72 | from progress import jobs_str |
| 73 | from progress import Progress |
| 74 | from project import DeleteWorktreeError |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 75 | from project import Project |
| 76 | from project import RemoteSpec |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 77 | from project import SyncBuffer |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 78 | from repo_logging import RepoLogger |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 79 | from repo_trace import Trace |
Mike Frysinger | 19e409c | 2021-05-05 19:44:35 -0400 | [diff] [blame] | 80 | import ssh |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 81 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 83 | |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 84 | _ONE_DAY_S = 24 * 60 * 60 |
| 85 | |
Jason Chang | 1783332 | 2023-05-23 13:06:55 -0700 | [diff] [blame] | 86 | _REPO_ALLOW_SHALLOW = os.environ.get("REPO_ALLOW_SHALLOW") |
| 87 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 88 | logger = RepoLogger(__file__) |
| 89 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 90 | |
Josip Sokcevic | 5554572 | 2024-02-22 16:38:00 -0800 | [diff] [blame] | 91 | def _SafeCheckoutOrder(checkouts: List[Project]) -> List[List[Project]]: |
| 92 | """Generate a sequence of checkouts that is safe to perform. The client |
| 93 | should checkout everything from n-th index before moving to n+1. |
| 94 | |
| 95 | This is only useful if manifest contains nested projects. |
| 96 | |
| 97 | E.g. if foo, foo/bar and foo/bar/baz are project paths, then foo needs to |
| 98 | finish before foo/bar can proceed, and foo/bar needs to finish before |
| 99 | foo/bar/baz.""" |
| 100 | res = [[]] |
| 101 | current = res[0] |
| 102 | |
| 103 | # depth_stack contains a current stack of parent paths. |
| 104 | depth_stack = [] |
Josip Sokcevic | 4679022 | 2024-03-07 22:18:58 +0000 | [diff] [blame] | 105 | # Checkouts are iterated in the hierarchical order. That way, it can easily |
| 106 | # be determined if the previous checkout is parent of the current checkout. |
| 107 | # We are splitting by the path separator so the final result is |
| 108 | # hierarchical, and not just lexicographical. For example, if the projects |
| 109 | # are: foo, foo/bar, foo-bar, lexicographical order produces foo, foo-bar |
| 110 | # and foo/bar, which doesn't work. |
| 111 | for checkout in sorted(checkouts, key=lambda x: x.relpath.split("/")): |
Josip Sokcevic | 5554572 | 2024-02-22 16:38:00 -0800 | [diff] [blame] | 112 | checkout_path = Path(checkout.relpath) |
| 113 | while depth_stack: |
| 114 | try: |
| 115 | checkout_path.relative_to(depth_stack[-1]) |
| 116 | except ValueError: |
| 117 | # Path.relative_to returns ValueError if paths are not relative. |
| 118 | # TODO(sokcevic): Switch to is_relative_to once min supported |
| 119 | # version is py3.9. |
| 120 | depth_stack.pop() |
| 121 | else: |
| 122 | if len(depth_stack) >= len(res): |
| 123 | # Another depth created. |
| 124 | res.append([]) |
| 125 | break |
| 126 | |
| 127 | current = res[len(depth_stack)] |
| 128 | current.append(checkout) |
| 129 | depth_stack.append(checkout_path) |
| 130 | |
| 131 | return res |
| 132 | |
| 133 | |
Josip Sokcevic | 454fdaf | 2024-10-07 17:33:38 +0000 | [diff] [blame] | 134 | def _chunksize(projects: int, jobs: int) -> int: |
| 135 | """Calculate chunk size for the given number of projects and jobs.""" |
| 136 | return min(max(1, projects // jobs), WORKER_BATCH_SIZE) |
| 137 | |
| 138 | |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 139 | class _FetchOneResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 140 | """_FetchOne return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 141 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 142 | Attributes: |
| 143 | success (bool): True if successful. |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 144 | project_idx (int): The fetched project index. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 145 | start (float): The starting time.time(). |
| 146 | finish (float): The ending time.time(). |
| 147 | remote_fetched (bool): True if the remote was actually queried. |
| 148 | """ |
| 149 | |
| 150 | success: bool |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 151 | errors: List[Exception] |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 152 | project_idx: int |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 153 | start: float |
| 154 | finish: float |
| 155 | remote_fetched: bool |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | class _FetchResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 159 | """_Fetch return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 160 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 161 | Attributes: |
| 162 | success (bool): True if successful. |
| 163 | projects (Set[str]): The names of the git directories of fetched projects. |
| 164 | """ |
| 165 | |
| 166 | success: bool |
| 167 | projects: Set[str] |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 168 | |
| 169 | |
| 170 | class _FetchMainResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 171 | """_FetchMain return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 172 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 173 | Attributes: |
| 174 | all_projects (List[Project]): The fetched projects. |
| 175 | """ |
| 176 | |
| 177 | all_projects: List[Project] |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | class _CheckoutOneResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 181 | """_CheckoutOne return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 182 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 183 | Attributes: |
| 184 | success (bool): True if successful. |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 185 | project_idx (int): The project index. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 186 | start (float): The starting time.time(). |
| 187 | finish (float): The ending time.time(). |
| 188 | """ |
| 189 | |
| 190 | success: bool |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 191 | errors: List[Exception] |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 192 | project_idx: int |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 193 | start: float |
| 194 | finish: float |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 195 | |
| 196 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 197 | class SuperprojectError(SyncError): |
| 198 | """Superproject sync repo.""" |
| 199 | |
| 200 | |
| 201 | class SyncFailFastError(SyncError): |
| 202 | """Sync exit error when --fail-fast set.""" |
| 203 | |
| 204 | |
| 205 | class SmartSyncError(SyncError): |
| 206 | """Smart sync exit error.""" |
| 207 | |
| 208 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 209 | class ManifestInterruptError(RepoError): |
| 210 | """Aggregate Error to be logged when a user interrupts a manifest update.""" |
| 211 | |
| 212 | def __init__(self, output, **kwargs): |
| 213 | super().__init__(output, **kwargs) |
| 214 | self.output = output |
| 215 | |
| 216 | def __str__(self): |
| 217 | error_type = type(self).__name__ |
| 218 | return f"{error_type}:{self.output}" |
| 219 | |
| 220 | |
| 221 | class TeeStringIO(io.StringIO): |
| 222 | """StringIO class that can write to an additional destination.""" |
| 223 | |
| 224 | def __init__( |
| 225 | self, io: Union[io.TextIOWrapper, None], *args, **kwargs |
| 226 | ) -> None: |
| 227 | super().__init__(*args, **kwargs) |
| 228 | self.io = io |
| 229 | |
| 230 | def write(self, s: str) -> int: |
| 231 | """Write to additional destination.""" |
Daniel Kutik | b0430b5 | 2023-10-23 21:16:04 +0200 | [diff] [blame] | 232 | ret = super().write(s) |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 233 | if self.io is not None: |
| 234 | self.io.write(s) |
Daniel Kutik | b0430b5 | 2023-10-23 21:16:04 +0200 | [diff] [blame] | 235 | return ret |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 236 | |
| 237 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 238 | class Sync(Command, MirrorSafeCommand): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 239 | COMMON = True |
| 240 | MULTI_MANIFEST_SUPPORT = True |
| 241 | helpSummary = "Update working tree to the latest revision" |
| 242 | helpUsage = """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 243 | %prog [...] |
| 244 | """ |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 245 | helpDescription = """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 246 | The '%prog' command synchronizes local project directories |
| 247 | with the remote repositories specified in the manifest. If a local |
| 248 | project does not yet exist, it will clone a new local directory from |
| 249 | the remote repository and set up tracking branches as specified in |
| 250 | the manifest. If the local project already exists, '%prog' |
| 251 | will update the remote branches and rebase any new local changes |
| 252 | on top of the new remote changes. |
| 253 | |
| 254 | '%prog' will synchronize all projects listed at the command |
| 255 | line. Projects can be specified either by name, or by a relative |
| 256 | or absolute path to the project's local directory. If no projects |
| 257 | are specified, '%prog' will synchronize all projects listed in |
| 258 | the manifest. |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 259 | |
| 260 | The -d/--detach option can be used to switch specified projects |
| 261 | back to the manifest revision. This option is especially helpful |
| 262 | if the project is currently on a topic branch, but the manifest |
| 263 | revision is temporarily needed. |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 264 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 265 | The -s/--smart-sync option can be used to sync to a known good |
| 266 | build as specified by the manifest-server element in the current |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 267 | manifest. The -t/--smart-tag option is similar and allows you to |
| 268 | specify a custom tag/label. |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 269 | |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 270 | The -u/--manifest-server-username and -p/--manifest-server-password |
| 271 | options can be used to specify a username and password to authenticate |
| 272 | with the manifest server when using the -s or -t option. |
| 273 | |
| 274 | If -u and -p are not specified when using the -s or -t option, '%prog' |
| 275 | will attempt to read authentication credentials for the manifest server |
| 276 | from the user's .netrc file. |
| 277 | |
| 278 | '%prog' will not use authentication credentials from -u/-p or .netrc |
| 279 | if the manifest server specified in the manifest file already includes |
| 280 | credentials. |
| 281 | |
Mike Frysinger | d9e5cf0 | 2019-08-26 03:12:55 -0400 | [diff] [blame] | 282 | By default, all projects will be synced. The --fail-fast option can be used |
Mike Frysinger | 7ae210a | 2020-05-24 14:56:52 -0400 | [diff] [blame] | 283 | to halt syncing as soon as possible when the first project fails to sync. |
Andrei Warkentin | 5df6de0 | 2010-07-02 17:58:31 -0500 | [diff] [blame] | 284 | |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 285 | The --force-sync option can be used to overwrite existing git |
| 286 | directories if they have previously been linked to a different |
Roger Shimizu | ac29ac3 | 2020-06-06 02:33:40 +0900 | [diff] [blame] | 287 | object directory. WARNING: This may cause data to be lost since |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 288 | refs may be removed when overwriting. |
| 289 | |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 290 | The --force-checkout option can be used to force git to switch revs even if the |
| 291 | index or the working tree differs from HEAD, and if there are untracked files. |
| 292 | WARNING: This may cause data to be lost since uncommitted changes may be |
| 293 | removed. |
| 294 | |
Oleksii Okolielov | d3c0f59 | 2018-12-17 19:23:44 -0500 | [diff] [blame] | 295 | The --force-remove-dirty option can be used to remove previously used |
| 296 | projects with uncommitted changes. WARNING: This may cause data to be |
| 297 | lost since uncommitted changes may be removed with projects that no longer |
| 298 | exist in the manifest. |
| 299 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 300 | The --no-clone-bundle option disables any attempt to use |
| 301 | $URL/clone.bundle to bootstrap a new Git repository from a |
| 302 | resumeable bundle file on a content delivery network. This |
| 303 | may be necessary if there are problems with the local Python |
| 304 | HTTP client or proxy configuration, but the Git binary works. |
| 305 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 306 | The --fetch-submodules option enables fetching Git submodules |
| 307 | of a project from server. |
| 308 | |
David Pursehouse | f2fad61 | 2015-01-29 14:36:28 +0900 | [diff] [blame] | 309 | The -c/--current-branch option can be used to only fetch objects that |
| 310 | are on the branch specified by a project's revision. |
| 311 | |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 312 | The --optimized-fetch option can be used to only fetch projects that |
| 313 | are fixed to a sha1 revision if the sha1 revision does not already |
| 314 | exist locally. |
| 315 | |
David Pursehouse | 74cfd27 | 2015-10-14 10:50:15 +0900 | [diff] [blame] | 316 | The --prune option can be used to remove any refs that no longer |
| 317 | exist on the remote. |
| 318 | |
LaMont Jones | 7efab53 | 2022-09-01 15:41:12 +0000 | [diff] [blame] | 319 | The --auto-gc option can be used to trigger garbage collection on all |
| 320 | projects. By default, repo does not run garbage collection. |
| 321 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 322 | # SSH Connections |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 323 | |
| 324 | If at least one project remote URL uses an SSH connection (ssh://, |
| 325 | git+ssh://, or user@host:path syntax) repo will automatically |
| 326 | enable the SSH ControlMaster option when connecting to that host. |
| 327 | This feature permits other projects in the same '%prog' session to |
| 328 | reuse the same SSH tunnel, saving connection setup overheads. |
| 329 | |
| 330 | To disable this behavior on UNIX platforms, set the GIT_SSH |
| 331 | environment variable to 'ssh'. For example: |
| 332 | |
| 333 | export GIT_SSH=ssh |
| 334 | %prog |
| 335 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 336 | # Compatibility |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 337 | |
| 338 | This feature is automatically disabled on Windows, due to the lack |
| 339 | of UNIX domain socket support. |
| 340 | |
| 341 | This feature is not compatible with url.insteadof rewrites in the |
| 342 | user's ~/.gitconfig. '%prog' is currently not able to perform the |
| 343 | rewrite early enough to establish the ControlMaster tunnel. |
| 344 | |
| 345 | If the remote SSH daemon is Gerrit Code Review, version 2.0.10 or |
| 346 | later is required to fix a server side protocol bug. |
| 347 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 348 | """ |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 349 | # A value of 0 means we want parallel jobs, but we'll determine the default |
| 350 | # value later on. |
| 351 | PARALLEL_JOBS = 0 |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 352 | |
Gavin Mak | daebd6c | 2025-04-09 13:59:27 -0700 | [diff] [blame] | 353 | _JOBS_WARN_THRESHOLD = 100 |
| 354 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 355 | def _Options(self, p, show_smart=True): |
| 356 | p.add_option( |
| 357 | "--jobs-network", |
| 358 | default=None, |
| 359 | type=int, |
| 360 | metavar="JOBS", |
| 361 | help="number of network jobs to run in parallel (defaults to " |
| 362 | "--jobs or 1)", |
| 363 | ) |
| 364 | p.add_option( |
| 365 | "--jobs-checkout", |
| 366 | default=None, |
| 367 | type=int, |
| 368 | metavar="JOBS", |
| 369 | help="number of local checkout jobs to run in parallel (defaults " |
| 370 | f"to --jobs or {DEFAULT_LOCAL_JOBS})", |
| 371 | ) |
Mike Frysinger | 49de8ef | 2021-04-09 00:21:02 -0400 | [diff] [blame] | 372 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 373 | p.add_option( |
| 374 | "-f", |
| 375 | "--force-broken", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 376 | action="store_true", |
| 377 | help="obsolete option (to be deleted in the future)", |
| 378 | ) |
| 379 | p.add_option( |
| 380 | "--fail-fast", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 381 | action="store_true", |
| 382 | help="stop syncing after first error is hit", |
| 383 | ) |
| 384 | p.add_option( |
| 385 | "--force-sync", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 386 | action="store_true", |
| 387 | help="overwrite an existing git directory if it needs to " |
| 388 | "point to a different object directory. WARNING: this " |
| 389 | "may cause loss of data", |
| 390 | ) |
| 391 | p.add_option( |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 392 | "--force-checkout", |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 393 | action="store_true", |
| 394 | help="force checkout even if it results in throwing away " |
| 395 | "uncommitted modifications. " |
| 396 | "WARNING: this may cause loss of data", |
| 397 | ) |
| 398 | p.add_option( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 399 | "--force-remove-dirty", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 400 | action="store_true", |
| 401 | help="force remove projects with uncommitted modifications if " |
| 402 | "projects no longer exist in the manifest. " |
| 403 | "WARNING: this may cause loss of data", |
| 404 | ) |
| 405 | p.add_option( |
Jeroen Dhollander | c44ad09 | 2024-08-20 10:28:41 +0200 | [diff] [blame] | 406 | "--rebase", |
Jeroen Dhollander | c44ad09 | 2024-08-20 10:28:41 +0200 | [diff] [blame] | 407 | action="store_true", |
| 408 | help="rebase local commits regardless of whether they are " |
| 409 | "published", |
| 410 | ) |
| 411 | p.add_option( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 412 | "-l", |
| 413 | "--local-only", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 414 | action="store_true", |
| 415 | help="only update working tree, don't fetch", |
| 416 | ) |
| 417 | p.add_option( |
| 418 | "--no-manifest-update", |
| 419 | "--nmu", |
| 420 | dest="mp_update", |
| 421 | action="store_false", |
| 422 | default="true", |
| 423 | help="use the existing manifest checkout as-is. " |
| 424 | "(do not update to the latest revision)", |
| 425 | ) |
| 426 | p.add_option( |
| 427 | "-n", |
| 428 | "--network-only", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 429 | action="store_true", |
| 430 | help="fetch only, don't update working tree", |
| 431 | ) |
| 432 | p.add_option( |
| 433 | "-d", |
| 434 | "--detach", |
| 435 | dest="detach_head", |
| 436 | action="store_true", |
| 437 | help="detach projects back to manifest revision", |
| 438 | ) |
| 439 | p.add_option( |
| 440 | "-c", |
| 441 | "--current-branch", |
| 442 | dest="current_branch_only", |
| 443 | action="store_true", |
| 444 | help="fetch only current branch from server", |
| 445 | ) |
| 446 | p.add_option( |
| 447 | "--no-current-branch", |
| 448 | dest="current_branch_only", |
| 449 | action="store_false", |
| 450 | help="fetch all branches from server", |
| 451 | ) |
| 452 | p.add_option( |
| 453 | "-m", |
| 454 | "--manifest-name", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 455 | help="temporary manifest to use for this sync", |
| 456 | metavar="NAME.xml", |
| 457 | ) |
| 458 | p.add_option( |
| 459 | "--clone-bundle", |
| 460 | action="store_true", |
| 461 | help="enable use of /clone.bundle on HTTP/HTTPS", |
| 462 | ) |
| 463 | p.add_option( |
| 464 | "--no-clone-bundle", |
| 465 | dest="clone_bundle", |
| 466 | action="store_false", |
| 467 | help="disable use of /clone.bundle on HTTP/HTTPS", |
| 468 | ) |
| 469 | p.add_option( |
| 470 | "-u", |
| 471 | "--manifest-server-username", |
| 472 | action="store", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 473 | help="username to authenticate with the manifest server", |
| 474 | ) |
| 475 | p.add_option( |
| 476 | "-p", |
| 477 | "--manifest-server-password", |
| 478 | action="store", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 479 | help="password to authenticate with the manifest server", |
| 480 | ) |
| 481 | p.add_option( |
| 482 | "--fetch-submodules", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 483 | action="store_true", |
| 484 | help="fetch submodules from server", |
| 485 | ) |
| 486 | p.add_option( |
| 487 | "--use-superproject", |
| 488 | action="store_true", |
| 489 | help="use the manifest superproject to sync projects; implies -c", |
| 490 | ) |
| 491 | p.add_option( |
| 492 | "--no-use-superproject", |
| 493 | action="store_false", |
| 494 | dest="use_superproject", |
| 495 | help="disable use of manifest superprojects", |
| 496 | ) |
| 497 | p.add_option("--tags", action="store_true", help="fetch tags") |
| 498 | p.add_option( |
| 499 | "--no-tags", |
| 500 | dest="tags", |
| 501 | action="store_false", |
| 502 | help="don't fetch tags (default)", |
| 503 | ) |
| 504 | p.add_option( |
| 505 | "--optimized-fetch", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 506 | action="store_true", |
| 507 | help="only fetch projects fixed to sha1 if revision does not exist " |
| 508 | "locally", |
| 509 | ) |
| 510 | p.add_option( |
| 511 | "--retry-fetches", |
| 512 | default=0, |
| 513 | action="store", |
| 514 | type="int", |
| 515 | help="number of times to retry fetches on transient errors", |
| 516 | ) |
| 517 | p.add_option( |
| 518 | "--prune", |
| 519 | action="store_true", |
| 520 | help="delete refs that no longer exist on the remote (default)", |
| 521 | ) |
| 522 | p.add_option( |
| 523 | "--no-prune", |
| 524 | dest="prune", |
| 525 | action="store_false", |
| 526 | help="do not delete refs that no longer exist on the remote", |
| 527 | ) |
| 528 | p.add_option( |
| 529 | "--auto-gc", |
| 530 | action="store_true", |
| 531 | default=None, |
| 532 | help="run garbage collection on all synced projects", |
| 533 | ) |
| 534 | p.add_option( |
| 535 | "--no-auto-gc", |
| 536 | dest="auto_gc", |
| 537 | action="store_false", |
| 538 | help="do not run garbage collection on any projects (default)", |
| 539 | ) |
| 540 | if show_smart: |
| 541 | p.add_option( |
| 542 | "-s", |
| 543 | "--smart-sync", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 544 | action="store_true", |
| 545 | help="smart sync using manifest from the latest known good " |
| 546 | "build", |
| 547 | ) |
| 548 | p.add_option( |
| 549 | "-t", |
| 550 | "--smart-tag", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 551 | action="store", |
| 552 | help="smart sync using manifest from a known tag", |
| 553 | ) |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 554 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 555 | g = p.add_option_group("repo Version options") |
| 556 | g.add_option( |
| 557 | "--no-repo-verify", |
| 558 | dest="repo_verify", |
| 559 | default=True, |
| 560 | action="store_false", |
| 561 | help="do not verify repo source code", |
| 562 | ) |
| 563 | g.add_option( |
| 564 | "--repo-upgraded", |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 565 | action="store_true", |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 566 | help=optparse.SUPPRESS_HELP, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 567 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 568 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 569 | def _GetBranch(self, manifest_project): |
| 570 | """Returns the branch name for getting the approved smartsync manifest. |
LaMont Jones | a46047a | 2022-04-07 21:57:06 +0000 | [diff] [blame] | 571 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 572 | Args: |
| 573 | manifest_project: The manifestProject to query. |
| 574 | """ |
| 575 | b = manifest_project.GetBranch(manifest_project.CurrentBranch) |
| 576 | branch = b.merge |
| 577 | if branch.startswith(R_HEADS): |
| 578 | branch = branch[len(R_HEADS) :] |
| 579 | return branch |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 580 | |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 581 | @classmethod |
| 582 | def _GetCurrentBranchOnly(cls, opt, manifest): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 583 | """Returns whether current-branch or use-superproject options are |
| 584 | enabled. |
Daniel Andersson | d52ca42 | 2022-04-01 12:55:38 +0200 | [diff] [blame] | 585 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 586 | Args: |
| 587 | opt: Program options returned from optparse. See _Options(). |
| 588 | manifest: The manifest to use. |
LaMont Jones | a46047a | 2022-04-07 21:57:06 +0000 | [diff] [blame] | 589 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 590 | Returns: |
| 591 | True if a superproject is requested, otherwise the value of the |
| 592 | current_branch option (True, False or None). |
| 593 | """ |
| 594 | return ( |
| 595 | git_superproject.UseSuperproject(opt.use_superproject, manifest) |
| 596 | or opt.current_branch_only |
| 597 | ) |
Raman Tenneti | 2ae44d7 | 2021-03-23 15:12:27 -0700 | [diff] [blame] | 598 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 599 | def _UpdateProjectsRevisionId( |
| 600 | self, opt, args, superproject_logging_data, manifest |
| 601 | ): |
| 602 | """Update revisionId of projects with the commit from the superproject. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 603 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 604 | This function updates each project's revisionId with the commit hash |
| 605 | from the superproject. It writes the updated manifest into a file and |
| 606 | reloads the manifest from it. When appropriate, sub manifests are also |
| 607 | processed. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 608 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 609 | Args: |
| 610 | opt: Program options returned from optparse. See _Options(). |
| 611 | args: Arguments to pass to GetProjects. See the GetProjects |
| 612 | docstring for details. |
| 613 | superproject_logging_data: A dictionary of superproject data to log. |
| 614 | manifest: The manifest to use. |
| 615 | """ |
| 616 | have_superproject = manifest.superproject or any( |
| 617 | m.superproject for m in manifest.all_children |
| 618 | ) |
| 619 | if not have_superproject: |
| 620 | return |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 621 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 622 | if opt.local_only and manifest.superproject: |
| 623 | manifest_path = manifest.superproject.manifest_path |
| 624 | if manifest_path: |
| 625 | self._ReloadManifest(manifest_path, manifest) |
| 626 | return |
Raman Tenneti | ae86a46 | 2021-07-27 08:54:59 -0700 | [diff] [blame] | 627 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 628 | all_projects = self.GetProjects( |
| 629 | args, |
| 630 | missing_ok=True, |
| 631 | submodules_ok=opt.fetch_submodules, |
| 632 | manifest=manifest, |
| 633 | all_manifests=not opt.this_manifest_only, |
| 634 | ) |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 635 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 636 | per_manifest = collections.defaultdict(list) |
| 637 | if opt.this_manifest_only: |
| 638 | per_manifest[manifest.path_prefix] = all_projects |
| 639 | else: |
| 640 | for p in all_projects: |
| 641 | per_manifest[p.manifest.path_prefix].append(p) |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 642 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 643 | superproject_logging_data = {} |
| 644 | need_unload = False |
| 645 | for m in self.ManifestList(opt): |
| 646 | if m.path_prefix not in per_manifest: |
| 647 | continue |
| 648 | use_super = git_superproject.UseSuperproject( |
| 649 | opt.use_superproject, m |
| 650 | ) |
| 651 | if superproject_logging_data: |
| 652 | superproject_logging_data["multimanifest"] = True |
| 653 | superproject_logging_data.update( |
| 654 | superproject=use_super, |
| 655 | haslocalmanifests=bool(m.HasLocalManifests), |
| 656 | hassuperprojecttag=bool(m.superproject), |
| 657 | ) |
| 658 | if use_super and (m.IsMirror or m.IsArchive): |
| 659 | # Don't use superproject, because we have no working tree. |
| 660 | use_super = False |
| 661 | superproject_logging_data["superproject"] = False |
| 662 | superproject_logging_data["noworktree"] = True |
| 663 | if opt.use_superproject is not False: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 664 | logger.warning( |
| 665 | "%s: not using superproject because there is no " |
| 666 | "working tree.", |
| 667 | m.path_prefix, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 668 | ) |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 669 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 670 | if not use_super: |
| 671 | continue |
Tomasz Wasilczyk | 208f344 | 2024-01-05 12:23:10 -0800 | [diff] [blame] | 672 | m.superproject.SetQuiet(not opt.verbose) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 673 | print_messages = git_superproject.PrintMessages( |
| 674 | opt.use_superproject, m |
| 675 | ) |
| 676 | m.superproject.SetPrintMessages(print_messages) |
| 677 | update_result = m.superproject.UpdateProjectsRevisionId( |
| 678 | per_manifest[m.path_prefix], git_event_log=self.git_event_log |
| 679 | ) |
| 680 | manifest_path = update_result.manifest_path |
| 681 | superproject_logging_data["updatedrevisionid"] = bool(manifest_path) |
| 682 | if manifest_path: |
| 683 | m.SetManifestOverride(manifest_path) |
| 684 | need_unload = True |
| 685 | else: |
| 686 | if print_messages: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 687 | logger.warning( |
| 688 | "%s: warning: Update of revisionId from superproject " |
| 689 | "has failed, repo sync will not use superproject to " |
| 690 | "fetch the source. Please resync with the " |
| 691 | "--no-use-superproject option to avoid this repo " |
| 692 | "warning.", |
| 693 | m.path_prefix, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 694 | ) |
| 695 | if update_result.fatal and opt.use_superproject is not None: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 696 | raise SuperprojectError() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 697 | if need_unload: |
| 698 | m.outer_client.manifest.Unload() |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 699 | |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 700 | @classmethod |
| 701 | def _FetchProjectList(cls, opt, projects): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 702 | """Main function of the fetch worker. |
Mike Frysinger | b2fa30a | 2021-02-24 00:15:32 -0500 | [diff] [blame] | 703 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 704 | The projects we're given share the same underlying git object store, so |
| 705 | we have to fetch them in serial. |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 706 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 707 | Delegates most of the work to _FetchOne. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 708 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 709 | Args: |
| 710 | opt: Program options returned from optparse. See _Options(). |
| 711 | projects: Projects to fetch. |
| 712 | """ |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 713 | return [cls._FetchOne(opt, x) for x in projects] |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 714 | |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 715 | @classmethod |
| 716 | def _FetchOne(cls, opt, project_idx): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 717 | """Fetch git objects for a single project. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 718 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 719 | Args: |
| 720 | opt: Program options returned from optparse. See _Options(). |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 721 | project_idx: Project index for the project to fetch. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 722 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 723 | Returns: |
| 724 | Whether the fetch was successful. |
| 725 | """ |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 726 | project = cls.get_parallel_context()["projects"][project_idx] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 727 | start = time.time() |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 728 | k = f"{project.name} @ {project.relpath}" |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 729 | cls.get_parallel_context()["sync_dict"][k] = start |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 730 | success = False |
| 731 | remote_fetched = False |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 732 | errors = [] |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 733 | buf = TeeStringIO(sys.stdout if opt.verbose else None) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 734 | try: |
| 735 | sync_result = project.Sync_NetworkHalf( |
| 736 | quiet=opt.quiet, |
| 737 | verbose=opt.verbose, |
| 738 | output_redir=buf, |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 739 | current_branch_only=cls._GetCurrentBranchOnly( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 740 | opt, project.manifest |
| 741 | ), |
| 742 | force_sync=opt.force_sync, |
| 743 | clone_bundle=opt.clone_bundle, |
| 744 | tags=opt.tags, |
| 745 | archive=project.manifest.IsArchive, |
| 746 | optimized_fetch=opt.optimized_fetch, |
| 747 | retry_fetches=opt.retry_fetches, |
| 748 | prune=opt.prune, |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 749 | ssh_proxy=cls.get_parallel_context()["ssh_proxy"], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 750 | clone_filter=project.manifest.CloneFilter, |
| 751 | partial_clone_exclude=project.manifest.PartialCloneExclude, |
Jason Chang | 1783332 | 2023-05-23 13:06:55 -0700 | [diff] [blame] | 752 | clone_filter_for_depth=project.manifest.CloneFilterForDepth, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 753 | ) |
| 754 | success = sync_result.success |
| 755 | remote_fetched = sync_result.remote_fetched |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 756 | if sync_result.error: |
| 757 | errors.append(sync_result.error) |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 758 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 759 | output = buf.getvalue() |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 760 | if output and buf.io is None and not success: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 761 | print("\n" + output.rstrip()) |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 762 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 763 | if not success: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 764 | logger.error( |
| 765 | "error: Cannot fetch %s from %s", |
| 766 | project.name, |
| 767 | project.remote.url, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 768 | ) |
| 769 | except KeyboardInterrupt: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 770 | logger.error("Keyboard interrupt while processing %s", project.name) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 771 | except GitError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 772 | logger.error("error.GitError: Cannot fetch %s", e) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 773 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 774 | except Exception as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 775 | logger.error( |
| 776 | "error: Cannot fetch %s (%s: %s)", |
| 777 | project.name, |
| 778 | type(e).__name__, |
| 779 | e, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 780 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 781 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 782 | raise |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 783 | finally: |
| 784 | del cls.get_parallel_context()["sync_dict"][k] |
Mike Frysinger | 7b586f2 | 2021-02-23 18:38:39 -0500 | [diff] [blame] | 785 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 786 | finish = time.time() |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 787 | return _FetchOneResult( |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 788 | success, errors, project_idx, start, finish, remote_fetched |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 789 | ) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 790 | |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 791 | def _GetSyncProgressMessage(self): |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 792 | earliest_time = float("inf") |
| 793 | earliest_proj = None |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 794 | items = self.get_parallel_context()["sync_dict"].items() |
Gavin Mak | 945c006 | 2023-05-30 20:04:07 +0000 | [diff] [blame] | 795 | for project, t in items: |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 796 | if t < earliest_time: |
| 797 | earliest_time = t |
| 798 | earliest_proj = project |
| 799 | |
Josip Sokcevic | 71122f9 | 2023-05-26 02:44:37 +0000 | [diff] [blame] | 800 | if not earliest_proj: |
Gavin Mak | 945c006 | 2023-05-30 20:04:07 +0000 | [diff] [blame] | 801 | # This function is called when sync is still running but in some |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 802 | # cases (by chance), sync_dict can contain no entries. Return some |
Gavin Mak | 945c006 | 2023-05-30 20:04:07 +0000 | [diff] [blame] | 803 | # text to indicate that sync is still working. |
| 804 | return "..working.." |
Josip Sokcevic | 71122f9 | 2023-05-26 02:44:37 +0000 | [diff] [blame] | 805 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 806 | elapsed = time.time() - earliest_time |
Gavin Mak | 945c006 | 2023-05-30 20:04:07 +0000 | [diff] [blame] | 807 | jobs = jobs_str(len(items)) |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 808 | return f"{jobs} | {elapsed_str(elapsed)} {earliest_proj}" |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 809 | |
Kuang-che Wu | ab2d321 | 2024-11-06 13:03:42 +0800 | [diff] [blame] | 810 | @classmethod |
| 811 | def InitWorker(cls): |
| 812 | # Force connect to the manager server now. |
| 813 | # This is good because workers are initialized one by one. Without this, |
| 814 | # multiple workers may connect to the manager when handling the first |
| 815 | # job at the same time. Then the connection may fail if too many |
| 816 | # connections are pending and execeeded the socket listening backlog, |
| 817 | # especially on MacOS. |
| 818 | len(cls.get_parallel_context()["sync_dict"]) |
| 819 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 820 | def _Fetch(self, projects, opt, err_event, ssh_proxy, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 821 | ret = True |
Mike Frysinger | b2fa30a | 2021-02-24 00:15:32 -0500 | [diff] [blame] | 822 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 823 | fetched = set() |
| 824 | remote_fetched = set() |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 825 | pm = Progress( |
| 826 | "Fetching", |
| 827 | len(projects), |
| 828 | delay=False, |
| 829 | quiet=opt.quiet, |
| 830 | show_elapsed=True, |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 831 | elide=True, |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 832 | ) |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 833 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 834 | sync_event = _threading.Event() |
| 835 | |
| 836 | def _MonitorSyncLoop(): |
| 837 | while True: |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 838 | pm.update(inc=0, msg=self._GetSyncProgressMessage()) |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 839 | if sync_event.wait(timeout=1): |
| 840 | return |
| 841 | |
| 842 | sync_progress_thread = _threading.Thread(target=_MonitorSyncLoop) |
| 843 | sync_progress_thread.daemon = True |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 844 | |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 845 | def _ProcessResults(pool, pm, results_sets): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 846 | ret = True |
| 847 | for results in results_sets: |
| 848 | for result in results: |
| 849 | success = result.success |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 850 | project = projects[result.project_idx] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 851 | start = result.start |
| 852 | finish = result.finish |
| 853 | self._fetch_times.Set(project, finish - start) |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 854 | self._local_sync_state.SetFetchTime(project) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 855 | self.event_log.AddSync( |
| 856 | project, |
| 857 | event_log.TASK_SYNC_NETWORK, |
| 858 | start, |
| 859 | finish, |
| 860 | success, |
| 861 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 862 | if result.errors: |
| 863 | errors.extend(result.errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 864 | if result.remote_fetched: |
| 865 | remote_fetched.add(project) |
| 866 | # Check for any errors before running any more tasks. |
| 867 | # ...we'll let existing jobs finish, though. |
| 868 | if not success: |
| 869 | ret = False |
| 870 | else: |
| 871 | fetched.add(project.gitdir) |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 872 | pm.update() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 873 | if not ret and opt.fail_fast: |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 874 | if pool: |
| 875 | pool.close() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 876 | break |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 877 | return ret |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 878 | |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 879 | with self.ParallelContext(): |
| 880 | self.get_parallel_context()["projects"] = projects |
| 881 | self.get_parallel_context()[ |
| 882 | "sync_dict" |
| 883 | ] = multiprocessing.Manager().dict() |
Mike Frysinger | ebf04a4 | 2021-02-23 20:48:04 -0500 | [diff] [blame] | 884 | |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 885 | objdir_project_map = dict() |
| 886 | for index, project in enumerate(projects): |
| 887 | objdir_project_map.setdefault(project.objdir, []).append(index) |
| 888 | projects_list = list(objdir_project_map.values()) |
| 889 | |
Peter Kjellerstedt | 616e314 | 2024-11-20 21:10:29 +0100 | [diff] [blame] | 890 | jobs = max(1, min(opt.jobs_network, len(projects_list))) |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 891 | |
| 892 | # We pass the ssh proxy settings via the class. This allows |
| 893 | # multiprocessing to pickle it up when spawning children. We can't |
| 894 | # pass it as an argument to _FetchProjectList below as |
| 895 | # multiprocessing is unable to pickle those. |
| 896 | self.get_parallel_context()["ssh_proxy"] = ssh_proxy |
| 897 | |
| 898 | sync_progress_thread.start() |
Josip Sokcevic | 454fdaf | 2024-10-07 17:33:38 +0000 | [diff] [blame] | 899 | if not opt.quiet: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 900 | pm.update(inc=0, msg="warming up") |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 901 | try: |
| 902 | ret = self.ExecuteInParallel( |
| 903 | jobs, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 904 | functools.partial(self._FetchProjectList, opt), |
| 905 | projects_list, |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 906 | callback=_ProcessResults, |
| 907 | output=pm, |
| 908 | # Use chunksize=1 to avoid the chance that some workers are |
| 909 | # idle while other workers still have more than one job in |
| 910 | # their chunk queue. |
| 911 | chunksize=1, |
Kuang-che Wu | ab2d321 | 2024-11-06 13:03:42 +0800 | [diff] [blame] | 912 | initializer=self.InitWorker, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 913 | ) |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 914 | finally: |
| 915 | sync_event.set() |
| 916 | sync_progress_thread.join() |
LaMont Jones | fa8d939 | 2022-11-02 22:01:29 +0000 | [diff] [blame] | 917 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 918 | self._fetch_times.Save() |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 919 | self._local_sync_state.Save() |
LaMont Jones | 43549d8 | 2022-11-30 19:55:30 +0000 | [diff] [blame] | 920 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 921 | if not self.outer_client.manifest.IsArchive: |
| 922 | self._GCProjects(projects, opt, err_event) |
Mike Frysinger | 65af260 | 2021-04-08 22:47:44 -0400 | [diff] [blame] | 923 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 924 | return _FetchResult(ret, fetched) |
LaMont Jones | fa8d939 | 2022-11-02 22:01:29 +0000 | [diff] [blame] | 925 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 926 | def _FetchMain( |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 927 | self, opt, args, all_projects, err_event, ssh_proxy, manifest, errors |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 928 | ): |
| 929 | """The main network fetch loop. |
Mike Frysinger | 65af260 | 2021-04-08 22:47:44 -0400 | [diff] [blame] | 930 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 931 | Args: |
| 932 | opt: Program options returned from optparse. See _Options(). |
| 933 | args: Command line args used to filter out projects. |
| 934 | all_projects: List of all projects that should be fetched. |
| 935 | err_event: Whether an error was hit while processing. |
| 936 | ssh_proxy: SSH manager for clients & masters. |
| 937 | manifest: The manifest to use. |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 938 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 939 | Returns: |
| 940 | List of all projects that should be checked out. |
| 941 | """ |
| 942 | rp = manifest.repoProject |
LaMont Jones | 891e8f7 | 2022-09-08 20:17:58 +0000 | [diff] [blame] | 943 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 944 | to_fetch = [] |
| 945 | now = time.time() |
| 946 | if _ONE_DAY_S <= (now - rp.LastFetch): |
| 947 | to_fetch.append(rp) |
| 948 | to_fetch.extend(all_projects) |
| 949 | to_fetch.sort(key=self._fetch_times.Get, reverse=True) |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 950 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 951 | result = self._Fetch(to_fetch, opt, err_event, ssh_proxy, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 952 | success = result.success |
| 953 | fetched = result.projects |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 954 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 955 | if not success: |
| 956 | err_event.set() |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 957 | |
Fredrik de Groot | ebdf040 | 2024-10-22 14:14:59 +0200 | [diff] [blame] | 958 | # Call self update, unless requested not to |
| 959 | if os.environ.get("REPO_SKIP_SELF_UPDATE", "0") == "0": |
| 960 | _PostRepoFetch(rp, opt.repo_verify) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 961 | if opt.network_only: |
| 962 | # Bail out now; the rest touches the working tree. |
| 963 | if err_event.is_set(): |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 964 | e = SyncError( |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 965 | "error: Exited sync due to fetch errors.", |
| 966 | aggregate_errors=errors, |
| 967 | ) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 968 | |
| 969 | logger.error(e) |
| 970 | raise e |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 971 | return _FetchMainResult([]) |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 972 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 973 | # Iteratively fetch missing and/or nested unregistered submodules. |
| 974 | previously_missing_set = set() |
| 975 | while True: |
| 976 | self._ReloadManifest(None, manifest) |
| 977 | all_projects = self.GetProjects( |
| 978 | args, |
| 979 | missing_ok=True, |
| 980 | submodules_ok=opt.fetch_submodules, |
LaMont Jones | a46047a | 2022-04-07 21:57:06 +0000 | [diff] [blame] | 981 | manifest=manifest, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 982 | all_manifests=not opt.this_manifest_only, |
| 983 | ) |
| 984 | missing = [] |
| 985 | for project in all_projects: |
| 986 | if project.gitdir not in fetched: |
| 987 | missing.append(project) |
| 988 | if not missing: |
| 989 | break |
| 990 | # Stop us from non-stopped fetching actually-missing repos: If set |
| 991 | # of missing repos has not been changed from last fetch, we break. |
Jason R. Coombs | 0bcffd8 | 2023-10-20 23:29:42 +0545 | [diff] [blame] | 992 | missing_set = {p.name for p in missing} |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 993 | if previously_missing_set == missing_set: |
| 994 | break |
| 995 | previously_missing_set = missing_set |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 996 | result = self._Fetch(missing, opt, err_event, ssh_proxy, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 997 | success = result.success |
| 998 | new_fetched = result.projects |
| 999 | if not success: |
| 1000 | err_event.set() |
| 1001 | fetched.update(new_fetched) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 1002 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1003 | return _FetchMainResult(all_projects) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 1004 | |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1005 | @classmethod |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 1006 | def _CheckoutOne( |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1007 | cls, |
Jeroen Dhollander | c44ad09 | 2024-08-20 10:28:41 +0200 | [diff] [blame] | 1008 | detach_head, |
| 1009 | force_sync, |
| 1010 | force_checkout, |
| 1011 | force_rebase, |
| 1012 | verbose, |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1013 | project_idx, |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 1014 | ): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1015 | """Checkout work tree for one project |
jiajia tang | a590e64 | 2021-04-25 20:02:02 +0800 | [diff] [blame] | 1016 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1017 | Args: |
| 1018 | detach_head: Whether to leave a detached HEAD. |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 1019 | force_sync: Force checking out of .git directory (e.g. overwrite |
| 1020 | existing git directory that was previously linked to a different |
| 1021 | object directory). |
| 1022 | force_checkout: Force checking out of the repo content. |
Jeroen Dhollander | c44ad09 | 2024-08-20 10:28:41 +0200 | [diff] [blame] | 1023 | force_rebase: Force rebase. |
Tomasz Wasilczyk | 4c80921 | 2023-12-08 13:42:17 -0800 | [diff] [blame] | 1024 | verbose: Whether to show verbose messages. |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1025 | project_idx: Project index for the project to checkout. |
jiajia tang | a590e64 | 2021-04-25 20:02:02 +0800 | [diff] [blame] | 1026 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1027 | Returns: |
| 1028 | Whether the fetch was successful. |
| 1029 | """ |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1030 | project = cls.get_parallel_context()["projects"][project_idx] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1031 | start = time.time() |
| 1032 | syncbuf = SyncBuffer( |
| 1033 | project.manifest.manifestProject.config, detach_head=detach_head |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 1034 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1035 | success = False |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1036 | errors = [] |
David Pursehouse | 59b4174 | 2015-05-07 14:36:09 +0900 | [diff] [blame] | 1037 | try: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1038 | project.Sync_LocalHalf( |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 1039 | syncbuf, |
| 1040 | force_sync=force_sync, |
| 1041 | force_checkout=force_checkout, |
Jeroen Dhollander | c44ad09 | 2024-08-20 10:28:41 +0200 | [diff] [blame] | 1042 | force_rebase=force_rebase, |
Josip Sokcevic | edadb25 | 2024-02-29 09:48:37 -0800 | [diff] [blame] | 1043 | errors=errors, |
| 1044 | verbose=verbose, |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1045 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1046 | success = syncbuf.Finish() |
Josip Sokcevic | d93fe60 | 2025-01-08 18:31:46 +0000 | [diff] [blame] | 1047 | except KeyboardInterrupt: |
| 1048 | logger.error("Keyboard interrupt while processing %s", project.name) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1049 | except GitError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1050 | logger.error( |
| 1051 | "error.GitError: Cannot checkout %s: %s", project.name, e |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1052 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1053 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1054 | except Exception as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1055 | logger.error( |
| 1056 | "error: Cannot checkout %s: %s: %s", |
| 1057 | project.name, |
| 1058 | type(e).__name__, |
| 1059 | e, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1060 | ) |
| 1061 | raise |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 1062 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1063 | if not success: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1064 | logger.error("error: Cannot checkout %s", project.name) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1065 | finish = time.time() |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1066 | return _CheckoutOneResult(success, errors, project_idx, start, finish) |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 1067 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1068 | def _Checkout(self, all_projects, opt, err_results, checkout_errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1069 | """Checkout projects listed in all_projects |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1070 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1071 | Args: |
| 1072 | all_projects: List of all projects that should be checked out. |
| 1073 | opt: Program options returned from optparse. See _Options(). |
| 1074 | err_results: A list of strings, paths to git repos where checkout |
| 1075 | failed. |
| 1076 | """ |
| 1077 | # Only checkout projects with worktrees. |
| 1078 | all_projects = [x for x in all_projects if x.worktree] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1079 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1080 | def _ProcessResults(pool, pm, results): |
| 1081 | ret = True |
| 1082 | for result in results: |
| 1083 | success = result.success |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1084 | project = self.get_parallel_context()["projects"][ |
| 1085 | result.project_idx |
| 1086 | ] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1087 | start = result.start |
| 1088 | finish = result.finish |
| 1089 | self.event_log.AddSync( |
| 1090 | project, event_log.TASK_SYNC_LOCAL, start, finish, success |
| 1091 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1092 | |
| 1093 | if result.errors: |
| 1094 | checkout_errors.extend(result.errors) |
| 1095 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1096 | # Check for any errors before running any more tasks. |
| 1097 | # ...we'll let existing jobs finish, though. |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 1098 | if success: |
| 1099 | self._local_sync_state.SetCheckoutTime(project) |
| 1100 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1101 | ret = False |
| 1102 | err_results.append( |
| 1103 | project.RelPath(local=opt.this_manifest_only) |
| 1104 | ) |
| 1105 | if opt.fail_fast: |
| 1106 | if pool: |
| 1107 | pool.close() |
| 1108 | return ret |
| 1109 | pm.update(msg=project.name) |
| 1110 | return ret |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 1111 | |
Josip Sokcevic | 5554572 | 2024-02-22 16:38:00 -0800 | [diff] [blame] | 1112 | for projects in _SafeCheckoutOrder(all_projects): |
Kuang-che Wu | 39ffd99 | 2024-10-18 23:32:08 +0800 | [diff] [blame] | 1113 | with self.ParallelContext(): |
| 1114 | self.get_parallel_context()["projects"] = projects |
| 1115 | proc_res = self.ExecuteInParallel( |
| 1116 | opt.jobs_checkout, |
| 1117 | functools.partial( |
| 1118 | self._CheckoutOne, |
| 1119 | opt.detach_head, |
| 1120 | opt.force_sync, |
| 1121 | opt.force_checkout, |
| 1122 | opt.rebase, |
| 1123 | opt.verbose, |
| 1124 | ), |
| 1125 | range(len(projects)), |
| 1126 | callback=_ProcessResults, |
| 1127 | output=Progress( |
| 1128 | "Checking out", len(all_projects), quiet=opt.quiet |
| 1129 | ), |
| 1130 | # Use chunksize=1 to avoid the chance that some workers are |
| 1131 | # idle while other workers still have more than one job in |
| 1132 | # their chunk queue. |
| 1133 | chunksize=1, |
| 1134 | ) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1135 | |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 1136 | self._local_sync_state.Save() |
| 1137 | return proc_res and not err_results |
| 1138 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1139 | @staticmethod |
| 1140 | def _GetPreciousObjectsState(project: Project, opt): |
| 1141 | """Get the preciousObjects state for the project. |
Mike Frysinger | 355f439 | 2022-07-20 17:15:29 -0400 | [diff] [blame] | 1142 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1143 | Args: |
| 1144 | project (Project): the project to examine, and possibly correct. |
| 1145 | opt (optparse.Values): options given to sync. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 1146 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1147 | Returns: |
| 1148 | Expected state of extensions.preciousObjects: |
| 1149 | False: Should be disabled. (not present) |
| 1150 | True: Should be enabled. |
| 1151 | """ |
| 1152 | if project.use_git_worktrees: |
| 1153 | return False |
| 1154 | projects = project.manifest.GetProjectsWithName( |
| 1155 | project.name, all_manifests=True |
| 1156 | ) |
| 1157 | if len(projects) == 1: |
| 1158 | return False |
| 1159 | if len(projects) > 1: |
| 1160 | # Objects are potentially shared with another project. |
| 1161 | # See the logic in Project.Sync_NetworkHalf regarding UseAlternates. |
| 1162 | # - When False, shared projects share (via symlink) |
| 1163 | # .repo/project-objects/{PROJECT_NAME}.git as the one-and-only |
| 1164 | # objects directory. All objects are precious, since there is no |
| 1165 | # project with a complete set of refs. |
| 1166 | # - When True, shared projects share (via info/alternates) |
| 1167 | # .repo/project-objects/{PROJECT_NAME}.git as an alternate object |
| 1168 | # store, which is written only on the first clone of the project, |
| 1169 | # and is not written subsequently. (When Sync_NetworkHalf sees |
| 1170 | # that it exists, it makes sure that the alternates file points |
| 1171 | # there, and uses a project-local .git/objects directory for all |
| 1172 | # syncs going forward. |
| 1173 | # We do not support switching between the options. The environment |
| 1174 | # variable is present for testing and migration only. |
| 1175 | return not project.UseAlternates |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1176 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1177 | return False |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 1178 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1179 | def _SetPreciousObjectsState(self, project: Project, opt): |
| 1180 | """Correct the preciousObjects state for the project. |
| 1181 | |
| 1182 | Args: |
| 1183 | project: the project to examine, and possibly correct. |
| 1184 | opt: options given to sync. |
| 1185 | """ |
| 1186 | expected = self._GetPreciousObjectsState(project, opt) |
| 1187 | actual = ( |
| 1188 | project.config.GetBoolean("extensions.preciousObjects") or False |
| 1189 | ) |
| 1190 | relpath = project.RelPath(local=opt.this_manifest_only) |
| 1191 | |
| 1192 | if expected != actual: |
| 1193 | # If this is unexpected, log it and repair. |
| 1194 | Trace( |
| 1195 | f"{relpath} expected preciousObjects={expected}, got {actual}" |
| 1196 | ) |
| 1197 | if expected: |
| 1198 | if not opt.quiet: |
Aravind Vasudevan | 83c66ec | 2023-09-28 19:06:59 +0000 | [diff] [blame] | 1199 | print( |
| 1200 | "\r%s: Shared project %s found, disabling pruning." |
| 1201 | % (relpath, project.name) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1202 | ) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1203 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1204 | if git_require((2, 7, 0)): |
| 1205 | project.EnableRepositoryExtension("preciousObjects") |
| 1206 | else: |
| 1207 | # This isn't perfect, but it's the best we can do with old |
| 1208 | # git. |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1209 | logger.warning( |
| 1210 | "%s: WARNING: shared projects are unreliable when " |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1211 | "using old versions of git; please upgrade to " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1212 | "git-2.7.0+.", |
| 1213 | relpath, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1214 | ) |
| 1215 | project.config.SetString("gc.pruneExpire", "never") |
| 1216 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1217 | project.config.SetString("extensions.preciousObjects", None) |
| 1218 | project.config.SetString("gc.pruneExpire", None) |
| 1219 | |
| 1220 | def _GCProjects(self, projects, opt, err_event): |
| 1221 | """Perform garbage collection. |
| 1222 | |
| 1223 | If We are skipping garbage collection (opt.auto_gc not set), we still |
| 1224 | want to potentially mark objects precious, so that `git gc` does not |
| 1225 | discard shared objects. |
| 1226 | """ |
| 1227 | if not opt.auto_gc: |
| 1228 | # Just repair preciousObjects state, and return. |
| 1229 | for project in projects: |
| 1230 | self._SetPreciousObjectsState(project, opt) |
| 1231 | return |
| 1232 | |
| 1233 | pm = Progress( |
| 1234 | "Garbage collecting", len(projects), delay=False, quiet=opt.quiet |
| 1235 | ) |
| 1236 | pm.update(inc=0, msg="prescan") |
| 1237 | |
| 1238 | tidy_dirs = {} |
| 1239 | for project in projects: |
| 1240 | self._SetPreciousObjectsState(project, opt) |
| 1241 | |
| 1242 | project.config.SetString("gc.autoDetach", "false") |
| 1243 | # Only call git gc once per objdir, but call pack-refs for the |
| 1244 | # remainder. |
| 1245 | if project.objdir not in tidy_dirs: |
| 1246 | tidy_dirs[project.objdir] = ( |
| 1247 | True, # Run a full gc. |
| 1248 | project.bare_git, |
| 1249 | ) |
| 1250 | elif project.gitdir not in tidy_dirs: |
| 1251 | tidy_dirs[project.gitdir] = ( |
| 1252 | False, # Do not run a full gc; just run pack-refs. |
| 1253 | project.bare_git, |
| 1254 | ) |
| 1255 | |
| 1256 | jobs = opt.jobs |
| 1257 | |
| 1258 | if jobs < 2: |
| 1259 | for run_gc, bare_git in tidy_dirs.values(): |
| 1260 | pm.update(msg=bare_git._project.name) |
| 1261 | |
| 1262 | if run_gc: |
| 1263 | bare_git.gc("--auto") |
| 1264 | else: |
| 1265 | bare_git.pack_refs() |
| 1266 | pm.end() |
| 1267 | return |
| 1268 | |
| 1269 | cpu_count = os.cpu_count() |
| 1270 | config = {"pack.threads": cpu_count // jobs if cpu_count > jobs else 1} |
| 1271 | |
| 1272 | threads = set() |
| 1273 | sem = _threading.Semaphore(jobs) |
| 1274 | |
| 1275 | def tidy_up(run_gc, bare_git): |
| 1276 | pm.start(bare_git._project.name) |
| 1277 | try: |
| 1278 | try: |
| 1279 | if run_gc: |
| 1280 | bare_git.gc("--auto", config=config) |
| 1281 | else: |
| 1282 | bare_git.pack_refs(config=config) |
| 1283 | except GitError: |
| 1284 | err_event.set() |
| 1285 | except Exception: |
| 1286 | err_event.set() |
| 1287 | raise |
| 1288 | finally: |
| 1289 | pm.finish(bare_git._project.name) |
| 1290 | sem.release() |
| 1291 | |
| 1292 | for run_gc, bare_git in tidy_dirs.values(): |
| 1293 | if err_event.is_set() and opt.fail_fast: |
| 1294 | break |
| 1295 | sem.acquire() |
| 1296 | t = _threading.Thread( |
| 1297 | target=tidy_up, |
| 1298 | args=( |
| 1299 | run_gc, |
| 1300 | bare_git, |
| 1301 | ), |
| 1302 | ) |
| 1303 | t.daemon = True |
| 1304 | threads.add(t) |
| 1305 | t.start() |
| 1306 | |
| 1307 | for t in threads: |
| 1308 | t.join() |
| 1309 | pm.end() |
| 1310 | |
| 1311 | def _ReloadManifest(self, manifest_name, manifest): |
| 1312 | """Reload the manfiest from the file specified by the |manifest_name|. |
| 1313 | |
| 1314 | It unloads the manifest if |manifest_name| is None. |
| 1315 | |
| 1316 | Args: |
| 1317 | manifest_name: Manifest file to be reloaded. |
| 1318 | manifest: The manifest to use. |
| 1319 | """ |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 1320 | if manifest_name: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1321 | # Override calls Unload already. |
| 1322 | manifest.Override(manifest_name) |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 1323 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1324 | manifest.Unload() |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1325 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1326 | def UpdateProjectList(self, opt, manifest): |
| 1327 | """Update the cached projects list for |manifest| |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 1328 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1329 | In a multi-manifest checkout, each manifest has its own project.list. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1330 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1331 | Args: |
| 1332 | opt: Program options returned from optparse. See _Options(). |
| 1333 | manifest: The manifest to use. |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 1334 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1335 | Returns: |
| 1336 | 0: success |
| 1337 | 1: failure |
| 1338 | """ |
| 1339 | new_project_paths = [] |
| 1340 | for project in self.GetProjects( |
| 1341 | None, missing_ok=True, manifest=manifest, all_manifests=False |
| 1342 | ): |
| 1343 | if project.relpath: |
| 1344 | new_project_paths.append(project.relpath) |
| 1345 | file_name = "project.list" |
| 1346 | file_path = os.path.join(manifest.subdir, file_name) |
| 1347 | old_project_paths = [] |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 1348 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1349 | if os.path.exists(file_path): |
Jason R. Coombs | 034950b | 2023-10-20 23:32:02 +0545 | [diff] [blame] | 1350 | with open(file_path) as fd: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1351 | old_project_paths = fd.read().split("\n") |
| 1352 | # In reversed order, so subfolders are deleted before parent folder. |
| 1353 | for path in sorted(old_project_paths, reverse=True): |
| 1354 | if not path: |
| 1355 | continue |
| 1356 | if path not in new_project_paths: |
| 1357 | # If the path has already been deleted, we don't need to do |
| 1358 | # it. |
| 1359 | gitdir = os.path.join(manifest.topdir, path, ".git") |
| 1360 | if os.path.exists(gitdir): |
| 1361 | project = Project( |
| 1362 | manifest=manifest, |
| 1363 | name=path, |
| 1364 | remote=RemoteSpec("origin"), |
| 1365 | gitdir=gitdir, |
| 1366 | objdir=gitdir, |
| 1367 | use_git_worktrees=os.path.isfile(gitdir), |
| 1368 | worktree=os.path.join(manifest.topdir, path), |
| 1369 | relpath=path, |
| 1370 | revisionExpr="HEAD", |
| 1371 | revisionId=None, |
| 1372 | groups=None, |
| 1373 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1374 | project.DeleteWorktree( |
Tomasz Wasilczyk | 4c80921 | 2023-12-08 13:42:17 -0800 | [diff] [blame] | 1375 | verbose=opt.verbose, force=opt.force_remove_dirty |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1376 | ) |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 1377 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1378 | new_project_paths.sort() |
| 1379 | with open(file_path, "w") as fd: |
| 1380 | fd.write("\n".join(new_project_paths)) |
| 1381 | fd.write("\n") |
| 1382 | return 0 |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1383 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1384 | def UpdateCopyLinkfileList(self, manifest): |
| 1385 | """Save all dests of copyfile and linkfile, and update them if needed. |
Shawn O. Pearce | cd1d7ff | 2009-06-04 16:15:53 -0700 | [diff] [blame] | 1386 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1387 | Returns: |
| 1388 | Whether update was successful. |
| 1389 | """ |
| 1390 | new_paths = {} |
| 1391 | new_linkfile_paths = [] |
| 1392 | new_copyfile_paths = [] |
| 1393 | for project in self.GetProjects( |
| 1394 | None, missing_ok=True, manifest=manifest, all_manifests=False |
| 1395 | ): |
| 1396 | new_linkfile_paths.extend(x.dest for x in project.linkfiles) |
| 1397 | new_copyfile_paths.extend(x.dest for x in project.copyfiles) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 1398 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1399 | new_paths = { |
| 1400 | "linkfile": new_linkfile_paths, |
| 1401 | "copyfile": new_copyfile_paths, |
| 1402 | } |
jiajia tang | a590e64 | 2021-04-25 20:02:02 +0800 | [diff] [blame] | 1403 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1404 | copylinkfile_name = "copy-link-files.json" |
| 1405 | copylinkfile_path = os.path.join(manifest.subdir, copylinkfile_name) |
| 1406 | old_copylinkfile_paths = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1407 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1408 | if os.path.exists(copylinkfile_path): |
| 1409 | with open(copylinkfile_path, "rb") as fp: |
| 1410 | try: |
| 1411 | old_copylinkfile_paths = json.load(fp) |
| 1412 | except Exception: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1413 | logger.error( |
| 1414 | "error: %s is not a json formatted file.", |
| 1415 | copylinkfile_path, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1416 | ) |
| 1417 | platform_utils.remove(copylinkfile_path) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1418 | raise |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1419 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1420 | need_remove_files = [] |
| 1421 | need_remove_files.extend( |
| 1422 | set(old_copylinkfile_paths.get("linkfile", [])) |
| 1423 | - set(new_linkfile_paths) |
| 1424 | ) |
| 1425 | need_remove_files.extend( |
| 1426 | set(old_copylinkfile_paths.get("copyfile", [])) |
| 1427 | - set(new_copyfile_paths) |
| 1428 | ) |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 1429 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1430 | for need_remove_file in need_remove_files: |
| 1431 | # Try to remove the updated copyfile or linkfile. |
| 1432 | # So, if the file is not exist, nothing need to do. |
Josip Sokcevic | 9500aca | 2024-12-13 18:24:20 +0000 | [diff] [blame] | 1433 | platform_utils.remove( |
| 1434 | os.path.join(self.client.topdir, need_remove_file), |
| 1435 | missing_ok=True, |
| 1436 | ) |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 1437 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1438 | # Create copy-link-files.json, save dest path of "copyfile" and |
| 1439 | # "linkfile". |
| 1440 | with open(copylinkfile_path, "w", encoding="utf-8") as fp: |
| 1441 | json.dump(new_paths, fp) |
| 1442 | return True |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 1443 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1444 | def _SmartSyncSetup(self, opt, smart_sync_manifest_path, manifest): |
| 1445 | if not manifest.manifest_server: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1446 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1447 | "error: cannot smart sync: no manifest server defined in " |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1448 | "manifest" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1449 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1450 | |
| 1451 | manifest_server = manifest.manifest_server |
| 1452 | if not opt.quiet: |
Aravind Vasudevan | 83c66ec | 2023-09-28 19:06:59 +0000 | [diff] [blame] | 1453 | print("Using manifest server %s" % manifest_server) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1454 | |
| 1455 | if "@" not in manifest_server: |
| 1456 | username = None |
| 1457 | password = None |
| 1458 | if opt.manifest_server_username and opt.manifest_server_password: |
| 1459 | username = opt.manifest_server_username |
| 1460 | password = opt.manifest_server_password |
| 1461 | else: |
| 1462 | try: |
| 1463 | info = netrc.netrc() |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 1464 | except OSError: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1465 | # .netrc file does not exist or could not be opened. |
| 1466 | pass |
| 1467 | else: |
| 1468 | try: |
| 1469 | parse_result = urllib.parse.urlparse(manifest_server) |
| 1470 | if parse_result.hostname: |
| 1471 | auth = info.authenticators(parse_result.hostname) |
| 1472 | if auth: |
| 1473 | username, _account, password = auth |
| 1474 | else: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1475 | logger.error( |
| 1476 | "No credentials found for %s in .netrc", |
| 1477 | parse_result.hostname, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1478 | ) |
| 1479 | except netrc.NetrcParseError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1480 | logger.error("Error parsing .netrc file: %s", e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1481 | |
| 1482 | if username and password: |
| 1483 | manifest_server = manifest_server.replace( |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 1484 | "://", f"://{username}:{password}@", 1 |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1485 | ) |
| 1486 | |
| 1487 | transport = PersistentTransport(manifest_server) |
| 1488 | if manifest_server.startswith("persistent-"): |
| 1489 | manifest_server = manifest_server[len("persistent-") :] |
| 1490 | |
Mike Frysinger | dfdf577 | 2025-01-30 19:11:36 -0500 | [diff] [blame] | 1491 | # Changes in behavior should update docs/smart-sync.md accordingly. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1492 | try: |
| 1493 | server = xmlrpc.client.Server(manifest_server, transport=transport) |
| 1494 | if opt.smart_sync: |
| 1495 | branch = self._GetBranch(manifest.manifestProject) |
| 1496 | |
| 1497 | if "SYNC_TARGET" in os.environ: |
| 1498 | target = os.environ["SYNC_TARGET"] |
| 1499 | [success, manifest_str] = server.GetApprovedManifest( |
| 1500 | branch, target |
| 1501 | ) |
| 1502 | elif ( |
| 1503 | "TARGET_PRODUCT" in os.environ |
| 1504 | and "TARGET_BUILD_VARIANT" in os.environ |
Navil | 1e19f7d | 2024-09-11 16:49:49 +0000 | [diff] [blame] | 1505 | and "TARGET_RELEASE" in os.environ |
| 1506 | ): |
| 1507 | target = "%s-%s-%s" % ( |
| 1508 | os.environ["TARGET_PRODUCT"], |
| 1509 | os.environ["TARGET_RELEASE"], |
| 1510 | os.environ["TARGET_BUILD_VARIANT"], |
| 1511 | ) |
| 1512 | [success, manifest_str] = server.GetApprovedManifest( |
| 1513 | branch, target |
| 1514 | ) |
| 1515 | elif ( |
| 1516 | "TARGET_PRODUCT" in os.environ |
| 1517 | and "TARGET_BUILD_VARIANT" in os.environ |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1518 | ): |
| 1519 | target = "%s-%s" % ( |
| 1520 | os.environ["TARGET_PRODUCT"], |
| 1521 | os.environ["TARGET_BUILD_VARIANT"], |
| 1522 | ) |
| 1523 | [success, manifest_str] = server.GetApprovedManifest( |
| 1524 | branch, target |
| 1525 | ) |
| 1526 | else: |
| 1527 | [success, manifest_str] = server.GetApprovedManifest(branch) |
| 1528 | else: |
| 1529 | assert opt.smart_tag |
| 1530 | [success, manifest_str] = server.GetManifest(opt.smart_tag) |
| 1531 | |
| 1532 | if success: |
| 1533 | manifest_name = os.path.basename(smart_sync_manifest_path) |
| 1534 | try: |
| 1535 | with open(smart_sync_manifest_path, "w") as f: |
| 1536 | f.write(manifest_str) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 1537 | except OSError as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1538 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1539 | "error: cannot write manifest to %s:\n%s" |
| 1540 | % (smart_sync_manifest_path, e), |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1541 | aggregate_errors=[e], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1542 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1543 | self._ReloadManifest(manifest_name, manifest) |
| 1544 | else: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1545 | raise SmartSyncError( |
| 1546 | "error: manifest server RPC call failed: %s" % manifest_str |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1547 | ) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 1548 | except (OSError, xmlrpc.client.Fault) as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1549 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1550 | "error: cannot connect to manifest server %s:\n%s" |
| 1551 | % (manifest.manifest_server, e), |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1552 | aggregate_errors=[e], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1553 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1554 | except xmlrpc.client.ProtocolError as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1555 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1556 | "error: cannot connect to manifest server %s:\n%d %s" |
| 1557 | % (manifest.manifest_server, e.errcode, e.errmsg), |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1558 | aggregate_errors=[e], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1559 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1560 | |
| 1561 | return manifest_name |
| 1562 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1563 | def _UpdateAllManifestProjects(self, opt, mp, manifest_name, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1564 | """Fetch & update the local manifest project. |
| 1565 | |
| 1566 | After syncing the manifest project, if the manifest has any sub |
| 1567 | manifests, those are recursively processed. |
| 1568 | |
| 1569 | Args: |
| 1570 | opt: Program options returned from optparse. See _Options(). |
| 1571 | mp: the manifestProject to query. |
| 1572 | manifest_name: Manifest file to be reloaded. |
| 1573 | """ |
| 1574 | if not mp.standalone_manifest_url: |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1575 | self._UpdateManifestProject(opt, mp, manifest_name, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1576 | |
| 1577 | if mp.manifest.submanifests: |
| 1578 | for submanifest in mp.manifest.submanifests.values(): |
| 1579 | child = submanifest.repo_client.manifest |
| 1580 | child.manifestProject.SyncWithPossibleInit( |
| 1581 | submanifest, |
| 1582 | current_branch_only=self._GetCurrentBranchOnly(opt, child), |
| 1583 | verbose=opt.verbose, |
| 1584 | tags=opt.tags, |
| 1585 | git_event_log=self.git_event_log, |
| 1586 | ) |
| 1587 | self._UpdateAllManifestProjects( |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1588 | opt, child.manifestProject, None, errors |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1589 | ) |
| 1590 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1591 | def _UpdateManifestProject(self, opt, mp, manifest_name, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1592 | """Fetch & update the local manifest project. |
| 1593 | |
| 1594 | Args: |
| 1595 | opt: Program options returned from optparse. See _Options(). |
| 1596 | mp: the manifestProject to query. |
| 1597 | manifest_name: Manifest file to be reloaded. |
| 1598 | """ |
| 1599 | if not opt.local_only: |
| 1600 | start = time.time() |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1601 | buf = TeeStringIO(sys.stdout) |
| 1602 | try: |
| 1603 | result = mp.Sync_NetworkHalf( |
Tomasz Wasilczyk | 208f344 | 2024-01-05 12:23:10 -0800 | [diff] [blame] | 1604 | quiet=not opt.verbose, |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1605 | output_redir=buf, |
| 1606 | verbose=opt.verbose, |
| 1607 | current_branch_only=self._GetCurrentBranchOnly( |
| 1608 | opt, mp.manifest |
| 1609 | ), |
| 1610 | force_sync=opt.force_sync, |
| 1611 | tags=opt.tags, |
| 1612 | optimized_fetch=opt.optimized_fetch, |
| 1613 | retry_fetches=opt.retry_fetches, |
| 1614 | submodules=mp.manifest.HasSubmodules, |
| 1615 | clone_filter=mp.manifest.CloneFilter, |
| 1616 | partial_clone_exclude=mp.manifest.PartialCloneExclude, |
| 1617 | clone_filter_for_depth=mp.manifest.CloneFilterForDepth, |
| 1618 | ) |
| 1619 | if result.error: |
| 1620 | errors.append(result.error) |
| 1621 | except KeyboardInterrupt: |
| 1622 | errors.append( |
| 1623 | ManifestInterruptError(buf.getvalue(), project=mp.name) |
| 1624 | ) |
| 1625 | raise |
| 1626 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1627 | finish = time.time() |
| 1628 | self.event_log.AddSync( |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1629 | mp, event_log.TASK_SYNC_NETWORK, start, finish, result.success |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1630 | ) |
| 1631 | |
| 1632 | if mp.HasChanges: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1633 | errors = [] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1634 | syncbuf = SyncBuffer(mp.config) |
| 1635 | start = time.time() |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1636 | mp.Sync_LocalHalf( |
Tomasz Wasilczyk | 4c80921 | 2023-12-08 13:42:17 -0800 | [diff] [blame] | 1637 | syncbuf, |
| 1638 | submodules=mp.manifest.HasSubmodules, |
| 1639 | errors=errors, |
| 1640 | verbose=opt.verbose, |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1641 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1642 | clean = syncbuf.Finish() |
| 1643 | self.event_log.AddSync( |
| 1644 | mp, event_log.TASK_SYNC_LOCAL, start, time.time(), clean |
| 1645 | ) |
| 1646 | if not clean: |
Yiwei Zhang | d379e77 | 2023-12-20 20:39:59 +0000 | [diff] [blame] | 1647 | raise UpdateManifestError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1648 | self._ReloadManifest(manifest_name, mp.manifest) |
| 1649 | |
| 1650 | def ValidateOptions(self, opt, args): |
| 1651 | if opt.force_broken: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1652 | logger.warning( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1653 | "warning: -f/--force-broken is now the default behavior, and " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1654 | "the options are deprecated" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1655 | ) |
| 1656 | if opt.network_only and opt.detach_head: |
| 1657 | self.OptionParser.error("cannot combine -n and -d") |
| 1658 | if opt.network_only and opt.local_only: |
| 1659 | self.OptionParser.error("cannot combine -n and -l") |
| 1660 | if opt.manifest_name and opt.smart_sync: |
| 1661 | self.OptionParser.error("cannot combine -m and -s") |
| 1662 | if opt.manifest_name and opt.smart_tag: |
| 1663 | self.OptionParser.error("cannot combine -m and -t") |
| 1664 | if opt.manifest_server_username or opt.manifest_server_password: |
| 1665 | if not (opt.smart_sync or opt.smart_tag): |
| 1666 | self.OptionParser.error( |
| 1667 | "-u and -p may only be combined with -s or -t" |
| 1668 | ) |
| 1669 | if None in [ |
| 1670 | opt.manifest_server_username, |
| 1671 | opt.manifest_server_password, |
| 1672 | ]: |
| 1673 | self.OptionParser.error("both -u and -p must be given") |
| 1674 | |
| 1675 | if opt.prune is None: |
| 1676 | opt.prune = True |
| 1677 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1678 | def _ValidateOptionsWithManifest(self, opt, mp): |
| 1679 | """Like ValidateOptions, but after we've updated the manifest. |
| 1680 | |
| 1681 | Needed to handle sync-xxx option defaults in the manifest. |
| 1682 | |
| 1683 | Args: |
| 1684 | opt: The options to process. |
| 1685 | mp: The manifest project to pull defaults from. |
| 1686 | """ |
| 1687 | if not opt.jobs: |
| 1688 | # If the user hasn't made a choice, use the manifest value. |
| 1689 | opt.jobs = mp.manifest.default.sync_j |
| 1690 | if opt.jobs: |
| 1691 | # If --jobs has a non-default value, propagate it as the default for |
| 1692 | # --jobs-xxx flags too. |
| 1693 | if not opt.jobs_network: |
| 1694 | opt.jobs_network = opt.jobs |
| 1695 | if not opt.jobs_checkout: |
| 1696 | opt.jobs_checkout = opt.jobs |
| 1697 | else: |
| 1698 | # Neither user nor manifest have made a choice, so setup defaults. |
| 1699 | if not opt.jobs_network: |
| 1700 | opt.jobs_network = 1 |
| 1701 | if not opt.jobs_checkout: |
| 1702 | opt.jobs_checkout = DEFAULT_LOCAL_JOBS |
| 1703 | opt.jobs = os.cpu_count() |
| 1704 | |
| 1705 | # Try to stay under user rlimit settings. |
| 1706 | # |
| 1707 | # Since each worker requires at 3 file descriptors to run `git fetch`, |
| 1708 | # use that to scale down the number of jobs. Unfortunately there isn't |
| 1709 | # an easy way to determine this reliably as systems change, but it was |
| 1710 | # last measured by hand in 2011. |
| 1711 | soft_limit, _ = _rlimit_nofile() |
| 1712 | jobs_soft_limit = max(1, (soft_limit - 5) // 3) |
| 1713 | opt.jobs = min(opt.jobs, jobs_soft_limit) |
| 1714 | opt.jobs_network = min(opt.jobs_network, jobs_soft_limit) |
| 1715 | opt.jobs_checkout = min(opt.jobs_checkout, jobs_soft_limit) |
| 1716 | |
Gavin Mak | daebd6c | 2025-04-09 13:59:27 -0700 | [diff] [blame] | 1717 | # Warn once if effective job counts seem excessively high. |
| 1718 | # Prioritize --jobs, then --jobs-network, then --jobs-checkout. |
| 1719 | job_options_to_check = ( |
| 1720 | ("--jobs", opt.jobs), |
| 1721 | ("--jobs-network", opt.jobs_network), |
| 1722 | ("--jobs-checkout", opt.jobs_checkout), |
| 1723 | ) |
| 1724 | for name, value in job_options_to_check: |
| 1725 | if value > self._JOBS_WARN_THRESHOLD: |
| 1726 | logger.warning( |
| 1727 | "High job count (%d > %d) specified for %s; this may " |
| 1728 | "lead to excessive resource usage or diminishing returns.", |
| 1729 | value, |
| 1730 | self._JOBS_WARN_THRESHOLD, |
| 1731 | name, |
| 1732 | ) |
| 1733 | break |
| 1734 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1735 | def Execute(self, opt, args): |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1736 | errors = [] |
| 1737 | try: |
| 1738 | self._ExecuteHelper(opt, args, errors) |
Jason Chang | 26fa318 | 2024-02-05 15:15:20 -0800 | [diff] [blame] | 1739 | except (RepoExitError, RepoChangedException): |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1740 | raise |
| 1741 | except (KeyboardInterrupt, Exception) as e: |
| 1742 | raise RepoUnhandledExceptionError(e, aggregate_errors=errors) |
| 1743 | |
| 1744 | def _ExecuteHelper(self, opt, args, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1745 | manifest = self.outer_manifest |
| 1746 | if not opt.outer_manifest: |
| 1747 | manifest = self.manifest |
| 1748 | |
| 1749 | if opt.manifest_name: |
| 1750 | manifest.Override(opt.manifest_name) |
| 1751 | |
| 1752 | manifest_name = opt.manifest_name |
| 1753 | smart_sync_manifest_path = os.path.join( |
| 1754 | manifest.manifestProject.worktree, "smart_sync_override.xml" |
| 1755 | ) |
| 1756 | |
| 1757 | if opt.clone_bundle is None: |
| 1758 | opt.clone_bundle = manifest.CloneBundle |
| 1759 | |
| 1760 | if opt.smart_sync or opt.smart_tag: |
| 1761 | manifest_name = self._SmartSyncSetup( |
| 1762 | opt, smart_sync_manifest_path, manifest |
| 1763 | ) |
| 1764 | else: |
| 1765 | if os.path.isfile(smart_sync_manifest_path): |
| 1766 | try: |
| 1767 | platform_utils.remove(smart_sync_manifest_path) |
| 1768 | except OSError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1769 | logger.error( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1770 | "error: failed to remove existing smart sync override " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1771 | "manifest: %s", |
| 1772 | e, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1773 | ) |
| 1774 | |
| 1775 | err_event = multiprocessing.Event() |
| 1776 | |
| 1777 | rp = manifest.repoProject |
| 1778 | rp.PreSync() |
| 1779 | cb = rp.CurrentBranch |
| 1780 | if cb: |
| 1781 | base = rp.GetBranch(cb).merge |
| 1782 | if not base or not base.startswith("refs/heads/"): |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1783 | logger.warning( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1784 | "warning: repo is not tracking a remote branch, so it will " |
| 1785 | "not receive updates; run `repo init --repo-rev=stable` to " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1786 | "fix." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1787 | ) |
| 1788 | |
| 1789 | for m in self.ManifestList(opt): |
| 1790 | if not m.manifestProject.standalone_manifest_url: |
| 1791 | m.manifestProject.PreSync() |
| 1792 | |
| 1793 | if opt.repo_upgraded: |
| 1794 | _PostRepoUpgrade(manifest, quiet=opt.quiet) |
| 1795 | |
| 1796 | mp = manifest.manifestProject |
Jason Chang | 1783332 | 2023-05-23 13:06:55 -0700 | [diff] [blame] | 1797 | |
| 1798 | if _REPO_ALLOW_SHALLOW is not None: |
| 1799 | if _REPO_ALLOW_SHALLOW == "1": |
| 1800 | mp.ConfigureCloneFilterForDepth(None) |
| 1801 | elif ( |
| 1802 | _REPO_ALLOW_SHALLOW == "0" and mp.clone_filter_for_depth is None |
| 1803 | ): |
| 1804 | mp.ConfigureCloneFilterForDepth("blob:none") |
| 1805 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1806 | if opt.mp_update: |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1807 | self._UpdateAllManifestProjects(opt, mp, manifest_name, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1808 | else: |
Aravind Vasudevan | 83c66ec | 2023-09-28 19:06:59 +0000 | [diff] [blame] | 1809 | print("Skipping update of local manifest project.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1810 | |
| 1811 | # Now that the manifests are up-to-date, setup options whose defaults |
| 1812 | # might be in the manifest. |
| 1813 | self._ValidateOptionsWithManifest(opt, mp) |
| 1814 | |
| 1815 | superproject_logging_data = {} |
| 1816 | self._UpdateProjectsRevisionId( |
| 1817 | opt, args, superproject_logging_data, manifest |
| 1818 | ) |
| 1819 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1820 | all_projects = self.GetProjects( |
| 1821 | args, |
| 1822 | missing_ok=True, |
| 1823 | submodules_ok=opt.fetch_submodules, |
| 1824 | manifest=manifest, |
| 1825 | all_manifests=not opt.this_manifest_only, |
| 1826 | ) |
| 1827 | |
| 1828 | err_network_sync = False |
| 1829 | err_update_projects = False |
| 1830 | err_update_linkfiles = False |
| 1831 | |
Jason Chang | 1d3b4fb | 2023-06-20 16:55:27 -0700 | [diff] [blame] | 1832 | # Log the repo projects by existing and new. |
| 1833 | existing = [x for x in all_projects if x.Exists] |
| 1834 | mp.config.SetString("repo.existingprojectcount", str(len(existing))) |
| 1835 | mp.config.SetString( |
| 1836 | "repo.newprojectcount", str(len(all_projects) - len(existing)) |
| 1837 | ) |
| 1838 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1839 | self._fetch_times = _FetchTimes(manifest) |
Gavin Mak | 16109a6 | 2023-08-22 01:24:46 +0000 | [diff] [blame] | 1840 | self._local_sync_state = LocalSyncState(manifest) |
Josip Sokcevic | 5ae8292 | 2025-01-31 12:00:52 -0800 | [diff] [blame] | 1841 | if not opt.local_only: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1842 | with multiprocessing.Manager() as manager: |
| 1843 | with ssh.ProxyManager(manager) as ssh_proxy: |
| 1844 | # Initialize the socket dir once in the parent. |
| 1845 | ssh_proxy.sock() |
| 1846 | result = self._FetchMain( |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame] | 1847 | opt, |
| 1848 | args, |
| 1849 | all_projects, |
| 1850 | err_event, |
| 1851 | ssh_proxy, |
| 1852 | manifest, |
| 1853 | errors, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1854 | ) |
| 1855 | all_projects = result.all_projects |
| 1856 | |
| 1857 | if opt.network_only: |
| 1858 | return |
| 1859 | |
| 1860 | # If we saw an error, exit with code 1 so that other scripts can |
| 1861 | # check. |
| 1862 | if err_event.is_set(): |
| 1863 | err_network_sync = True |
| 1864 | if opt.fail_fast: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1865 | logger.error( |
| 1866 | "error: Exited sync due to fetch errors.\n" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1867 | "Local checkouts *not* updated. Resolve network issues " |
| 1868 | "& retry.\n" |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1869 | "`repo sync -l` will update some local checkouts." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1870 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1871 | raise SyncFailFastError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1872 | |
| 1873 | for m in self.ManifestList(opt): |
| 1874 | if m.IsMirror or m.IsArchive: |
| 1875 | # Bail out now, we have no working tree. |
| 1876 | continue |
| 1877 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1878 | try: |
| 1879 | self.UpdateProjectList(opt, m) |
| 1880 | except Exception as e: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1881 | err_event.set() |
| 1882 | err_update_projects = True |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1883 | errors.append(e) |
| 1884 | if isinstance(e, DeleteWorktreeError): |
| 1885 | errors.extend(e.aggregate_errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1886 | if opt.fail_fast: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1887 | logger.error("error: Local checkouts *not* updated.") |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1888 | raise SyncFailFastError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1889 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1890 | try: |
| 1891 | self.UpdateCopyLinkfileList(m) |
| 1892 | except Exception as e: |
| 1893 | err_update_linkfiles = True |
| 1894 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1895 | err_event.set() |
| 1896 | if opt.fail_fast: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1897 | logger.error( |
| 1898 | "error: Local update copyfile or linkfile failed." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1899 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1900 | raise SyncFailFastError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1901 | |
| 1902 | err_results = [] |
| 1903 | # NB: We don't exit here because this is the last step. |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1904 | err_checkout = not self._Checkout( |
| 1905 | all_projects, opt, err_results, errors |
| 1906 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1907 | if err_checkout: |
| 1908 | err_event.set() |
| 1909 | |
| 1910 | printed_notices = set() |
| 1911 | # If there's a notice that's supposed to print at the end of the sync, |
| 1912 | # print it now... But avoid printing duplicate messages, and preserve |
| 1913 | # order. |
| 1914 | for m in sorted(self.ManifestList(opt), key=lambda x: x.path_prefix): |
| 1915 | if m.notice and m.notice not in printed_notices: |
| 1916 | print(m.notice) |
| 1917 | printed_notices.add(m.notice) |
| 1918 | |
| 1919 | # If we saw an error, exit with code 1 so that other scripts can check. |
| 1920 | if err_event.is_set(): |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1921 | |
| 1922 | def print_and_log(err_msg): |
| 1923 | self.git_event_log.ErrorEvent(err_msg) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1924 | logger.error("%s", err_msg) |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1925 | |
| 1926 | print_and_log("error: Unable to fully sync the tree") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1927 | if err_network_sync: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1928 | print_and_log("error: Downloading network changes failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1929 | if err_update_projects: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1930 | print_and_log("error: Updating local project lists failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1931 | if err_update_linkfiles: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1932 | print_and_log("error: Updating copyfiles or linkfiles failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1933 | if err_checkout: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1934 | print_and_log("error: Checking out local projects failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1935 | if err_results: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1936 | # Don't log repositories, as it may contain sensitive info. |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1937 | logger.error("Failing repos:\n%s", "\n".join(err_results)) |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1938 | # Not useful to log. |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1939 | logger.error( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1940 | 'Try re-running with "-j1 --fail-fast" to exit at the first ' |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1941 | "error." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1942 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1943 | raise SyncError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1944 | |
| 1945 | # Log the previous sync analysis state from the config. |
| 1946 | self.git_event_log.LogDataConfigEvents( |
| 1947 | mp.config.GetSyncAnalysisStateData(), "previous_sync_state" |
| 1948 | ) |
| 1949 | |
| 1950 | # Update and log with the new sync analysis state. |
| 1951 | mp.config.UpdateSyncAnalysisState(opt, superproject_logging_data) |
| 1952 | self.git_event_log.LogDataConfigEvents( |
| 1953 | mp.config.GetSyncAnalysisStateData(), "current_sync_state" |
| 1954 | ) |
| 1955 | |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 1956 | self._local_sync_state.PruneRemovedProjects() |
| 1957 | if self._local_sync_state.IsPartiallySynced(): |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1958 | logger.warning( |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 1959 | "warning: Partial syncs are not supported. For the best " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1960 | "experience, sync the entire tree." |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 1961 | ) |
| 1962 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1963 | if not opt.quiet: |
Aravind Vasudevan | 83c66ec | 2023-09-28 19:06:59 +0000 | [diff] [blame] | 1964 | print("repo sync has finished successfully.") |
Mike Frysinger | e19d9e1 | 2020-02-12 11:23:32 -0500 | [diff] [blame] | 1965 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 1966 | |
Shawn O. Pearce | 80d2ceb | 2012-10-26 12:23:05 -0700 | [diff] [blame] | 1967 | def _PostRepoUpgrade(manifest, quiet=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1968 | # Link the docs for the internal .repo/ layout for people. |
| 1969 | link = os.path.join(manifest.repodir, "internal-fs-layout.md") |
| 1970 | if not platform_utils.islink(link): |
| 1971 | target = os.path.join("repo", "docs", "internal-fs-layout.md") |
| 1972 | try: |
| 1973 | platform_utils.symlink(target, link) |
| 1974 | except Exception: |
| 1975 | pass |
Mike Frysinger | fdeb20f | 2021-11-14 03:53:04 -0500 | [diff] [blame] | 1976 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1977 | wrapper = Wrapper() |
| 1978 | if wrapper.NeedSetupGnuPG(): |
| 1979 | wrapper.SetupGnuPG(quiet) |
| 1980 | for project in manifest.projects: |
| 1981 | if project.Exists: |
| 1982 | project.PostRepoUpgrade() |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 1983 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 1984 | |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 1985 | def _PostRepoFetch(rp, repo_verify=True, verbose=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1986 | if rp.HasChanges: |
Aravind Vasudevan | 8bc5000 | 2023-10-13 19:22:47 +0000 | [diff] [blame] | 1987 | logger.warning("info: A new version of repo is available") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1988 | wrapper = Wrapper() |
| 1989 | try: |
| 1990 | rev = rp.bare_git.describe(rp.GetRevisionId()) |
| 1991 | except GitError: |
| 1992 | rev = None |
| 1993 | _, new_rev = wrapper.check_repo_rev( |
| 1994 | rp.gitdir, rev, repo_verify=repo_verify |
| 1995 | ) |
| 1996 | # See if we're held back due to missing signed tag. |
| 1997 | current_revid = rp.bare_git.rev_parse("HEAD") |
| 1998 | new_revid = rp.bare_git.rev_parse("--verify", new_rev) |
| 1999 | if current_revid != new_revid: |
| 2000 | # We want to switch to the new rev, but also not trash any |
| 2001 | # uncommitted changes. This helps with local testing/hacking. |
| 2002 | # If a local change has been made, we will throw that away. |
| 2003 | # We also have to make sure this will switch to an older commit if |
| 2004 | # that's the latest tag in order to support release rollback. |
| 2005 | try: |
Josip Sokcevic | fc901b9 | 2025-03-12 20:40:49 +0000 | [diff] [blame] | 2006 | # Refresh index since reset --keep won't do it. |
| 2007 | rp.work_git.update_index("-q", "--refresh") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2008 | rp.work_git.reset("--keep", new_rev) |
| 2009 | except GitError as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 2010 | raise RepoUnhandledExceptionError(e) |
Aravind Vasudevan | 83c66ec | 2023-09-28 19:06:59 +0000 | [diff] [blame] | 2011 | print("info: Restarting repo with latest version") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2012 | raise RepoChangedException(["--repo-upgraded"]) |
| 2013 | else: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 2014 | logger.warning("warning: Skipped upgrade to unverified version") |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 2015 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2016 | if verbose: |
Aravind Vasudevan | ce0ed79 | 2023-10-06 18:36:22 +0000 | [diff] [blame] | 2017 | print("repo version %s is current" % rp.work_git.describe(HEAD)) |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 2018 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 2019 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 2020 | class _FetchTimes: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2021 | _ALPHA = 0.5 |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 2022 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2023 | def __init__(self, manifest): |
| 2024 | self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json") |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2025 | self._saved = None |
| 2026 | self._seen = {} |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 2027 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2028 | def Get(self, project): |
| 2029 | self._Load() |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2030 | return self._saved.get(project.name, _ONE_DAY_S) |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 2031 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2032 | def Set(self, project, t): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2033 | name = project.name |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2034 | |
| 2035 | # For shared projects, save the longest time. |
| 2036 | self._seen[name] = max(self._seen.get(name, 0), t) |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 2037 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2038 | def _Load(self): |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2039 | if self._saved is None: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2040 | try: |
| 2041 | with open(self._path) as f: |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2042 | self._saved = json.load(f) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 2043 | except (OSError, ValueError): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2044 | platform_utils.remove(self._path, missing_ok=True) |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2045 | self._saved = {} |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 2046 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2047 | def Save(self): |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2048 | if self._saved is None: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2049 | return |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 2050 | |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2051 | for name, t in self._seen.items(): |
| 2052 | # Keep a moving average across the previous/current sync runs. |
| 2053 | old = self._saved.get(name, t) |
| 2054 | self._seen[name] = (self._ALPHA * t) + ((1 - self._ALPHA) * old) |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 2055 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2056 | try: |
| 2057 | with open(self._path, "w") as f: |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 2058 | json.dump(self._seen, f, indent=2) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 2059 | except (OSError, TypeError): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2060 | platform_utils.remove(self._path, missing_ok=True) |
| 2061 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2062 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 2063 | class LocalSyncState: |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 2064 | _LAST_FETCH = "last_fetch" |
| 2065 | _LAST_CHECKOUT = "last_checkout" |
| 2066 | |
| 2067 | def __init__(self, manifest): |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 2068 | self._manifest = manifest |
| 2069 | self._path = os.path.join( |
| 2070 | self._manifest.repodir, ".repo_localsyncstate.json" |
| 2071 | ) |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 2072 | self._time = time.time() |
| 2073 | self._state = None |
| 2074 | self._Load() |
| 2075 | |
| 2076 | def SetFetchTime(self, project): |
| 2077 | self._Set(project, self._LAST_FETCH) |
| 2078 | |
| 2079 | def SetCheckoutTime(self, project): |
| 2080 | self._Set(project, self._LAST_CHECKOUT) |
| 2081 | |
| 2082 | def GetFetchTime(self, project): |
| 2083 | return self._Get(project, self._LAST_FETCH) |
| 2084 | |
| 2085 | def GetCheckoutTime(self, project): |
| 2086 | return self._Get(project, self._LAST_CHECKOUT) |
| 2087 | |
| 2088 | def _Get(self, project, key): |
| 2089 | self._Load() |
| 2090 | p = project.relpath |
| 2091 | if p not in self._state: |
| 2092 | return |
| 2093 | return self._state[p].get(key) |
| 2094 | |
| 2095 | def _Set(self, project, key): |
| 2096 | p = project.relpath |
| 2097 | if p not in self._state: |
| 2098 | self._state[p] = {} |
| 2099 | self._state[p][key] = self._time |
| 2100 | |
| 2101 | def _Load(self): |
| 2102 | if self._state is None: |
| 2103 | try: |
| 2104 | with open(self._path) as f: |
| 2105 | self._state = json.load(f) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 2106 | except (OSError, ValueError): |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 2107 | platform_utils.remove(self._path, missing_ok=True) |
| 2108 | self._state = {} |
| 2109 | |
| 2110 | def Save(self): |
| 2111 | if not self._state: |
| 2112 | return |
| 2113 | try: |
| 2114 | with open(self._path, "w") as f: |
| 2115 | json.dump(self._state, f, indent=2) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 2116 | except (OSError, TypeError): |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 2117 | platform_utils.remove(self._path, missing_ok=True) |
| 2118 | |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 2119 | def PruneRemovedProjects(self): |
| 2120 | """Remove entries don't exist on disk and save.""" |
| 2121 | if not self._state: |
| 2122 | return |
| 2123 | delete = set() |
| 2124 | for path in self._state: |
| 2125 | gitdir = os.path.join(self._manifest.topdir, path, ".git") |
Matt Schulte | 0dd0a83 | 2023-11-30 11:00:16 -0800 | [diff] [blame] | 2126 | if not os.path.exists(gitdir) or os.path.islink(gitdir): |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 2127 | delete.add(path) |
| 2128 | if not delete: |
| 2129 | return |
| 2130 | for path in delete: |
| 2131 | del self._state[path] |
| 2132 | self.Save() |
| 2133 | |
| 2134 | def IsPartiallySynced(self): |
| 2135 | """Return whether a partial sync state is detected.""" |
| 2136 | self._Load() |
| 2137 | prev_checkout_t = None |
Gavin Mak | 321b793 | 2023-08-22 03:10:01 +0000 | [diff] [blame] | 2138 | for path, data in self._state.items(): |
| 2139 | if path == self._manifest.repoProject.relpath: |
| 2140 | # The repo project isn't included in most syncs so we should |
| 2141 | # ignore it here. |
| 2142 | continue |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 2143 | checkout_t = data.get(self._LAST_CHECKOUT) |
| 2144 | if not checkout_t: |
| 2145 | return True |
| 2146 | prev_checkout_t = prev_checkout_t or checkout_t |
| 2147 | if prev_checkout_t != checkout_t: |
| 2148 | return True |
| 2149 | return False |
| 2150 | |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 2151 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2152 | # This is a replacement for xmlrpc.client.Transport using urllib2 |
| 2153 | # and supporting persistent-http[s]. It cannot change hosts from |
| 2154 | # request to request like the normal transport, the real url |
| 2155 | # is passed during initialization. |
| 2156 | class PersistentTransport(xmlrpc.client.Transport): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2157 | def __init__(self, orig_host): |
Daniel Kutik | b99272c | 2023-10-23 21:20:07 +0200 | [diff] [blame] | 2158 | super().__init__() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2159 | self.orig_host = orig_host |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2160 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2161 | def request(self, host, handler, request_body, verbose=False): |
| 2162 | with GetUrlCookieFile(self.orig_host, not verbose) as ( |
| 2163 | cookiefile, |
| 2164 | proxy, |
| 2165 | ): |
| 2166 | # Python doesn't understand cookies with the #HttpOnly_ prefix |
| 2167 | # Since we're only using them for HTTP, copy the file temporarily, |
| 2168 | # stripping those prefixes away. |
| 2169 | if cookiefile: |
| 2170 | tmpcookiefile = tempfile.NamedTemporaryFile(mode="w") |
| 2171 | tmpcookiefile.write("# HTTP Cookie File") |
| 2172 | try: |
| 2173 | with open(cookiefile) as f: |
| 2174 | for line in f: |
| 2175 | if line.startswith("#HttpOnly_"): |
| 2176 | line = line[len("#HttpOnly_") :] |
| 2177 | tmpcookiefile.write(line) |
| 2178 | tmpcookiefile.flush() |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2179 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2180 | cookiejar = cookielib.MozillaCookieJar(tmpcookiefile.name) |
| 2181 | try: |
| 2182 | cookiejar.load() |
| 2183 | except cookielib.LoadError: |
| 2184 | cookiejar = cookielib.CookieJar() |
| 2185 | finally: |
| 2186 | tmpcookiefile.close() |
| 2187 | else: |
| 2188 | cookiejar = cookielib.CookieJar() |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2189 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2190 | proxyhandler = urllib.request.ProxyHandler |
| 2191 | if proxy: |
| 2192 | proxyhandler = urllib.request.ProxyHandler( |
| 2193 | {"http": proxy, "https": proxy} |
| 2194 | ) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2195 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2196 | opener = urllib.request.build_opener( |
| 2197 | urllib.request.HTTPCookieProcessor(cookiejar), proxyhandler |
| 2198 | ) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2199 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2200 | url = urllib.parse.urljoin(self.orig_host, handler) |
| 2201 | parse_results = urllib.parse.urlparse(url) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2202 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2203 | scheme = parse_results.scheme |
| 2204 | if scheme == "persistent-http": |
| 2205 | scheme = "http" |
| 2206 | if scheme == "persistent-https": |
| 2207 | # If we're proxying through persistent-https, use http. The |
| 2208 | # proxy itself will do the https. |
| 2209 | if proxy: |
| 2210 | scheme = "http" |
| 2211 | else: |
| 2212 | scheme = "https" |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2213 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2214 | # Parse out any authentication information using the base class. |
| 2215 | host, extra_headers, _ = self.get_host_info(parse_results.netloc) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2216 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2217 | url = urllib.parse.urlunparse( |
| 2218 | ( |
| 2219 | scheme, |
| 2220 | host, |
| 2221 | parse_results.path, |
| 2222 | parse_results.params, |
| 2223 | parse_results.query, |
| 2224 | parse_results.fragment, |
| 2225 | ) |
| 2226 | ) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2227 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2228 | request = urllib.request.Request(url, request_body) |
| 2229 | if extra_headers is not None: |
| 2230 | for name, header in extra_headers: |
| 2231 | request.add_header(name, header) |
| 2232 | request.add_header("Content-Type", "text/xml") |
| 2233 | try: |
| 2234 | response = opener.open(request) |
| 2235 | except urllib.error.HTTPError as e: |
| 2236 | if e.code == 501: |
| 2237 | # We may have been redirected through a login process |
| 2238 | # but our POST turned into a GET. Retry. |
| 2239 | response = opener.open(request) |
| 2240 | else: |
| 2241 | raise |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2242 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2243 | p, u = xmlrpc.client.getparser() |
| 2244 | # Response should be fairly small, so read it all at once. |
| 2245 | # This way we can show it to the user in case of error (e.g. HTML). |
| 2246 | data = response.read() |
| 2247 | try: |
| 2248 | p.feed(data) |
| 2249 | except xml.parsers.expat.ExpatError as e: |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 2250 | raise OSError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2251 | f"Parsing the manifest failed: {e}\n" |
| 2252 | f"Please report this to your manifest server admin.\n" |
| 2253 | f'Here is the full response:\n{data.decode("utf-8")}' |
| 2254 | ) |
| 2255 | p.close() |
| 2256 | return u.close() |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2257 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2258 | def close(self): |
| 2259 | pass |