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