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 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 15 | import contextlib |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 16 | import datetime |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 17 | import errno |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 18 | import http.client |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 19 | import json |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import os |
| 21 | import re |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 22 | import ssl |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 23 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | import sys |
Joanna Wang | ea5239d | 2022-12-02 09:47:29 -0500 | [diff] [blame] | 25 | from typing import Union |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 26 | import urllib.error |
| 27 | import urllib.request |
Shawn O. Pearce | f00e0ce | 2009-08-22 18:39:49 -0700 | [diff] [blame] | 28 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 29 | from error import GitError |
| 30 | from error import UploadError |
| 31 | from git_command import GitCommand |
| 32 | from git_refs import R_CHANGES |
| 33 | from git_refs import R_HEADS |
| 34 | from git_refs import R_TAGS |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 35 | import platform_utils |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 36 | from repo_trace import Trace |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 37 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 39 | # Prefix that is prepended to all the keys of SyncAnalysisState's data |
| 40 | # that is saved in the config. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 41 | SYNC_STATE_PREFIX = "repo.syncstate." |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 42 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 43 | ID_RE = re.compile(r"^[0-9a-f]{40}$") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 45 | REVIEW_CACHE = dict() |
| 46 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 47 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 48 | def IsChange(rev): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 49 | return rev.startswith(R_CHANGES) |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 50 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 51 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 52 | def IsId(rev): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 53 | return ID_RE.match(rev) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 55 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 56 | def IsTag(rev): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 57 | return rev.startswith(R_TAGS) |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 58 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 59 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 60 | def IsImmutable(rev): |
| 61 | return IsChange(rev) or IsId(rev) or IsTag(rev) |
| 62 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 63 | |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 64 | def _key(name): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 65 | parts = name.split(".") |
| 66 | if len(parts) < 2: |
| 67 | return name.lower() |
| 68 | parts[0] = parts[0].lower() |
| 69 | parts[-1] = parts[-1].lower() |
| 70 | return ".".join(parts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 72 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 73 | class GitConfig: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 74 | _ForUser = None |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 75 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 76 | _ForSystem = None |
| 77 | _SYSTEM_CONFIG = "/etc/gitconfig" |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 78 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 79 | @classmethod |
| 80 | def ForSystem(cls): |
| 81 | if cls._ForSystem is None: |
| 82 | cls._ForSystem = cls(configfile=cls._SYSTEM_CONFIG) |
| 83 | return cls._ForSystem |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 84 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 85 | @classmethod |
| 86 | def ForUser(cls): |
| 87 | if cls._ForUser is None: |
| 88 | cls._ForUser = cls(configfile=cls._getUserConfig()) |
| 89 | return cls._ForUser |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 91 | @staticmethod |
| 92 | def _getUserConfig(): |
flexagoon | 1711bc2 | 2025-01-23 16:05:47 +0300 | [diff] [blame] | 93 | """Get the user-specific config file. |
| 94 | |
| 95 | Prefers the XDG config location if available, with fallback to |
| 96 | ~/.gitconfig |
| 97 | |
| 98 | This matches git behavior: |
| 99 | https://git-scm.com/docs/git-config#FILES |
| 100 | """ |
| 101 | xdg_config_home = os.getenv( |
| 102 | "XDG_CONFIG_HOME", os.path.expanduser("~/.config") |
| 103 | ) |
| 104 | xdg_config_file = os.path.join(xdg_config_home, "git", "config") |
| 105 | if os.path.exists(xdg_config_file): |
| 106 | return xdg_config_file |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 107 | return os.path.expanduser("~/.gitconfig") |
Gavin Mak | 7e3b65b | 2023-01-26 23:27:51 +0000 | [diff] [blame] | 108 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 109 | @classmethod |
| 110 | def ForRepository(cls, gitdir, defaults=None): |
| 111 | return cls(configfile=os.path.join(gitdir, "config"), defaults=defaults) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 113 | def __init__(self, configfile, defaults=None, jsonFile=None): |
| 114 | self.file = configfile |
| 115 | self.defaults = defaults |
| 116 | self._cache_dict = None |
| 117 | self._section_dict = None |
| 118 | self._remotes = {} |
| 119 | self._branches = {} |
Shawn O. Pearce | 1b34c91 | 2009-05-21 18:52:49 -0700 | [diff] [blame] | 120 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 121 | self._json = jsonFile |
| 122 | if self._json is None: |
| 123 | self._json = os.path.join( |
| 124 | os.path.dirname(self.file), |
| 125 | ".repo_" + os.path.basename(self.file) + ".json", |
| 126 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 128 | def ClearCache(self): |
| 129 | """Clear the in-memory cache of config.""" |
| 130 | self._cache_dict = None |
Jack Neus | c474c9c | 2021-07-26 23:08:54 +0000 | [diff] [blame] | 131 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 132 | def Has(self, name, include_defaults=True): |
| 133 | """Return true if this configuration file has the key.""" |
| 134 | if _key(name) in self._cache: |
| 135 | return True |
| 136 | if include_defaults and self.defaults: |
| 137 | return self.defaults.Has(name, include_defaults=True) |
| 138 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 140 | def GetInt(self, name: str) -> Union[int, None]: |
| 141 | """Returns an integer from the configuration file. |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 142 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 143 | This follows the git config syntax. |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 144 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 145 | Args: |
| 146 | name: The key to lookup. |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 147 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 148 | Returns: |
| 149 | None if the value was not defined, or is not an int. |
| 150 | Otherwise, the number itself. |
| 151 | """ |
| 152 | v = self.GetString(name) |
| 153 | if v is None: |
| 154 | return None |
| 155 | v = v.strip() |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 156 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 157 | mult = 1 |
| 158 | if v.endswith("k"): |
| 159 | v = v[:-1] |
| 160 | mult = 1024 |
| 161 | elif v.endswith("m"): |
| 162 | v = v[:-1] |
| 163 | mult = 1024 * 1024 |
| 164 | elif v.endswith("g"): |
| 165 | v = v[:-1] |
| 166 | mult = 1024 * 1024 * 1024 |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 167 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 168 | base = 10 |
| 169 | if v.startswith("0x"): |
| 170 | base = 16 |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 171 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 172 | try: |
| 173 | return int(v, base=base) * mult |
| 174 | except ValueError: |
| 175 | print( |
| 176 | f"warning: expected {name} to represent an integer, got {v} " |
| 177 | "instead", |
| 178 | file=sys.stderr, |
| 179 | ) |
| 180 | return None |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 181 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 182 | def DumpConfigDict(self): |
| 183 | """Returns the current configuration dict. |
Ian Kasprzak | 835a34b | 2021-03-05 11:04:49 -0800 | [diff] [blame] | 184 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 185 | Configuration data is information only (e.g. logging) and |
| 186 | should not be considered a stable data-source. |
Ian Kasprzak | 835a34b | 2021-03-05 11:04:49 -0800 | [diff] [blame] | 187 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 188 | Returns: |
| 189 | dict of {, } for git configuration cache. |
| 190 | are strings converted by GetString. |
| 191 | """ |
| 192 | config_dict = {} |
| 193 | for key in self._cache: |
| 194 | config_dict[key] = self.GetString(key) |
| 195 | return config_dict |
Ian Kasprzak | 835a34b | 2021-03-05 11:04:49 -0800 | [diff] [blame] | 196 | |
Daniel Kutik | 49c9b06 | 2023-10-20 18:25:25 +0200 | [diff] [blame] | 197 | def GetBoolean(self, name: str) -> Union[bool, None]: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 198 | """Returns a boolean from the configuration file. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 199 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 200 | Returns: |
| 201 | None: The value was not defined, or is not a boolean. |
| 202 | True: The value was set to true or yes. |
| 203 | False: The value was set to false or no. |
| 204 | """ |
| 205 | v = self.GetString(name) |
| 206 | if v is None: |
| 207 | return None |
| 208 | v = v.lower() |
| 209 | if v in ("true", "yes"): |
| 210 | return True |
| 211 | if v in ("false", "no"): |
| 212 | return False |
| 213 | print( |
| 214 | f"warning: expected {name} to represent a boolean, got {v} instead", |
| 215 | file=sys.stderr, |
| 216 | ) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 217 | return None |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 218 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 219 | def SetBoolean(self, name, value): |
| 220 | """Set the truthy value for a key.""" |
| 221 | if value is not None: |
| 222 | value = "true" if value else "false" |
| 223 | self.SetString(name, value) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 224 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 225 | def GetString(self, name: str, all_keys: bool = False) -> Union[str, None]: |
| 226 | """Get the first value for a key, or None if it is not defined. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 228 | This configuration file is used first, if the key is not |
| 229 | defined or all_keys = True then the defaults are also searched. |
| 230 | """ |
| 231 | try: |
| 232 | v = self._cache[_key(name)] |
| 233 | except KeyError: |
| 234 | if self.defaults: |
| 235 | return self.defaults.GetString(name, all_keys=all_keys) |
| 236 | v = [] |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 237 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 238 | if not all_keys: |
| 239 | if v: |
| 240 | return v[0] |
| 241 | return None |
Mike Frysinger | f88282c | 2021-09-28 15:59:40 -0400 | [diff] [blame] | 242 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 243 | r = [] |
| 244 | r.extend(v) |
| 245 | if self.defaults: |
| 246 | r.extend(self.defaults.GetString(name, all_keys=True)) |
| 247 | return r |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 248 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 249 | def SetString(self, name, value): |
| 250 | """Set the value(s) for a key. |
| 251 | Only this configuration file is modified. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 252 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 253 | The supplied value should be either a string, or a list of strings (to |
| 254 | store multiple values), or None (to delete the key). |
| 255 | """ |
| 256 | key = _key(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 257 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 258 | try: |
| 259 | old = self._cache[key] |
| 260 | except KeyError: |
| 261 | old = [] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 262 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 263 | if value is None: |
| 264 | if old: |
| 265 | del self._cache[key] |
| 266 | self._do("--unset-all", name) |
| 267 | |
| 268 | elif isinstance(value, list): |
| 269 | if len(value) == 0: |
| 270 | self.SetString(name, None) |
| 271 | |
| 272 | elif len(value) == 1: |
| 273 | self.SetString(name, value[0]) |
| 274 | |
| 275 | elif old != value: |
| 276 | self._cache[key] = list(value) |
| 277 | self._do("--replace-all", name, value[0]) |
| 278 | for i in range(1, len(value)): |
| 279 | self._do("--add", name, value[i]) |
| 280 | |
| 281 | elif len(old) != 1 or old[0] != value: |
| 282 | self._cache[key] = [value] |
| 283 | self._do("--replace-all", name, value) |
| 284 | |
| 285 | def GetRemote(self, name): |
| 286 | """Get the remote.$name.* configuration values as an object.""" |
| 287 | try: |
| 288 | r = self._remotes[name] |
| 289 | except KeyError: |
| 290 | r = Remote(self, name) |
| 291 | self._remotes[r.name] = r |
| 292 | return r |
| 293 | |
| 294 | def GetBranch(self, name): |
| 295 | """Get the branch.$name.* configuration values as an object.""" |
| 296 | try: |
| 297 | b = self._branches[name] |
| 298 | except KeyError: |
| 299 | b = Branch(self, name) |
| 300 | self._branches[b.name] = b |
| 301 | return b |
| 302 | |
| 303 | def GetSyncAnalysisStateData(self): |
| 304 | """Returns data to be logged for the analysis of sync performance.""" |
| 305 | return { |
| 306 | k: v |
| 307 | for k, v in self.DumpConfigDict().items() |
| 308 | if k.startswith(SYNC_STATE_PREFIX) |
| 309 | } |
| 310 | |
| 311 | def UpdateSyncAnalysisState(self, options, superproject_logging_data): |
| 312 | """Update Config's SYNC_STATE_PREFIX* data with the latest sync data. |
| 313 | |
| 314 | Args: |
| 315 | options: Options passed to sync returned from optparse. See |
| 316 | _Options(). |
| 317 | superproject_logging_data: A dictionary of superproject data that is |
| 318 | to be logged. |
| 319 | |
| 320 | Returns: |
| 321 | SyncAnalysisState object. |
| 322 | """ |
| 323 | return SyncAnalysisState(self, options, superproject_logging_data) |
| 324 | |
| 325 | def GetSubSections(self, section): |
| 326 | """List all subsection names matching $section.*.*""" |
| 327 | return self._sections.get(section, set()) |
| 328 | |
| 329 | def HasSection(self, section, subsection=""): |
| 330 | """Does at least one key in section.subsection exist?""" |
| 331 | try: |
| 332 | return subsection in self._sections[section] |
| 333 | except KeyError: |
| 334 | return False |
| 335 | |
| 336 | def UrlInsteadOf(self, url): |
| 337 | """Resolve any url.*.insteadof references.""" |
| 338 | for new_url in self.GetSubSections("url"): |
| 339 | for old_url in self.GetString("url.%s.insteadof" % new_url, True): |
| 340 | if old_url is not None and url.startswith(old_url): |
| 341 | return new_url + url[len(old_url) :] |
| 342 | return url |
| 343 | |
| 344 | @property |
| 345 | def _sections(self): |
| 346 | d = self._section_dict |
| 347 | if d is None: |
| 348 | d = {} |
| 349 | for name in self._cache.keys(): |
| 350 | p = name.split(".") |
| 351 | if 2 == len(p): |
| 352 | section = p[0] |
| 353 | subsect = "" |
| 354 | else: |
| 355 | section = p[0] |
| 356 | subsect = ".".join(p[1:-1]) |
| 357 | if section not in d: |
| 358 | d[section] = set() |
| 359 | d[section].add(subsect) |
| 360 | self._section_dict = d |
| 361 | return d |
| 362 | |
| 363 | @property |
| 364 | def _cache(self): |
| 365 | if self._cache_dict is None: |
| 366 | self._cache_dict = self._Read() |
| 367 | return self._cache_dict |
| 368 | |
| 369 | def _Read(self): |
| 370 | d = self._ReadJson() |
| 371 | if d is None: |
| 372 | d = self._ReadGit() |
| 373 | self._SaveJson(d) |
| 374 | return d |
| 375 | |
| 376 | def _ReadJson(self): |
| 377 | try: |
| 378 | if os.path.getmtime(self._json) <= os.path.getmtime(self.file): |
| 379 | platform_utils.remove(self._json) |
| 380 | return None |
| 381 | except OSError: |
| 382 | return None |
| 383 | try: |
| 384 | with Trace(": parsing %s", self.file): |
| 385 | with open(self._json) as fd: |
| 386 | return json.load(fd) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 387 | except (OSError, ValueError): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 388 | platform_utils.remove(self._json, missing_ok=True) |
| 389 | return None |
| 390 | |
| 391 | def _SaveJson(self, cache): |
| 392 | try: |
| 393 | with open(self._json, "w") as fd: |
| 394 | json.dump(cache, fd, indent=2) |
Jason R. Coombs | ae824fb | 2023-10-20 23:32:40 +0545 | [diff] [blame] | 395 | except (OSError, TypeError): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 396 | platform_utils.remove(self._json, missing_ok=True) |
| 397 | |
| 398 | def _ReadGit(self): |
| 399 | """ |
| 400 | Read configuration data from git. |
| 401 | |
| 402 | This internal method populates the GitConfig cache. |
| 403 | |
| 404 | """ |
| 405 | c = {} |
| 406 | if not os.path.exists(self.file): |
| 407 | return c |
| 408 | |
| 409 | d = self._do("--null", "--list") |
| 410 | for line in d.rstrip("\0").split("\0"): |
| 411 | if "\n" in line: |
| 412 | key, val = line.split("\n", 1) |
| 413 | else: |
| 414 | key = line |
| 415 | val = None |
| 416 | |
| 417 | if key in c: |
| 418 | c[key].append(val) |
| 419 | else: |
| 420 | c[key] = [val] |
| 421 | |
| 422 | return c |
| 423 | |
| 424 | def _do(self, *args): |
| 425 | if self.file == self._SYSTEM_CONFIG: |
| 426 | command = ["config", "--system", "--includes"] |
| 427 | else: |
| 428 | command = ["config", "--file", self.file, "--includes"] |
| 429 | command.extend(args) |
| 430 | |
| 431 | p = GitCommand(None, command, capture_stdout=True, capture_stderr=True) |
| 432 | if p.Wait() == 0: |
| 433 | return p.stdout |
| 434 | else: |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 435 | raise GitError(f"git config {str(args)}: {p.stderr}") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 436 | |
| 437 | |
Mike Frysinger | f841ca4 | 2020-02-18 21:31:51 -0500 | [diff] [blame] | 438 | class RepoConfig(GitConfig): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 439 | """User settings for repo itself.""" |
Mike Frysinger | f841ca4 | 2020-02-18 21:31:51 -0500 | [diff] [blame] | 440 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 441 | @staticmethod |
| 442 | def _getUserConfig(): |
| 443 | repo_config_dir = os.getenv("REPO_CONFIG_DIR", os.path.expanduser("~")) |
| 444 | return os.path.join(repo_config_dir, ".repoconfig/config") |
Mike Frysinger | f841ca4 | 2020-02-18 21:31:51 -0500 | [diff] [blame] | 445 | |
| 446 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 447 | class RefSpec: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 448 | """A Git refspec line, split into its components: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 450 | forced: True if the line starts with '+' |
| 451 | src: Left side of the line |
| 452 | dst: Right side of the line |
| 453 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 454 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 455 | @classmethod |
| 456 | def FromString(cls, rs): |
| 457 | lhs, rhs = rs.split(":", 2) |
| 458 | if lhs.startswith("+"): |
| 459 | lhs = lhs[1:] |
| 460 | forced = True |
| 461 | else: |
| 462 | forced = False |
| 463 | return cls(forced, lhs, rhs) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 464 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 465 | def __init__(self, forced, lhs, rhs): |
| 466 | self.forced = forced |
| 467 | self.src = lhs |
| 468 | self.dst = rhs |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 469 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 470 | def SourceMatches(self, rev): |
| 471 | if self.src: |
| 472 | if rev == self.src: |
| 473 | return True |
| 474 | if self.src.endswith("/*") and rev.startswith(self.src[:-1]): |
| 475 | return True |
| 476 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 477 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 478 | def DestMatches(self, ref): |
| 479 | if self.dst: |
| 480 | if ref == self.dst: |
| 481 | return True |
| 482 | if self.dst.endswith("/*") and ref.startswith(self.dst[:-1]): |
| 483 | return True |
| 484 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 485 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 486 | def MapSource(self, rev): |
| 487 | if self.src.endswith("/*"): |
| 488 | return self.dst[:-1] + rev[len(self.src) - 1 :] |
| 489 | return self.dst |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 490 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 491 | def __str__(self): |
| 492 | s = "" |
| 493 | if self.forced: |
| 494 | s += "+" |
| 495 | if self.src: |
| 496 | s += self.src |
| 497 | if self.dst: |
| 498 | s += ":" |
| 499 | s += self.dst |
| 500 | return s |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 501 | |
| 502 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 503 | URI_ALL = re.compile(r"^([a-z][a-z+-]*)://([^@/]*@?[^/]*)/") |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 504 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 505 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 506 | def GetSchemeFromUrl(url): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 507 | m = URI_ALL.match(url) |
| 508 | if m: |
| 509 | return m.group(1) |
| 510 | return None |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 511 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 512 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 513 | @contextlib.contextmanager |
| 514 | def GetUrlCookieFile(url, quiet): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 515 | if url.startswith("persistent-"): |
| 516 | try: |
| 517 | p = subprocess.Popen( |
| 518 | ["git-remote-persistent-https", "-print_config", url], |
| 519 | stdin=subprocess.PIPE, |
| 520 | stdout=subprocess.PIPE, |
| 521 | stderr=subprocess.PIPE, |
| 522 | ) |
| 523 | try: |
| 524 | cookieprefix = "http.cookiefile=" |
| 525 | proxyprefix = "http.proxy=" |
| 526 | cookiefile = None |
| 527 | proxy = None |
| 528 | for line in p.stdout: |
| 529 | line = line.strip().decode("utf-8") |
| 530 | if line.startswith(cookieprefix): |
| 531 | cookiefile = os.path.expanduser( |
| 532 | line[len(cookieprefix) :] |
| 533 | ) |
| 534 | if line.startswith(proxyprefix): |
| 535 | proxy = line[len(proxyprefix) :] |
| 536 | # Leave subprocess open, as cookie file may be transient. |
| 537 | if cookiefile or proxy: |
| 538 | yield cookiefile, proxy |
| 539 | return |
| 540 | finally: |
| 541 | p.stdin.close() |
| 542 | if p.wait(): |
| 543 | err_msg = p.stderr.read().decode("utf-8") |
| 544 | if " -print_config" in err_msg: |
| 545 | pass # Persistent proxy doesn't support -print_config. |
| 546 | elif not quiet: |
| 547 | print(err_msg, file=sys.stderr) |
| 548 | except OSError as e: |
| 549 | if e.errno == errno.ENOENT: |
| 550 | pass # No persistent proxy. |
| 551 | raise |
| 552 | cookiefile = GitConfig.ForUser().GetString("http.cookiefile") |
| 553 | if cookiefile: |
| 554 | cookiefile = os.path.expanduser(cookiefile) |
| 555 | yield cookiefile, None |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 556 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 557 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 558 | class Remote: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 559 | """Configuration options related to a remote.""" |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 560 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 561 | def __init__(self, config, name): |
| 562 | self._config = config |
| 563 | self.name = name |
| 564 | self.url = self._Get("url") |
| 565 | self.pushUrl = self._Get("pushurl") |
| 566 | self.review = self._Get("review") |
| 567 | self.projectname = self._Get("projectname") |
| 568 | self.fetch = list( |
| 569 | map(RefSpec.FromString, self._Get("fetch", all_keys=True)) |
| 570 | ) |
| 571 | self._review_url = None |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 572 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 573 | def _InsteadOf(self): |
| 574 | globCfg = GitConfig.ForUser() |
| 575 | urlList = globCfg.GetSubSections("url") |
| 576 | longest = "" |
| 577 | longestUrl = "" |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 578 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 579 | for url in urlList: |
| 580 | key = "url." + url + ".insteadOf" |
| 581 | insteadOfList = globCfg.GetString(key, all_keys=True) |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 582 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 583 | for insteadOf in insteadOfList: |
| 584 | if self.url.startswith(insteadOf) and len(insteadOf) > len( |
| 585 | longest |
| 586 | ): |
| 587 | longest = insteadOf |
| 588 | longestUrl = url |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 589 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 590 | if len(longest) == 0: |
| 591 | return self.url |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 592 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 593 | return self.url.replace(longest, longestUrl, 1) |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 594 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 595 | def PreConnectFetch(self, ssh_proxy): |
| 596 | """Run any setup for this remote before we connect to it. |
Mike Frysinger | 19e409c | 2021-05-05 19:44:35 -0400 | [diff] [blame] | 597 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 598 | In practice, if the remote is using SSH, we'll attempt to create a new |
| 599 | SSH master session to it for reuse across projects. |
Mike Frysinger | 19e409c | 2021-05-05 19:44:35 -0400 | [diff] [blame] | 600 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 601 | Args: |
| 602 | ssh_proxy: The SSH settings for managing master sessions. |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 603 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 604 | Returns: |
| 605 | Whether the preconnect phase for this remote was successful. |
| 606 | """ |
| 607 | if not ssh_proxy: |
| 608 | return True |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 609 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 610 | connectionUrl = self._InsteadOf() |
| 611 | return ssh_proxy.preconnect(connectionUrl) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 612 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 613 | def ReviewUrl(self, userEmail, validate_certs): |
| 614 | if self._review_url is None: |
| 615 | if self.review is None: |
| 616 | return None |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 617 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 618 | u = self.review |
| 619 | if u.startswith("persistent-"): |
| 620 | u = u[len("persistent-") :] |
| 621 | if u.split(":")[0] not in ("http", "https", "sso", "ssh"): |
| 622 | u = "http://%s" % u |
| 623 | if u.endswith("/Gerrit"): |
| 624 | u = u[: len(u) - len("/Gerrit")] |
| 625 | if u.endswith("/ssh_info"): |
| 626 | u = u[: len(u) - len("/ssh_info")] |
| 627 | if not u.endswith("/"): |
| 628 | u += "/" |
| 629 | http_url = u |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 630 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 631 | if u in REVIEW_CACHE: |
| 632 | self._review_url = REVIEW_CACHE[u] |
| 633 | elif "REPO_HOST_PORT_INFO" in os.environ: |
| 634 | host, port = os.environ["REPO_HOST_PORT_INFO"].split() |
| 635 | self._review_url = self._SshReviewUrl(userEmail, host, port) |
| 636 | REVIEW_CACHE[u] = self._review_url |
| 637 | elif u.startswith("sso:") or u.startswith("ssh:"): |
| 638 | self._review_url = u # Assume it's right |
| 639 | REVIEW_CACHE[u] = self._review_url |
| 640 | elif "REPO_IGNORE_SSH_INFO" in os.environ: |
| 641 | self._review_url = http_url |
| 642 | REVIEW_CACHE[u] = self._review_url |
| 643 | else: |
| 644 | try: |
| 645 | info_url = u + "ssh_info" |
| 646 | if not validate_certs: |
| 647 | context = ssl._create_unverified_context() |
| 648 | info = urllib.request.urlopen( |
| 649 | info_url, context=context |
| 650 | ).read() |
| 651 | else: |
| 652 | info = urllib.request.urlopen(info_url).read() |
| 653 | if info == b"NOT_AVAILABLE" or b"<" in info: |
| 654 | # If `info` contains '<', we assume the server gave us |
| 655 | # some sort of HTML response back, like maybe a login |
| 656 | # page. |
| 657 | # |
| 658 | # Assume HTTP if SSH is not enabled or ssh_info doesn't |
| 659 | # look right. |
| 660 | self._review_url = http_url |
| 661 | else: |
| 662 | info = info.decode("utf-8") |
| 663 | host, port = info.split() |
| 664 | self._review_url = self._SshReviewUrl( |
| 665 | userEmail, host, port |
| 666 | ) |
| 667 | except urllib.error.HTTPError as e: |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 668 | raise UploadError(f"{self.review}: {str(e)}") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 669 | except urllib.error.URLError as e: |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 670 | raise UploadError(f"{self.review}: {str(e)}") |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 671 | except http.client.HTTPException as e: |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 672 | raise UploadError(f"{self.review}: {e.__class__.__name__}") |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 673 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 674 | REVIEW_CACHE[u] = self._review_url |
| 675 | return self._review_url + self.projectname |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 676 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 677 | def _SshReviewUrl(self, userEmail, host, port): |
| 678 | username = self._config.GetString("review.%s.username" % self.review) |
| 679 | if username is None: |
| 680 | username = userEmail.split("@")[0] |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 681 | return f"ssh://{username}@{host}:{port}/" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 682 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 683 | def ToLocal(self, rev): |
| 684 | """Convert a remote revision string to something we have locally.""" |
| 685 | if self.name == "." or IsId(rev): |
| 686 | return rev |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 687 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 688 | if not rev.startswith("refs/"): |
| 689 | rev = R_HEADS + rev |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 690 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 691 | for spec in self.fetch: |
| 692 | if spec.SourceMatches(rev): |
| 693 | return spec.MapSource(rev) |
Nasser Grainawi | 909d58b | 2014-09-19 12:13:04 -0600 | [diff] [blame] | 694 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 695 | if not rev.startswith(R_HEADS): |
| 696 | return rev |
Nasser Grainawi | 909d58b | 2014-09-19 12:13:04 -0600 | [diff] [blame] | 697 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 698 | raise GitError( |
| 699 | "%s: remote %s does not have %s" |
| 700 | % (self.projectname, self.name, rev) |
| 701 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 702 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 703 | def WritesTo(self, ref): |
| 704 | """True if the remote stores to the tracking ref.""" |
| 705 | for spec in self.fetch: |
| 706 | if spec.DestMatches(ref): |
| 707 | return True |
| 708 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 709 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 710 | def ResetFetch(self, mirror=False): |
| 711 | """Set the fetch refspec to its default value.""" |
| 712 | if mirror: |
| 713 | dst = "refs/heads/*" |
| 714 | else: |
| 715 | dst = "refs/remotes/%s/*" % self.name |
| 716 | self.fetch = [RefSpec(True, "refs/heads/*", dst)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 717 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 718 | def Save(self): |
| 719 | """Save this remote to the configuration.""" |
| 720 | self._Set("url", self.url) |
| 721 | if self.pushUrl is not None: |
| 722 | self._Set("pushurl", self.pushUrl + "/" + self.projectname) |
| 723 | else: |
| 724 | self._Set("pushurl", self.pushUrl) |
| 725 | self._Set("review", self.review) |
| 726 | self._Set("projectname", self.projectname) |
| 727 | self._Set("fetch", list(map(str, self.fetch))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 728 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 729 | def _Set(self, key, value): |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 730 | key = f"remote.{self.name}.{key}" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 731 | return self._config.SetString(key, value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 732 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 733 | def _Get(self, key, all_keys=False): |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 734 | key = f"remote.{self.name}.{key}" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 735 | return self._config.GetString(key, all_keys=all_keys) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 736 | |
| 737 | |
Mike Frysinger | d4aee65 | 2023-10-19 05:13:32 -0400 | [diff] [blame] | 738 | class Branch: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 739 | """Configuration options related to a single branch.""" |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 740 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 741 | def __init__(self, config, name): |
| 742 | self._config = config |
| 743 | self.name = name |
| 744 | self.merge = self._Get("merge") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 746 | r = self._Get("remote") |
| 747 | if r: |
| 748 | self.remote = self._config.GetRemote(r) |
| 749 | else: |
| 750 | self.remote = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 751 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 752 | @property |
| 753 | def LocalMerge(self): |
| 754 | """Convert the merge spec to a local name.""" |
| 755 | if self.remote and self.merge: |
| 756 | return self.remote.ToLocal(self.merge) |
| 757 | return None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 758 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 759 | def Save(self): |
| 760 | """Save this branch back into the configuration.""" |
| 761 | if self._config.HasSection("branch", self.name): |
| 762 | if self.remote: |
| 763 | self._Set("remote", self.remote.name) |
| 764 | else: |
| 765 | self._Set("remote", None) |
| 766 | self._Set("merge", self.merge) |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 767 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 768 | else: |
| 769 | with open(self._config.file, "a") as fd: |
| 770 | fd.write('[branch "%s"]\n' % self.name) |
| 771 | if self.remote: |
| 772 | fd.write("\tremote = %s\n" % self.remote.name) |
| 773 | if self.merge: |
| 774 | fd.write("\tmerge = %s\n" % self.merge) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 775 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 776 | def _Set(self, key, value): |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 777 | key = f"branch.{self.name}.{key}" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 778 | return self._config.SetString(key, value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 779 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 780 | def _Get(self, key, all_keys=False): |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 781 | key = f"branch.{self.name}.{key}" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 782 | return self._config.GetString(key, all_keys=all_keys) |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 783 | |
| 784 | |
| 785 | class SyncAnalysisState: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 786 | """Configuration options related to logging of sync state for analysis. |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 787 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 788 | This object is versioned. |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 789 | """ |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 790 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 791 | def __init__(self, config, options, superproject_logging_data): |
| 792 | """Initializes SyncAnalysisState. |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 793 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 794 | Saves the following data into the |config| object. |
| 795 | - sys.argv, options, superproject's logging data. |
| 796 | - repo.*, branch.* and remote.* parameters from config object. |
| 797 | - Current time as synctime. |
| 798 | - Version number of the object. |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 799 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 800 | All the keys saved by this object are prepended with SYNC_STATE_PREFIX. |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 801 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 802 | Args: |
| 803 | config: GitConfig object to store all options. |
| 804 | options: Options passed to sync returned from optparse. See |
| 805 | _Options(). |
| 806 | superproject_logging_data: A dictionary of superproject data that is |
| 807 | to be logged. |
| 808 | """ |
| 809 | self._config = config |
LuK1337 | aadd12c | 2023-09-16 09:36:49 +0200 | [diff] [blame] | 810 | now = datetime.datetime.now(datetime.timezone.utc) |
| 811 | self._Set("main.synctime", now.isoformat(timespec="microseconds")) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 812 | self._Set("main.version", "1") |
| 813 | self._Set("sys.argv", sys.argv) |
| 814 | for key, value in superproject_logging_data.items(): |
| 815 | self._Set(f"superproject.{key}", value) |
| 816 | for key, value in options.__dict__.items(): |
| 817 | self._Set(f"options.{key}", value) |
| 818 | config_items = config.DumpConfigDict().items() |
| 819 | EXTRACT_NAMESPACES = {"repo", "branch", "remote"} |
| 820 | self._SetDictionary( |
| 821 | { |
| 822 | k: v |
| 823 | for k, v in config_items |
| 824 | if not k.startswith(SYNC_STATE_PREFIX) |
| 825 | and k.split(".", 1)[0] in EXTRACT_NAMESPACES |
| 826 | } |
| 827 | ) |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 828 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 829 | def _SetDictionary(self, data): |
| 830 | """Save all key/value pairs of |data| dictionary. |
| 831 | |
| 832 | Args: |
| 833 | data: A dictionary whose key/value are to be saved. |
| 834 | """ |
| 835 | for key, value in data.items(): |
| 836 | self._Set(key, value) |
| 837 | |
| 838 | def _Set(self, key, value): |
| 839 | """Set the |value| for a |key| in the |_config| member. |
| 840 | |
| 841 | |key| is prepended with the value of SYNC_STATE_PREFIX constant. |
| 842 | |
| 843 | Args: |
| 844 | key: Name of the key. |
| 845 | value: |value| could be of any type. If it is 'bool', it will be |
| 846 | saved as a Boolean and for all other types, it will be saved as |
| 847 | a String. |
| 848 | """ |
| 849 | if value is None: |
| 850 | return |
| 851 | sync_key = f"{SYNC_STATE_PREFIX}{key}" |
| 852 | sync_key = sync_key.replace("_", "") |
| 853 | if isinstance(value, str): |
| 854 | self._config.SetString(sync_key, value) |
| 855 | elif isinstance(value, bool): |
| 856 | self._config.SetBoolean(sync_key, value) |
| 857 | else: |
| 858 | self._config.SetString(sync_key, str(value)) |