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